blob: 32be756a29a2159b0664e48cc6a73227f954850f [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
152 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
153 return( PSA_ERROR_NOT_SUPPORTED );
154 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
157 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
Gilles Peskine9a944802018-06-21 09:35:35 +0200160 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
161 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
162 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
163 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
164 case MBEDTLS_ERR_ASN1_INVALID_DATA:
165 return( PSA_ERROR_INVALID_ARGUMENT );
166 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
168 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
169 return( PSA_ERROR_BUFFER_TOO_SMALL );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
Jaeden Amero93e21112019-02-20 13:57:28 +0000181#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000182 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000183#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
184 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CCM_BAD_INPUT:
192 return( PSA_ERROR_INVALID_ARGUMENT );
193 case MBEDTLS_ERR_CCM_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200
201 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
209 return( PSA_ERROR_INVALID_ARGUMENT );
210 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
211 return( PSA_ERROR_INSUFFICIENT_MEMORY );
212 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
213 return( PSA_ERROR_INVALID_PADDING );
214 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200215 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200219 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
223 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
227 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100228 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
229 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
232 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
233 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
239 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
240 return( PSA_ERROR_NOT_SUPPORTED );
241 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinee59236f2018-01-27 23:32:46 +0100244 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
245 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
246 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248
249 case MBEDTLS_ERR_GCM_AUTH_FAILED:
250 return( PSA_ERROR_INVALID_SIGNATURE );
251 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200252 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
257 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100258 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
259 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100260 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
263 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
264 return( PSA_ERROR_NOT_SUPPORTED );
265 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
266 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
267#endif
268
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MD_ALLOC_FAILED:
279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
280 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
283 return( PSA_ERROR_HARDWARE_FAILURE );
284
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
286 return( PSA_ERROR_STORAGE_FAILURE );
287 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_ALLOC_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
305 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
316 return( PSA_ERROR_NOT_PERMITTED );
317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
324 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
331 return( PSA_ERROR_NOT_SUPPORTED );
332
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_RSA_INVALID_PADDING:
339 return( PSA_ERROR_INVALID_PADDING );
340 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
343 return( PSA_ERROR_INVALID_ARGUMENT );
344 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
345 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200346 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
350 return( PSA_ERROR_BUFFER_TOO_SMALL );
351 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100352 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
360 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
363 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
364 return( PSA_ERROR_INVALID_ARGUMENT );
365 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
366 return( PSA_ERROR_HARDWARE_FAILURE );
367
itayzafrir5c753392018-05-08 11:18:38 +0300368 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300369 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300370 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300371 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
372 return( PSA_ERROR_BUFFER_TOO_SMALL );
373 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
374 return( PSA_ERROR_NOT_SUPPORTED );
375 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
376 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
377 return( PSA_ERROR_INVALID_SIGNATURE );
378 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
379 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100380 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
381 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300382 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
383 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100384
Janos Follatha13b9052019-11-22 12:48:59 +0000385 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
386 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300387
Gilles Peskinee59236f2018-01-27 23:32:46 +0100388 default:
David Saadab4ecc272019-02-14 13:48:10 +0200389 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100390 }
391}
392
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200393
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200394
395
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396/****************************************************************/
397/* Key management */
398/****************************************************************/
399
Gilles Peskine73167e12019-07-12 23:44:37 +0200400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
401static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
402{
Gilles Peskine8e338702019-07-30 20:06:31 +0200403 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200404}
405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
406
John Durkop07cc04a2020-11-16 22:08:34 -0800407/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
408 * current test driver in key_management.c is using this function
409 * when accelerators are used for ECC key pair and public key.
410 * Once that dependency is resolved these guards can be removed.
411 */
John Durkop9814fa22020-11-04 12:28:15 -0800412#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800413 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
415 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 size_t bits,
418 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419{
420 switch( curve )
421 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 528:
436 if( bits_is_sloppy )
437 return( MBEDTLS_ECP_DP_SECP521R1 );
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 }
440 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100441
Paul Elliott8ff510a2020-06-02 17:19:28 +0100442 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
460 if( bits_is_sloppy )
461 return( MBEDTLS_ECP_DP_CURVE25519 );
462 break;
463 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100464 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 }
466 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100467
Paul Elliott8ff510a2020-06-02 17:19:28 +0100468 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100477 }
478 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480
481 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
John Durkop9814fa22020-11-04 12:28:15 -0800483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
486 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200487
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200488static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
489 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490{
491 /* Check that the bit size is acceptable for the key type */
492 switch( type )
493 {
494 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200496 case PSA_KEY_TYPE_DERIVE:
497 break;
David Browne04acc22021-01-26 11:39:16 -0700498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
500 if( bits != 128 && bits != 192 && bits != 256 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
David Browne04acc22021-01-26 11:39:16 -0700504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_CAMELLIA:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
David Browne04acc22021-01-26 11:39:16 -0700510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_DES:
512 if( bits != 64 && bits != 128 && bits != 192 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
David Browne04acc22021-01-26 11:39:16 -0700516#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 case PSA_KEY_TYPE_ARC4:
518 if( bits < 8 || bits > 2048 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700522#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200523 case PSA_KEY_TYPE_CHACHA20:
524 if( bits != 256 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 return( PSA_SUCCESS );
535}
536
Gilles Peskineb46bef22019-07-30 21:32:04 +0200537/** Return the size of the key in the given slot, in bits.
538 *
539 * \param[in] slot A key slot.
540 *
541 * \return The key size in bits, read from the metadata in the slot.
542 */
543static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
544{
545 return( slot->attr.bits );
546}
547
Steven Cooreman75b74362020-07-28 14:30:13 +0200548/** Try to allocate a buffer to an empty key slot.
549 *
550 * \param[in,out] slot Key slot to attach buffer to.
551 * \param[in] buffer_length Requested size of the buffer.
552 *
553 * \retval #PSA_SUCCESS
554 * The buffer has been successfully allocated.
555 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
556 * Not enough memory was available for allocation.
557 * \retval #PSA_ERROR_ALREADY_EXISTS
558 * Trying to allocate a buffer to a non-empty key slot.
559 */
560static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
561 size_t buffer_length )
562{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100563 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200564 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200565
Ronald Cronea0f8a62020-11-25 17:52:23 +0100566 slot->key.data = mbedtls_calloc( 1, buffer_length );
567 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200568 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200569
Ronald Cronea0f8a62020-11-25 17:52:23 +0100570 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200571 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200572}
573
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200574psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
575 const uint8_t* data,
576 size_t data_length )
577{
578 psa_status_t status = psa_allocate_buffer_to_slot( slot,
579 data_length );
580 if( status != PSA_SUCCESS )
581 return( status );
582
Ronald Cronea0f8a62020-11-25 17:52:23 +0100583 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200584 return( PSA_SUCCESS );
585}
586
Ronald Crona0fe59f2020-11-29 11:14:53 +0100587psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100588 const psa_key_attributes_t *attributes,
589 const uint8_t *data, size_t data_length,
590 uint8_t *key_buffer, size_t key_buffer_size,
591 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100592{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100593 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
594 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100595
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200596 /* zero-length keys are never supported. */
597 if( data_length == 0 )
598 return( PSA_ERROR_NOT_SUPPORTED );
599
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100600 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100601 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100602 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200603
Steven Cooreman75b74362020-07-28 14:30:13 +0200604 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
605 if( data_length > SIZE_MAX / 8 )
606 return( PSA_ERROR_NOT_SUPPORTED );
607
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200608 /* Enforce a size limit, and in particular ensure that the bit
609 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100610 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200611 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200612
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100613 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200614 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200615 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200616
Ronald Crondd04d422020-11-28 15:14:42 +0100617 /* Copy the key material. */
618 memcpy( key_buffer, data, data_length );
619 *key_buffer_length = data_length;
620 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200621
Steven Cooreman40120f62020-10-29 11:42:22 +0100622 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100623 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100624 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200625 {
John Durkop9814fa22020-11-04 12:28:15 -0800626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
627 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100628 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200629 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100630 return( mbedtls_psa_ecp_import_key( attributes,
631 data, data_length,
632 key_buffer, key_buffer_size,
633 key_buffer_length,
634 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100635 }
John Durkop9814fa22020-11-04 12:28:15 -0800636#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
637 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700638#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
639 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100640 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200641 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 return( mbedtls_psa_rsa_import_key( attributes,
643 data, data_length,
644 key_buffer, key_buffer_size,
645 key_buffer_length,
646 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200647 }
John Durkop9814fa22020-11-04 12:28:15 -0800648#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
649 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100651
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100652 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100653}
654
Gilles Peskinef603c712019-01-19 13:40:11 +0100655/** Calculate the intersection of two algorithm usage policies.
656 *
657 * Return 0 (which allows no operation) on incompatibility.
658 */
659static psa_algorithm_t psa_key_policy_algorithm_intersection(
660 psa_algorithm_t alg1,
661 psa_algorithm_t alg2 )
662{
Gilles Peskine549ea862019-05-22 11:45:59 +0200663 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100664 if( alg1 == alg2 )
665 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100666 /* If the policies are from the same hash-and-sign family, check
667 * if one is a wildcard. If so the other has the specific algorithm. */
668 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
669 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
670 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100671 {
Steven Cooreman03488022021-02-18 12:07:20 +0100672 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
673 return( alg2 );
674 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
675 return( alg1 );
676 }
677 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100678 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100679 * restrictive tag length. */
680 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
681 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
682 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
683 {
684 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
685 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
686 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100687
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100688 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100689 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
690 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100691 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100692 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100693 }
694 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100695 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100696 ( alg1_len <= alg2_len ) )
697 {
698 return( alg2 );
699 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100700 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100701 ( alg2_len <= alg1_len ) )
702 {
703 return( alg1 );
704 }
705 }
706 /* If the policies are from the same MAC family, check whether one
707 * of them is a minimum-MAC-length policy. Calculate the most
708 * restrictive tag length. */
709 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
710 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
711 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
712 {
713 size_t alg1_len = PSA_MAC_TRUNCATED_LENGTH( alg1 );
714 size_t alg2_len = PSA_MAC_TRUNCATED_LENGTH( alg2 );
715 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100716
Steven Cooreman03488022021-02-18 12:07:20 +0100717 /* If both are wildcards, return most restricitve wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100718 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
719 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100720 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100721 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100722 }
723 /* If only one is a wildcard, return specific algorithm if compatible.
724 * Special case: specific MAC algorithm with '0' as length means full-
725 * length MAC, which is always allowed by a wildcard with the same
726 * base algorithm. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100727 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100728 ( ( alg1_len <= alg2_len ) ||
729 ( alg2 == PSA_ALG_FULL_LENGTH_MAC( alg1 ) ) ) )
730 {
731 return( alg2 );
732 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100733 if( ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100734 ( ( alg2_len <= alg1_len ) ||
735 ( alg1 == PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) ) )
736 {
737 return( alg1 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100738 }
Gilles Peskinef603c712019-01-19 13:40:11 +0100739 }
740 /* If the policies are incompatible, allow nothing. */
741 return( 0 );
742}
743
Gilles Peskined6f371b2019-05-10 19:33:38 +0200744static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
745 psa_algorithm_t requested_alg )
746{
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100747 /* A requested algorithm cannot be a wildcard. */
748 if( PSA_ALG_IS_WILDCARD( requested_alg ) )
749 return( 0 );
Gilles Peskine549ea862019-05-22 11:45:59 +0200750 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200751 if( requested_alg == policy_alg )
752 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100753 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
754 * and requested_alg is the same hash-and-sign family with any hash,
755 * then requested_alg is compliant with policy_alg. */
756 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
757 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200758 {
Steven Cooreman03488022021-02-18 12:07:20 +0100759 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
760 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
761 }
762 /* If policy_alg is a wildcard AEAD algorithm of the same base as
763 * the requested algorithm, check the requested tag length to be
764 * equal-length or longer than the wildcard-specified length. */
765 if( PSA_ALG_IS_AEAD( policy_alg ) &&
766 PSA_ALG_IS_AEAD( requested_alg ) &&
767 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
768 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100769 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100770 {
771 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
772 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
773 }
774 /* If policy_alg is a wildcard MAC algorithm of the same base as
775 * the requested algorithm, check the requested tag length to be
776 * equal-length or longer than the wildcard-specified length. */
777 if( PSA_ALG_IS_MAC( policy_alg ) &&
778 PSA_ALG_IS_MAC( requested_alg ) &&
779 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
780 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100781 ( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100782 {
783 /* Special case: full-length MAC is encoded with 0-length.
784 * A minimum-length policy will always allow a full-length MAC. */
785 if( PSA_ALG_FULL_LENGTH_MAC( requested_alg ) == requested_alg )
786 return( 1 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100787
Steven Cooreman03488022021-02-18 12:07:20 +0100788 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
789 PSA_MAC_TRUNCATED_LENGTH( requested_alg ) );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200790 }
Steven Cooremance48e852020-10-05 16:02:45 +0200791 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200792 * a key derivation with that key agreement should also be allowed. This
793 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200794 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
795 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
796 {
797 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
798 policy_alg );
799 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100800 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200801 return( 0 );
802}
803
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100804/** Test whether a policy permits an algorithm.
805 *
806 * The caller must test usage flags separately.
807 */
808static int psa_key_policy_permits( const psa_key_policy_t *policy,
809 psa_algorithm_t alg )
810{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200811 return( psa_key_algorithm_permits( policy->alg, alg ) ||
812 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100813}
814
Gilles Peskinef603c712019-01-19 13:40:11 +0100815/** Restrict a key policy based on a constraint.
816 *
817 * \param[in,out] policy The policy to restrict.
818 * \param[in] constraint The policy constraint to apply.
819 *
820 * \retval #PSA_SUCCESS
821 * \c *policy contains the intersection of the original value of
822 * \c *policy and \c *constraint.
823 * \retval #PSA_ERROR_INVALID_ARGUMENT
824 * \c *policy and \c *constraint are incompatible.
825 * \c *policy is unchanged.
826 */
827static psa_status_t psa_restrict_key_policy(
828 psa_key_policy_t *policy,
829 const psa_key_policy_t *constraint )
830{
831 psa_algorithm_t intersection_alg =
832 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 psa_algorithm_t intersection_alg2 =
834 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100835 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
836 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200837 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
838 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100839 policy->usage &= constraint->usage;
840 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200841 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100842 return( PSA_SUCCESS );
843}
844
Ronald Cron5c522922020-11-14 16:35:34 +0100845/** Get the description of a key given its identifier and policy constraints
846 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200847 *
Ronald Cron5c522922020-11-14 16:35:34 +0100848 * The key must have allow all the usage flags set in \p usage. If \p alg is
849 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +0100850 *
Ronald Cron5c522922020-11-14 16:35:34 +0100851 * In case of a persistent key, the function loads the description of the key
852 * into a key slot if not already done.
853 *
854 * On success, the returned key slot is locked. It is the responsibility of
855 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200856 */
Ronald Cron5c522922020-11-14 16:35:34 +0100857static psa_status_t psa_get_and_lock_key_slot_with_policy(
858 mbedtls_svc_key_id_t key,
859 psa_key_slot_t **p_slot,
860 psa_key_usage_t usage,
861 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100862{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
864 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100865
Ronald Cron5c522922020-11-14 16:35:34 +0100866 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100867 if( status != PSA_SUCCESS )
868 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200869 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100870
871 /* Enforce that usage policy for the key slot contains all the flags
872 * required by the usage parameter. There is one exception: public
873 * keys can always be exported, so we treat public key objects as
874 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200875 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100876 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200877
878 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200879 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200880 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100881
882 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200883 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200884 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +0100885
Darryl Green06fd18d2018-07-16 11:21:11 +0100886 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200887
888error:
889 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100890 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200891
892 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100893}
Darryl Green940d72c2018-07-13 13:18:51 +0100894
Ronald Cron5c522922020-11-14 16:35:34 +0100895/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200896 *
897 * A transparent key is a key for which the key material is directly
898 * available, as opposed to a key in a secure element.
899 *
Ronald Cron5c522922020-11-14 16:35:34 +0100900 * This is a temporary function to use instead of
901 * psa_get_and_lock_key_slot_with_policy() until secure element support is
902 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200903 *
Ronald Cron5c522922020-11-14 16:35:34 +0100904 * On success, the returned key slot is locked. It is the responsibility of the
905 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200906 */
907#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100908static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
909 mbedtls_svc_key_id_t key,
910 psa_key_slot_t **p_slot,
911 psa_key_usage_t usage,
912 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200913{
Ronald Cron5c522922020-11-14 16:35:34 +0100914 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
915 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200916 if( status != PSA_SUCCESS )
917 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200918
Gilles Peskineadad8132019-07-25 11:31:23 +0200919 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200920 {
Ronald Cron5c522922020-11-14 16:35:34 +0100921 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200922 *p_slot = NULL;
923 return( PSA_ERROR_NOT_SUPPORTED );
924 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200925
Gilles Peskine28f8f302019-07-24 13:30:31 +0200926 return( PSA_SUCCESS );
927}
928#else /* MBEDTLS_PSA_CRYPTO_SE_C */
929/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100930#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
931 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200932#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
933
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100934/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100935static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000936{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100937 /* Data pointer will always be either a valid pointer or NULL in an
938 * initialized slot, so we can just free it. */
939 if( slot->key.data != NULL )
940 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
941
942 mbedtls_free( slot->key.data );
943 slot->key.data = NULL;
944 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000945
946 return( PSA_SUCCESS );
947}
948
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100949/** Completely wipe a slot in memory, including its policy.
950 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100951psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100952{
953 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100954
955 /*
956 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100957 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100958 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
959 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100960 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100961 */
Ronald Cron5c522922020-11-14 16:35:34 +0100962 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100963 {
964#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100965 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100966#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100967 status = PSA_ERROR_CORRUPTION_DETECTED;
968 }
969
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200970 /* Multipart operations may still be using the key. This is safe
971 * because all multipart operation objects are independent from
972 * the key slot: if they need to access the key after the setup
973 * phase, they have a copy of the key. Note that this means that
974 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100975 /* At this point, key material and other type-specific content has
976 * been wiped. Clear remaining metadata. We can call memset and not
977 * zeroize because the metadata is not particularly sensitive. */
978 memset( slot, 0, sizeof( *slot ) );
979 return( status );
980}
981
Ronald Croncf56a0a2020-08-04 09:51:30 +0200982psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100983{
Gilles Peskine2f060a82018-12-04 17:12:32 +0100984 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +0200985 psa_status_t status; /* status of the last operation */
986 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +0200987#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
988 psa_se_drv_table_entry_t *driver;
989#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100990
Ronald Croncf56a0a2020-08-04 09:51:30 +0200991 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +0200992 return( PSA_SUCCESS );
993
Ronald Cronf2911112020-10-29 17:51:10 +0100994 /*
Ronald Cron19daca92020-11-10 18:08:03 +0100995 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +0100996 * key, this will load the key description from persistent memory if not
997 * done yet. We cannot avoid this loading as without it we don't know if
998 * the key is operated by an SE or not and this information is needed by
999 * the current implementation.
1000 */
Ronald Cron5c522922020-11-14 16:35:34 +01001001 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001002 if( status != PSA_SUCCESS )
1003 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001004
Ronald Cronf2911112020-10-29 17:51:10 +01001005 /*
1006 * If the key slot containing the key description is under access by the
1007 * library (apart from the present access), the key cannot be destroyed
1008 * yet. For the time being, just return in error. Eventually (to be
1009 * implemented), the key should be destroyed when all accesses have
1010 * stopped.
1011 */
Ronald Cron5c522922020-11-14 16:35:34 +01001012 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001013 {
Ronald Cron5c522922020-11-14 16:35:34 +01001014 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001015 return( PSA_ERROR_GENERIC_ERROR );
1016 }
1017
Gilles Peskine354f7672019-07-12 23:46:38 +02001018#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001019 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001020 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001021 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001022 /* For a key in a secure element, we need to do three things:
1023 * remove the key file in internal storage, destroy the
1024 * key inside the secure element, and update the driver's
1025 * persistent data. Start a transaction that will encompass these
1026 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001027 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001029 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001030 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 status = psa_crypto_save_transaction( );
1032 if( status != PSA_SUCCESS )
1033 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001034 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001035 /* We should still try to destroy the key in the secure
1036 * element and the key metadata in storage. This is especially
1037 * important if the error is that the storage is full.
1038 * But how to do it exactly without risking an inconsistent
1039 * state after a reset?
1040 * https://github.com/ARMmbed/mbed-crypto/issues/215
1041 */
1042 overall_status = status;
1043 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 }
1045
Ronald Cronea0f8a62020-11-25 17:52:23 +01001046 status = psa_destroy_se_key( driver,
1047 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001048 if( overall_status == PSA_SUCCESS )
1049 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001050 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001051#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1052
Darryl Greend49a4992018-06-18 17:27:26 +01001053#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001054 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001055 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 status = psa_destroy_persistent_key( slot->attr.id );
1057 if( overall_status == PSA_SUCCESS )
1058 overall_status = status;
1059
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001060 /* TODO: other slots may have a copy of the same key. We should
1061 * invalidate them.
1062 * https://github.com/ARMmbed/mbed-crypto/issues/214
1063 */
Darryl Greend49a4992018-06-18 17:27:26 +01001064 }
1065#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001066
Gilles Peskinefc762652019-07-22 19:30:34 +02001067#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1068 if( driver != NULL )
1069 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001070 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001071 if( overall_status == PSA_SUCCESS )
1072 overall_status = status;
1073 status = psa_crypto_stop_transaction( );
1074 if( overall_status == PSA_SUCCESS )
1075 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001076 }
1077#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1078
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1080exit:
1081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001082 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001083 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1084 if( overall_status == PSA_SUCCESS )
1085 overall_status = status;
1086 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001087}
1088
John Durkop07cc04a2020-11-16 22:08:34 -08001089#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001090 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001091static psa_status_t psa_get_rsa_public_exponent(
1092 const mbedtls_rsa_context *rsa,
1093 psa_key_attributes_t *attributes )
1094{
1095 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001096 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001097 uint8_t *buffer = NULL;
1098 size_t buflen;
1099 mbedtls_mpi_init( &mpi );
1100
1101 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1102 if( ret != 0 )
1103 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001104 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1105 {
1106 /* It's the default value, which is reported as an empty string,
1107 * so there's nothing to do. */
1108 goto exit;
1109 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001110
1111 buflen = mbedtls_mpi_size( &mpi );
1112 buffer = mbedtls_calloc( 1, buflen );
1113 if( buffer == NULL )
1114 {
1115 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1116 goto exit;
1117 }
1118 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1119 if( ret != 0 )
1120 goto exit;
1121 attributes->domain_parameters = buffer;
1122 attributes->domain_parameters_size = buflen;
1123
1124exit:
1125 mbedtls_mpi_free( &mpi );
1126 if( ret != 0 )
1127 mbedtls_free( buffer );
1128 return( mbedtls_to_psa_error( ret ) );
1129}
John Durkop07cc04a2020-11-16 22:08:34 -08001130#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001131 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001132
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001133/** Retrieve all the publicly-accessible attributes of a key.
1134 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001135psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001136 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001137{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001138 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001139 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001140 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001141
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001142 psa_reset_key_attributes( attributes );
1143
Ronald Cron5c522922020-11-14 16:35:34 +01001144 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001145 if( status != PSA_SUCCESS )
1146 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001147
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001148 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001149 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1150 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1151
1152#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1153 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001154 psa_set_key_slot_number( attributes,
1155 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001156#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001157
Gilles Peskine8e338702019-07-30 20:06:31 +02001158 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001159 {
John Durkop07cc04a2020-11-16 22:08:34 -08001160#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001161 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001162 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001163 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001164#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001165 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001166 * is not yet implemented.
1167 * https://github.com/ARMmbed/mbed-crypto/issues/216
1168 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001169 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001170 break;
1171#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001172 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001173 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001174
Ronald Cron90857082020-11-25 14:58:33 +01001175 status = mbedtls_psa_rsa_load_representation(
1176 slot->attr.type,
1177 slot->key.data,
1178 slot->key.bytes,
1179 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001180 if( status != PSA_SUCCESS )
1181 break;
1182
Steven Cooremana2371e52020-07-28 14:30:39 +02001183 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001184 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001185 mbedtls_rsa_free( rsa );
1186 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001187 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001188 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001189#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001190 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001191 default:
1192 /* Nothing else to do. */
1193 break;
1194 }
1195
1196 if( status != PSA_SUCCESS )
1197 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001198
Ronald Cron5c522922020-11-14 16:35:34 +01001199 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001200
Ronald Cron5c522922020-11-14 16:35:34 +01001201 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001202}
1203
Gilles Peskinec8000c02019-08-02 20:15:51 +02001204#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1205psa_status_t psa_get_key_slot_number(
1206 const psa_key_attributes_t *attributes,
1207 psa_key_slot_number_t *slot_number )
1208{
1209 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1210 {
1211 *slot_number = attributes->slot_number;
1212 return( PSA_SUCCESS );
1213 }
1214 else
1215 return( PSA_ERROR_INVALID_ARGUMENT );
1216}
1217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1218
Ronald Crond18b5f82020-11-25 16:46:05 +01001219static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1220 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001221 uint8_t *data,
1222 size_t data_size,
1223 size_t *data_length )
1224{
Ronald Crond18b5f82020-11-25 16:46:05 +01001225 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001226 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001227 memcpy( data, key_buffer, key_buffer_size );
1228 memset( data + key_buffer_size, 0,
1229 data_size - key_buffer_size );
1230 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001231 return( PSA_SUCCESS );
1232}
1233
Ronald Cron7285cda2020-11-26 14:46:37 +01001234psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001235 const psa_key_attributes_t *attributes,
1236 const uint8_t *key_buffer, size_t key_buffer_size,
1237 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001238{
Ronald Crond18b5f82020-11-25 16:46:05 +01001239 psa_key_type_t type = attributes->core.type;
1240
Ronald Crond18b5f82020-11-25 16:46:05 +01001241 if( key_type_is_raw_bytes( type ) ||
1242 PSA_KEY_TYPE_IS_RSA( type ) ||
1243 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001244 {
1245 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001246 key_buffer, key_buffer_size,
1247 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001248 }
1249 else
1250 {
1251 /* This shouldn't happen in the reference implementation, but
1252 it is valid for a special-purpose implementation to omit
1253 support for exporting certain key types. */
1254 return( PSA_ERROR_NOT_SUPPORTED );
1255 }
1256}
1257
1258psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1259 uint8_t *data,
1260 size_t data_size,
1261 size_t *data_length )
1262{
1263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1264 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1265 psa_key_slot_t *slot;
1266
Ronald Crone907e552021-01-18 13:22:38 +01001267 /* Reject a zero-length output buffer now, since this can never be a
1268 * valid key representation. This way we know that data must be a valid
1269 * pointer and we can do things like memset(data, ..., data_size). */
1270 if( data_size == 0 )
1271 return( PSA_ERROR_BUFFER_TOO_SMALL );
1272
Ronald Cron9486f9d2020-11-26 13:36:23 +01001273 /* Set the key to empty now, so that even when there are errors, we always
1274 * set data_length to a value between 0 and data_size. On error, setting
1275 * the key to empty is a good choice because an empty key representation is
1276 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001277 *data_length = 0;
1278
Ronald Cron9486f9d2020-11-26 13:36:23 +01001279 /* Export requires the EXPORT flag. There is an exception for public keys,
1280 * which don't require any flag, but
1281 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1282 */
1283 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1284 PSA_KEY_USAGE_EXPORT, 0 );
1285 if( status != PSA_SUCCESS )
1286 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001287
Ronald Crond18b5f82020-11-25 16:46:05 +01001288 psa_key_attributes_t attributes = {
1289 .core = slot->attr
1290 };
Ronald Cron67227982020-11-26 15:16:05 +01001291 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001292 slot->key.data, slot->key.bytes,
1293 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001294
Ronald Cron9486f9d2020-11-26 13:36:23 +01001295 unlock_status = psa_unlock_key_slot( slot );
1296
1297 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1298}
1299
Ronald Cron7285cda2020-11-26 14:46:37 +01001300psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001301 const psa_key_attributes_t *attributes,
1302 const uint8_t *key_buffer,
1303 size_t key_buffer_size,
1304 uint8_t *data,
1305 size_t data_size,
1306 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001307{
Ronald Crond18b5f82020-11-25 16:46:05 +01001308 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001309
Ronald Crond18b5f82020-11-25 16:46:05 +01001310 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001311 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001312 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001313 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001314 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001315 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001316 key_buffer, key_buffer_size,
1317 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001318 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001319
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001321 {
John Durkop0e005192020-10-31 22:06:54 -07001322#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1323 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001324 return( mbedtls_psa_rsa_export_public_key( attributes,
1325 key_buffer,
1326 key_buffer_size,
1327 data,
1328 data_size,
1329 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001330#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001331 /* We don't know how to convert a private RSA key to public. */
1332 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001333#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1334 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001335 }
1336 else
Moran Pekera998bc62018-04-16 18:16:20 +03001337 {
John Durkop6ba40d12020-11-10 08:50:04 -08001338#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1339 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001340 return( mbedtls_psa_ecp_export_public_key( attributes,
1341 key_buffer,
1342 key_buffer_size,
1343 data,
1344 data_size,
1345 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001346#else
1347 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001348 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001349#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1350 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001351 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001352 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001353 else
1354 {
1355 /* This shouldn't happen in the reference implementation, but
1356 it is valid for a special-purpose implementation to omit
1357 support for exporting certain key types. */
1358 return( PSA_ERROR_NOT_SUPPORTED );
1359 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001360}
Ronald Crond18b5f82020-11-25 16:46:05 +01001361
Ronald Croncf56a0a2020-08-04 09:51:30 +02001362psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001363 uint8_t *data,
1364 size_t data_size,
1365 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001366{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001367 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001368 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001369 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001370
Ronald Crone907e552021-01-18 13:22:38 +01001371 /* Reject a zero-length output buffer now, since this can never be a
1372 * valid key representation. This way we know that data must be a valid
1373 * pointer and we can do things like memset(data, ..., data_size). */
1374 if( data_size == 0 )
1375 return( PSA_ERROR_BUFFER_TOO_SMALL );
1376
Darryl Greendd8fb772018-11-07 16:00:44 +00001377 /* Set the key to empty now, so that even when there are errors, we always
1378 * set data_length to a value between 0 and data_size. On error, setting
1379 * the key to empty is a good choice because an empty key representation is
1380 * unlikely to be accepted anywhere. */
1381 *data_length = 0;
1382
1383 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001384 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001385 if( status != PSA_SUCCESS )
1386 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001387
Ronald Cron9486f9d2020-11-26 13:36:23 +01001388 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1389 {
1390 status = PSA_ERROR_INVALID_ARGUMENT;
1391 goto exit;
1392 }
1393
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 psa_key_attributes_t attributes = {
1395 .core = slot->attr
1396 };
Ronald Cron67227982020-11-26 15:16:05 +01001397 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001398 &attributes, slot->key.data, slot->key.bytes,
1399 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400
1401exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001402 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001403
Ronald Cron5c522922020-11-14 16:35:34 +01001404 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001405}
1406
Gilles Peskine91e8c332019-08-02 19:19:39 +02001407#if defined(static_assert)
1408static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1409 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001410static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001411 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001412static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001413 "One or more key attribute flag is listed as both internal-only and external-only" );
1414#endif
1415
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001416/** Validate that a key policy is internally well-formed.
1417 *
1418 * This function only rejects invalid policies. It does not validate the
1419 * consistency of the policy with respect to other attributes of the key
1420 * such as the key type.
1421 */
1422static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001423{
1424 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001425 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001426 PSA_KEY_USAGE_ENCRYPT |
1427 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001428 PSA_KEY_USAGE_SIGN_HASH |
1429 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001430 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1431 return( PSA_ERROR_INVALID_ARGUMENT );
1432
Gilles Peskine4747d192019-04-17 15:05:45 +02001433 return( PSA_SUCCESS );
1434}
1435
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001436/** Validate the internal consistency of key attributes.
1437 *
1438 * This function only rejects invalid attribute values. If does not
1439 * validate the consistency of the attributes with any key data that may
1440 * be involved in the creation of the key.
1441 *
1442 * Call this function early in the key creation process.
1443 *
1444 * \param[in] attributes Key attributes for the new key.
1445 * \param[out] p_drv On any return, the driver for the key, if any.
1446 * NULL for a transparent key.
1447 *
1448 */
1449static psa_status_t psa_validate_key_attributes(
1450 const psa_key_attributes_t *attributes,
1451 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001452{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001453 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001454 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001455 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001456
Ronald Cron54b90082020-10-29 15:26:43 +01001457 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001458 if( status != PSA_SUCCESS )
1459 return( status );
1460
Ronald Crond2ed4812020-07-17 16:11:30 +02001461 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001462 if( status != PSA_SUCCESS )
1463 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001464
Ronald Cron65f38a32020-10-23 17:11:13 +02001465 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1466 {
1467 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1468 return( PSA_ERROR_INVALID_ARGUMENT );
1469 }
1470 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001471 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001472 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001473 if( status != PSA_SUCCESS )
1474 return( status );
1475 }
1476
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001477 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001478 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001479 return( status );
1480
1481 /* Refuse to create overly large keys.
1482 * Note that this doesn't trigger on import if the attributes don't
1483 * explicitly specify a size (so psa_get_key_bits returns 0), so
1484 * psa_import_key() needs its own checks. */
1485 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1486 return( PSA_ERROR_NOT_SUPPORTED );
1487
Gilles Peskine91e8c332019-08-02 19:19:39 +02001488 /* Reject invalid flags. These should not be reachable through the API. */
1489 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1490 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1491 return( PSA_ERROR_INVALID_ARGUMENT );
1492
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001493 return( PSA_SUCCESS );
1494}
1495
Gilles Peskine4747d192019-04-17 15:05:45 +02001496/** Prepare a key slot to receive key material.
1497 *
1498 * This function allocates a key slot and sets its metadata.
1499 *
1500 * If this function fails, call psa_fail_key_creation().
1501 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001502 * This function is intended to be used as follows:
1503 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001504 * it with the specified attributes, and in case of a volatile key assign it
1505 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001506 * -# Populate the slot with the key material.
1507 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1508 * In case of failure at any step, stop the sequence and call
1509 * psa_fail_key_creation().
1510 *
Ronald Cron5c522922020-11-14 16:35:34 +01001511 * On success, the key slot is locked. It is the responsibility of the caller
1512 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001513 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001514 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001515 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001516 * \param[out] p_slot On success, a pointer to the prepared slot.
1517 * \param[out] p_drv On any return, the driver for the key, if any.
1518 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001519 *
1520 * \retval #PSA_SUCCESS
1521 * The key slot is ready to receive key material.
1522 * \return If this function fails, the key slot is an invalid state.
1523 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001524 */
1525static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001526 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001527 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001528 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001529 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001530{
1531 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001532 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001533 psa_key_slot_t *slot;
1534
Gilles Peskinedf179142019-07-15 22:02:14 +02001535 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001536 *p_drv = NULL;
1537
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001538 status = psa_validate_key_attributes( attributes, p_drv );
1539 if( status != PSA_SUCCESS )
1540 return( status );
1541
Ronald Cronc4d1b512020-07-31 11:26:37 +02001542 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001543 if( status != PSA_SUCCESS )
1544 return( status );
1545 slot = *p_slot;
1546
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001547 /* We're storing the declared bit-size of the key. It's up to each
1548 * creation mechanism to verify that this information is correct.
1549 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001550 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001551 * is optional (import, copy). In case of a volatile key, assign it the
1552 * volatile key identifier associated to the slot returned to contain its
1553 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001554
1555 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001556 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1557 {
1558#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1559 slot->attr.id = volatile_key_id;
1560#else
1561 slot->attr.id.key_id = volatile_key_id;
1562#endif
1563 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001564
Gilles Peskine91e8c332019-08-02 19:19:39 +02001565 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001566 * external-only flags, query `attributes`. Thanks to the check
1567 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001568 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001569 * may have set. */
1570 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001571
Gilles Peskinecbaff462019-07-12 23:46:04 +02001572#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001573 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001574 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001575 * create the key file in internal storage, create the
1576 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001577 * persistent data. This is done by starting a transaction that will
1578 * encompass these three actions.
1579 * For registering a volatile key, we just need to find an appropriate
1580 * slot number inside the SE. Since the key is designated volatile, creating
1581 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001582 /* The first thing to do is to find a slot number for the new key.
1583 * We save the slot number in persistent storage as part of the
1584 * transaction data. It will be needed to recover if the power
1585 * fails during the key creation process, to clean up on the secure
1586 * element side after restarting. Obtaining a slot number from the
1587 * secure element driver updates its persistent state, but we do not yet
1588 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001589 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001590 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001591 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001592 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001593 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001594 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001595 if( status != PSA_SUCCESS )
1596 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001597
1598 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001599 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001600 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1601 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001602 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001603 psa_crypto_transaction.key.id = slot->attr.id;
1604 status = psa_crypto_save_transaction( );
1605 if( status != PSA_SUCCESS )
1606 {
1607 (void) psa_crypto_stop_transaction( );
1608 return( status );
1609 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001610 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001611
1612 status = psa_copy_key_material_into_slot(
1613 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001614 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001615
1616 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1617 {
1618 /* Key registration only makes sense with a secure element. */
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001621#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1622
Ronald Cronc4d1b512020-07-31 11:26:37 +02001623 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001624}
Gilles Peskine4747d192019-04-17 15:05:45 +02001625
1626/** Finalize the creation of a key once its key material has been set.
1627 *
1628 * This entails writing the key to persistent storage.
1629 *
1630 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001631 * See the documentation of psa_start_key_creation() for the intended use
1632 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001633 *
Ronald Cron5c522922020-11-14 16:35:34 +01001634 * If the finalization succeeds, the function unlocks the key slot (it was
1635 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1636 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001637 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001638 * \param[in,out] slot Pointer to the slot with key material.
1639 * \param[in] driver The secure element driver for the key,
1640 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001641 * \param[out] key On success, identifier of the key. Note that the
1642 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001643 *
1644 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001645 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001646 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1647 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1648 * \retval #PSA_ERROR_ALREADY_EXISTS
1649 * \retval #PSA_ERROR_DATA_INVALID
1650 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001651 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001652 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001653 * \return If this function fails, the key slot is an invalid state.
1654 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001656static psa_status_t psa_finish_key_creation(
1657 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001658 psa_se_drv_table_entry_t *driver,
1659 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001660{
1661 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001662 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001663 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001664
1665#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001666 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001668#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1669 if( driver != NULL )
1670 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001671 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001672 psa_key_slot_number_t slot_number =
1673 psa_key_slot_get_slot_number( slot ) ;
1674
Gilles Peskineb46bef22019-07-30 21:32:04 +02001675#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001676 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001677 sizeof( data.slot_number ),
1678 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001679#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001680 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001681 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001682 (uint8_t*) &data,
1683 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001684 }
1685 else
1686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1687 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001688 /* Key material is saved in export representation in the slot, so
1689 * just pass the slot buffer for storage. */
1690 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001691 slot->key.data,
1692 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001693 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001694 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001695#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1696
Gilles Peskinecbaff462019-07-12 23:46:04 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001698 /* Finish the transaction for a key creation. This does not
1699 * happen when registering an existing key. Detect this case
1700 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001701 * creation of a persistent key in a secure element requires a transaction,
1702 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001703 if( driver != NULL &&
1704 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001705 {
1706 status = psa_save_se_persistent_data( driver );
1707 if( status != PSA_SUCCESS )
1708 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001709 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001710 return( status );
1711 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001712 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 }
1714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1715
Ronald Cron50972942020-11-14 11:28:25 +01001716 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001717 {
1718 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001719 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001720 if( status != PSA_SUCCESS )
1721 *key = MBEDTLS_SVC_KEY_ID_INIT;
1722 }
Ronald Cron50972942020-11-14 11:28:25 +01001723
Gilles Peskine4747d192019-04-17 15:05:45 +02001724 return( status );
1725}
1726
1727/** Abort the creation of a key.
1728 *
1729 * You may call this function after calling psa_start_key_creation(),
1730 * or after psa_finish_key_creation() fails. In other circumstances, this
1731 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001732 * See the documentation of psa_start_key_creation() for the intended use
1733 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001734 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001735 * \param[in,out] slot Pointer to the slot with key material.
1736 * \param[in] driver The secure element driver for the key,
1737 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001738 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001739static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001740 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001741{
Gilles Peskine011e4282019-06-26 18:34:38 +02001742 (void) driver;
1743
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 if( slot == NULL )
1745 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001746
1747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001748 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001749 * element, and the failure happened later (when saving metadata
1750 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001751 * element.
1752 * https://github.com/ARMmbed/mbed-crypto/issues/217
1753 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001754
Gilles Peskined7729582019-08-05 15:55:54 +02001755 /* Abort the ongoing transaction if any (there may not be one if
1756 * the creation process failed before starting one, or if the
1757 * key creation is a registration of a key in a secure element).
1758 * Earlier functions must already have done what it takes to undo any
1759 * partial creation. All that's left is to update the transaction data
1760 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001761 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1763
Gilles Peskine4747d192019-04-17 15:05:45 +02001764 psa_wipe_key_slot( slot );
1765}
1766
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001767/** Validate optional attributes during key creation.
1768 *
1769 * Some key attributes are optional during key creation. If they are
1770 * specified in the attributes structure, check that they are consistent
1771 * with the data in the slot.
1772 *
1773 * This function should be called near the end of key creation, after
1774 * the slot in memory is fully populated but before saving persistent data.
1775 */
1776static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001777 const psa_key_slot_t *slot,
1778 const psa_key_attributes_t *attributes )
1779{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001780 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001781 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001782 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 return( PSA_ERROR_INVALID_ARGUMENT );
1784 }
1785
1786 if( attributes->domain_parameters_size != 0 )
1787 {
John Durkop0e005192020-10-31 22:06:54 -07001788#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1789 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001790 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001791 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001792 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001793 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001794 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001795
Ronald Cron90857082020-11-25 14:58:33 +01001796 psa_status_t status = mbedtls_psa_rsa_load_representation(
1797 slot->attr.type,
1798 slot->key.data,
1799 slot->key.bytes,
1800 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001801 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001802 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001803
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 mbedtls_mpi_init( &actual );
1805 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001806 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001807 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001808 mbedtls_rsa_free( rsa );
1809 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 if( ret != 0 )
1811 goto rsa_exit;
1812 ret = mbedtls_mpi_read_binary( &required,
1813 attributes->domain_parameters,
1814 attributes->domain_parameters_size );
1815 if( ret != 0 )
1816 goto rsa_exit;
1817 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1818 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 rsa_exit:
1820 mbedtls_mpi_free( &actual );
1821 mbedtls_mpi_free( &required );
1822 if( ret != 0)
1823 return( mbedtls_to_psa_error( ret ) );
1824 }
1825 else
John Durkop9814fa22020-11-04 12:28:15 -08001826#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1827 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 {
1829 return( PSA_ERROR_INVALID_ARGUMENT );
1830 }
1831 }
1832
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001833 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001834 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001835 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001836 return( PSA_ERROR_INVALID_ARGUMENT );
1837 }
1838
1839 return( PSA_SUCCESS );
1840}
1841
Gilles Peskine4747d192019-04-17 15:05:45 +02001842psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001843 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001844 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001845 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001846{
1847 psa_status_t status;
1848 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001849 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001850 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851
Ronald Cron81709fc2020-11-14 12:10:32 +01001852 *key = MBEDTLS_SVC_KEY_ID_INIT;
1853
Gilles Peskine0f84d622019-09-12 19:03:13 +02001854 /* Reject zero-length symmetric keys (including raw data key objects).
1855 * This also rejects any key which might be encoded as an empty string,
1856 * which is never valid. */
1857 if( data_length == 0 )
1858 return( PSA_ERROR_INVALID_ARGUMENT );
1859
Gilles Peskinedf179142019-07-15 22:02:14 +02001860 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001861 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001862 if( status != PSA_SUCCESS )
1863 goto exit;
1864
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001865 /* In the case of a transparent key or an opaque key stored in local
1866 * storage (thus not in the case of generating a key in a secure element
1867 * or cryptoprocessor with storage), we have to allocate a buffer to
1868 * hold the generated key material. */
1869 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001870 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001871 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001872 if( status != PSA_SUCCESS )
1873 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001874 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001875
1876 bits = slot->attr.bits;
1877 status = psa_driver_wrapper_import_key( attributes,
1878 data, data_length,
1879 slot->key.data,
1880 slot->key.bytes,
1881 &slot->key.bytes, &bits );
1882 if( status != PSA_SUCCESS )
1883 goto exit;
1884
1885 if( slot->attr.bits == 0 )
1886 slot->attr.bits = (psa_key_bits_t) bits;
1887 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001888 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001889 status = PSA_ERROR_INVALID_ARGUMENT;
1890 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001891 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001892
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001893 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001894 if( status != PSA_SUCCESS )
1895 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001896
Ronald Cron81709fc2020-11-14 12:10:32 +01001897 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001899 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001900 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001901
Gilles Peskine4747d192019-04-17 15:05:45 +02001902 return( status );
1903}
1904
Gilles Peskined7729582019-08-05 15:55:54 +02001905#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1906psa_status_t mbedtls_psa_register_se_key(
1907 const psa_key_attributes_t *attributes )
1908{
1909 psa_status_t status;
1910 psa_key_slot_t *slot = NULL;
1911 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001912 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001913
1914 /* Leaving attributes unspecified is not currently supported.
1915 * It could make sense to query the key type and size from the
1916 * secure element, but not all secure elements support this
1917 * and the driver HAL doesn't currently support it. */
1918 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1919 return( PSA_ERROR_NOT_SUPPORTED );
1920 if( psa_get_key_bits( attributes ) == 0 )
1921 return( PSA_ERROR_NOT_SUPPORTED );
1922
1923 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001924 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001925 if( status != PSA_SUCCESS )
1926 goto exit;
1927
Ronald Cron81709fc2020-11-14 12:10:32 +01001928 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001929
1930exit:
1931 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001932 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001933
Gilles Peskined7729582019-08-05 15:55:54 +02001934 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001935 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001936 return( status );
1937}
1938#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1939
Gilles Peskinef603c712019-01-19 13:40:11 +01001940static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001941 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001942{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001943 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001944 source->key.data,
1945 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001947 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001948
Steven Cooreman398aee52020-10-13 14:35:45 +02001949 target->attr.type = source->attr.type;
1950 target->attr.bits = source->attr.bits;
1951
1952 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001953}
1954
Ronald Croncf56a0a2020-08-04 09:51:30 +02001955psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001956 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001957 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001958{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001961 psa_key_slot_t *source_slot = NULL;
1962 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001963 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001964 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001965
Ronald Cron81709fc2020-11-14 12:10:32 +01001966 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1967
Ronald Cron5c522922020-11-14 16:35:34 +01001968 status = psa_get_and_lock_transparent_key_slot_with_policy(
1969 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001970 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001971 goto exit;
1972
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001973 status = psa_validate_optional_attributes( source_slot,
1974 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001975 if( status != PSA_SUCCESS )
1976 goto exit;
1977
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001978 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001979 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001980 if( status != PSA_SUCCESS )
1981 goto exit;
1982
Ronald Cron81709fc2020-11-14 12:10:32 +01001983 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
1984 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001985 if( status != PSA_SUCCESS )
1986 goto exit;
1987
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001988#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1989 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001990 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001991 /* Copying to a secure element is not implemented yet. */
1992 status = PSA_ERROR_NOT_SUPPORTED;
1993 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001995#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001996
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001999 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002000
Ronald Cron81709fc2020-11-14 12:10:32 +01002001 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002003 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002004 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002005
Ronald Cron5c522922020-11-14 16:35:34 +01002006 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002007
Ronald Cron5c522922020-11-14 16:35:34 +01002008 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002009}
2010
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002011
2012
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002013/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002014/* Message digests */
2015/****************************************************************/
2016
John Durkop07cc04a2020-11-16 22:08:34 -08002017#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002018 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2019 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002020 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002021static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002022{
2023 switch( alg )
2024 {
John Durkopee4e6602020-11-27 08:48:46 -08002025#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002026 case PSA_ALG_MD2:
2027 return( &mbedtls_md2_info );
2028#endif
John Durkopee4e6602020-11-27 08:48:46 -08002029#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002030 case PSA_ALG_MD4:
2031 return( &mbedtls_md4_info );
2032#endif
John Durkopee4e6602020-11-27 08:48:46 -08002033#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002034 case PSA_ALG_MD5:
2035 return( &mbedtls_md5_info );
2036#endif
John Durkopee4e6602020-11-27 08:48:46 -08002037#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002038 case PSA_ALG_RIPEMD160:
2039 return( &mbedtls_ripemd160_info );
2040#endif
John Durkopee4e6602020-11-27 08:48:46 -08002041#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002042 case PSA_ALG_SHA_1:
2043 return( &mbedtls_sha1_info );
2044#endif
John Durkopee4e6602020-11-27 08:48:46 -08002045#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002046 case PSA_ALG_SHA_224:
2047 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002048#endif
2049#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002050 case PSA_ALG_SHA_256:
2051 return( &mbedtls_sha256_info );
2052#endif
John Durkopee4e6602020-11-27 08:48:46 -08002053#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002054 case PSA_ALG_SHA_384:
2055 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002056#endif
John Durkopee4e6602020-11-27 08:48:46 -08002057#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_512:
2059 return( &mbedtls_sha512_info );
2060#endif
2061 default:
2062 return( NULL );
2063 }
2064}
John Durkop07cc04a2020-11-16 22:08:34 -08002065#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002066 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2067 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2068 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002069
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002070psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2071{
2072 switch( operation->alg )
2073 {
Gilles Peskine81736312018-06-26 15:04:31 +02002074 case 0:
2075 /* The object has (apparently) been initialized but it is not
2076 * in use. It's ok to call abort on such an object, and there's
2077 * nothing to do. */
2078 break;
John Durkopee4e6602020-11-27 08:48:46 -08002079#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002080 case PSA_ALG_MD2:
2081 mbedtls_md2_free( &operation->ctx.md2 );
2082 break;
2083#endif
John Durkopee4e6602020-11-27 08:48:46 -08002084#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002085 case PSA_ALG_MD4:
2086 mbedtls_md4_free( &operation->ctx.md4 );
2087 break;
2088#endif
John Durkopee4e6602020-11-27 08:48:46 -08002089#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002090 case PSA_ALG_MD5:
2091 mbedtls_md5_free( &operation->ctx.md5 );
2092 break;
2093#endif
John Durkopee4e6602020-11-27 08:48:46 -08002094#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095 case PSA_ALG_RIPEMD160:
2096 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2097 break;
2098#endif
John Durkopee4e6602020-11-27 08:48:46 -08002099#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002100 case PSA_ALG_SHA_1:
2101 mbedtls_sha1_free( &operation->ctx.sha1 );
2102 break;
2103#endif
John Durkop6ca23272020-12-03 06:01:32 -08002104#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002105 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002106 mbedtls_sha256_free( &operation->ctx.sha256 );
2107 break;
2108#endif
2109#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110 case PSA_ALG_SHA_256:
2111 mbedtls_sha256_free( &operation->ctx.sha256 );
2112 break;
2113#endif
John Durkop6ca23272020-12-03 06:01:32 -08002114#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002115 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002116 mbedtls_sha512_free( &operation->ctx.sha512 );
2117 break;
2118#endif
2119#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002120 case PSA_ALG_SHA_512:
2121 mbedtls_sha512_free( &operation->ctx.sha512 );
2122 break;
2123#endif
2124 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 }
2127 operation->alg = 0;
2128 return( PSA_SUCCESS );
2129}
2130
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002131psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002132 psa_algorithm_t alg )
2133{
Janos Follath24eed8d2019-11-22 13:21:35 +00002134 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002135
2136 /* A context must be freshly initialized before it can be set up. */
2137 if( operation->alg != 0 )
2138 {
2139 return( PSA_ERROR_BAD_STATE );
2140 }
2141
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002142 switch( alg )
2143 {
John Durkopee4e6602020-11-27 08:48:46 -08002144#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145 case PSA_ALG_MD2:
2146 mbedtls_md2_init( &operation->ctx.md2 );
2147 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2148 break;
2149#endif
John Durkopee4e6602020-11-27 08:48:46 -08002150#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151 case PSA_ALG_MD4:
2152 mbedtls_md4_init( &operation->ctx.md4 );
2153 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2154 break;
2155#endif
John Durkopee4e6602020-11-27 08:48:46 -08002156#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002157 case PSA_ALG_MD5:
2158 mbedtls_md5_init( &operation->ctx.md5 );
2159 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2160 break;
2161#endif
John Durkopee4e6602020-11-27 08:48:46 -08002162#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163 case PSA_ALG_RIPEMD160:
2164 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2165 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2166 break;
2167#endif
John Durkopee4e6602020-11-27 08:48:46 -08002168#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002169 case PSA_ALG_SHA_1:
2170 mbedtls_sha1_init( &operation->ctx.sha1 );
2171 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2172 break;
2173#endif
John Durkopee4e6602020-11-27 08:48:46 -08002174#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002175 case PSA_ALG_SHA_224:
2176 mbedtls_sha256_init( &operation->ctx.sha256 );
2177 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2178 break;
John Durkopee4e6602020-11-27 08:48:46 -08002179#endif
2180#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 case PSA_ALG_SHA_256:
2182 mbedtls_sha256_init( &operation->ctx.sha256 );
2183 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2184 break;
2185#endif
John Durkopee4e6602020-11-27 08:48:46 -08002186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002187 case PSA_ALG_SHA_384:
2188 mbedtls_sha512_init( &operation->ctx.sha512 );
2189 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2190 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002191#endif
John Durkopee4e6602020-11-27 08:48:46 -08002192#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002193 case PSA_ALG_SHA_512:
2194 mbedtls_sha512_init( &operation->ctx.sha512 );
2195 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2196 break;
2197#endif
2198 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002199 return( PSA_ALG_IS_HASH( alg ) ?
2200 PSA_ERROR_NOT_SUPPORTED :
2201 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 }
2203 if( ret == 0 )
2204 operation->alg = alg;
2205 else
2206 psa_hash_abort( operation );
2207 return( mbedtls_to_psa_error( ret ) );
2208}
2209
2210psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2211 const uint8_t *input,
2212 size_t input_length )
2213{
Janos Follath24eed8d2019-11-22 13:21:35 +00002214 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002215
2216 /* Don't require hash implementations to behave correctly on a
2217 * zero-length input, which may have an invalid pointer. */
2218 if( input_length == 0 )
2219 return( PSA_SUCCESS );
2220
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221 switch( operation->alg )
2222 {
John Durkopee4e6602020-11-27 08:48:46 -08002223#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_MD2:
2225 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2226 input, input_length );
2227 break;
2228#endif
John Durkopee4e6602020-11-27 08:48:46 -08002229#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002230 case PSA_ALG_MD4:
2231 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2232 input, input_length );
2233 break;
2234#endif
John Durkopee4e6602020-11-27 08:48:46 -08002235#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236 case PSA_ALG_MD5:
2237 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2238 input, input_length );
2239 break;
2240#endif
John Durkopee4e6602020-11-27 08:48:46 -08002241#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 case PSA_ALG_RIPEMD160:
2243 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2244 input, input_length );
2245 break;
2246#endif
John Durkopee4e6602020-11-27 08:48:46 -08002247#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 case PSA_ALG_SHA_1:
2249 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2250 input, input_length );
2251 break;
2252#endif
John Durkop6ca23272020-12-03 06:01:32 -08002253#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002255 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2256 input, input_length );
2257 break;
2258#endif
2259#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_SHA_256:
2261 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2262 input, input_length );
2263 break;
2264#endif
John Durkop6ca23272020-12-03 06:01:32 -08002265#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002267 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2268 input, input_length );
2269 break;
2270#endif
2271#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 case PSA_ALG_SHA_512:
2273 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2274 input, input_length );
2275 break;
2276#endif
2277 default:
John Durkopee4e6602020-11-27 08:48:46 -08002278 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002281
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282 if( ret != 0 )
2283 psa_hash_abort( operation );
2284 return( mbedtls_to_psa_error( ret ) );
2285}
2286
2287psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2288 uint8_t *hash,
2289 size_t hash_size,
2290 size_t *hash_length )
2291{
itayzafrir40835d42018-08-02 13:14:17 +03002292 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002293 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002294 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295
2296 /* Fill the output buffer with something that isn't a valid hash
2297 * (barring an attack on the hash and deliberately-crafted input),
2298 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002299 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002300 /* If hash_size is 0 then hash may be NULL and then the
2301 * call to memset would have undefined behavior. */
2302 if( hash_size != 0 )
2303 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304
2305 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002306 {
2307 status = PSA_ERROR_BUFFER_TOO_SMALL;
2308 goto exit;
2309 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310
2311 switch( operation->alg )
2312 {
John Durkopee4e6602020-11-27 08:48:46 -08002313#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 case PSA_ALG_MD2:
2315 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2316 break;
2317#endif
John Durkopee4e6602020-11-27 08:48:46 -08002318#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 case PSA_ALG_MD4:
2320 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2321 break;
2322#endif
John Durkopee4e6602020-11-27 08:48:46 -08002323#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 case PSA_ALG_MD5:
2325 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2326 break;
2327#endif
John Durkopee4e6602020-11-27 08:48:46 -08002328#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_RIPEMD160:
2330 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2331 break;
2332#endif
John Durkopee4e6602020-11-27 08:48:46 -08002333#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 case PSA_ALG_SHA_1:
2335 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2336 break;
2337#endif
John Durkop6ca23272020-12-03 06:01:32 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002340 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2341 break;
2342#endif
2343#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002344 case PSA_ALG_SHA_256:
2345 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2346 break;
2347#endif
John Durkop6ca23272020-12-03 06:01:32 -08002348#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002349 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002350 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2351 break;
2352#endif
2353#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 case PSA_ALG_SHA_512:
2355 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2356 break;
2357#endif
2358 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002359 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 }
itayzafrir40835d42018-08-02 13:14:17 +03002361 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002362
itayzafrir40835d42018-08-02 13:14:17 +03002363exit:
2364 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002366 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367 return( psa_hash_abort( operation ) );
2368 }
2369 else
2370 {
2371 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002372 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373 }
2374}
2375
Gilles Peskine2d277862018-06-18 15:41:12 +02002376psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2377 const uint8_t *hash,
2378 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379{
2380 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2381 size_t actual_hash_length;
2382 psa_status_t status = psa_hash_finish( operation,
2383 actual_hash, sizeof( actual_hash ),
2384 &actual_hash_length );
2385 if( status != PSA_SUCCESS )
2386 return( status );
2387 if( actual_hash_length != hash_length )
2388 return( PSA_ERROR_INVALID_SIGNATURE );
2389 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2390 return( PSA_ERROR_INVALID_SIGNATURE );
2391 return( PSA_SUCCESS );
2392}
2393
Gilles Peskine0a749c82019-11-28 19:33:58 +01002394psa_status_t psa_hash_compute( psa_algorithm_t alg,
2395 const uint8_t *input, size_t input_length,
2396 uint8_t *hash, size_t hash_size,
2397 size_t *hash_length )
2398{
2399 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2401
2402 *hash_length = hash_size;
2403 status = psa_hash_setup( &operation, alg );
2404 if( status != PSA_SUCCESS )
2405 goto exit;
2406 status = psa_hash_update( &operation, input, input_length );
2407 if( status != PSA_SUCCESS )
2408 goto exit;
2409 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2410 if( status != PSA_SUCCESS )
2411 goto exit;
2412
2413exit:
2414 if( status == PSA_SUCCESS )
2415 status = psa_hash_abort( &operation );
2416 else
2417 psa_hash_abort( &operation );
2418 return( status );
2419}
2420
2421psa_status_t psa_hash_compare( psa_algorithm_t alg,
2422 const uint8_t *input, size_t input_length,
2423 const uint8_t *hash, size_t hash_length )
2424{
2425 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2426 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2427
2428 status = psa_hash_setup( &operation, alg );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431 status = psa_hash_update( &operation, input, input_length );
2432 if( status != PSA_SUCCESS )
2433 goto exit;
2434 status = psa_hash_verify( &operation, hash, hash_length );
2435 if( status != PSA_SUCCESS )
2436 goto exit;
2437
2438exit:
2439 if( status == PSA_SUCCESS )
2440 status = psa_hash_abort( &operation );
2441 else
2442 psa_hash_abort( &operation );
2443 return( status );
2444}
2445
Gilles Peskineeb35d782019-01-22 17:56:16 +01002446psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2447 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002448{
2449 if( target_operation->alg != 0 )
2450 return( PSA_ERROR_BAD_STATE );
2451
2452 switch( source_operation->alg )
2453 {
2454 case 0:
2455 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002456#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002457 case PSA_ALG_MD2:
2458 mbedtls_md2_clone( &target_operation->ctx.md2,
2459 &source_operation->ctx.md2 );
2460 break;
2461#endif
John Durkopee4e6602020-11-27 08:48:46 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002463 case PSA_ALG_MD4:
2464 mbedtls_md4_clone( &target_operation->ctx.md4,
2465 &source_operation->ctx.md4 );
2466 break;
2467#endif
John Durkopee4e6602020-11-27 08:48:46 -08002468#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469 case PSA_ALG_MD5:
2470 mbedtls_md5_clone( &target_operation->ctx.md5,
2471 &source_operation->ctx.md5 );
2472 break;
2473#endif
John Durkopee4e6602020-11-27 08:48:46 -08002474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002475 case PSA_ALG_RIPEMD160:
2476 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2477 &source_operation->ctx.ripemd160 );
2478 break;
2479#endif
John Durkopee4e6602020-11-27 08:48:46 -08002480#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002481 case PSA_ALG_SHA_1:
2482 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2483 &source_operation->ctx.sha1 );
2484 break;
2485#endif
John Durkop6ca23272020-12-03 06:01:32 -08002486#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002487 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002488 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2489 &source_operation->ctx.sha256 );
2490 break;
2491#endif
2492#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002493 case PSA_ALG_SHA_256:
2494 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2495 &source_operation->ctx.sha256 );
2496 break;
2497#endif
John Durkop6ca23272020-12-03 06:01:32 -08002498#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002499 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002500 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2501 &source_operation->ctx.sha512 );
2502 break;
2503#endif
2504#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002505 case PSA_ALG_SHA_512:
2506 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2507 &source_operation->ctx.sha512 );
2508 break;
2509#endif
2510 default:
2511 return( PSA_ERROR_NOT_SUPPORTED );
2512 }
2513
2514 target_operation->alg = source_operation->alg;
2515 return( PSA_SUCCESS );
2516}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002517
2518
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520/* MAC */
2521/****************************************************************/
2522
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002523static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 psa_algorithm_t alg,
2525 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002526 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002527 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002530 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002531
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002532 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002533 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002534
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2536 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002537 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002539 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 mode = MBEDTLS_MODE_STREAM;
2541 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002542 case PSA_ALG_CTR:
2543 mode = MBEDTLS_MODE_CTR;
2544 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002545 case PSA_ALG_CFB:
2546 mode = MBEDTLS_MODE_CFB;
2547 break;
2548 case PSA_ALG_OFB:
2549 mode = MBEDTLS_MODE_OFB;
2550 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002551 case PSA_ALG_ECB_NO_PADDING:
2552 mode = MBEDTLS_MODE_ECB;
2553 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002554 case PSA_ALG_CBC_NO_PADDING:
2555 mode = MBEDTLS_MODE_CBC;
2556 break;
2557 case PSA_ALG_CBC_PKCS7:
2558 mode = MBEDTLS_MODE_CBC;
2559 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002560 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 mode = MBEDTLS_MODE_CCM;
2562 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002563 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 mode = MBEDTLS_MODE_GCM;
2565 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002566 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002567 mode = MBEDTLS_MODE_CHACHAPOLY;
2568 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 default:
2570 return( NULL );
2571 }
2572 }
2573 else if( alg == PSA_ALG_CMAC )
2574 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002575 else
2576 return( NULL );
2577
2578 switch( key_type )
2579 {
2580 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002581 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 break;
2583 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002584 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2585 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002586 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002587 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002589 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002590 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2591 * but two-key Triple-DES is functionally three-key Triple-DES
2592 * with K1=K3, so that's how we present it to mbedtls. */
2593 if( key_bits == 128 )
2594 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002595 break;
2596 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002597 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002598 break;
2599 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002600 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002602 case PSA_KEY_TYPE_CHACHA20:
2603 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2604 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 default:
2606 return( NULL );
2607 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002608 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002609 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002610
Jaeden Amero23bbb752018-06-26 14:16:54 +01002611 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2612 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002613}
2614
John Durkop6ba40d12020-11-10 08:50:04 -08002615#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002616static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002617{
Gilles Peskine2d277862018-06-18 15:41:12 +02002618 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002619 {
2620 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002621 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002622 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002623 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002624 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002625 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002626 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002627 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002628 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002629 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002630 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002631 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002632 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002633 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002634 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002635 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002636 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002637 return( 128 );
2638 default:
2639 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002640 }
2641}
John Durkop07cc04a2020-11-16 22:08:34 -08002642#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002643
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644/* Initialize the MAC operation structure. Once this function has been
2645 * called, psa_mac_abort can run and will do the right thing. */
2646static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2647 psa_algorithm_t alg )
2648{
2649 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2650
2651 operation->alg = alg;
2652 operation->key_set = 0;
2653 operation->iv_set = 0;
2654 operation->iv_required = 0;
2655 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002656 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002657
2658#if defined(MBEDTLS_CMAC_C)
2659 if( alg == PSA_ALG_CMAC )
2660 {
2661 operation->iv_required = 0;
2662 mbedtls_cipher_init( &operation->ctx.cmac );
2663 status = PSA_SUCCESS;
2664 }
2665 else
2666#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002667#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002668 if( PSA_ALG_IS_HMAC( operation->alg ) )
2669 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002670 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2671 operation->ctx.hmac.hash_ctx.alg = 0;
2672 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002673 }
2674 else
John Durkopd0321952020-10-29 21:37:36 -07002675#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002677 if( ! PSA_ALG_IS_MAC( alg ) )
2678 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002679 }
2680
2681 if( status != PSA_SUCCESS )
2682 memset( operation, 0, sizeof( *operation ) );
2683 return( status );
2684}
2685
John Durkop6ba40d12020-11-10 08:50:04 -08002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002687static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2688{
Gilles Peskine3f108122018-12-07 18:14:53 +01002689 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002690 return( psa_hash_abort( &hmac->hash_ctx ) );
2691}
John Durkop6ba40d12020-11-10 08:50:04 -08002692#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002693
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2695{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002698 /* The object has (apparently) been initialized but it is not
2699 * in use. It's ok to call abort on such an object, and there's
2700 * nothing to do. */
2701 return( PSA_SUCCESS );
2702 }
2703 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002705 if( operation->alg == PSA_ALG_CMAC )
2706 {
2707 mbedtls_cipher_free( &operation->ctx.cmac );
2708 }
2709 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002711#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712 if( PSA_ALG_IS_HMAC( operation->alg ) )
2713 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715 }
2716 else
John Durkopd0321952020-10-29 21:37:36 -07002717#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002718 {
2719 /* Sanity check (shouldn't happen: operation->alg should
2720 * always have been initialized to a valid value). */
2721 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002722 }
Moran Peker41deec42018-04-04 15:43:05 +03002723
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 operation->alg = 0;
2725 operation->key_set = 0;
2726 operation->iv_set = 0;
2727 operation->iv_required = 0;
2728 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002729 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002730
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002732
2733bad_state:
2734 /* If abort is called on an uninitialized object, we can't trust
2735 * anything. Wipe the object in case it contains confidential data.
2736 * This may result in a memory leak if a pointer gets overwritten,
2737 * but it's too late to do anything about this. */
2738 memset( operation, 0, sizeof( *operation ) );
2739 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740}
2741
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002742#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002743static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002744 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002745 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002746 const mbedtls_cipher_info_t *cipher_info )
2747{
Janos Follath24eed8d2019-11-22 13:21:35 +00002748 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002749
2750 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002751
2752 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2753 if( ret != 0 )
2754 return( ret );
2755
2756 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002757 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 key_bits );
2759 return( ret );
2760}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002761#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002762
John Durkop6ba40d12020-11-10 08:50:04 -08002763#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002764static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2765 const uint8_t *key,
2766 size_t key_length,
2767 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002769 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002771 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002772 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773 psa_status_t status;
2774
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002775 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2776 * overflow below. This should never trigger if the hash algorithm
2777 * is implemented correctly. */
2778 /* The size checks against the ipad and opad buffers cannot be written
2779 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2780 * because that triggers -Wlogical-op on GCC 7.3. */
2781 if( block_size > sizeof( ipad ) )
2782 return( PSA_ERROR_NOT_SUPPORTED );
2783 if( block_size > sizeof( hmac->opad ) )
2784 return( PSA_ERROR_NOT_SUPPORTED );
2785 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 return( PSA_ERROR_NOT_SUPPORTED );
2787
Gilles Peskined223b522018-06-11 18:12:58 +02002788 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002790 status = psa_hash_compute( hash_alg, key, key_length,
2791 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002792 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002793 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794 }
Gilles Peskine96889972018-07-12 17:07:03 +02002795 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2796 * but it is permitted. It is common when HMAC is used in HKDF, for
2797 * example. Don't call `memcpy` in the 0-length because `key` could be
2798 * an invalid pointer which would make the behavior undefined. */
2799 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002800 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002801
Gilles Peskined223b522018-06-11 18:12:58 +02002802 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2803 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002804 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002805 ipad[i] ^= 0x36;
2806 memset( ipad + key_length, 0x36, block_size - key_length );
2807
2808 /* Copy the key material from ipad to opad, flipping the requisite bits,
2809 * and filling the rest of opad with the requisite constant. */
2810 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002811 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2812 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813
Gilles Peskine01126fa2018-07-12 17:04:55 +02002814 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002816 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002817
Gilles Peskine01126fa2018-07-12 17:04:55 +02002818 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002819
2820cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002821 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002822
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002823 return( status );
2824}
John Durkop6ba40d12020-11-10 08:50:04 -08002825#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002826
Gilles Peskine89167cb2018-07-08 20:12:23 +02002827static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002828 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002829 psa_algorithm_t alg,
2830 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002832 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002833 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002834 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002835 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002836 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002837 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002838 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002839 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002841 /* A context must be freshly initialized before it can be set up. */
2842 if( operation->alg != 0 )
2843 {
2844 return( PSA_ERROR_BAD_STATE );
2845 }
2846
Gilles Peskined911eb72018-08-14 15:18:45 +02002847 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002848 if( status != PSA_SUCCESS )
2849 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002850 if( is_sign )
2851 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852
Ronald Cron5c522922020-11-14 16:35:34 +01002853 status = psa_get_and_lock_transparent_key_slot_with_policy(
2854 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002855 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002856 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002857 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002858
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002860 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861 {
2862 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002863 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002864 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 if( cipher_info == NULL )
2867 {
2868 status = PSA_ERROR_NOT_SUPPORTED;
2869 goto exit;
2870 }
2871 operation->mac_size = cipher_info->block_size;
2872 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2873 status = mbedtls_to_psa_error( ret );
2874 }
2875 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002877#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002878 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002879 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002880 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002881 if( hash_alg == 0 )
2882 {
2883 status = PSA_ERROR_NOT_SUPPORTED;
2884 goto exit;
2885 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002886
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002887 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002888 /* Sanity check. This shouldn't fail on a valid configuration. */
2889 if( operation->mac_size == 0 ||
2890 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2891 {
2892 status = PSA_ERROR_NOT_SUPPORTED;
2893 goto exit;
2894 }
2895
Gilles Peskine8e338702019-07-30 20:06:31 +02002896 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897 {
2898 status = PSA_ERROR_INVALID_ARGUMENT;
2899 goto exit;
2900 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002901
Gilles Peskine01126fa2018-07-12 17:04:55 +02002902 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002903 slot->key.data,
2904 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002905 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002906 }
2907 else
John Durkopd0321952020-10-29 21:37:36 -07002908#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002909 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002910 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002911 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002912 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002913
Gilles Peskined911eb72018-08-14 15:18:45 +02002914 if( truncated == 0 )
2915 {
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002916 /* The "normal" case: untruncated algorithm. Re-validate the
2917 * key policy with explicit MAC length set in the algorithm
2918 * when the algorithm policy is at-least-this-length to catch
2919 * a corner case due to the default MAC length being unknown
2920 * at key loading time. */
2921 if( PSA_ALG_IS_MAC( slot->attr.policy.alg ) &&
2922 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg ) == full_length_alg ) &&
2923 ( slot->attr.policy.alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2924 {
2925 /* validate policy length */
2926 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg ) > operation->mac_size )
2927 status = PSA_ERROR_NOT_PERMITTED;
2928 }
2929
2930 if( PSA_ALG_IS_MAC( slot->attr.policy.alg2 ) &&
2931 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg2 ) == full_length_alg ) &&
2932 ( slot->attr.policy.alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2933 {
2934 /* validate policy length */
2935 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg2 ) > operation->mac_size )
2936 status = PSA_ERROR_NOT_PERMITTED;
2937 }
Gilles Peskined911eb72018-08-14 15:18:45 +02002938 }
2939 else if( truncated < 4 )
2940 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002941 /* A very short MAC is too short for security since it can be
2942 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2943 * so we make this our minimum, even though 32 bits is still
2944 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002945 status = PSA_ERROR_NOT_SUPPORTED;
2946 }
2947 else if( truncated > operation->mac_size )
2948 {
2949 /* It's impossible to "truncate" to a larger length. */
2950 status = PSA_ERROR_INVALID_ARGUMENT;
2951 }
2952 else
2953 operation->mac_size = truncated;
2954
Gilles Peskinefbfac682018-07-08 20:51:54 +02002955exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002956 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002958 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002960 else
2961 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962 operation->key_set = 1;
2963 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002964
Ronald Cron5c522922020-11-14 16:35:34 +01002965 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002966
Ronald Cron5c522922020-11-14 16:35:34 +01002967 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968}
2969
Gilles Peskine89167cb2018-07-08 20:12:23 +02002970psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002971 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002972 psa_algorithm_t alg )
2973{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002974 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002975}
2976
2977psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002978 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002979 psa_algorithm_t alg )
2980{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002981 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002982}
2983
Gilles Peskine8c9def32018-02-08 10:02:12 +01002984psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2985 const uint8_t *input,
2986 size_t input_length )
2987{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002988 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002989 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002990 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002992 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002993 operation->has_input = 1;
2994
Gilles Peskine8c9def32018-02-08 10:02:12 +01002995#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002996 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002997 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002998 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2999 input, input_length );
3000 status = mbedtls_to_psa_error( ret );
3001 }
3002 else
3003#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003004#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003005 if( PSA_ALG_IS_HMAC( operation->alg ) )
3006 {
3007 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3008 input_length );
3009 }
3010 else
John Durkopd0321952020-10-29 21:37:36 -07003011#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003012 {
3013 /* This shouldn't happen if `operation` was initialized by
3014 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003015 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003016 }
3017
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 if( status != PSA_SUCCESS )
3019 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003020 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021}
3022
John Durkop6ba40d12020-11-10 08:50:04 -08003023#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003024static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3025 uint8_t *mac,
3026 size_t mac_size )
3027{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003028 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003029 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3030 size_t hash_size = 0;
3031 size_t block_size = psa_get_hash_block_size( hash_alg );
3032 psa_status_t status;
3033
Gilles Peskine01126fa2018-07-12 17:04:55 +02003034 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3035 if( status != PSA_SUCCESS )
3036 return( status );
3037 /* From here on, tmp needs to be wiped. */
3038
3039 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3040 if( status != PSA_SUCCESS )
3041 goto exit;
3042
3043 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3044 if( status != PSA_SUCCESS )
3045 goto exit;
3046
3047 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3048 if( status != PSA_SUCCESS )
3049 goto exit;
3050
Gilles Peskined911eb72018-08-14 15:18:45 +02003051 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3052 if( status != PSA_SUCCESS )
3053 goto exit;
3054
3055 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003056
3057exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003058 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003059 return( status );
3060}
John Durkop6ba40d12020-11-10 08:50:04 -08003061#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003062
mohammad16036df908f2018-04-02 08:34:15 -07003063static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003064 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003067 if( ! operation->key_set )
3068 return( PSA_ERROR_BAD_STATE );
3069 if( operation->iv_required && ! operation->iv_set )
3070 return( PSA_ERROR_BAD_STATE );
3071
Gilles Peskine8c9def32018-02-08 10:02:12 +01003072 if( mac_size < operation->mac_size )
3073 return( PSA_ERROR_BUFFER_TOO_SMALL );
3074
Gilles Peskine8c9def32018-02-08 10:02:12 +01003075#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003076 if( operation->alg == PSA_ALG_CMAC )
3077 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003078 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003079 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3080 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003081 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003082 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003083 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003085 else
3086#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003087#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003088 if( PSA_ALG_IS_HMAC( operation->alg ) )
3089 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003090 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003091 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 }
3093 else
John Durkopd0321952020-10-29 21:37:36 -07003094#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003095 {
3096 /* This shouldn't happen if `operation` was initialized by
3097 * a setup function. */
3098 return( PSA_ERROR_BAD_STATE );
3099 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003100}
3101
Gilles Peskineacd4be32018-07-08 19:56:25 +02003102psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3103 uint8_t *mac,
3104 size_t mac_size,
3105 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003106{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107 psa_status_t status;
3108
Jaeden Amero252ef282019-02-15 14:05:35 +00003109 if( operation->alg == 0 )
3110 {
3111 return( PSA_ERROR_BAD_STATE );
3112 }
3113
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003114 /* Fill the output buffer with something that isn't a valid mac
3115 * (barring an attack on the mac and deliberately-crafted input),
3116 * in case the caller doesn't check the return status properly. */
3117 *mac_length = mac_size;
3118 /* If mac_size is 0 then mac may be NULL and then the
3119 * call to memset would have undefined behavior. */
3120 if( mac_size != 0 )
3121 memset( mac, '!', mac_size );
3122
Gilles Peskine89167cb2018-07-08 20:12:23 +02003123 if( ! operation->is_sign )
3124 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003125 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003126 }
mohammad16036df908f2018-04-02 08:34:15 -07003127
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003128 status = psa_mac_finish_internal( operation, mac, mac_size );
3129
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003130 if( status == PSA_SUCCESS )
3131 {
3132 status = psa_mac_abort( operation );
3133 if( status == PSA_SUCCESS )
3134 *mac_length = operation->mac_size;
3135 else
3136 memset( mac, '!', mac_size );
3137 }
3138 else
3139 psa_mac_abort( operation );
3140 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003141}
3142
Gilles Peskineacd4be32018-07-08 19:56:25 +02003143psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3144 const uint8_t *mac,
3145 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003146{
Gilles Peskine828ed142018-06-18 23:25:51 +02003147 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003148 psa_status_t status;
3149
Jaeden Amero252ef282019-02-15 14:05:35 +00003150 if( operation->alg == 0 )
3151 {
3152 return( PSA_ERROR_BAD_STATE );
3153 }
3154
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155 if( operation->is_sign )
3156 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003157 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003158 }
3159 if( operation->mac_size != mac_length )
3160 {
3161 status = PSA_ERROR_INVALID_SIGNATURE;
3162 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003163 }
mohammad16036df908f2018-04-02 08:34:15 -07003164
3165 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003166 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003167 if( status != PSA_SUCCESS )
3168 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003169
3170 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3171 status = PSA_ERROR_INVALID_SIGNATURE;
3172
3173cleanup:
3174 if( status == PSA_SUCCESS )
3175 status = psa_mac_abort( operation );
3176 else
3177 psa_mac_abort( operation );
3178
Gilles Peskine3f108122018-12-07 18:14:53 +01003179 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003180
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003181 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182}
3183
3184
Gilles Peskine20035e32018-02-03 22:44:14 +01003185
Gilles Peskine20035e32018-02-03 22:44:14 +01003186/****************************************************************/
3187/* Asymmetric cryptography */
3188/****************************************************************/
3189
John Durkop6ba40d12020-11-10 08:50:04 -08003190#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3191 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003192/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003193 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003194static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3195 size_t hash_length,
3196 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003197{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003198 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003199 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003200 *md_alg = mbedtls_md_get_type( md_info );
3201
3202 /* The Mbed TLS RSA module uses an unsigned int for hash length
3203 * parameters. Validate that it fits so that we don't risk an
3204 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003205#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003206 if( hash_length > UINT_MAX )
3207 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003208#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003209
John Durkop0e005192020-10-31 22:06:54 -07003210#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003211 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3212 * must be correct. */
3213 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3214 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003215 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003216 if( md_info == NULL )
3217 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003218 if( mbedtls_md_get_size( md_info ) != hash_length )
3219 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003220 }
John Durkop0e005192020-10-31 22:06:54 -07003221#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003222
John Durkop0e005192020-10-31 22:06:54 -07003223#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003224 /* PSS requires a hash internally. */
3225 if( PSA_ALG_IS_RSA_PSS( alg ) )
3226 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003227 if( md_info == NULL )
3228 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003229 }
John Durkop0e005192020-10-31 22:06:54 -07003230#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003231
Gilles Peskine61b91d42018-06-08 16:09:36 +02003232 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003233}
3234
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3236 psa_algorithm_t alg,
3237 const uint8_t *hash,
3238 size_t hash_length,
3239 uint8_t *signature,
3240 size_t signature_size,
3241 size_t *signature_length )
3242{
3243 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003244 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003245 mbedtls_md_type_t md_alg;
3246
3247 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3248 if( status != PSA_SUCCESS )
3249 return( status );
3250
Gilles Peskine630a18a2018-06-29 17:49:35 +02003251 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003252 return( PSA_ERROR_BUFFER_TOO_SMALL );
3253
John Durkop0e005192020-10-31 22:06:54 -07003254#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003255 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3256 {
3257 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3258 MBEDTLS_MD_NONE );
3259 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003260 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003261 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003262 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003263 md_alg,
3264 (unsigned int) hash_length,
3265 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 signature );
3267 }
3268 else
John Durkop0e005192020-10-31 22:06:54 -07003269#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3270#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003271 if( PSA_ALG_IS_RSA_PSS( alg ) )
3272 {
3273 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3274 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003275 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003276 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003277 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003278 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003279 (unsigned int) hash_length,
3280 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003281 signature );
3282 }
3283 else
John Durkop0e005192020-10-31 22:06:54 -07003284#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003285 {
3286 return( PSA_ERROR_INVALID_ARGUMENT );
3287 }
3288
3289 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003290 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003291 return( mbedtls_to_psa_error( ret ) );
3292}
3293
3294static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3295 psa_algorithm_t alg,
3296 const uint8_t *hash,
3297 size_t hash_length,
3298 const uint8_t *signature,
3299 size_t signature_length )
3300{
3301 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003302 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003303 mbedtls_md_type_t md_alg;
3304
3305 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3306 if( status != PSA_SUCCESS )
3307 return( status );
3308
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003309 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3310 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003311
John Durkop0e005192020-10-31 22:06:54 -07003312#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003313 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3314 {
3315 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3316 MBEDTLS_MD_NONE );
3317 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003318 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003319 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003320 MBEDTLS_RSA_PUBLIC,
3321 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003322 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003323 hash,
3324 signature );
3325 }
3326 else
John Durkop0e005192020-10-31 22:06:54 -07003327#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3328#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003329 if( PSA_ALG_IS_RSA_PSS( alg ) )
3330 {
3331 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3332 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003333 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003334 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003335 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003336 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003337 (unsigned int) hash_length,
3338 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003339 signature );
3340 }
3341 else
John Durkop0e005192020-10-31 22:06:54 -07003342#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003343 {
3344 return( PSA_ERROR_INVALID_ARGUMENT );
3345 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003346
3347 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3348 * the rest of the signature is invalid". This has little use in
3349 * practice and PSA doesn't report this distinction. */
3350 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3351 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003352 return( mbedtls_to_psa_error( ret ) );
3353}
John Durkop6ba40d12020-11-10 08:50:04 -08003354#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3355 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003356
John Durkop6ba40d12020-11-10 08:50:04 -08003357#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3358 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003359/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3360 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3361 * (even though these functions don't modify it). */
3362static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3363 psa_algorithm_t alg,
3364 const uint8_t *hash,
3365 size_t hash_length,
3366 uint8_t *signature,
3367 size_t signature_size,
3368 size_t *signature_length )
3369{
Janos Follath24eed8d2019-11-22 13:21:35 +00003370 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003371 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003372 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003373 mbedtls_mpi_init( &r );
3374 mbedtls_mpi_init( &s );
3375
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003376 if( signature_size < 2 * curve_bytes )
3377 {
3378 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3379 goto cleanup;
3380 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003381
John Durkop0ea39e02020-10-13 19:58:20 -07003382#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003383 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3384 {
3385 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3386 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3387 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003388 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3389 &ecp->d, hash,
3390 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003391 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003392 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003393 }
3394 else
John Durkop0ea39e02020-10-13 19:58:20 -07003395#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003396 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003397 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003398 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3399 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003400 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003401 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003402 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003403
3404 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3405 signature,
3406 curve_bytes ) );
3407 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3408 signature + curve_bytes,
3409 curve_bytes ) );
3410
3411cleanup:
3412 mbedtls_mpi_free( &r );
3413 mbedtls_mpi_free( &s );
3414 if( ret == 0 )
3415 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003416 return( mbedtls_to_psa_error( ret ) );
3417}
3418
3419static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3420 const uint8_t *hash,
3421 size_t hash_length,
3422 const uint8_t *signature,
3423 size_t signature_length )
3424{
Janos Follath24eed8d2019-11-22 13:21:35 +00003425 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003426 mbedtls_mpi r, s;
3427 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3428 mbedtls_mpi_init( &r );
3429 mbedtls_mpi_init( &s );
3430
3431 if( signature_length != 2 * curve_bytes )
3432 return( PSA_ERROR_INVALID_SIGNATURE );
3433
3434 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3435 signature,
3436 curve_bytes ) );
3437 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3438 signature + curve_bytes,
3439 curve_bytes ) );
3440
Steven Cooremanacda8342020-07-24 23:09:52 +02003441 /* Check whether the public part is loaded. If not, load it. */
3442 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3443 {
3444 MBEDTLS_MPI_CHK(
3445 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003446 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003447 }
3448
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003449 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3450 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003451
3452cleanup:
3453 mbedtls_mpi_free( &r );
3454 mbedtls_mpi_free( &s );
3455 return( mbedtls_to_psa_error( ret ) );
3456}
John Durkop6ba40d12020-11-10 08:50:04 -08003457#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3458 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003459
Ronald Croncf56a0a2020-08-04 09:51:30 +02003460psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003461 psa_algorithm_t alg,
3462 const uint8_t *hash,
3463 size_t hash_length,
3464 uint8_t *signature,
3465 size_t signature_size,
3466 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003467{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003468 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003469 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003470 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003471
3472 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003473 /* Immediately reject a zero-length signature buffer. This guarantees
3474 * that signature must be a valid pointer. (On the other hand, the hash
3475 * buffer can in principle be empty since it doesn't actually have
3476 * to be a hash.) */
3477 if( signature_size == 0 )
3478 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003479
Ronald Cron5c522922020-11-14 16:35:34 +01003480 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3481 PSA_KEY_USAGE_SIGN_HASH,
3482 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003483 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003484 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003485 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003486 {
3487 status = PSA_ERROR_INVALID_ARGUMENT;
3488 goto exit;
3489 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003490
Steven Cooremancd84cb42020-07-16 20:28:36 +02003491 /* Try any of the available accelerators first */
3492 status = psa_driver_wrapper_sign_hash( slot,
3493 alg,
3494 hash,
3495 hash_length,
3496 signature,
3497 signature_size,
3498 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003499 if( status != PSA_ERROR_NOT_SUPPORTED ||
3500 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003501 goto exit;
3502
Steven Cooreman7a250572020-07-17 16:43:05 +02003503 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003504#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3505 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003506 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003507 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003508 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003509
Ronald Cron90857082020-11-25 14:58:33 +01003510 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3511 slot->key.data,
3512 slot->key.bytes,
3513 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003514 if( status != PSA_SUCCESS )
3515 goto exit;
3516
Steven Cooremana2371e52020-07-28 14:30:39 +02003517 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003518 alg,
3519 hash, hash_length,
3520 signature, signature_size,
3521 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003522
Steven Cooremana2371e52020-07-28 14:30:39 +02003523 mbedtls_rsa_free( rsa );
3524 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003525 }
3526 else
John Durkop6ba40d12020-11-10 08:50:04 -08003527#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3528 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003529 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003530 {
John Durkop9814fa22020-11-04 12:28:15 -08003531#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3532 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003533 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003534#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003535 PSA_ALG_IS_ECDSA( alg )
3536#else
3537 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3538#endif
3539 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003540 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003541 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003542 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003543 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003544 slot->key.data,
3545 slot->key.bytes,
3546 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003547 if( status != PSA_SUCCESS )
3548 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003549 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003550 alg,
3551 hash, hash_length,
3552 signature, signature_size,
3553 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003554 mbedtls_ecp_keypair_free( ecp );
3555 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003556 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003557 else
John Durkop9814fa22020-11-04 12:28:15 -08003558#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3559 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003560 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003561 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003562 }
itayzafrir5c753392018-05-08 11:18:38 +03003563 }
3564 else
itayzafrir5c753392018-05-08 11:18:38 +03003565 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003566 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003567 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003568
3569exit:
3570 /* Fill the unused part of the output buffer (the whole buffer on error,
3571 * the trailing part on success) with something that isn't a valid mac
3572 * (barring an attack on the mac and deliberately-crafted input),
3573 * in case the caller doesn't check the return status properly. */
3574 if( status == PSA_SUCCESS )
3575 memset( signature + *signature_length, '!',
3576 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003577 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003578 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003579 /* If signature_size is 0 then we have nothing to do. We must not call
3580 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003581
Ronald Cron5c522922020-11-14 16:35:34 +01003582 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003583
Ronald Cron5c522922020-11-14 16:35:34 +01003584 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003585}
3586
Ronald Croncf56a0a2020-08-04 09:51:30 +02003587psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003588 psa_algorithm_t alg,
3589 const uint8_t *hash,
3590 size_t hash_length,
3591 const uint8_t *signature,
3592 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003593{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003594 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003595 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003596 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003597
Ronald Cron5c522922020-11-14 16:35:34 +01003598 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3599 PSA_KEY_USAGE_VERIFY_HASH,
3600 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003601 if( status != PSA_SUCCESS )
3602 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003603
Steven Cooreman55ae2172020-07-17 19:46:15 +02003604 /* Try any of the available accelerators first */
3605 status = psa_driver_wrapper_verify_hash( slot,
3606 alg,
3607 hash,
3608 hash_length,
3609 signature,
3610 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003611 if( status != PSA_ERROR_NOT_SUPPORTED ||
3612 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003613 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003614
John Durkop6ba40d12020-11-10 08:50:04 -08003615#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3616 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003617 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003618 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003619 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003620
Ronald Cron90857082020-11-25 14:58:33 +01003621 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3622 slot->key.data,
3623 slot->key.bytes,
3624 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003625 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003626 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003627
Steven Cooremana2371e52020-07-28 14:30:39 +02003628 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003629 alg,
3630 hash, hash_length,
3631 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003632 mbedtls_rsa_free( rsa );
3633 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003634 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 }
3636 else
John Durkop6ba40d12020-11-10 08:50:04 -08003637#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3638 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003639 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003640 {
John Durkop9814fa22020-11-04 12:28:15 -08003641#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3642 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003643 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003644 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003645 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003646 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003647 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003648 slot->key.data,
3649 slot->key.bytes,
3650 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003651 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003652 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003653 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003654 hash, hash_length,
3655 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003656 mbedtls_ecp_keypair_free( ecp );
3657 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003658 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003659 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003660 else
John Durkop9814fa22020-11-04 12:28:15 -08003661#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3662 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003663 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003664 status = PSA_ERROR_INVALID_ARGUMENT;
3665 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003666 }
itayzafrir5c753392018-05-08 11:18:38 +03003667 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003668 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003669 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003670 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003671 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003672
3673exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003674 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003675
Ronald Cron5c522922020-11-14 16:35:34 +01003676 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003677}
3678
John Durkop0e005192020-10-31 22:06:54 -07003679#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003680static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3681 mbedtls_rsa_context *rsa )
3682{
3683 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3684 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3685 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3686 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3687}
John Durkop0e005192020-10-31 22:06:54 -07003688#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003689
Ronald Croncf56a0a2020-08-04 09:51:30 +02003690psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003691 psa_algorithm_t alg,
3692 const uint8_t *input,
3693 size_t input_length,
3694 const uint8_t *salt,
3695 size_t salt_length,
3696 uint8_t *output,
3697 size_t output_size,
3698 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003700 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003701 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003702 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003703
Darryl Green5cc689a2018-07-24 15:34:10 +01003704 (void) input;
3705 (void) input_length;
3706 (void) salt;
3707 (void) output;
3708 (void) output_size;
3709
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003710 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003712 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3713 return( PSA_ERROR_INVALID_ARGUMENT );
3714
Ronald Cron5c522922020-11-14 16:35:34 +01003715 status = psa_get_and_lock_transparent_key_slot_with_policy(
3716 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003717 if( status != PSA_SUCCESS )
3718 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003719 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3720 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003721 {
3722 status = PSA_ERROR_INVALID_ARGUMENT;
3723 goto exit;
3724 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003725
John Durkop6ba40d12020-11-10 08:50:04 -08003726#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3727 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003728 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003729 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003730 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003731 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3732 slot->key.data,
3733 slot->key.bytes,
3734 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003735 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003736 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003737
3738 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003739 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003740 status = PSA_ERROR_BUFFER_TOO_SMALL;
3741 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003742 }
John Durkop0e005192020-10-31 22:06:54 -07003743#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003744 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003745 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003746 status = mbedtls_to_psa_error(
3747 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003748 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003749 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003750 MBEDTLS_RSA_PUBLIC,
3751 input_length,
3752 input,
3753 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754 }
3755 else
John Durkop0e005192020-10-31 22:06:54 -07003756#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3757#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003758 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003759 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003760 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003761 status = mbedtls_to_psa_error(
3762 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003763 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003764 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003765 MBEDTLS_RSA_PUBLIC,
3766 salt, salt_length,
3767 input_length,
3768 input,
3769 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003770 }
3771 else
John Durkop0e005192020-10-31 22:06:54 -07003772#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003774 status = PSA_ERROR_INVALID_ARGUMENT;
3775 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003776 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003777rsa_exit:
3778 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003779 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003780
Steven Cooremana2371e52020-07-28 14:30:39 +02003781 mbedtls_rsa_free( rsa );
3782 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003784 else
John Durkop9814fa22020-11-04 12:28:15 -08003785#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003786 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003787 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003788 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003789 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003790
3791exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003792 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003793
Ronald Cron5c522922020-11-14 16:35:34 +01003794 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003795}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003796
Ronald Croncf56a0a2020-08-04 09:51:30 +02003797psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003798 psa_algorithm_t alg,
3799 const uint8_t *input,
3800 size_t input_length,
3801 const uint8_t *salt,
3802 size_t salt_length,
3803 uint8_t *output,
3804 size_t output_size,
3805 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003806{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003807 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003808 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003809 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003810
Darryl Green5cc689a2018-07-24 15:34:10 +01003811 (void) input;
3812 (void) input_length;
3813 (void) salt;
3814 (void) output;
3815 (void) output_size;
3816
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003817 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003818
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003819 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3820 return( PSA_ERROR_INVALID_ARGUMENT );
3821
Ronald Cron5c522922020-11-14 16:35:34 +01003822 status = psa_get_and_lock_transparent_key_slot_with_policy(
3823 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003824 if( status != PSA_SUCCESS )
3825 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003826 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003827 {
3828 status = PSA_ERROR_INVALID_ARGUMENT;
3829 goto exit;
3830 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831
John Durkop6ba40d12020-11-10 08:50:04 -08003832#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3833 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003834 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003835 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003836 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003837 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3838 slot->key.data,
3839 slot->key.bytes,
3840 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003841 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003842 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003843
Steven Cooremana2371e52020-07-28 14:30:39 +02003844 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003845 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003846 status = PSA_ERROR_INVALID_ARGUMENT;
3847 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003848 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003849
John Durkop0e005192020-10-31 22:06:54 -07003850#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003851 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003852 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003853 status = mbedtls_to_psa_error(
3854 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003855 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003856 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003857 MBEDTLS_RSA_PRIVATE,
3858 output_length,
3859 input,
3860 output,
3861 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862 }
3863 else
John Durkop0e005192020-10-31 22:06:54 -07003864#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3865#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003866 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003867 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003868 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003869 status = mbedtls_to_psa_error(
3870 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003871 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003872 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003873 MBEDTLS_RSA_PRIVATE,
3874 salt, salt_length,
3875 output_length,
3876 input,
3877 output,
3878 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879 }
3880 else
John Durkop0e005192020-10-31 22:06:54 -07003881#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003882 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003883 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003885
Steven Cooreman4fed4552020-08-03 14:46:03 +02003886rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003887 mbedtls_rsa_free( rsa );
3888 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003890 else
John Durkop6ba40d12020-11-10 08:50:04 -08003891#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3892 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003894 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003896
3897exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003898 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003899
Ronald Cron5c522922020-11-14 16:35:34 +01003900 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003901}
Gilles Peskine20035e32018-02-03 22:44:14 +01003902
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003903
3904
mohammad1603503973b2018-03-12 15:59:30 +02003905/****************************************************************/
3906/* Symmetric cryptography */
3907/****************************************************************/
3908
Gilles Peskinee553c652018-06-04 16:22:46 +02003909static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003910 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003911 psa_algorithm_t alg,
3912 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003913{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003914 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003915 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003916 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003917 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003918 size_t key_bits;
3919 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003920 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3921 PSA_KEY_USAGE_ENCRYPT :
3922 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003923
Steven Cooremand3feccd2020-09-01 15:56:14 +02003924 /* A context must be freshly initialized before it can be set up. */
3925 if( operation->alg != 0 )
3926 return( PSA_ERROR_BAD_STATE );
3927
Steven Cooremana07b9972020-09-10 14:54:14 +02003928 /* The requested algorithm must be one that can be processed by cipher. */
3929 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003930 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003931
Steven Cooremanef8575e2020-09-11 11:44:50 +02003932 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003933 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003934 if( status != PSA_SUCCESS )
3935 goto exit;
3936
3937 /* Initialize the operation struct members, except for alg. The alg member
3938 * is used to indicate to psa_cipher_abort that there are resources to free,
3939 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003940 operation->key_set = 0;
3941 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003942 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003943 operation->iv_size = 0;
3944 operation->block_size = 0;
3945 if( alg == PSA_ALG_ECB_NO_PADDING )
3946 operation->iv_required = 0;
3947 else
3948 operation->iv_required = 1;
3949
Steven Cooremana07b9972020-09-10 14:54:14 +02003950 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003951 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003952 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003953 slot,
3954 alg );
3955 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003956 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003957 slot,
3958 alg );
3959
Steven Cooremanef8575e2020-09-11 11:44:50 +02003960 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003961 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003962 /* Once the driver context is initialised, it needs to be freed using
3963 * psa_cipher_abort. Indicate this through setting alg. */
3964 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003965 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003966
Steven Cooremand3feccd2020-09-01 15:56:14 +02003967 if( status != PSA_ERROR_NOT_SUPPORTED ||
3968 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3969 goto exit;
3970
Steven Cooremana07b9972020-09-10 14:54:14 +02003971 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003972 * available for the given algorithm & key. */
3973 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003974
Steven Cooremana07b9972020-09-10 14:54:14 +02003975 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003976 * psa_cipher_abort. Indicate there is something to be freed through setting
3977 * alg, and indicate the operation is being done using mbedtls crypto through
3978 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003979 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003980 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003981
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003982 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003983 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003984 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003985 {
3986 status = PSA_ERROR_NOT_SUPPORTED;
3987 goto exit;
3988 }
mohammad1603503973b2018-03-12 15:59:30 +02003989
mohammad1603503973b2018-03-12 15:59:30 +02003990 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003991 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003992 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003993
David Brown12ca5032021-02-11 11:02:00 -07003994#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003995 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003996 {
3997 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003998 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003999 memcpy( keys, slot->key.data, 16 );
4000 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004001 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4002 keys,
4003 192, cipher_operation );
4004 }
4005 else
4006#endif
4007 {
4008 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004009 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004010 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004011 }
Moran Peker41deec42018-04-04 15:43:05 +03004012 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004013 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004014
David Brown63ca2602021-01-26 11:51:12 -07004015#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4016 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004017 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004018 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004019 case PSA_ALG_CBC_NO_PADDING:
4020 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4021 MBEDTLS_PADDING_NONE );
4022 break;
4023 case PSA_ALG_CBC_PKCS7:
4024 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4025 MBEDTLS_PADDING_PKCS7 );
4026 break;
4027 default:
4028 /* The algorithm doesn't involve padding. */
4029 ret = 0;
4030 break;
4031 }
4032 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004033 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004034#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004035
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004036 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004037 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004038 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4039 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004040 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004041 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004042 }
David Brown1bfe4d72021-02-16 12:54:35 -07004043#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004044 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004045 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004046 operation->iv_size = 12;
4047#endif
mohammad1603503973b2018-03-12 15:59:30 +02004048
Steven Cooreman7df02922020-09-09 15:28:49 +02004049 status = PSA_SUCCESS;
4050
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004051exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004052 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004053 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004054 if( status == PSA_SUCCESS )
4055 {
4056 /* Update operation flags for both driver and software implementations */
4057 operation->key_set = 1;
4058 }
4059 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004060 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004061
Ronald Cron5c522922020-11-14 16:35:34 +01004062 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004063
Ronald Cron5c522922020-11-14 16:35:34 +01004064 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004065}
4066
Gilles Peskinefe119512018-07-08 21:39:34 +02004067psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004068 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004069 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004070{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004071 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004072}
4073
Gilles Peskinefe119512018-07-08 21:39:34 +02004074psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004075 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004076 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004077{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004078 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004079}
4080
Gilles Peskinefe119512018-07-08 21:39:34 +02004081psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004082 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004083 size_t iv_size,
4084 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004085{
itayzafrir534bd7c2018-08-02 13:56:32 +03004086 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004087 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004088 if( operation->iv_set || ! operation->iv_required )
4089 {
4090 return( PSA_ERROR_BAD_STATE );
4091 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004092
Steven Cooremanef8575e2020-09-11 11:44:50 +02004093 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004094 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004095 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004096 iv,
4097 iv_size,
4098 iv_length );
4099 goto exit;
4100 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004101
Moran Peker41deec42018-04-04 15:43:05 +03004102 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004103 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004104 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004105 goto exit;
4106 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004107 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004108 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004109 if( ret != 0 )
4110 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004111 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004112 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004113 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004114
mohammad16038481e742018-03-18 13:57:31 +02004115 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004116 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004117
Moran Peker395db872018-05-31 14:07:14 +03004118exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004119 if( status == PSA_SUCCESS )
4120 operation->iv_set = 1;
4121 else
Moran Peker395db872018-05-31 14:07:14 +03004122 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004123 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004124}
4125
Gilles Peskinefe119512018-07-08 21:39:34 +02004126psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004127 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004128 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004129{
itayzafrir534bd7c2018-08-02 13:56:32 +03004130 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004131 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004132 if( operation->iv_set || ! operation->iv_required )
4133 {
4134 return( PSA_ERROR_BAD_STATE );
4135 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004136
Steven Cooremanef8575e2020-09-11 11:44:50 +02004137 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004138 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004139 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004140 iv,
4141 iv_length );
4142 goto exit;
4143 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004144
Moran Pekera28258c2018-05-29 16:25:04 +03004145 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004146 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004147 status = PSA_ERROR_INVALID_ARGUMENT;
4148 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004149 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004150 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4151 status = mbedtls_to_psa_error( ret );
4152exit:
4153 if( status == PSA_SUCCESS )
4154 operation->iv_set = 1;
4155 else
mohammad1603503973b2018-03-12 15:59:30 +02004156 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004157 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004158}
4159
Steven Cooreman177deba2020-09-07 17:14:14 +02004160/* Process input for which the algorithm is set to ECB mode. This requires
4161 * manual processing, since the PSA API is defined as being able to process
4162 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4163 * underlying mbedtls_cipher_update only takes full blocks. */
4164static psa_status_t psa_cipher_update_ecb_internal(
4165 mbedtls_cipher_context_t *ctx,
4166 const uint8_t *input,
4167 size_t input_length,
4168 uint8_t *output,
4169 size_t output_size,
4170 size_t *output_length )
4171{
4172 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4173 size_t block_size = ctx->cipher_info->block_size;
4174 size_t internal_output_length = 0;
4175 *output_length = 0;
4176
4177 if( input_length == 0 )
4178 {
4179 status = PSA_SUCCESS;
4180 goto exit;
4181 }
4182
4183 if( ctx->unprocessed_len > 0 )
4184 {
4185 /* Fill up to block size, and run the block if there's a full one. */
4186 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4187
4188 if( input_length < bytes_to_copy )
4189 bytes_to_copy = input_length;
4190
4191 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4192 input, bytes_to_copy );
4193 input_length -= bytes_to_copy;
4194 input += bytes_to_copy;
4195 ctx->unprocessed_len += bytes_to_copy;
4196
4197 if( ctx->unprocessed_len == block_size )
4198 {
4199 status = mbedtls_to_psa_error(
4200 mbedtls_cipher_update( ctx,
4201 ctx->unprocessed_data,
4202 block_size,
4203 output, &internal_output_length ) );
4204
4205 if( status != PSA_SUCCESS )
4206 goto exit;
4207
4208 output += internal_output_length;
4209 output_size -= internal_output_length;
4210 *output_length += internal_output_length;
4211 ctx->unprocessed_len = 0;
4212 }
4213 }
4214
4215 while( input_length >= block_size )
4216 {
4217 /* Run all full blocks we have, one by one */
4218 status = mbedtls_to_psa_error(
4219 mbedtls_cipher_update( ctx, input,
4220 block_size,
4221 output, &internal_output_length ) );
4222
4223 if( status != PSA_SUCCESS )
4224 goto exit;
4225
4226 input_length -= block_size;
4227 input += block_size;
4228
4229 output += internal_output_length;
4230 output_size -= internal_output_length;
4231 *output_length += internal_output_length;
4232 }
4233
4234 if( input_length > 0 )
4235 {
4236 /* Save unprocessed bytes for later processing */
4237 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4238 input, input_length );
4239 ctx->unprocessed_len += input_length;
4240 }
4241
4242 status = PSA_SUCCESS;
4243
4244exit:
4245 return( status );
4246}
4247
Gilles Peskinee553c652018-06-04 16:22:46 +02004248psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4249 const uint8_t *input,
4250 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004251 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004252 size_t output_size,
4253 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004254{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004255 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004256 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004257 if( operation->alg == 0 )
4258 {
4259 return( PSA_ERROR_BAD_STATE );
4260 }
4261 if( operation->iv_required && ! operation->iv_set )
4262 {
4263 return( PSA_ERROR_BAD_STATE );
4264 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004265
Steven Cooremanef8575e2020-09-11 11:44:50 +02004266 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004267 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004268 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004269 input,
4270 input_length,
4271 output,
4272 output_size,
4273 output_length );
4274 goto exit;
4275 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004276
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004277 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004278 {
4279 /* Take the unprocessed partial block left over from previous
4280 * update calls, if any, plus the input to this call. Remove
4281 * the last partial block, if any. You get the data that will be
4282 * output in this call. */
4283 expected_output_size =
4284 ( operation->ctx.cipher.unprocessed_len + input_length )
4285 / operation->block_size * operation->block_size;
4286 }
4287 else
4288 {
4289 expected_output_size = input_length;
4290 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004291
Gilles Peskine89d789c2018-06-04 17:17:16 +02004292 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004293 {
4294 status = PSA_ERROR_BUFFER_TOO_SMALL;
4295 goto exit;
4296 }
mohammad160382759612018-03-12 18:16:40 +02004297
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004298 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4299 {
4300 /* mbedtls_cipher_update has an API inconsistency: it will only
4301 * process a single block at a time in ECB mode. Abstract away that
4302 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004303 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4304 input,
4305 input_length,
4306 output,
4307 output_size,
4308 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004309 }
4310 else
4311 {
4312 status = mbedtls_to_psa_error(
4313 mbedtls_cipher_update( &operation->ctx.cipher, input,
4314 input_length, output, output_length ) );
4315 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004316exit:
4317 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004318 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004319 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004320}
4321
Gilles Peskinee553c652018-06-04 16:22:46 +02004322psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4323 uint8_t *output,
4324 size_t output_size,
4325 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004326{
David Saadab4ecc272019-02-14 13:48:10 +02004327 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004328 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004329 if( operation->alg == 0 )
4330 {
4331 return( PSA_ERROR_BAD_STATE );
4332 }
4333 if( operation->iv_required && ! operation->iv_set )
4334 {
4335 return( PSA_ERROR_BAD_STATE );
4336 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004337
Steven Cooremanef8575e2020-09-11 11:44:50 +02004338 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004339 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004340 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004341 output,
4342 output_size,
4343 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004344 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004345 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004346
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004347 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004348 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004349 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004350 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004351 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004352 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004353 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004354 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004355 }
4356
Steven Cooremanef8575e2020-09-11 11:44:50 +02004357 status = mbedtls_to_psa_error(
4358 mbedtls_cipher_finish( &operation->ctx.cipher,
4359 temp_output_buffer,
4360 output_length ) );
4361 if( status != PSA_SUCCESS )
4362 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004363
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004364 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004365 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004366 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004367 memcpy( output, temp_output_buffer, *output_length );
4368 else
Janos Follath315b51c2018-07-09 16:04:51 +01004369 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004370
Steven Cooremanef8575e2020-09-11 11:44:50 +02004371exit:
4372 if( operation->mbedtls_in_use == 1 )
4373 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004374
Steven Cooremanef8575e2020-09-11 11:44:50 +02004375 if( status == PSA_SUCCESS )
4376 return( psa_cipher_abort( operation ) );
4377 else
4378 {
4379 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004380 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004381
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 return( status );
4383 }
mohammad1603503973b2018-03-12 15:59:30 +02004384}
4385
Gilles Peskinee553c652018-06-04 16:22:46 +02004386psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4387{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004388 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004389 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004390 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004391 * in use. It's ok to call abort on such an object, and there's
4392 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004393 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004394 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004395
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004396 /* Sanity check (shouldn't happen: operation->alg should
4397 * always have been initialized to a valid value). */
4398 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4399 return( PSA_ERROR_BAD_STATE );
4400
Steven Cooremanef8575e2020-09-11 11:44:50 +02004401 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004402 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004403 else
4404 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004405
Moran Peker41deec42018-04-04 15:43:05 +03004406 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004407 operation->key_set = 0;
4408 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004409 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004410 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004411 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004412 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004413
Moran Peker395db872018-05-31 14:07:14 +03004414 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004415}
4416
Gilles Peskinea0655c32018-04-30 17:06:50 +02004417
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004418
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004419
mohammad16035955c982018-04-26 00:53:03 +03004420/****************************************************************/
4421/* AEAD */
4422/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004423
Gilles Peskineedf9a652018-08-17 18:11:56 +02004424typedef struct
4425{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004426 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004427 const mbedtls_cipher_info_t *cipher_info;
4428 union
4429 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004430 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004431#if defined(MBEDTLS_CCM_C)
4432 mbedtls_ccm_context ccm;
4433#endif /* MBEDTLS_CCM_C */
4434#if defined(MBEDTLS_GCM_C)
4435 mbedtls_gcm_context gcm;
4436#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004437#if defined(MBEDTLS_CHACHAPOLY_C)
4438 mbedtls_chachapoly_context chachapoly;
4439#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004440 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004441 psa_algorithm_t core_alg;
4442 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004443 uint8_t tag_length;
4444} aead_operation_t;
4445
Ronald Cronf95a2b12020-10-22 15:24:49 +02004446#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4447
Gilles Peskine30a9e412019-01-14 18:36:12 +01004448static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004449{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004450 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004451 {
4452#if defined(MBEDTLS_CCM_C)
4453 case PSA_ALG_CCM:
4454 mbedtls_ccm_free( &operation->ctx.ccm );
4455 break;
4456#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004457#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004458 case PSA_ALG_GCM:
4459 mbedtls_gcm_free( &operation->ctx.gcm );
4460 break;
4461#endif /* MBEDTLS_GCM_C */
4462 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004463
Ronald Cron5c522922020-11-14 16:35:34 +01004464 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004465}
4466
4467static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004468 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004469 psa_key_usage_t usage,
4470 psa_algorithm_t alg )
4471{
4472 psa_status_t status;
4473 size_t key_bits;
4474 mbedtls_cipher_id_t cipher_id;
4475
Ronald Cron5c522922020-11-14 16:35:34 +01004476 status = psa_get_and_lock_transparent_key_slot_with_policy(
4477 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004478 if( status != PSA_SUCCESS )
4479 return( status );
4480
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004481 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004482
4483 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004484 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004485 &cipher_id );
4486 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004487 {
4488 status = PSA_ERROR_NOT_SUPPORTED;
4489 goto cleanup;
4490 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004491
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004492 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004493 {
4494#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004495 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004496 operation->core_alg = PSA_ALG_CCM;
4497 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004498 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4499 * The call to mbedtls_ccm_encrypt_and_tag or
4500 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004501 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004502 {
4503 status = PSA_ERROR_INVALID_ARGUMENT;
4504 goto cleanup;
4505 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004506 mbedtls_ccm_init( &operation->ctx.ccm );
4507 status = mbedtls_to_psa_error(
4508 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004509 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004510 (unsigned int) key_bits ) );
4511 if( status != 0 )
4512 goto cleanup;
4513 break;
4514#endif /* MBEDTLS_CCM_C */
4515
4516#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004517 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004518 operation->core_alg = PSA_ALG_GCM;
4519 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004520 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4521 * The call to mbedtls_gcm_crypt_and_tag or
4522 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004523 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004524 {
4525 status = PSA_ERROR_INVALID_ARGUMENT;
4526 goto cleanup;
4527 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004528 mbedtls_gcm_init( &operation->ctx.gcm );
4529 status = mbedtls_to_psa_error(
4530 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004531 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004532 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004533 if( status != 0 )
4534 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004535 break;
4536#endif /* MBEDTLS_GCM_C */
4537
Gilles Peskine26869f22019-05-06 15:25:00 +02004538#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004539 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004540 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4541 operation->full_tag_length = 16;
4542 /* We only support the default tag length. */
4543 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004544 {
4545 status = PSA_ERROR_NOT_SUPPORTED;
4546 goto cleanup;
4547 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004548 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4549 status = mbedtls_to_psa_error(
4550 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004551 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004552 if( status != 0 )
4553 goto cleanup;
4554 break;
4555#endif /* MBEDTLS_CHACHAPOLY_C */
4556
Gilles Peskineedf9a652018-08-17 18:11:56 +02004557 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004558 status = PSA_ERROR_NOT_SUPPORTED;
4559 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 }
4561
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004562 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4563 {
4564 status = PSA_ERROR_INVALID_ARGUMENT;
4565 goto cleanup;
4566 }
4567 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004568
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569 return( PSA_SUCCESS );
4570
4571cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004572 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004573 return( status );
4574}
4575
Ronald Croncf56a0a2020-08-04 09:51:30 +02004576psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004577 psa_algorithm_t alg,
4578 const uint8_t *nonce,
4579 size_t nonce_length,
4580 const uint8_t *additional_data,
4581 size_t additional_data_length,
4582 const uint8_t *plaintext,
4583 size_t plaintext_length,
4584 uint8_t *ciphertext,
4585 size_t ciphertext_size,
4586 size_t *ciphertext_length )
4587{
mohammad16035955c982018-04-26 00:53:03 +03004588 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004589 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004590 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004591
mohammad1603f08a5502018-06-03 15:05:47 +03004592 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004593
Ronald Croncf56a0a2020-08-04 09:51:30 +02004594 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004595 if( status != PSA_SUCCESS )
4596 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004597
Gilles Peskineedf9a652018-08-17 18:11:56 +02004598 /* For all currently supported modes, the tag is at the end of the
4599 * ciphertext. */
4600 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4601 {
4602 status = PSA_ERROR_BUFFER_TOO_SMALL;
4603 goto exit;
4604 }
4605 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004606
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004607#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004608 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004609 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004610 status = mbedtls_to_psa_error(
4611 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4612 MBEDTLS_GCM_ENCRYPT,
4613 plaintext_length,
4614 nonce, nonce_length,
4615 additional_data, additional_data_length,
4616 plaintext, ciphertext,
4617 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004618 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004619 else
4620#endif /* MBEDTLS_GCM_C */
4621#if defined(MBEDTLS_CCM_C)
4622 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004623 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624 status = mbedtls_to_psa_error(
4625 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4626 plaintext_length,
4627 nonce, nonce_length,
4628 additional_data,
4629 additional_data_length,
4630 plaintext, ciphertext,
4631 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004632 }
mohammad16035c8845f2018-05-09 05:40:09 -07004633 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004634#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004635#if defined(MBEDTLS_CHACHAPOLY_C)
4636 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4637 {
4638 if( nonce_length != 12 || operation.tag_length != 16 )
4639 {
4640 status = PSA_ERROR_NOT_SUPPORTED;
4641 goto exit;
4642 }
4643 status = mbedtls_to_psa_error(
4644 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4645 plaintext_length,
4646 nonce,
4647 additional_data,
4648 additional_data_length,
4649 plaintext,
4650 ciphertext,
4651 tag ) );
4652 }
4653 else
4654#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004655 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004656 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004657 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004658 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004659
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4661 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004662
Gilles Peskineedf9a652018-08-17 18:11:56 +02004663exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004664 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 if( status == PSA_SUCCESS )
4666 *ciphertext_length = plaintext_length + operation.tag_length;
4667 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004668}
4669
Gilles Peskineee652a32018-06-01 19:23:52 +02004670/* Locate the tag in a ciphertext buffer containing the encrypted data
4671 * followed by the tag. Return the length of the part preceding the tag in
4672 * *plaintext_length. This is the size of the plaintext in modes where
4673 * the encrypted data has the same size as the plaintext, such as
4674 * CCM and GCM. */
4675static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4676 const uint8_t *ciphertext,
4677 size_t ciphertext_length,
4678 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004679 const uint8_t **p_tag )
4680{
4681 size_t payload_length;
4682 if( tag_length > ciphertext_length )
4683 return( PSA_ERROR_INVALID_ARGUMENT );
4684 payload_length = ciphertext_length - tag_length;
4685 if( payload_length > plaintext_size )
4686 return( PSA_ERROR_BUFFER_TOO_SMALL );
4687 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004688 return( PSA_SUCCESS );
4689}
4690
Ronald Croncf56a0a2020-08-04 09:51:30 +02004691psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004692 psa_algorithm_t alg,
4693 const uint8_t *nonce,
4694 size_t nonce_length,
4695 const uint8_t *additional_data,
4696 size_t additional_data_length,
4697 const uint8_t *ciphertext,
4698 size_t ciphertext_length,
4699 uint8_t *plaintext,
4700 size_t plaintext_size,
4701 size_t *plaintext_length )
4702{
mohammad16035955c982018-04-26 00:53:03 +03004703 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004704 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004705 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004706
Gilles Peskineee652a32018-06-01 19:23:52 +02004707 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004708
Ronald Croncf56a0a2020-08-04 09:51:30 +02004709 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004710 if( status != PSA_SUCCESS )
4711 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004712
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004713 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4714 ciphertext, ciphertext_length,
4715 plaintext_size, &tag );
4716 if( status != PSA_SUCCESS )
4717 goto exit;
4718
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004719#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004720 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004721 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004722 status = mbedtls_to_psa_error(
4723 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4724 ciphertext_length - operation.tag_length,
4725 nonce, nonce_length,
4726 additional_data,
4727 additional_data_length,
4728 tag, operation.tag_length,
4729 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004730 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004731 else
4732#endif /* MBEDTLS_GCM_C */
4733#if defined(MBEDTLS_CCM_C)
4734 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004735 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004736 status = mbedtls_to_psa_error(
4737 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4738 ciphertext_length - operation.tag_length,
4739 nonce, nonce_length,
4740 additional_data,
4741 additional_data_length,
4742 ciphertext, plaintext,
4743 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004744 }
mohammad160339574652018-06-01 04:39:53 -07004745 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004746#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004747#if defined(MBEDTLS_CHACHAPOLY_C)
4748 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4749 {
4750 if( nonce_length != 12 || operation.tag_length != 16 )
4751 {
4752 status = PSA_ERROR_NOT_SUPPORTED;
4753 goto exit;
4754 }
4755 status = mbedtls_to_psa_error(
4756 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4757 ciphertext_length - operation.tag_length,
4758 nonce,
4759 additional_data,
4760 additional_data_length,
4761 tag,
4762 ciphertext,
4763 plaintext ) );
4764 }
4765 else
4766#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004767 {
mohammad1603554faad2018-06-03 15:07:38 +03004768 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004769 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004770
Gilles Peskineedf9a652018-08-17 18:11:56 +02004771 if( status != PSA_SUCCESS && plaintext_size != 0 )
4772 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004773
Gilles Peskineedf9a652018-08-17 18:11:56 +02004774exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004775 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004776 if( status == PSA_SUCCESS )
4777 *plaintext_length = ciphertext_length - operation.tag_length;
4778 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004779}
4780
Gilles Peskinea0655c32018-04-30 17:06:50 +02004781
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004782
Gilles Peskine20035e32018-02-03 22:44:14 +01004783/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004784/* Generators */
4785/****************************************************************/
4786
John Durkop07cc04a2020-11-16 22:08:34 -08004787#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4788 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4789 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4790#define AT_LEAST_ONE_BUILTIN_KDF
4791#endif
4792
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004793#define HKDF_STATE_INIT 0 /* no input yet */
4794#define HKDF_STATE_STARTED 1 /* got salt */
4795#define HKDF_STATE_KEYED 2 /* got key */
4796#define HKDF_STATE_OUTPUT 3 /* output started */
4797
Gilles Peskinecbe66502019-05-16 16:59:18 +02004798static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004799 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004800{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4802 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004803 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004804 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004805}
4806
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004807psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004808{
4809 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004810 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004812 {
4813 /* The object has (apparently) been initialized but it is not
4814 * in use. It's ok to call abort on such an object, and there's
4815 * nothing to do. */
4816 }
4817 else
John Durkopd0321952020-10-29 21:37:36 -07004818#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004820 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004821 mbedtls_free( operation->ctx.hkdf.info );
4822 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004823 }
John Durkop07cc04a2020-11-16 22:08:34 -08004824 else
4825#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4826#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4827 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4828 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004830 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004831 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004832 if( operation->ctx.tls12_prf.seed != NULL )
4833 {
4834 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4835 operation->ctx.tls12_prf.seed_length );
4836 mbedtls_free( operation->ctx.tls12_prf.seed );
4837 }
4838
4839 if( operation->ctx.tls12_prf.label != NULL )
4840 {
4841 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4842 operation->ctx.tls12_prf.label_length );
4843 mbedtls_free( operation->ctx.tls12_prf.label );
4844 }
4845
4846 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4847
4848 /* We leave the fields Ai and output_block to be erased safely by the
4849 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004850 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004851 else
John Durkop07cc04a2020-11-16 22:08:34 -08004852#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4853 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004854 {
4855 status = PSA_ERROR_BAD_STATE;
4856 }
Janos Follath083036a2019-06-11 10:22:26 +01004857 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004858 return( status );
4859}
4860
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004861psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004862 size_t *capacity)
4863{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004864 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004865 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004866 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004867 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004868 }
4869
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004870 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004871 return( PSA_SUCCESS );
4872}
4873
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004874psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875 size_t capacity )
4876{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004879 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004880 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004881 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004882 return( PSA_SUCCESS );
4883}
4884
John Durkopd0321952020-10-29 21:37:36 -07004885#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004886/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004887 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004888static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004889 psa_algorithm_t hash_alg,
4890 uint8_t *output,
4891 size_t output_length )
4892{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004893 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004894 psa_status_t status;
4895
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4897 return( PSA_ERROR_BAD_STATE );
4898 hkdf->state = HKDF_STATE_OUTPUT;
4899
Gilles Peskinebef7f142018-07-12 17:22:21 +02004900 while( output_length != 0 )
4901 {
4902 /* Copy what remains of the current block */
4903 uint8_t n = hash_length - hkdf->offset_in_block;
4904 if( n > output_length )
4905 n = (uint8_t) output_length;
4906 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4907 output += n;
4908 output_length -= n;
4909 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004910 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004911 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004912 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004913 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004915 * object was corrupted or if this function is called directly
4916 * inside the library. */
4917 if( hkdf->block_number == 0xff )
4918 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004919
4920 /* We need a new block */
4921 ++hkdf->block_number;
4922 hkdf->offset_in_block = 0;
4923 status = psa_hmac_setup_internal( &hkdf->hmac,
4924 hkdf->prk, hash_length,
4925 hash_alg );
4926 if( status != PSA_SUCCESS )
4927 return( status );
4928 if( hkdf->block_number != 1 )
4929 {
4930 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4931 hkdf->output_block,
4932 hash_length );
4933 if( status != PSA_SUCCESS )
4934 return( status );
4935 }
4936 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4937 hkdf->info,
4938 hkdf->info_length );
4939 if( status != PSA_SUCCESS )
4940 return( status );
4941 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4942 &hkdf->block_number, 1 );
4943 if( status != PSA_SUCCESS )
4944 return( status );
4945 status = psa_hmac_finish_internal( &hkdf->hmac,
4946 hkdf->output_block,
4947 sizeof( hkdf->output_block ) );
4948 if( status != PSA_SUCCESS )
4949 return( status );
4950 }
4951
4952 return( PSA_SUCCESS );
4953}
John Durkop07cc04a2020-11-16 22:08:34 -08004954#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004955
John Durkop07cc04a2020-11-16 22:08:34 -08004956#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4957 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004958static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4959 psa_tls12_prf_key_derivation_t *tls12_prf,
4960 psa_algorithm_t alg )
4961{
4962 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004963 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004964 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4965 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004966
Janos Follath7742fee2019-06-17 12:58:10 +01004967 /* We can't be wanting more output after block 0xff, otherwise
4968 * the capacity check in psa_key_derivation_output_bytes() would have
4969 * prevented this call. It could happen only if the operation
4970 * object was corrupted or if this function is called directly
4971 * inside the library. */
4972 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004973 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004974
4975 /* We need a new block */
4976 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004977 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004978
4979 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4980 *
4981 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4982 *
4983 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4984 * HMAC_hash(secret, A(2) + seed) +
4985 * HMAC_hash(secret, A(3) + seed) + ...
4986 *
4987 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004988 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004989 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004990 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004991 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004992 * is currently extracted as `output_block` and where i is
4993 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004994 */
4995
Janos Follathea29bfb2019-06-19 12:21:20 +01004996 /* Save the hash context before using it, to preserve the hash state with
4997 * only the inner padding in it. We need this, because inner padding depends
4998 * on the key (secret in the RFC's terminology). */
4999 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5000 if( status != PSA_SUCCESS )
5001 goto cleanup;
5002
5003 /* Calculate A(i) where i = tls12_prf->block_number. */
5004 if( tls12_prf->block_number == 1 )
5005 {
5006 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5007 * the variable seed and in this instance means it in the context of the
5008 * P_hash function, where seed = label + seed.) */
5009 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5010 tls12_prf->label, tls12_prf->label_length );
5011 if( status != PSA_SUCCESS )
5012 goto cleanup;
5013 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5014 tls12_prf->seed, tls12_prf->seed_length );
5015 if( status != PSA_SUCCESS )
5016 goto cleanup;
5017 }
5018 else
5019 {
5020 /* A(i) = HMAC_hash(secret, A(i-1)) */
5021 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5022 tls12_prf->Ai, hash_length );
5023 if( status != PSA_SUCCESS )
5024 goto cleanup;
5025 }
5026
5027 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5028 tls12_prf->Ai, hash_length );
5029 if( status != PSA_SUCCESS )
5030 goto cleanup;
5031 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5032 if( status != PSA_SUCCESS )
5033 goto cleanup;
5034
5035 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5036 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5037 tls12_prf->Ai, hash_length );
5038 if( status != PSA_SUCCESS )
5039 goto cleanup;
5040 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5041 tls12_prf->label, tls12_prf->label_length );
5042 if( status != PSA_SUCCESS )
5043 goto cleanup;
5044 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5045 tls12_prf->seed, tls12_prf->seed_length );
5046 if( status != PSA_SUCCESS )
5047 goto cleanup;
5048 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5049 tls12_prf->output_block, hash_length );
5050 if( status != PSA_SUCCESS )
5051 goto cleanup;
5052 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5053 if( status != PSA_SUCCESS )
5054 goto cleanup;
5055
Janos Follath7742fee2019-06-17 12:58:10 +01005056
5057cleanup:
5058
Janos Follathea29bfb2019-06-19 12:21:20 +01005059 cleanup_status = psa_hash_abort( &backup );
5060 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5061 status = cleanup_status;
5062
5063 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005064}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005065
Janos Follath844eb0e2019-06-19 12:10:49 +01005066static psa_status_t psa_key_derivation_tls12_prf_read(
5067 psa_tls12_prf_key_derivation_t *tls12_prf,
5068 psa_algorithm_t alg,
5069 uint8_t *output,
5070 size_t output_length )
5071{
5072 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005073 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005074 psa_status_t status;
5075 uint8_t offset, length;
5076
5077 while( output_length != 0 )
5078 {
5079 /* Check if we have fully processed the current block. */
5080 if( tls12_prf->left_in_block == 0 )
5081 {
5082 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5083 alg );
5084 if( status != PSA_SUCCESS )
5085 return( status );
5086
5087 continue;
5088 }
5089
5090 if( tls12_prf->left_in_block > output_length )
5091 length = (uint8_t) output_length;
5092 else
5093 length = tls12_prf->left_in_block;
5094
5095 offset = hash_length - tls12_prf->left_in_block;
5096 memcpy( output, tls12_prf->output_block + offset, length );
5097 output += length;
5098 output_length -= length;
5099 tls12_prf->left_in_block -= length;
5100 }
5101
5102 return( PSA_SUCCESS );
5103}
John Durkop07cc04a2020-11-16 22:08:34 -08005104#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5105 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005106
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005107psa_status_t psa_key_derivation_output_bytes(
5108 psa_key_derivation_operation_t *operation,
5109 uint8_t *output,
5110 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005111{
5112 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005113 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005114
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005115 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005116 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005117 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005118 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005119 }
5120
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005121 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005122 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005123 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005124 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005126 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005127 goto exit;
5128 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005129 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005130 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005131 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005132 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5134 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005135 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005136 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005137 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005138 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005139 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140
John Durkopd0321952020-10-29 21:37:36 -07005141#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005142 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005143 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005144 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005145 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005146 output, output_length );
5147 }
Janos Follathadbec812019-06-14 11:05:39 +01005148 else
John Durkop07cc04a2020-11-16 22:08:34 -08005149#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5150#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5151 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005152 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005153 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005154 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005155 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005156 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005157 output_length );
5158 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005159 else
John Durkop07cc04a2020-11-16 22:08:34 -08005160#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5161 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005162 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005163 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005164 return( PSA_ERROR_BAD_STATE );
5165 }
5166
5167exit:
5168 if( status != PSA_SUCCESS )
5169 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005170 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005171 * This allows us to differentiate between exhausted operations and
5172 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5173 * operations. */
5174 psa_algorithm_t alg = operation->alg;
5175 psa_key_derivation_abort( operation );
5176 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005177 memset( output, '!', output_length );
5178 }
5179 return( status );
5180}
5181
David Brown7807bf72021-02-09 16:28:23 -07005182#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005183static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5184{
5185 if( data_size >= 8 )
5186 mbedtls_des_key_set_parity( data );
5187 if( data_size >= 16 )
5188 mbedtls_des_key_set_parity( data + 8 );
5189 if( data_size >= 24 )
5190 mbedtls_des_key_set_parity( data + 16 );
5191}
David Brown7807bf72021-02-09 16:28:23 -07005192#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005193
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005194static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005195 psa_key_slot_t *slot,
5196 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005197 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005198{
5199 uint8_t *data = NULL;
5200 size_t bytes = PSA_BITS_TO_BYTES( bits );
5201 psa_status_t status;
5202
Gilles Peskine8e338702019-07-30 20:06:31 +02005203 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005204 return( PSA_ERROR_INVALID_ARGUMENT );
5205 if( bits % 8 != 0 )
5206 return( PSA_ERROR_INVALID_ARGUMENT );
5207 data = mbedtls_calloc( 1, bytes );
5208 if( data == NULL )
5209 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005212 if( status != PSA_SUCCESS )
5213 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005214#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005215 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005216 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005217#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005218
5219 status = psa_allocate_buffer_to_slot( slot, bytes );
5220 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005221 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005222
Ronald Cronbf33c932020-11-28 18:06:53 +01005223 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005224 psa_key_attributes_t attributes = {
5225 .core = slot->attr
5226 };
5227
Ronald Cronbf33c932020-11-28 18:06:53 +01005228 status = psa_driver_wrapper_import_key( &attributes,
5229 data, bytes,
5230 slot->key.data,
5231 slot->key.bytes,
5232 &slot->key.bytes, &bits );
5233 if( bits != slot->attr.bits )
5234 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005235
5236exit:
5237 mbedtls_free( data );
5238 return( status );
5239}
5240
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005241psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005243 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005244{
5245 psa_status_t status;
5246 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005247 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005248
Ronald Cron81709fc2020-11-14 12:10:32 +01005249 *key = MBEDTLS_SVC_KEY_ID_INIT;
5250
Gilles Peskine0f84d622019-09-12 19:03:13 +02005251 /* Reject any attempt to create a zero-length key so that we don't
5252 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5253 if( psa_get_key_bits( attributes ) == 0 )
5254 return( PSA_ERROR_INVALID_ARGUMENT );
5255
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005256 if( ! operation->can_output_key )
5257 return( PSA_ERROR_NOT_PERMITTED );
5258
Ronald Cron81709fc2020-11-14 12:10:32 +01005259 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5260 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005261#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5262 if( driver != NULL )
5263 {
5264 /* Deriving a key in a secure element is not implemented yet. */
5265 status = PSA_ERROR_NOT_SUPPORTED;
5266 }
5267#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005268 if( status == PSA_SUCCESS )
5269 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005270 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005271 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005273 }
5274 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005275 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005276 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005277 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005278
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005279 return( status );
5280}
5281
Gilles Peskineeab56e42018-07-12 17:12:33 +02005282
5283
5284/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005285/* Key derivation */
5286/****************************************************************/
5287
Steven Cooreman932ffb72021-02-15 12:14:32 +01005288#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005289static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005290 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005291 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005292{
John Durkop07cc04a2020-11-16 22:08:34 -08005293 int is_kdf_alg_supported;
5294
Janos Follath5fe19732019-06-20 15:09:30 +01005295 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5296 * initialisers for this union leave some bytes unspecified.) */
5297 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5298
Gilles Peskine969c5d62019-01-16 15:53:06 +01005299 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005300#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005301 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5302 is_kdf_alg_supported = 1;
5303 else
5304#endif
5305#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5306 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5307 is_kdf_alg_supported = 1;
5308 else
5309#endif
5310#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5311 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5312 is_kdf_alg_supported = 1;
5313 else
5314#endif
5315 is_kdf_alg_supported = 0;
5316
5317 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005319 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005320 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 if( hash_size == 0 )
5322 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005323 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5324 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005325 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005326 {
5327 return( PSA_ERROR_NOT_SUPPORTED );
5328 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005330 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005331 }
John Durkop07cc04a2020-11-16 22:08:34 -08005332
5333 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005334}
John Durkop07cc04a2020-11-16 22:08:34 -08005335#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005338 psa_algorithm_t alg )
5339{
5340 psa_status_t status;
5341
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005343 return( PSA_ERROR_BAD_STATE );
5344
Gilles Peskine6843c292019-01-18 16:44:49 +01005345 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5346 return( PSA_ERROR_INVALID_ARGUMENT );
5347 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005348 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005349#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005350 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005351 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005352#else
5353 return( PSA_ERROR_NOT_SUPPORTED );
5354#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355 }
5356 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5357 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005358#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005359 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005360#else
5361 return( PSA_ERROR_NOT_SUPPORTED );
5362#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005363 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005364 else
5365 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005366
5367 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005369 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005370}
5371
John Durkopd0321952020-10-29 21:37:36 -07005372#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005373static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005374 psa_algorithm_t hash_alg,
5375 psa_key_derivation_step_t step,
5376 const uint8_t *data,
5377 size_t data_length )
5378{
5379 psa_status_t status;
5380 switch( step )
5381 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005382 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005383 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005384 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005385 status = psa_hmac_setup_internal( &hkdf->hmac,
5386 data, data_length,
5387 hash_alg );
5388 if( status != PSA_SUCCESS )
5389 return( status );
5390 hkdf->state = HKDF_STATE_STARTED;
5391 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005392 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005393 /* If no salt was provided, use an empty salt. */
5394 if( hkdf->state == HKDF_STATE_INIT )
5395 {
5396 status = psa_hmac_setup_internal( &hkdf->hmac,
5397 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005398 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005399 if( status != PSA_SUCCESS )
5400 return( status );
5401 hkdf->state = HKDF_STATE_STARTED;
5402 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005403 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005404 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005405 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5406 data, data_length );
5407 if( status != PSA_SUCCESS )
5408 return( status );
5409 status = psa_hmac_finish_internal( &hkdf->hmac,
5410 hkdf->prk,
5411 sizeof( hkdf->prk ) );
5412 if( status != PSA_SUCCESS )
5413 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005414 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005415 hkdf->block_number = 0;
5416 hkdf->state = HKDF_STATE_KEYED;
5417 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005418 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005419 if( hkdf->state == HKDF_STATE_OUTPUT )
5420 return( PSA_ERROR_BAD_STATE );
5421 if( hkdf->info_set )
5422 return( PSA_ERROR_BAD_STATE );
5423 hkdf->info_length = data_length;
5424 if( data_length != 0 )
5425 {
5426 hkdf->info = mbedtls_calloc( 1, data_length );
5427 if( hkdf->info == NULL )
5428 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5429 memcpy( hkdf->info, data, data_length );
5430 }
5431 hkdf->info_set = 1;
5432 return( PSA_SUCCESS );
5433 default:
5434 return( PSA_ERROR_INVALID_ARGUMENT );
5435 }
5436}
John Durkop07cc04a2020-11-16 22:08:34 -08005437#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005438
John Durkop07cc04a2020-11-16 22:08:34 -08005439#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5440 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005441static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5442 const uint8_t *data,
5443 size_t data_length )
5444{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005445 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005446 return( PSA_ERROR_BAD_STATE );
5447
Janos Follathd6dce9f2019-07-04 09:11:38 +01005448 if( data_length != 0 )
5449 {
5450 prf->seed = mbedtls_calloc( 1, data_length );
5451 if( prf->seed == NULL )
5452 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005453
Janos Follathd6dce9f2019-07-04 09:11:38 +01005454 memcpy( prf->seed, data, data_length );
5455 prf->seed_length = data_length;
5456 }
Janos Follathf08e2652019-06-13 09:05:41 +01005457
Gilles Peskine43f958b2020-12-13 14:55:14 +01005458 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005459
5460 return( PSA_SUCCESS );
5461}
5462
Janos Follath81550542019-06-13 14:26:34 +01005463static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5464 psa_algorithm_t hash_alg,
5465 const uint8_t *data,
5466 size_t data_length )
5467{
5468 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005469 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005470 return( PSA_ERROR_BAD_STATE );
5471
5472 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5473 if( status != PSA_SUCCESS )
5474 return( status );
5475
Gilles Peskine43f958b2020-12-13 14:55:14 +01005476 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005477
5478 return( PSA_SUCCESS );
5479}
5480
Janos Follath63028dd2019-06-13 09:15:47 +01005481static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5482 const uint8_t *data,
5483 size_t data_length )
5484{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005485 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005486 return( PSA_ERROR_BAD_STATE );
5487
Janos Follathd6dce9f2019-07-04 09:11:38 +01005488 if( data_length != 0 )
5489 {
5490 prf->label = mbedtls_calloc( 1, data_length );
5491 if( prf->label == NULL )
5492 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005493
Janos Follathd6dce9f2019-07-04 09:11:38 +01005494 memcpy( prf->label, data, data_length );
5495 prf->label_length = data_length;
5496 }
Janos Follath63028dd2019-06-13 09:15:47 +01005497
Gilles Peskine43f958b2020-12-13 14:55:14 +01005498 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005499
5500 return( PSA_SUCCESS );
5501}
5502
Janos Follathb03233e2019-06-11 15:30:30 +01005503static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5504 psa_algorithm_t hash_alg,
5505 psa_key_derivation_step_t step,
5506 const uint8_t *data,
5507 size_t data_length )
5508{
Janos Follathb03233e2019-06-11 15:30:30 +01005509 switch( step )
5510 {
Janos Follathf08e2652019-06-13 09:05:41 +01005511 case PSA_KEY_DERIVATION_INPUT_SEED:
5512 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005513 case PSA_KEY_DERIVATION_INPUT_SECRET:
5514 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005515 case PSA_KEY_DERIVATION_INPUT_LABEL:
5516 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005517 default:
5518 return( PSA_ERROR_INVALID_ARGUMENT );
5519 }
5520}
John Durkop07cc04a2020-11-16 22:08:34 -08005521#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5522 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5523
5524#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5525static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5526 psa_tls12_prf_key_derivation_t *prf,
5527 psa_algorithm_t hash_alg,
5528 const uint8_t *data,
5529 size_t data_length )
5530{
5531 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005532 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005533 uint8_t *cur = pms;
5534
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005535 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005536 return( PSA_ERROR_INVALID_ARGUMENT );
5537
5538 /* Quoting RFC 4279, Section 2:
5539 *
5540 * The premaster secret is formed as follows: if the PSK is N octets
5541 * long, concatenate a uint16 with the value N, N zero octets, a second
5542 * uint16 with the value N, and the PSK itself.
5543 */
5544
5545 *cur++ = ( data_length >> 8 ) & 0xff;
5546 *cur++ = ( data_length >> 0 ) & 0xff;
5547 memset( cur, 0, data_length );
5548 cur += data_length;
5549 *cur++ = pms[0];
5550 *cur++ = pms[1];
5551 memcpy( cur, data, data_length );
5552 cur += data_length;
5553
5554 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5555
5556 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5557 return( status );
5558}
Janos Follath6660f0e2019-06-17 08:44:03 +01005559
5560static psa_status_t psa_tls12_prf_psk_to_ms_input(
5561 psa_tls12_prf_key_derivation_t *prf,
5562 psa_algorithm_t hash_alg,
5563 psa_key_derivation_step_t step,
5564 const uint8_t *data,
5565 size_t data_length )
5566{
5567 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005568 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005569 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5570 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005571 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005572
5573 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5574}
John Durkop07cc04a2020-11-16 22:08:34 -08005575#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005576
Gilles Peskineb8965192019-09-24 16:21:10 +02005577/** Check whether the given key type is acceptable for the given
5578 * input step of a key derivation.
5579 *
5580 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5581 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5582 * Both secret and non-secret inputs can alternatively have the type
5583 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5584 * that the input was passed as a buffer rather than via a key object.
5585 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005586static int psa_key_derivation_check_input_type(
5587 psa_key_derivation_step_t step,
5588 psa_key_type_t key_type )
5589{
5590 switch( step )
5591 {
5592 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005593 if( key_type == PSA_KEY_TYPE_DERIVE )
5594 return( PSA_SUCCESS );
5595 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005596 return( PSA_SUCCESS );
5597 break;
5598 case PSA_KEY_DERIVATION_INPUT_LABEL:
5599 case PSA_KEY_DERIVATION_INPUT_SALT:
5600 case PSA_KEY_DERIVATION_INPUT_INFO:
5601 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005602 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5603 return( PSA_SUCCESS );
5604 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005605 return( PSA_SUCCESS );
5606 break;
5607 }
5608 return( PSA_ERROR_INVALID_ARGUMENT );
5609}
5610
Janos Follathb80a94e2019-06-12 15:54:46 +01005611static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005612 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005613 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005614 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005615 const uint8_t *data,
5616 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005617{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005618 psa_status_t status;
5619 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5620
5621 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005622 if( status != PSA_SUCCESS )
5623 goto exit;
5624
John Durkopd0321952020-10-29 21:37:36 -07005625#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005626 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005627 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005628 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005629 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005630 step, data, data_length );
5631 }
John Durkop07cc04a2020-11-16 22:08:34 -08005632 else
5633#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5634#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5635 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636 {
Janos Follathb03233e2019-06-11 15:30:30 +01005637 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5638 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5639 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005640 }
John Durkop07cc04a2020-11-16 22:08:34 -08005641 else
5642#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5643#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5644 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005645 {
5646 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5647 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5648 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005649 }
5650 else
John Durkop07cc04a2020-11-16 22:08:34 -08005651#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005652 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005654 (void) data;
5655 (void) data_length;
5656 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005657 return( PSA_ERROR_BAD_STATE );
5658 }
5659
Gilles Peskine224b0d62019-09-23 18:13:17 +02005660exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005661 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005662 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005663 return( status );
5664}
5665
Janos Follath51f4a0f2019-06-14 11:35:55 +01005666psa_status_t psa_key_derivation_input_bytes(
5667 psa_key_derivation_operation_t *operation,
5668 psa_key_derivation_step_t step,
5669 const uint8_t *data,
5670 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005671{
Gilles Peskineb8965192019-09-24 16:21:10 +02005672 return( psa_key_derivation_input_internal( operation, step,
5673 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005674 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005675}
5676
Janos Follath51f4a0f2019-06-14 11:35:55 +01005677psa_status_t psa_key_derivation_input_key(
5678 psa_key_derivation_operation_t *operation,
5679 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005680 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005682 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005683 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005684 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005685
Ronald Cron5c522922020-11-14 16:35:34 +01005686 status = psa_get_and_lock_transparent_key_slot_with_policy(
5687 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005688 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005689 {
5690 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005691 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005692 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005693
5694 /* Passing a key object as a SECRET input unlocks the permission
5695 * to output to a key object. */
5696 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5697 operation->can_output_key = 1;
5698
Ronald Cronf95a2b12020-10-22 15:24:49 +02005699 status = psa_key_derivation_input_internal( operation,
5700 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005701 slot->key.data,
5702 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005703
Ronald Cron5c522922020-11-14 16:35:34 +01005704 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005705
Ronald Cron5c522922020-11-14 16:35:34 +01005706 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005707}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005708
5709
5710
5711/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005712/* Key agreement */
5713/****************************************************************/
5714
John Durkop6ba40d12020-11-10 08:50:04 -08005715#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005716static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5717 size_t peer_key_length,
5718 const mbedtls_ecp_keypair *our_key,
5719 uint8_t *shared_secret,
5720 size_t shared_secret_size,
5721 size_t *shared_secret_length )
5722{
Steven Cooremand4867872020-08-05 16:31:39 +02005723 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005724 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005725 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005726 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005727 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005728 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005729
Ronald Cron90857082020-11-25 14:58:33 +01005730 status = mbedtls_psa_ecp_load_representation(
5731 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005732 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005733 peer_key,
5734 peer_key_length,
5735 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005736 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005737 goto exit;
5738
Jaeden Amero97271b32019-01-10 19:38:51 +00005739 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005740 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005741 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005742 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005743 status = mbedtls_to_psa_error(
5744 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5745 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005746 goto exit;
5747
Jaeden Amero97271b32019-01-10 19:38:51 +00005748 status = mbedtls_to_psa_error(
5749 mbedtls_ecdh_calc_secret( &ecdh,
5750 shared_secret_length,
5751 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005752 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005753 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005754 if( status != PSA_SUCCESS )
5755 goto exit;
5756 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5757 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005758
5759exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005760 if( status != PSA_SUCCESS )
5761 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005762 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005763 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005764 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005765
Jaeden Amero97271b32019-01-10 19:38:51 +00005766 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005767}
John Durkop6ba40d12020-11-10 08:50:04 -08005768#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005769
Gilles Peskine01d718c2018-09-18 12:01:02 +02005770#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5771
Gilles Peskine0216fe12019-04-11 21:23:21 +02005772static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5773 psa_key_slot_t *private_key,
5774 const uint8_t *peer_key,
5775 size_t peer_key_length,
5776 uint8_t *shared_secret,
5777 size_t shared_secret_size,
5778 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005779{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005780 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005781 {
John Durkop6ba40d12020-11-10 08:50:04 -08005782#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005783 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005784 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005785 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005786 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005787 psa_status_t status = mbedtls_psa_ecp_load_representation(
5788 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005789 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005790 private_key->key.data,
5791 private_key->key.bytes,
5792 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005793 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005794 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005795 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005796 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005797 shared_secret, shared_secret_size,
5798 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005799 mbedtls_ecp_keypair_free( ecp );
5800 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005801 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005802#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005803 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005804 (void) private_key;
5805 (void) peer_key;
5806 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005807 (void) shared_secret;
5808 (void) shared_secret_size;
5809 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005810 return( PSA_ERROR_NOT_SUPPORTED );
5811 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005812}
5813
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005814/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005815 * to potentially free embedded data structures and wipe confidential data.
5816 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005817static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005818 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005819 psa_key_slot_t *private_key,
5820 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005821 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005822{
5823 psa_status_t status;
5824 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5825 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005826 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005827
5828 /* Step 1: run the secret agreement algorithm to generate the shared
5829 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005830 status = psa_key_agreement_raw_internal( ka_alg,
5831 private_key,
5832 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005833 shared_secret,
5834 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005835 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005836 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005837 goto exit;
5838
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005839 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005840 * the shared secret. A shared secret is permitted wherever a key
5841 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005842 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005843 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005844 shared_secret,
5845 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005846exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005847 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005848 return( status );
5849}
5850
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005851psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005852 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005853 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005854 const uint8_t *peer_key,
5855 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005856{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005858 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005859 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005860
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005861 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005862 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005863 status = psa_get_and_lock_transparent_key_slot_with_policy(
5864 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005865 if( status != PSA_SUCCESS )
5866 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005867 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005868 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005869 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005870 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005871 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005872 else
5873 {
5874 /* If a private key has been added as SECRET, we allow the derived
5875 * key material to be used as a key in PSA Crypto. */
5876 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5877 operation->can_output_key = 1;
5878 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005879
Ronald Cron5c522922020-11-14 16:35:34 +01005880 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005881
Ronald Cron5c522922020-11-14 16:35:34 +01005882 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005883}
5884
Gilles Peskinebe697d82019-05-16 18:00:41 +02005885psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005886 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005887 const uint8_t *peer_key,
5888 size_t peer_key_length,
5889 uint8_t *output,
5890 size_t output_size,
5891 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005892{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005893 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005894 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005895 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005896
5897 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5898 {
5899 status = PSA_ERROR_INVALID_ARGUMENT;
5900 goto exit;
5901 }
Ronald Cron5c522922020-11-14 16:35:34 +01005902 status = psa_get_and_lock_transparent_key_slot_with_policy(
5903 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005904 if( status != PSA_SUCCESS )
5905 goto exit;
5906
5907 status = psa_key_agreement_raw_internal( alg, slot,
5908 peer_key, peer_key_length,
5909 output, output_size,
5910 output_length );
5911
5912exit:
5913 if( status != PSA_SUCCESS )
5914 {
5915 /* If an error happens and is not handled properly, the output
5916 * may be used as a key to protect sensitive data. Arrange for such
5917 * a key to be random, which is likely to result in decryption or
5918 * verification errors. This is better than filling the buffer with
5919 * some constant data such as zeros, which would result in the data
5920 * being protected with a reproducible, easily knowable key.
5921 */
5922 psa_generate_random( output, output_size );
5923 *output_length = output_size;
5924 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005925
Ronald Cron5c522922020-11-14 16:35:34 +01005926 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005927
Ronald Cron5c522922020-11-14 16:35:34 +01005928 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005929}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005930
5931
Gilles Peskine30524eb2020-11-13 17:02:26 +01005932
Gilles Peskine86a440b2018-11-12 18:39:40 +01005933/****************************************************************/
5934/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005935/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005936
Gilles Peskine30524eb2020-11-13 17:02:26 +01005937/** Initialize the PSA random generator.
5938 */
5939static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5940{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005941#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5942 memset( rng, 0, sizeof( *rng ) );
5943#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5944
Gilles Peskine30524eb2020-11-13 17:02:26 +01005945 /* Set default configuration if
5946 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5947 if( rng->entropy_init == NULL )
5948 rng->entropy_init = mbedtls_entropy_init;
5949 if( rng->entropy_free == NULL )
5950 rng->entropy_free = mbedtls_entropy_free;
5951
5952 rng->entropy_init( &rng->entropy );
5953#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5954 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5955 /* The PSA entropy injection feature depends on using NV seed as an entropy
5956 * source. Add NV seed as an entropy source for PSA entropy injection. */
5957 mbedtls_entropy_add_source( &rng->entropy,
5958 mbedtls_nv_seed_poll, NULL,
5959 MBEDTLS_ENTROPY_BLOCK_SIZE,
5960 MBEDTLS_ENTROPY_SOURCE_STRONG );
5961#endif
5962
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005963 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005964#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005965}
5966
5967/** Deinitialize the PSA random generator.
5968 */
5969static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5970{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005971#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5972 memset( rng, 0, sizeof( *rng ) );
5973#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005974 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005975 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005976#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005977}
5978
5979/** Seed the PSA random generator.
5980 */
5981static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5982{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005983#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5984 /* Do nothing: the external RNG seeds itself. */
5985 (void) rng;
5986 return( PSA_SUCCESS );
5987#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005988 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005989 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5990 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005991 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005992#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005993}
5994
Gilles Peskinee59236f2018-01-27 23:32:46 +01005995psa_status_t psa_generate_random( uint8_t *output,
5996 size_t output_size )
5997{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005998 GUARD_MODULE_INITIALIZED;
5999
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006000#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6001
6002 size_t output_length = 0;
6003 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6004 output, output_size,
6005 &output_length );
6006 if( status != PSA_SUCCESS )
6007 return( status );
6008 /* Breaking up a request into smaller chunks is currently not supported
6009 * for the extrernal RNG interface. */
6010 if( output_length != output_size )
6011 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6012 return( PSA_SUCCESS );
6013
6014#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6015
Gilles Peskine71ddab92021-01-04 21:01:07 +01006016 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006017 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006018 size_t request_size =
6019 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6020 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6021 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006022 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6023 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006024 if( ret != 0 )
6025 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006026 output_size -= request_size;
6027 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006028 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006029 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006030#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006031}
6032
Gilles Peskine8814fc42020-12-14 15:33:44 +01006033/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006034 *
6035 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6036 * `psa_generate_random(...)`. The state parameter is ignored since the
6037 * PSA API doesn't support passing an explicit state.
6038 *
6039 * In the non-external case, psa_generate_random() calls an
6040 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6041 * and semantics as mbedtls_psa_get_random(). As an optimization,
6042 * instead of doing this back-and-forth between the PSA API and the
6043 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6044 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006045 */
6046#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6047int mbedtls_psa_get_random( void *p_rng,
6048 unsigned char *output,
6049 size_t output_size )
6050{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006051 /* This function takes a pointer to the RNG state because that's what
6052 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6053 * its own state internally and doesn't let the caller access that state.
6054 * So we just ignore the state parameter, and in practice we'll pass
6055 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006056 (void) p_rng;
6057 psa_status_t status = psa_generate_random( output, output_size );
6058 if( status == PSA_SUCCESS )
6059 return( 0 );
6060 else
6061 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6062}
6063#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6064
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006065#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6066#include "mbedtls/entropy_poll.h"
6067
Gilles Peskine7228da22019-07-15 11:06:15 +02006068psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006069 size_t seed_size )
6070{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006071 if( global_data.initialized )
6072 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006073
6074 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6075 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6076 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6077 return( PSA_ERROR_INVALID_ARGUMENT );
6078
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006079 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006080}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006081#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006082
Ronald Cron3772afe2021-02-08 16:10:05 +01006083/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006084 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006085 * \param type The key type
6086 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006087 *
6088 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006089 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006090 * \retval #PSA_ERROR_INVALID_ARGUMENT
6091 * The size in bits of the key is not valid.
6092 * \retval #PSA_ERROR_NOT_SUPPORTED
6093 * The type and/or the size in bits of the key or the combination of
6094 * the two is not supported.
6095 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006096static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6097 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006100
Gilles Peskinee59236f2018-01-27 23:32:46 +01006101 if( key_type_is_raw_bytes( type ) )
6102 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006103 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006104 if( status != PSA_SUCCESS )
6105 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006106 }
6107 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006108#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006109 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6110 {
6111 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6112 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006113
Ronald Cron01b2aba2020-10-05 09:42:02 +02006114 /* Accept only byte-aligned keys, for the same reasons as
6115 * in psa_import_rsa_key(). */
6116 if( bits % 8 != 0 )
6117 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006118 }
6119 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006120#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006121
Ronald Cron5c4d3862020-12-07 11:07:24 +01006122#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006123 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6124 {
Ronald Crond81ab562021-02-16 09:01:16 +01006125 /* To avoid empty block, return successfully here. */
6126 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006127 }
6128 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006129#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006130 {
6131 return( PSA_ERROR_NOT_SUPPORTED );
6132 }
6133
6134 return( PSA_SUCCESS );
6135}
6136
Ronald Cron55ed0592020-10-05 10:30:40 +02006137psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006138 const psa_key_attributes_t *attributes,
6139 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006140{
6141 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006142 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006143
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006144 if( ( attributes->domain_parameters == NULL ) &&
6145 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006146 return( PSA_ERROR_INVALID_ARGUMENT );
6147
Ronald Cron01b2aba2020-10-05 09:42:02 +02006148 if( key_type_is_raw_bytes( type ) )
6149 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006150 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006151 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006152 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006153
David Brown12ca5032021-02-11 11:02:00 -07006154#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006155 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006156 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006157#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006158 }
6159 else
6160
John Durkop07cc04a2020-11-16 22:08:34 -08006161#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006162 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006163 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006164 return( mbedtls_psa_rsa_generate_key( attributes,
6165 key_buffer,
6166 key_buffer_size,
6167 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006168 }
6169 else
John Durkop07cc04a2020-11-16 22:08:34 -08006170#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171
John Durkop9814fa22020-11-04 12:28:15 -08006172#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006173 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006174 {
Ronald Cron7023db52020-11-20 18:17:42 +01006175 return( mbedtls_psa_ecp_generate_key( attributes,
6176 key_buffer,
6177 key_buffer_size,
6178 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006179 }
6180 else
John Durkop9814fa22020-11-04 12:28:15 -08006181#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006182 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006183 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006185 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006186
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006187 return( PSA_SUCCESS );
6188}
Darryl Green0c6575a2018-11-07 16:05:30 +00006189
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006190psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006191 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006192{
6193 psa_status_t status;
6194 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006195 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006196 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006197
Ronald Cron81709fc2020-11-14 12:10:32 +01006198 *key = MBEDTLS_SVC_KEY_ID_INIT;
6199
Gilles Peskine0f84d622019-09-12 19:03:13 +02006200 /* Reject any attempt to create a zero-length key so that we don't
6201 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6202 if( psa_get_key_bits( attributes ) == 0 )
6203 return( PSA_ERROR_INVALID_ARGUMENT );
6204
Ronald Cron81709fc2020-11-14 12:10:32 +01006205 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6206 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006207 if( status != PSA_SUCCESS )
6208 goto exit;
6209
Ronald Cron977c2472020-10-13 08:32:21 +02006210 /* In the case of a transparent key or an opaque key stored in local
6211 * storage (thus not in the case of generating a key in a secure element
6212 * or cryptoprocessor with storage), we have to allocate a buffer to
6213 * hold the generated key material. */
6214 if( slot->key.data == NULL )
6215 {
6216 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6217 PSA_KEY_LOCATION_LOCAL_STORAGE )
6218 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006219 status = psa_validate_key_type_and_size_for_key_generation(
6220 attributes->core.type, attributes->core.bits );
6221 if( status != PSA_SUCCESS )
6222 goto exit;
6223
6224 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6225 attributes->core.type,
6226 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006227 }
6228 else
6229 {
6230 status = psa_driver_wrapper_get_key_buffer_size(
6231 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006232 if( status != PSA_SUCCESS )
6233 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006234 }
Ronald Cron977c2472020-10-13 08:32:21 +02006235
6236 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6237 if( status != PSA_SUCCESS )
6238 goto exit;
6239 }
6240
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006241 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006242 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006243
Ronald Cron2b56bc82020-10-05 10:02:26 +02006244 if( status != PSA_SUCCESS )
6245 psa_remove_key_data_from_memory( slot );
6246
Gilles Peskine11792082019-08-06 18:36:36 +02006247exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006248 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006249 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006250 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006251 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006252
Darryl Green0c6575a2018-11-07 16:05:30 +00006253 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254}
6255
Gilles Peskinee59236f2018-01-27 23:32:46 +01006256/****************************************************************/
6257/* Module setup */
6258/****************************************************************/
6259
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006260#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006261psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6262 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6263 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6264{
6265 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6266 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006267 global_data.rng.entropy_init = entropy_init;
6268 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006269 return( PSA_SUCCESS );
6270}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006271#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006272
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273void mbedtls_psa_crypto_free( void )
6274{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006275 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006276 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6277 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006278 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006279 }
6280 /* Wipe all remaining data, including configuration.
6281 * In particular, this sets all state indicator to the value
6282 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006283 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006284#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006285 /* Unregister all secure element drivers, so that we restart from
6286 * a pristine state. */
6287 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006288#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289}
6290
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006291#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6292/** Recover a transaction that was interrupted by a power failure.
6293 *
6294 * This function is called during initialization, before psa_crypto_init()
6295 * returns. If this function returns a failure status, the initialization
6296 * fails.
6297 */
6298static psa_status_t psa_crypto_recover_transaction(
6299 const psa_crypto_transaction_t *transaction )
6300{
6301 switch( transaction->unknown.type )
6302 {
6303 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6304 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006305 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006306 * is implemented.
6307 * https://github.com/ARMmbed/mbed-crypto/issues/218
6308 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006309 default:
6310 /* We found an unsupported transaction in the storage.
6311 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006312 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006313 }
6314}
6315#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6316
Gilles Peskinee59236f2018-01-27 23:32:46 +01006317psa_status_t psa_crypto_init( void )
6318{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006319 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006320
Gilles Peskinec6b69072018-11-20 21:42:52 +01006321 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006322 if( global_data.initialized != 0 )
6323 return( PSA_SUCCESS );
6324
Gilles Peskine30524eb2020-11-13 17:02:26 +01006325 /* Initialize and seed the random generator. */
6326 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006327 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006328 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006329 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006330 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006331 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006332
Gilles Peskine66fb1262018-12-10 16:29:04 +01006333 status = psa_initialize_key_slots( );
6334 if( status != PSA_SUCCESS )
6335 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006336
Gilles Peskined9348f22019-10-01 15:22:29 +02006337#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6338 status = psa_init_all_se_drivers( );
6339 if( status != PSA_SUCCESS )
6340 goto exit;
6341#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6342
Gilles Peskine4b734222019-07-24 15:56:31 +02006343#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006344 status = psa_crypto_load_transaction( );
6345 if( status == PSA_SUCCESS )
6346 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006347 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6348 if( status != PSA_SUCCESS )
6349 goto exit;
6350 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006351 }
6352 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6353 {
6354 /* There's no transaction to complete. It's all good. */
6355 status = PSA_SUCCESS;
6356 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006357#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006358
Gilles Peskinec6b69072018-11-20 21:42:52 +01006359 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006360 global_data.initialized = 1;
6361
6362exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006363 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006364 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006365 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366}
6367
6368#endif /* MBEDTLS_PSA_CRYPTO_C */