blob: eff86bcf0877c38027b4d53389d217d1bffd3263 [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 Cooremana1d83222021-02-25 10:20:29 +0100717 /* If both are wildcards, return most restrictive 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{
Gilles Peskine549ea862019-05-22 11:45:59 +0200747 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200748 if( requested_alg == policy_alg )
749 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100750 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
751 * and requested_alg is the same hash-and-sign family with any hash,
752 * then requested_alg is compliant with policy_alg. */
753 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
754 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200755 {
Steven Cooreman03488022021-02-18 12:07:20 +0100756 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
757 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
758 }
759 /* If policy_alg is a wildcard AEAD algorithm of the same base as
760 * the requested algorithm, check the requested tag length to be
761 * equal-length or longer than the wildcard-specified length. */
762 if( PSA_ALG_IS_AEAD( policy_alg ) &&
763 PSA_ALG_IS_AEAD( requested_alg ) &&
764 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
765 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100766 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100767 {
768 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
769 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
770 }
771 /* If policy_alg is a wildcard MAC algorithm of the same base as
772 * the requested algorithm, check the requested tag length to be
773 * equal-length or longer than the wildcard-specified length. */
774 if( PSA_ALG_IS_MAC( policy_alg ) &&
775 PSA_ALG_IS_MAC( requested_alg ) &&
776 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
777 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100778 ( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100779 {
780 /* Special case: full-length MAC is encoded with 0-length.
781 * A minimum-length policy will always allow a full-length MAC. */
782 if( PSA_ALG_FULL_LENGTH_MAC( requested_alg ) == requested_alg )
783 return( 1 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100784
Steven Cooreman03488022021-02-18 12:07:20 +0100785 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
786 PSA_MAC_TRUNCATED_LENGTH( requested_alg ) );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200787 }
Steven Cooremance48e852020-10-05 16:02:45 +0200788 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200789 * a key derivation with that key agreement should also be allowed. This
790 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200791 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
792 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
793 {
794 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
795 policy_alg );
796 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100797 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200798 return( 0 );
799}
800
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100801/** Test whether a policy permits an algorithm.
802 *
803 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100804 *
805 * \retval PSA_SUCCESS When \p alg is a specific algorithm
806 * allowed by the \p policy.
807 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
808 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
809 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100810 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100811static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
812 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100813{
Steven Cooremand788fab2021-02-25 11:29:17 +0100814 /* '0' is not a valid algorithm */
815 if( alg == 0 )
816 return( PSA_ERROR_INVALID_ARGUMENT );
817
Steven Cooreman7e39f052021-02-23 14:18:32 +0100818 /* A requested algorithm cannot be a wildcard. */
819 if( PSA_ALG_IS_WILDCARD( alg ) )
820 return( PSA_ERROR_INVALID_ARGUMENT );
821
822 if( psa_key_algorithm_permits( policy->alg, alg ) ||
823 psa_key_algorithm_permits( policy->alg2, alg ) )
824 return( PSA_SUCCESS );
825 else
826 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100827}
828
Gilles Peskinef603c712019-01-19 13:40:11 +0100829/** Restrict a key policy based on a constraint.
830 *
831 * \param[in,out] policy The policy to restrict.
832 * \param[in] constraint The policy constraint to apply.
833 *
834 * \retval #PSA_SUCCESS
835 * \c *policy contains the intersection of the original value of
836 * \c *policy and \c *constraint.
837 * \retval #PSA_ERROR_INVALID_ARGUMENT
838 * \c *policy and \c *constraint are incompatible.
839 * \c *policy is unchanged.
840 */
841static psa_status_t psa_restrict_key_policy(
842 psa_key_policy_t *policy,
843 const psa_key_policy_t *constraint )
844{
845 psa_algorithm_t intersection_alg =
846 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200847 psa_algorithm_t intersection_alg2 =
848 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100849 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
850 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200851 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
852 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100853 policy->usage &= constraint->usage;
854 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200855 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100856 return( PSA_SUCCESS );
857}
858
Ronald Cron5c522922020-11-14 16:35:34 +0100859/** Get the description of a key given its identifier and policy constraints
860 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200861 *
Ronald Cron5c522922020-11-14 16:35:34 +0100862 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100863 * nonzero, the key must allow operations with this algorithm. If \p alg is
864 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100865 *
Ronald Cron5c522922020-11-14 16:35:34 +0100866 * In case of a persistent key, the function loads the description of the key
867 * into a key slot if not already done.
868 *
869 * On success, the returned key slot is locked. It is the responsibility of
870 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200871 */
Ronald Cron5c522922020-11-14 16:35:34 +0100872static psa_status_t psa_get_and_lock_key_slot_with_policy(
873 mbedtls_svc_key_id_t key,
874 psa_key_slot_t **p_slot,
875 psa_key_usage_t usage,
876 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100877{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200878 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
879 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100880
Ronald Cron5c522922020-11-14 16:35:34 +0100881 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100882 if( status != PSA_SUCCESS )
883 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200884 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100885
886 /* Enforce that usage policy for the key slot contains all the flags
887 * required by the usage parameter. There is one exception: public
888 * keys can always be exported, so we treat public key objects as
889 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200890 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100891 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200892
893 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +0200894 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +0200895 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100896
897 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100898 if( alg != 0 )
899 {
900 status = psa_key_policy_permits( &slot->attr.policy, alg );
901 if( status != PSA_SUCCESS )
902 goto error;
903 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100904
Darryl Green06fd18d2018-07-16 11:21:11 +0100905 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200906
907error:
908 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100909 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200910
911 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100912}
Darryl Green940d72c2018-07-13 13:18:51 +0100913
Ronald Cron5c522922020-11-14 16:35:34 +0100914/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200915 *
916 * A transparent key is a key for which the key material is directly
917 * available, as opposed to a key in a secure element.
918 *
Ronald Cron5c522922020-11-14 16:35:34 +0100919 * This is a temporary function to use instead of
920 * psa_get_and_lock_key_slot_with_policy() until secure element support is
921 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200922 *
Ronald Cron5c522922020-11-14 16:35:34 +0100923 * On success, the returned key slot is locked. It is the responsibility of the
924 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200925 */
926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100927static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
928 mbedtls_svc_key_id_t key,
929 psa_key_slot_t **p_slot,
930 psa_key_usage_t usage,
931 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200932{
Ronald Cron5c522922020-11-14 16:35:34 +0100933 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
934 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200935 if( status != PSA_SUCCESS )
936 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200937
Gilles Peskineadad8132019-07-25 11:31:23 +0200938 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200939 {
Ronald Cron5c522922020-11-14 16:35:34 +0100940 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 *p_slot = NULL;
942 return( PSA_ERROR_NOT_SUPPORTED );
943 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200944
Gilles Peskine28f8f302019-07-24 13:30:31 +0200945 return( PSA_SUCCESS );
946}
947#else /* MBEDTLS_PSA_CRYPTO_SE_C */
948/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100949#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
950 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200951#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
952
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100953/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100954static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000955{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100956 /* Data pointer will always be either a valid pointer or NULL in an
957 * initialized slot, so we can just free it. */
958 if( slot->key.data != NULL )
959 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
960
961 mbedtls_free( slot->key.data );
962 slot->key.data = NULL;
963 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000964
965 return( PSA_SUCCESS );
966}
967
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100968/** Completely wipe a slot in memory, including its policy.
969 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100970psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100971{
972 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100973
974 /*
975 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100976 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100977 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
978 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100979 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100980 */
Ronald Cron5c522922020-11-14 16:35:34 +0100981 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100982 {
983#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100984 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100985#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100986 status = PSA_ERROR_CORRUPTION_DETECTED;
987 }
988
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200989 /* Multipart operations may still be using the key. This is safe
990 * because all multipart operation objects are independent from
991 * the key slot: if they need to access the key after the setup
992 * phase, they have a copy of the key. Note that this means that
993 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100994 /* At this point, key material and other type-specific content has
995 * been wiped. Clear remaining metadata. We can call memset and not
996 * zeroize because the metadata is not particularly sensitive. */
997 memset( slot, 0, sizeof( *slot ) );
998 return( status );
999}
1000
Ronald Croncf56a0a2020-08-04 09:51:30 +02001001psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001002{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001003 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001004 psa_status_t status; /* status of the last operation */
1005 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001006#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1007 psa_se_drv_table_entry_t *driver;
1008#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001009
Ronald Croncf56a0a2020-08-04 09:51:30 +02001010 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001011 return( PSA_SUCCESS );
1012
Ronald Cronf2911112020-10-29 17:51:10 +01001013 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001014 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001015 * key, this will load the key description from persistent memory if not
1016 * done yet. We cannot avoid this loading as without it we don't know if
1017 * the key is operated by an SE or not and this information is needed by
1018 * the current implementation.
1019 */
Ronald Cron5c522922020-11-14 16:35:34 +01001020 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001021 if( status != PSA_SUCCESS )
1022 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001023
Ronald Cronf2911112020-10-29 17:51:10 +01001024 /*
1025 * If the key slot containing the key description is under access by the
1026 * library (apart from the present access), the key cannot be destroyed
1027 * yet. For the time being, just return in error. Eventually (to be
1028 * implemented), the key should be destroyed when all accesses have
1029 * stopped.
1030 */
Ronald Cron5c522922020-11-14 16:35:34 +01001031 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001032 {
Ronald Cron5c522922020-11-14 16:35:34 +01001033 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001034 return( PSA_ERROR_GENERIC_ERROR );
1035 }
1036
Gilles Peskine354f7672019-07-12 23:46:38 +02001037#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001038 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001039 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001041 /* For a key in a secure element, we need to do three things:
1042 * remove the key file in internal storage, destroy the
1043 * key inside the secure element, and update the driver's
1044 * persistent data. Start a transaction that will encompass these
1045 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001047 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001048 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001049 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001050 status = psa_crypto_save_transaction( );
1051 if( status != PSA_SUCCESS )
1052 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001053 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001054 /* We should still try to destroy the key in the secure
1055 * element and the key metadata in storage. This is especially
1056 * important if the error is that the storage is full.
1057 * But how to do it exactly without risking an inconsistent
1058 * state after a reset?
1059 * https://github.com/ARMmbed/mbed-crypto/issues/215
1060 */
1061 overall_status = status;
1062 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001063 }
1064
Ronald Cronea0f8a62020-11-25 17:52:23 +01001065 status = psa_destroy_se_key( driver,
1066 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001067 if( overall_status == PSA_SUCCESS )
1068 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001069 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001070#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1071
Darryl Greend49a4992018-06-18 17:27:26 +01001072#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001073 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001074 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001075 status = psa_destroy_persistent_key( slot->attr.id );
1076 if( overall_status == PSA_SUCCESS )
1077 overall_status = status;
1078
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001079 /* TODO: other slots may have a copy of the same key. We should
1080 * invalidate them.
1081 * https://github.com/ARMmbed/mbed-crypto/issues/214
1082 */
Darryl Greend49a4992018-06-18 17:27:26 +01001083 }
1084#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001085
Gilles Peskinefc762652019-07-22 19:30:34 +02001086#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1087 if( driver != NULL )
1088 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001089 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001090 if( overall_status == PSA_SUCCESS )
1091 overall_status = status;
1092 status = psa_crypto_stop_transaction( );
1093 if( overall_status == PSA_SUCCESS )
1094 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001095 }
1096#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1097
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001098#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1099exit:
1100#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001101 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001102 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1103 if( overall_status == PSA_SUCCESS )
1104 overall_status = status;
1105 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001106}
1107
John Durkop07cc04a2020-11-16 22:08:34 -08001108#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001109 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001110static psa_status_t psa_get_rsa_public_exponent(
1111 const mbedtls_rsa_context *rsa,
1112 psa_key_attributes_t *attributes )
1113{
1114 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001115 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001116 uint8_t *buffer = NULL;
1117 size_t buflen;
1118 mbedtls_mpi_init( &mpi );
1119
1120 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1121 if( ret != 0 )
1122 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001123 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1124 {
1125 /* It's the default value, which is reported as an empty string,
1126 * so there's nothing to do. */
1127 goto exit;
1128 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001129
1130 buflen = mbedtls_mpi_size( &mpi );
1131 buffer = mbedtls_calloc( 1, buflen );
1132 if( buffer == NULL )
1133 {
1134 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1135 goto exit;
1136 }
1137 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1138 if( ret != 0 )
1139 goto exit;
1140 attributes->domain_parameters = buffer;
1141 attributes->domain_parameters_size = buflen;
1142
1143exit:
1144 mbedtls_mpi_free( &mpi );
1145 if( ret != 0 )
1146 mbedtls_free( buffer );
1147 return( mbedtls_to_psa_error( ret ) );
1148}
John Durkop07cc04a2020-11-16 22:08:34 -08001149#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001150 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001151
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001152/** Retrieve all the publicly-accessible attributes of a key.
1153 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001154psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001155 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001156{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001157 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001158 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001159 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001160
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001161 psa_reset_key_attributes( attributes );
1162
Ronald Cron5c522922020-11-14 16:35:34 +01001163 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001164 if( status != PSA_SUCCESS )
1165 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001166
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001167 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001168 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1169 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1170
1171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1172 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001173 psa_set_key_slot_number( attributes,
1174 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001175#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001176
Gilles Peskine8e338702019-07-30 20:06:31 +02001177 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001178 {
John Durkop07cc04a2020-11-16 22:08:34 -08001179#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001180 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001181 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001182 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001183#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001184 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001185 * is not yet implemented.
1186 * https://github.com/ARMmbed/mbed-crypto/issues/216
1187 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001188 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001189 break;
1190#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001191 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001192 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001193
Ronald Cron90857082020-11-25 14:58:33 +01001194 status = mbedtls_psa_rsa_load_representation(
1195 slot->attr.type,
1196 slot->key.data,
1197 slot->key.bytes,
1198 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001199 if( status != PSA_SUCCESS )
1200 break;
1201
Steven Cooremana2371e52020-07-28 14:30:39 +02001202 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001203 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001204 mbedtls_rsa_free( rsa );
1205 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001206 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001208#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001209 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 default:
1211 /* Nothing else to do. */
1212 break;
1213 }
1214
1215 if( status != PSA_SUCCESS )
1216 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001217
Ronald Cron5c522922020-11-14 16:35:34 +01001218 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001219
Ronald Cron5c522922020-11-14 16:35:34 +01001220 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001221}
1222
Gilles Peskinec8000c02019-08-02 20:15:51 +02001223#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1224psa_status_t psa_get_key_slot_number(
1225 const psa_key_attributes_t *attributes,
1226 psa_key_slot_number_t *slot_number )
1227{
1228 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1229 {
1230 *slot_number = attributes->slot_number;
1231 return( PSA_SUCCESS );
1232 }
1233 else
1234 return( PSA_ERROR_INVALID_ARGUMENT );
1235}
1236#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1237
Ronald Crond18b5f82020-11-25 16:46:05 +01001238static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1239 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001240 uint8_t *data,
1241 size_t data_size,
1242 size_t *data_length )
1243{
Ronald Crond18b5f82020-11-25 16:46:05 +01001244 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001245 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001246 memcpy( data, key_buffer, key_buffer_size );
1247 memset( data + key_buffer_size, 0,
1248 data_size - key_buffer_size );
1249 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001250 return( PSA_SUCCESS );
1251}
1252
Ronald Cron7285cda2020-11-26 14:46:37 +01001253psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001254 const psa_key_attributes_t *attributes,
1255 const uint8_t *key_buffer, size_t key_buffer_size,
1256 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001257{
Ronald Crond18b5f82020-11-25 16:46:05 +01001258 psa_key_type_t type = attributes->core.type;
1259
Ronald Crond18b5f82020-11-25 16:46:05 +01001260 if( key_type_is_raw_bytes( type ) ||
1261 PSA_KEY_TYPE_IS_RSA( type ) ||
1262 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001263 {
1264 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001265 key_buffer, key_buffer_size,
1266 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001267 }
1268 else
1269 {
1270 /* This shouldn't happen in the reference implementation, but
1271 it is valid for a special-purpose implementation to omit
1272 support for exporting certain key types. */
1273 return( PSA_ERROR_NOT_SUPPORTED );
1274 }
1275}
1276
1277psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1278 uint8_t *data,
1279 size_t data_size,
1280 size_t *data_length )
1281{
1282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1283 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1284 psa_key_slot_t *slot;
1285
Ronald Crone907e552021-01-18 13:22:38 +01001286 /* Reject a zero-length output buffer now, since this can never be a
1287 * valid key representation. This way we know that data must be a valid
1288 * pointer and we can do things like memset(data, ..., data_size). */
1289 if( data_size == 0 )
1290 return( PSA_ERROR_BUFFER_TOO_SMALL );
1291
Ronald Cron9486f9d2020-11-26 13:36:23 +01001292 /* Set the key to empty now, so that even when there are errors, we always
1293 * set data_length to a value between 0 and data_size. On error, setting
1294 * the key to empty is a good choice because an empty key representation is
1295 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001296 *data_length = 0;
1297
Ronald Cron9486f9d2020-11-26 13:36:23 +01001298 /* Export requires the EXPORT flag. There is an exception for public keys,
1299 * which don't require any flag, but
1300 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1301 */
1302 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1303 PSA_KEY_USAGE_EXPORT, 0 );
1304 if( status != PSA_SUCCESS )
1305 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001306
Ronald Crond18b5f82020-11-25 16:46:05 +01001307 psa_key_attributes_t attributes = {
1308 .core = slot->attr
1309 };
Ronald Cron67227982020-11-26 15:16:05 +01001310 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001311 slot->key.data, slot->key.bytes,
1312 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001313
Ronald Cron9486f9d2020-11-26 13:36:23 +01001314 unlock_status = psa_unlock_key_slot( slot );
1315
1316 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1317}
1318
Ronald Cron7285cda2020-11-26 14:46:37 +01001319psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001320 const psa_key_attributes_t *attributes,
1321 const uint8_t *key_buffer,
1322 size_t key_buffer_size,
1323 uint8_t *data,
1324 size_t data_size,
1325 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001326{
Ronald Crond18b5f82020-11-25 16:46:05 +01001327 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001328
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001330 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001331 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001332 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001333 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001334 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001335 key_buffer, key_buffer_size,
1336 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001337 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001338
Ronald Crond18b5f82020-11-25 16:46:05 +01001339 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001340 {
John Durkop0e005192020-10-31 22:06:54 -07001341#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1342 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001343 return( mbedtls_psa_rsa_export_public_key( attributes,
1344 key_buffer,
1345 key_buffer_size,
1346 data,
1347 data_size,
1348 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001350 /* We don't know how to convert a private RSA key to public. */
1351 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001352#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1353 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001354 }
1355 else
Moran Pekera998bc62018-04-16 18:16:20 +03001356 {
John Durkop6ba40d12020-11-10 08:50:04 -08001357#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1358 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001359 return( mbedtls_psa_ecp_export_public_key( attributes,
1360 key_buffer,
1361 key_buffer_size,
1362 data,
1363 data_size,
1364 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001365#else
1366 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001367 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001368#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1369 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001370 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001371 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001372 else
1373 {
1374 /* This shouldn't happen in the reference implementation, but
1375 it is valid for a special-purpose implementation to omit
1376 support for exporting certain key types. */
1377 return( PSA_ERROR_NOT_SUPPORTED );
1378 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001379}
Ronald Crond18b5f82020-11-25 16:46:05 +01001380
Ronald Croncf56a0a2020-08-04 09:51:30 +02001381psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001382 uint8_t *data,
1383 size_t data_size,
1384 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001385{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001387 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001388 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001389
Ronald Crone907e552021-01-18 13:22:38 +01001390 /* Reject a zero-length output buffer now, since this can never be a
1391 * valid key representation. This way we know that data must be a valid
1392 * pointer and we can do things like memset(data, ..., data_size). */
1393 if( data_size == 0 )
1394 return( PSA_ERROR_BUFFER_TOO_SMALL );
1395
Darryl Greendd8fb772018-11-07 16:00:44 +00001396 /* Set the key to empty now, so that even when there are errors, we always
1397 * set data_length to a value between 0 and data_size. On error, setting
1398 * the key to empty is a good choice because an empty key representation is
1399 * unlikely to be accepted anywhere. */
1400 *data_length = 0;
1401
1402 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001403 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001404 if( status != PSA_SUCCESS )
1405 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001406
Ronald Cron9486f9d2020-11-26 13:36:23 +01001407 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1408 {
1409 status = PSA_ERROR_INVALID_ARGUMENT;
1410 goto exit;
1411 }
1412
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 psa_key_attributes_t attributes = {
1414 .core = slot->attr
1415 };
Ronald Cron67227982020-11-26 15:16:05 +01001416 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001417 &attributes, slot->key.data, slot->key.bytes,
1418 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001419
1420exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001421 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001422
Ronald Cron5c522922020-11-14 16:35:34 +01001423 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001424}
1425
Gilles Peskine91e8c332019-08-02 19:19:39 +02001426#if defined(static_assert)
1427static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1428 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001429static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001430 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001431static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001432 "One or more key attribute flag is listed as both internal-only and external-only" );
1433#endif
1434
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001435/** Validate that a key policy is internally well-formed.
1436 *
1437 * This function only rejects invalid policies. It does not validate the
1438 * consistency of the policy with respect to other attributes of the key
1439 * such as the key type.
1440 */
1441static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001442{
1443 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001444 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001445 PSA_KEY_USAGE_ENCRYPT |
1446 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001447 PSA_KEY_USAGE_SIGN_HASH |
1448 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001449 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1450 return( PSA_ERROR_INVALID_ARGUMENT );
1451
Gilles Peskine4747d192019-04-17 15:05:45 +02001452 return( PSA_SUCCESS );
1453}
1454
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001455/** Validate the internal consistency of key attributes.
1456 *
1457 * This function only rejects invalid attribute values. If does not
1458 * validate the consistency of the attributes with any key data that may
1459 * be involved in the creation of the key.
1460 *
1461 * Call this function early in the key creation process.
1462 *
1463 * \param[in] attributes Key attributes for the new key.
1464 * \param[out] p_drv On any return, the driver for the key, if any.
1465 * NULL for a transparent key.
1466 *
1467 */
1468static psa_status_t psa_validate_key_attributes(
1469 const psa_key_attributes_t *attributes,
1470 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001471{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001472 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001473 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001474 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001475
Ronald Cron54b90082020-10-29 15:26:43 +01001476 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001477 if( status != PSA_SUCCESS )
1478 return( status );
1479
Ronald Crond2ed4812020-07-17 16:11:30 +02001480 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001481 if( status != PSA_SUCCESS )
1482 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001483
Ronald Cron65f38a32020-10-23 17:11:13 +02001484 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1485 {
1486 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1487 return( PSA_ERROR_INVALID_ARGUMENT );
1488 }
1489 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001490 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001491 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001492 if( status != PSA_SUCCESS )
1493 return( status );
1494 }
1495
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001496 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001497 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001498 return( status );
1499
1500 /* Refuse to create overly large keys.
1501 * Note that this doesn't trigger on import if the attributes don't
1502 * explicitly specify a size (so psa_get_key_bits returns 0), so
1503 * psa_import_key() needs its own checks. */
1504 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1505 return( PSA_ERROR_NOT_SUPPORTED );
1506
Gilles Peskine91e8c332019-08-02 19:19:39 +02001507 /* Reject invalid flags. These should not be reachable through the API. */
1508 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1509 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1510 return( PSA_ERROR_INVALID_ARGUMENT );
1511
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512 return( PSA_SUCCESS );
1513}
1514
Gilles Peskine4747d192019-04-17 15:05:45 +02001515/** Prepare a key slot to receive key material.
1516 *
1517 * This function allocates a key slot and sets its metadata.
1518 *
1519 * If this function fails, call psa_fail_key_creation().
1520 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001521 * This function is intended to be used as follows:
1522 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001523 * it with the specified attributes, and in case of a volatile key assign it
1524 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001525 * -# Populate the slot with the key material.
1526 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1527 * In case of failure at any step, stop the sequence and call
1528 * psa_fail_key_creation().
1529 *
Ronald Cron5c522922020-11-14 16:35:34 +01001530 * On success, the key slot is locked. It is the responsibility of the caller
1531 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001532 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001533 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001534 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001535 * \param[out] p_slot On success, a pointer to the prepared slot.
1536 * \param[out] p_drv On any return, the driver for the key, if any.
1537 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 *
1539 * \retval #PSA_SUCCESS
1540 * The key slot is ready to receive key material.
1541 * \return If this function fails, the key slot is an invalid state.
1542 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001543 */
1544static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001545 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001546 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001548 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001549{
1550 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001551 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001552 psa_key_slot_t *slot;
1553
Gilles Peskinedf179142019-07-15 22:02:14 +02001554 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001555 *p_drv = NULL;
1556
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557 status = psa_validate_key_attributes( attributes, p_drv );
1558 if( status != PSA_SUCCESS )
1559 return( status );
1560
Ronald Cronc4d1b512020-07-31 11:26:37 +02001561 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001562 if( status != PSA_SUCCESS )
1563 return( status );
1564 slot = *p_slot;
1565
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001566 /* We're storing the declared bit-size of the key. It's up to each
1567 * creation mechanism to verify that this information is correct.
1568 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001569 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001570 * is optional (import, copy). In case of a volatile key, assign it the
1571 * volatile key identifier associated to the slot returned to contain its
1572 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001573
1574 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001575 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1576 {
1577#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1578 slot->attr.id = volatile_key_id;
1579#else
1580 slot->attr.id.key_id = volatile_key_id;
1581#endif
1582 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001583
Gilles Peskine91e8c332019-08-02 19:19:39 +02001584 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001585 * external-only flags, query `attributes`. Thanks to the check
1586 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001587 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001588 * may have set. */
1589 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001590
Gilles Peskinecbaff462019-07-12 23:46:04 +02001591#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001592 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001593 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001594 * create the key file in internal storage, create the
1595 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001596 * persistent data. This is done by starting a transaction that will
1597 * encompass these three actions.
1598 * For registering a volatile key, we just need to find an appropriate
1599 * slot number inside the SE. Since the key is designated volatile, creating
1600 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001601 /* The first thing to do is to find a slot number for the new key.
1602 * We save the slot number in persistent storage as part of the
1603 * transaction data. It will be needed to recover if the power
1604 * fails during the key creation process, to clean up on the secure
1605 * element side after restarting. Obtaining a slot number from the
1606 * secure element driver updates its persistent state, but we do not yet
1607 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001608 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001609 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001610 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001611 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001612 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001613 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001614 if( status != PSA_SUCCESS )
1615 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001616
1617 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001618 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001619 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1620 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001621 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001622 psa_crypto_transaction.key.id = slot->attr.id;
1623 status = psa_crypto_save_transaction( );
1624 if( status != PSA_SUCCESS )
1625 {
1626 (void) psa_crypto_stop_transaction( );
1627 return( status );
1628 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001629 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001630
1631 status = psa_copy_key_material_into_slot(
1632 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001633 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001634
1635 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1636 {
1637 /* Key registration only makes sense with a secure element. */
1638 return( PSA_ERROR_INVALID_ARGUMENT );
1639 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001640#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1641
Ronald Cronc4d1b512020-07-31 11:26:37 +02001642 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001643}
Gilles Peskine4747d192019-04-17 15:05:45 +02001644
1645/** Finalize the creation of a key once its key material has been set.
1646 *
1647 * This entails writing the key to persistent storage.
1648 *
1649 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001650 * See the documentation of psa_start_key_creation() for the intended use
1651 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 *
Ronald Cron5c522922020-11-14 16:35:34 +01001653 * If the finalization succeeds, the function unlocks the key slot (it was
1654 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1655 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001656 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001657 * \param[in,out] slot Pointer to the slot with key material.
1658 * \param[in] driver The secure element driver for the key,
1659 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001660 * \param[out] key On success, identifier of the key. Note that the
1661 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001662 *
1663 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001664 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001665 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1666 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1667 * \retval #PSA_ERROR_ALREADY_EXISTS
1668 * \retval #PSA_ERROR_DATA_INVALID
1669 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001670 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001671 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001672 * \return If this function fails, the key slot is an invalid state.
1673 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001674 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001675static psa_status_t psa_finish_key_creation(
1676 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001677 psa_se_drv_table_entry_t *driver,
1678 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001679{
1680 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001681 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001682 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001683
1684#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001685 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1688 if( driver != NULL )
1689 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001690 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001691 psa_key_slot_number_t slot_number =
1692 psa_key_slot_get_slot_number( slot ) ;
1693
Gilles Peskineb46bef22019-07-30 21:32:04 +02001694#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001695 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001696 sizeof( data.slot_number ),
1697 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001698#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001699 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001700 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001701 (uint8_t*) &data,
1702 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001703 }
1704 else
1705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1706 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001707 /* Key material is saved in export representation in the slot, so
1708 * just pass the slot buffer for storage. */
1709 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001710 slot->key.data,
1711 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001712 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001714#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1715
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001717 /* Finish the transaction for a key creation. This does not
1718 * happen when registering an existing key. Detect this case
1719 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 * creation of a persistent key in a secure element requires a transaction,
1721 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001722 if( driver != NULL &&
1723 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001724 {
1725 status = psa_save_se_persistent_data( driver );
1726 if( status != PSA_SUCCESS )
1727 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001728 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729 return( status );
1730 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001731 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001732 }
1733#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1734
Ronald Cron50972942020-11-14 11:28:25 +01001735 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001736 {
1737 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001738 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001739 if( status != PSA_SUCCESS )
1740 *key = MBEDTLS_SVC_KEY_ID_INIT;
1741 }
Ronald Cron50972942020-11-14 11:28:25 +01001742
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 return( status );
1744}
1745
1746/** Abort the creation of a key.
1747 *
1748 * You may call this function after calling psa_start_key_creation(),
1749 * or after psa_finish_key_creation() fails. In other circumstances, this
1750 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001751 * See the documentation of psa_start_key_creation() for the intended use
1752 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001753 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001754 * \param[in,out] slot Pointer to the slot with key material.
1755 * \param[in] driver The secure element driver for the key,
1756 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001758static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001759 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001760{
Gilles Peskine011e4282019-06-26 18:34:38 +02001761 (void) driver;
1762
Gilles Peskine4747d192019-04-17 15:05:45 +02001763 if( slot == NULL )
1764 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001765
1766#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001767 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001768 * element, and the failure happened later (when saving metadata
1769 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001770 * element.
1771 * https://github.com/ARMmbed/mbed-crypto/issues/217
1772 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001773
Gilles Peskined7729582019-08-05 15:55:54 +02001774 /* Abort the ongoing transaction if any (there may not be one if
1775 * the creation process failed before starting one, or if the
1776 * key creation is a registration of a key in a secure element).
1777 * Earlier functions must already have done what it takes to undo any
1778 * partial creation. All that's left is to update the transaction data
1779 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001780 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001781#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1782
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 psa_wipe_key_slot( slot );
1784}
1785
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001786/** Validate optional attributes during key creation.
1787 *
1788 * Some key attributes are optional during key creation. If they are
1789 * specified in the attributes structure, check that they are consistent
1790 * with the data in the slot.
1791 *
1792 * This function should be called near the end of key creation, after
1793 * the slot in memory is fully populated but before saving persistent data.
1794 */
1795static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796 const psa_key_slot_t *slot,
1797 const psa_key_attributes_t *attributes )
1798{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001799 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001800 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001801 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 return( PSA_ERROR_INVALID_ARGUMENT );
1803 }
1804
1805 if( attributes->domain_parameters_size != 0 )
1806 {
John Durkop0e005192020-10-31 22:06:54 -07001807#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1808 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001809 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001810 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001811 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001812 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001813 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001814
Ronald Cron90857082020-11-25 14:58:33 +01001815 psa_status_t status = mbedtls_psa_rsa_load_representation(
1816 slot->attr.type,
1817 slot->key.data,
1818 slot->key.bytes,
1819 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001820 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001821 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001822
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001823 mbedtls_mpi_init( &actual );
1824 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001825 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001827 mbedtls_rsa_free( rsa );
1828 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001829 if( ret != 0 )
1830 goto rsa_exit;
1831 ret = mbedtls_mpi_read_binary( &required,
1832 attributes->domain_parameters,
1833 attributes->domain_parameters_size );
1834 if( ret != 0 )
1835 goto rsa_exit;
1836 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1837 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1838 rsa_exit:
1839 mbedtls_mpi_free( &actual );
1840 mbedtls_mpi_free( &required );
1841 if( ret != 0)
1842 return( mbedtls_to_psa_error( ret ) );
1843 }
1844 else
John Durkop9814fa22020-11-04 12:28:15 -08001845#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1846 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001847 {
1848 return( PSA_ERROR_INVALID_ARGUMENT );
1849 }
1850 }
1851
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001852 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001853 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001854 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001855 return( PSA_ERROR_INVALID_ARGUMENT );
1856 }
1857
1858 return( PSA_SUCCESS );
1859}
1860
Gilles Peskine4747d192019-04-17 15:05:45 +02001861psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001862 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001863 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001864 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001865{
1866 psa_status_t status;
1867 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001868 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001869 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001870
Ronald Cron81709fc2020-11-14 12:10:32 +01001871 *key = MBEDTLS_SVC_KEY_ID_INIT;
1872
Gilles Peskine0f84d622019-09-12 19:03:13 +02001873 /* Reject zero-length symmetric keys (including raw data key objects).
1874 * This also rejects any key which might be encoded as an empty string,
1875 * which is never valid. */
1876 if( data_length == 0 )
1877 return( PSA_ERROR_INVALID_ARGUMENT );
1878
Gilles Peskinedf179142019-07-15 22:02:14 +02001879 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001880 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001881 if( status != PSA_SUCCESS )
1882 goto exit;
1883
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001884 /* In the case of a transparent key or an opaque key stored in local
1885 * storage (thus not in the case of generating a key in a secure element
1886 * or cryptoprocessor with storage), we have to allocate a buffer to
1887 * hold the generated key material. */
1888 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001889 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001890 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001891 if( status != PSA_SUCCESS )
1892 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001893 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001894
1895 bits = slot->attr.bits;
1896 status = psa_driver_wrapper_import_key( attributes,
1897 data, data_length,
1898 slot->key.data,
1899 slot->key.bytes,
1900 &slot->key.bytes, &bits );
1901 if( status != PSA_SUCCESS )
1902 goto exit;
1903
1904 if( slot->attr.bits == 0 )
1905 slot->attr.bits = (psa_key_bits_t) bits;
1906 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001907 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001908 status = PSA_ERROR_INVALID_ARGUMENT;
1909 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001910 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001911
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001912 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001913 if( status != PSA_SUCCESS )
1914 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001915
Ronald Cron81709fc2020-11-14 12:10:32 +01001916 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001918 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001919 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001920
Gilles Peskine4747d192019-04-17 15:05:45 +02001921 return( status );
1922}
1923
Gilles Peskined7729582019-08-05 15:55:54 +02001924#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1925psa_status_t mbedtls_psa_register_se_key(
1926 const psa_key_attributes_t *attributes )
1927{
1928 psa_status_t status;
1929 psa_key_slot_t *slot = NULL;
1930 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001931 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001932
1933 /* Leaving attributes unspecified is not currently supported.
1934 * It could make sense to query the key type and size from the
1935 * secure element, but not all secure elements support this
1936 * and the driver HAL doesn't currently support it. */
1937 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1938 return( PSA_ERROR_NOT_SUPPORTED );
1939 if( psa_get_key_bits( attributes ) == 0 )
1940 return( PSA_ERROR_NOT_SUPPORTED );
1941
1942 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001943 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001944 if( status != PSA_SUCCESS )
1945 goto exit;
1946
Ronald Cron81709fc2020-11-14 12:10:32 +01001947 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001948
1949exit:
1950 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001951 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001952
Gilles Peskined7729582019-08-05 15:55:54 +02001953 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001954 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001955 return( status );
1956}
1957#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1958
Gilles Peskinef603c712019-01-19 13:40:11 +01001959static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001960 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001961{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001962 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001963 source->key.data,
1964 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001965 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001966 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001967
Steven Cooreman398aee52020-10-13 14:35:45 +02001968 target->attr.type = source->attr.type;
1969 target->attr.bits = source->attr.bits;
1970
1971 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001972}
1973
Ronald Croncf56a0a2020-08-04 09:51:30 +02001974psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001975 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001976 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001977{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001978 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001979 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001980 psa_key_slot_t *source_slot = NULL;
1981 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001983 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001984
Ronald Cron81709fc2020-11-14 12:10:32 +01001985 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1986
Ronald Cron5c522922020-11-14 16:35:34 +01001987 status = psa_get_and_lock_transparent_key_slot_with_policy(
1988 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001989 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 goto exit;
1991
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001992 status = psa_validate_optional_attributes( source_slot,
1993 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994 if( status != PSA_SUCCESS )
1995 goto exit;
1996
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001997 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001998 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001999 if( status != PSA_SUCCESS )
2000 goto exit;
2001
Ronald Cron81709fc2020-11-14 12:10:32 +01002002 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2003 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002004 if( status != PSA_SUCCESS )
2005 goto exit;
2006
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002007#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2008 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002009 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002010 /* Copying to a secure element is not implemented yet. */
2011 status = PSA_ERROR_NOT_SUPPORTED;
2012 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002013 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002014#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002015
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002016 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002017 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002018 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002019
Ronald Cron81709fc2020-11-14 12:10:32 +01002020 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002021exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002022 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002023 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002024
Ronald Cron5c522922020-11-14 16:35:34 +01002025 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002026
Ronald Cron5c522922020-11-14 16:35:34 +01002027 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002028}
2029
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002030
2031
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002032/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002033/* Message digests */
2034/****************************************************************/
2035
John Durkop07cc04a2020-11-16 22:08:34 -08002036#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002037 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2038 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002039 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002040static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002041{
2042 switch( alg )
2043 {
John Durkopee4e6602020-11-27 08:48:46 -08002044#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002045 case PSA_ALG_MD2:
2046 return( &mbedtls_md2_info );
2047#endif
John Durkopee4e6602020-11-27 08:48:46 -08002048#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002049 case PSA_ALG_MD4:
2050 return( &mbedtls_md4_info );
2051#endif
John Durkopee4e6602020-11-27 08:48:46 -08002052#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002053 case PSA_ALG_MD5:
2054 return( &mbedtls_md5_info );
2055#endif
John Durkopee4e6602020-11-27 08:48:46 -08002056#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002057 case PSA_ALG_RIPEMD160:
2058 return( &mbedtls_ripemd160_info );
2059#endif
John Durkopee4e6602020-11-27 08:48:46 -08002060#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002061 case PSA_ALG_SHA_1:
2062 return( &mbedtls_sha1_info );
2063#endif
John Durkopee4e6602020-11-27 08:48:46 -08002064#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002065 case PSA_ALG_SHA_224:
2066 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002067#endif
2068#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002069 case PSA_ALG_SHA_256:
2070 return( &mbedtls_sha256_info );
2071#endif
John Durkopee4e6602020-11-27 08:48:46 -08002072#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002073 case PSA_ALG_SHA_384:
2074 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002075#endif
John Durkopee4e6602020-11-27 08:48:46 -08002076#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002077 case PSA_ALG_SHA_512:
2078 return( &mbedtls_sha512_info );
2079#endif
2080 default:
2081 return( NULL );
2082 }
2083}
John Durkop07cc04a2020-11-16 22:08:34 -08002084#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002085 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2086 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2087 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002088
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002089psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2090{
2091 switch( operation->alg )
2092 {
Gilles Peskine81736312018-06-26 15:04:31 +02002093 case 0:
2094 /* The object has (apparently) been initialized but it is not
2095 * in use. It's ok to call abort on such an object, and there's
2096 * nothing to do. */
2097 break;
John Durkopee4e6602020-11-27 08:48:46 -08002098#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002099 case PSA_ALG_MD2:
2100 mbedtls_md2_free( &operation->ctx.md2 );
2101 break;
2102#endif
John Durkopee4e6602020-11-27 08:48:46 -08002103#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002104 case PSA_ALG_MD4:
2105 mbedtls_md4_free( &operation->ctx.md4 );
2106 break;
2107#endif
John Durkopee4e6602020-11-27 08:48:46 -08002108#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 case PSA_ALG_MD5:
2110 mbedtls_md5_free( &operation->ctx.md5 );
2111 break;
2112#endif
John Durkopee4e6602020-11-27 08:48:46 -08002113#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002114 case PSA_ALG_RIPEMD160:
2115 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2116 break;
2117#endif
John Durkopee4e6602020-11-27 08:48:46 -08002118#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 case PSA_ALG_SHA_1:
2120 mbedtls_sha1_free( &operation->ctx.sha1 );
2121 break;
2122#endif
John Durkop6ca23272020-12-03 06:01:32 -08002123#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002125 mbedtls_sha256_free( &operation->ctx.sha256 );
2126 break;
2127#endif
2128#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002129 case PSA_ALG_SHA_256:
2130 mbedtls_sha256_free( &operation->ctx.sha256 );
2131 break;
2132#endif
John Durkop6ca23272020-12-03 06:01:32 -08002133#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002134 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002135 mbedtls_sha512_free( &operation->ctx.sha512 );
2136 break;
2137#endif
2138#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002139 case PSA_ALG_SHA_512:
2140 mbedtls_sha512_free( &operation->ctx.sha512 );
2141 break;
2142#endif
2143 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002144 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002145 }
2146 operation->alg = 0;
2147 return( PSA_SUCCESS );
2148}
2149
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002150psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002151 psa_algorithm_t alg )
2152{
Janos Follath24eed8d2019-11-22 13:21:35 +00002153 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002154
2155 /* A context must be freshly initialized before it can be set up. */
2156 if( operation->alg != 0 )
2157 {
2158 return( PSA_ERROR_BAD_STATE );
2159 }
2160
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002161 switch( alg )
2162 {
John Durkopee4e6602020-11-27 08:48:46 -08002163#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002164 case PSA_ALG_MD2:
2165 mbedtls_md2_init( &operation->ctx.md2 );
2166 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2167 break;
2168#endif
John Durkopee4e6602020-11-27 08:48:46 -08002169#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002170 case PSA_ALG_MD4:
2171 mbedtls_md4_init( &operation->ctx.md4 );
2172 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2173 break;
2174#endif
John Durkopee4e6602020-11-27 08:48:46 -08002175#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176 case PSA_ALG_MD5:
2177 mbedtls_md5_init( &operation->ctx.md5 );
2178 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2179 break;
2180#endif
John Durkopee4e6602020-11-27 08:48:46 -08002181#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182 case PSA_ALG_RIPEMD160:
2183 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2184 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2185 break;
2186#endif
John Durkopee4e6602020-11-27 08:48:46 -08002187#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002188 case PSA_ALG_SHA_1:
2189 mbedtls_sha1_init( &operation->ctx.sha1 );
2190 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2191 break;
2192#endif
John Durkopee4e6602020-11-27 08:48:46 -08002193#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002194 case PSA_ALG_SHA_224:
2195 mbedtls_sha256_init( &operation->ctx.sha256 );
2196 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2197 break;
John Durkopee4e6602020-11-27 08:48:46 -08002198#endif
2199#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002200 case PSA_ALG_SHA_256:
2201 mbedtls_sha256_init( &operation->ctx.sha256 );
2202 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2203 break;
2204#endif
John Durkopee4e6602020-11-27 08:48:46 -08002205#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002206 case PSA_ALG_SHA_384:
2207 mbedtls_sha512_init( &operation->ctx.sha512 );
2208 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2209 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002210#endif
John Durkopee4e6602020-11-27 08:48:46 -08002211#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212 case PSA_ALG_SHA_512:
2213 mbedtls_sha512_init( &operation->ctx.sha512 );
2214 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2215 break;
2216#endif
2217 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002218 return( PSA_ALG_IS_HASH( alg ) ?
2219 PSA_ERROR_NOT_SUPPORTED :
2220 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002221 }
2222 if( ret == 0 )
2223 operation->alg = alg;
2224 else
2225 psa_hash_abort( operation );
2226 return( mbedtls_to_psa_error( ret ) );
2227}
2228
2229psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2230 const uint8_t *input,
2231 size_t input_length )
2232{
Janos Follath24eed8d2019-11-22 13:21:35 +00002233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002234
2235 /* Don't require hash implementations to behave correctly on a
2236 * zero-length input, which may have an invalid pointer. */
2237 if( input_length == 0 )
2238 return( PSA_SUCCESS );
2239
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002240 switch( operation->alg )
2241 {
John Durkopee4e6602020-11-27 08:48:46 -08002242#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002243 case PSA_ALG_MD2:
2244 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2245 input, input_length );
2246 break;
2247#endif
John Durkopee4e6602020-11-27 08:48:46 -08002248#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_MD4:
2250 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2251 input, input_length );
2252 break;
2253#endif
John Durkopee4e6602020-11-27 08:48:46 -08002254#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 case PSA_ALG_MD5:
2256 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2257 input, input_length );
2258 break;
2259#endif
John Durkopee4e6602020-11-27 08:48:46 -08002260#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261 case PSA_ALG_RIPEMD160:
2262 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2263 input, input_length );
2264 break;
2265#endif
John Durkopee4e6602020-11-27 08:48:46 -08002266#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267 case PSA_ALG_SHA_1:
2268 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2269 input, input_length );
2270 break;
2271#endif
John Durkop6ca23272020-12-03 06:01:32 -08002272#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002274 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2275 input, input_length );
2276 break;
2277#endif
2278#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 case PSA_ALG_SHA_256:
2280 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2281 input, input_length );
2282 break;
2283#endif
John Durkop6ca23272020-12-03 06:01:32 -08002284#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002286 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2287 input, input_length );
2288 break;
2289#endif
2290#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291 case PSA_ALG_SHA_512:
2292 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2293 input, input_length );
2294 break;
2295#endif
2296 default:
John Durkopee4e6602020-11-27 08:48:46 -08002297 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002298 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002300
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301 if( ret != 0 )
2302 psa_hash_abort( operation );
2303 return( mbedtls_to_psa_error( ret ) );
2304}
2305
2306psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2307 uint8_t *hash,
2308 size_t hash_size,
2309 size_t *hash_length )
2310{
itayzafrir40835d42018-08-02 13:14:17 +03002311 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002313 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314
2315 /* Fill the output buffer with something that isn't a valid hash
2316 * (barring an attack on the hash and deliberately-crafted input),
2317 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002318 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002319 /* If hash_size is 0 then hash may be NULL and then the
2320 * call to memset would have undefined behavior. */
2321 if( hash_size != 0 )
2322 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323
2324 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002325 {
2326 status = PSA_ERROR_BUFFER_TOO_SMALL;
2327 goto exit;
2328 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329
2330 switch( operation->alg )
2331 {
John Durkopee4e6602020-11-27 08:48:46 -08002332#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 case PSA_ALG_MD2:
2334 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2335 break;
2336#endif
John Durkopee4e6602020-11-27 08:48:46 -08002337#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 case PSA_ALG_MD4:
2339 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2340 break;
2341#endif
John Durkopee4e6602020-11-27 08:48:46 -08002342#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 case PSA_ALG_MD5:
2344 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2345 break;
2346#endif
John Durkopee4e6602020-11-27 08:48:46 -08002347#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 case PSA_ALG_RIPEMD160:
2349 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2350 break;
2351#endif
John Durkopee4e6602020-11-27 08:48:46 -08002352#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353 case PSA_ALG_SHA_1:
2354 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2355 break;
2356#endif
John Durkop6ca23272020-12-03 06:01:32 -08002357#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002359 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2360 break;
2361#endif
2362#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002363 case PSA_ALG_SHA_256:
2364 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2365 break;
2366#endif
John Durkop6ca23272020-12-03 06:01:32 -08002367#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002369 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2370 break;
2371#endif
2372#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002373 case PSA_ALG_SHA_512:
2374 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2375 break;
2376#endif
2377 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002378 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002379 }
itayzafrir40835d42018-08-02 13:14:17 +03002380 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381
itayzafrir40835d42018-08-02 13:14:17 +03002382exit:
2383 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002385 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386 return( psa_hash_abort( operation ) );
2387 }
2388 else
2389 {
2390 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002391 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392 }
2393}
2394
Gilles Peskine2d277862018-06-18 15:41:12 +02002395psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2396 const uint8_t *hash,
2397 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398{
2399 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2400 size_t actual_hash_length;
2401 psa_status_t status = psa_hash_finish( operation,
2402 actual_hash, sizeof( actual_hash ),
2403 &actual_hash_length );
2404 if( status != PSA_SUCCESS )
2405 return( status );
2406 if( actual_hash_length != hash_length )
2407 return( PSA_ERROR_INVALID_SIGNATURE );
2408 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2409 return( PSA_ERROR_INVALID_SIGNATURE );
2410 return( PSA_SUCCESS );
2411}
2412
Gilles Peskine0a749c82019-11-28 19:33:58 +01002413psa_status_t psa_hash_compute( psa_algorithm_t alg,
2414 const uint8_t *input, size_t input_length,
2415 uint8_t *hash, size_t hash_size,
2416 size_t *hash_length )
2417{
2418 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2419 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2420
2421 *hash_length = hash_size;
2422 status = psa_hash_setup( &operation, alg );
2423 if( status != PSA_SUCCESS )
2424 goto exit;
2425 status = psa_hash_update( &operation, input, input_length );
2426 if( status != PSA_SUCCESS )
2427 goto exit;
2428 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2429 if( status != PSA_SUCCESS )
2430 goto exit;
2431
2432exit:
2433 if( status == PSA_SUCCESS )
2434 status = psa_hash_abort( &operation );
2435 else
2436 psa_hash_abort( &operation );
2437 return( status );
2438}
2439
2440psa_status_t psa_hash_compare( psa_algorithm_t alg,
2441 const uint8_t *input, size_t input_length,
2442 const uint8_t *hash, size_t hash_length )
2443{
2444 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2445 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2446
2447 status = psa_hash_setup( &operation, alg );
2448 if( status != PSA_SUCCESS )
2449 goto exit;
2450 status = psa_hash_update( &operation, input, input_length );
2451 if( status != PSA_SUCCESS )
2452 goto exit;
2453 status = psa_hash_verify( &operation, hash, hash_length );
2454 if( status != PSA_SUCCESS )
2455 goto exit;
2456
2457exit:
2458 if( status == PSA_SUCCESS )
2459 status = psa_hash_abort( &operation );
2460 else
2461 psa_hash_abort( &operation );
2462 return( status );
2463}
2464
Gilles Peskineeb35d782019-01-22 17:56:16 +01002465psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2466 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002467{
2468 if( target_operation->alg != 0 )
2469 return( PSA_ERROR_BAD_STATE );
2470
2471 switch( source_operation->alg )
2472 {
2473 case 0:
2474 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002475#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002476 case PSA_ALG_MD2:
2477 mbedtls_md2_clone( &target_operation->ctx.md2,
2478 &source_operation->ctx.md2 );
2479 break;
2480#endif
John Durkopee4e6602020-11-27 08:48:46 -08002481#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002482 case PSA_ALG_MD4:
2483 mbedtls_md4_clone( &target_operation->ctx.md4,
2484 &source_operation->ctx.md4 );
2485 break;
2486#endif
John Durkopee4e6602020-11-27 08:48:46 -08002487#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002488 case PSA_ALG_MD5:
2489 mbedtls_md5_clone( &target_operation->ctx.md5,
2490 &source_operation->ctx.md5 );
2491 break;
2492#endif
John Durkopee4e6602020-11-27 08:48:46 -08002493#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002494 case PSA_ALG_RIPEMD160:
2495 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2496 &source_operation->ctx.ripemd160 );
2497 break;
2498#endif
John Durkopee4e6602020-11-27 08:48:46 -08002499#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002500 case PSA_ALG_SHA_1:
2501 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2502 &source_operation->ctx.sha1 );
2503 break;
2504#endif
John Durkop6ca23272020-12-03 06:01:32 -08002505#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002506 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002507 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2508 &source_operation->ctx.sha256 );
2509 break;
2510#endif
2511#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002512 case PSA_ALG_SHA_256:
2513 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2514 &source_operation->ctx.sha256 );
2515 break;
2516#endif
John Durkop6ca23272020-12-03 06:01:32 -08002517#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002518 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002519 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2520 &source_operation->ctx.sha512 );
2521 break;
2522#endif
2523#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002524 case PSA_ALG_SHA_512:
2525 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2526 &source_operation->ctx.sha512 );
2527 break;
2528#endif
2529 default:
2530 return( PSA_ERROR_NOT_SUPPORTED );
2531 }
2532
2533 target_operation->alg = source_operation->alg;
2534 return( PSA_SUCCESS );
2535}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002536
2537
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539/* MAC */
2540/****************************************************************/
2541
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002542static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002543 psa_algorithm_t alg,
2544 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002545 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002546 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002547{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002549 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002551 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002552 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002553
Gilles Peskine8c9def32018-02-08 10:02:12 +01002554 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2555 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002556 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002558 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 mode = MBEDTLS_MODE_STREAM;
2560 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 case PSA_ALG_CTR:
2562 mode = MBEDTLS_MODE_CTR;
2563 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002564 case PSA_ALG_CFB:
2565 mode = MBEDTLS_MODE_CFB;
2566 break;
2567 case PSA_ALG_OFB:
2568 mode = MBEDTLS_MODE_OFB;
2569 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002570 case PSA_ALG_ECB_NO_PADDING:
2571 mode = MBEDTLS_MODE_ECB;
2572 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002573 case PSA_ALG_CBC_NO_PADDING:
2574 mode = MBEDTLS_MODE_CBC;
2575 break;
2576 case PSA_ALG_CBC_PKCS7:
2577 mode = MBEDTLS_MODE_CBC;
2578 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002579 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002580 mode = MBEDTLS_MODE_CCM;
2581 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002582 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002583 mode = MBEDTLS_MODE_GCM;
2584 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002585 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002586 mode = MBEDTLS_MODE_CHACHAPOLY;
2587 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002588 default:
2589 return( NULL );
2590 }
2591 }
2592 else if( alg == PSA_ALG_CMAC )
2593 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002594 else
2595 return( NULL );
2596
2597 switch( key_type )
2598 {
2599 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002600 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002601 break;
2602 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002603 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2604 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002605 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002606 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002608 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002609 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2610 * but two-key Triple-DES is functionally three-key Triple-DES
2611 * with K1=K3, so that's how we present it to mbedtls. */
2612 if( key_bits == 128 )
2613 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002614 break;
2615 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002616 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002617 break;
2618 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002619 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002620 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002621 case PSA_KEY_TYPE_CHACHA20:
2622 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2623 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002624 default:
2625 return( NULL );
2626 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002627 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002628 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002629
Jaeden Amero23bbb752018-06-26 14:16:54 +01002630 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2631 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002632}
2633
John Durkop6ba40d12020-11-10 08:50:04 -08002634#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002635static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002636{
Gilles Peskine2d277862018-06-18 15:41:12 +02002637 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002638 {
2639 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002640 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002641 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002642 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002643 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002644 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002645 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002646 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002647 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002648 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002649 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002650 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002651 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002652 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002653 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002654 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002655 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002656 return( 128 );
2657 default:
2658 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002659 }
2660}
John Durkop07cc04a2020-11-16 22:08:34 -08002661#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002662
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002663/* Initialize the MAC operation structure. Once this function has been
2664 * called, psa_mac_abort can run and will do the right thing. */
2665static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2666 psa_algorithm_t alg )
2667{
2668 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2669
2670 operation->alg = alg;
2671 operation->key_set = 0;
2672 operation->iv_set = 0;
2673 operation->iv_required = 0;
2674 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002675 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002676
2677#if defined(MBEDTLS_CMAC_C)
2678 if( alg == PSA_ALG_CMAC )
2679 {
2680 operation->iv_required = 0;
2681 mbedtls_cipher_init( &operation->ctx.cmac );
2682 status = PSA_SUCCESS;
2683 }
2684 else
2685#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002686#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002687 if( PSA_ALG_IS_HMAC( operation->alg ) )
2688 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002689 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2690 operation->ctx.hmac.hash_ctx.alg = 0;
2691 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002692 }
2693 else
John Durkopd0321952020-10-29 21:37:36 -07002694#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002695 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002696 if( ! PSA_ALG_IS_MAC( alg ) )
2697 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002698 }
2699
2700 if( status != PSA_SUCCESS )
2701 memset( operation, 0, sizeof( *operation ) );
2702 return( status );
2703}
2704
John Durkop6ba40d12020-11-10 08:50:04 -08002705#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002706static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2707{
Gilles Peskine3f108122018-12-07 18:14:53 +01002708 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002709 return( psa_hash_abort( &hmac->hash_ctx ) );
2710}
John Durkop6ba40d12020-11-10 08:50:04 -08002711#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002712
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2714{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002715 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002717 /* The object has (apparently) been initialized but it is not
2718 * in use. It's ok to call abort on such an object, and there's
2719 * nothing to do. */
2720 return( PSA_SUCCESS );
2721 }
2722 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002724 if( operation->alg == PSA_ALG_CMAC )
2725 {
2726 mbedtls_cipher_free( &operation->ctx.cmac );
2727 }
2728 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002730#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002731 if( PSA_ALG_IS_HMAC( operation->alg ) )
2732 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002733 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002734 }
2735 else
John Durkopd0321952020-10-29 21:37:36 -07002736#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002737 {
2738 /* Sanity check (shouldn't happen: operation->alg should
2739 * always have been initialized to a valid value). */
2740 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 }
Moran Peker41deec42018-04-04 15:43:05 +03002742
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 operation->alg = 0;
2744 operation->key_set = 0;
2745 operation->iv_set = 0;
2746 operation->iv_required = 0;
2747 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002748 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002749
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002751
2752bad_state:
2753 /* If abort is called on an uninitialized object, we can't trust
2754 * anything. Wipe the object in case it contains confidential data.
2755 * This may result in a memory leak if a pointer gets overwritten,
2756 * but it's too late to do anything about this. */
2757 memset( operation, 0, sizeof( *operation ) );
2758 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759}
2760
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002761#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002762static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002764 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765 const mbedtls_cipher_info_t *cipher_info )
2766{
Janos Follath24eed8d2019-11-22 13:21:35 +00002767 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768
2769 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770
2771 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2772 if( ret != 0 )
2773 return( ret );
2774
2775 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002776 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777 key_bits );
2778 return( ret );
2779}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002780#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002781
John Durkop6ba40d12020-11-10 08:50:04 -08002782#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002783static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2784 const uint8_t *key,
2785 size_t key_length,
2786 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002788 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002790 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002792 psa_status_t status;
2793
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002794 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2795 * overflow below. This should never trigger if the hash algorithm
2796 * is implemented correctly. */
2797 /* The size checks against the ipad and opad buffers cannot be written
2798 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2799 * because that triggers -Wlogical-op on GCC 7.3. */
2800 if( block_size > sizeof( ipad ) )
2801 return( PSA_ERROR_NOT_SUPPORTED );
2802 if( block_size > sizeof( hmac->opad ) )
2803 return( PSA_ERROR_NOT_SUPPORTED );
2804 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002805 return( PSA_ERROR_NOT_SUPPORTED );
2806
Gilles Peskined223b522018-06-11 18:12:58 +02002807 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002808 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002809 status = psa_hash_compute( hash_alg, key, key_length,
2810 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002811 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002812 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813 }
Gilles Peskine96889972018-07-12 17:07:03 +02002814 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2815 * but it is permitted. It is common when HMAC is used in HKDF, for
2816 * example. Don't call `memcpy` in the 0-length because `key` could be
2817 * an invalid pointer which would make the behavior undefined. */
2818 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002819 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002820
Gilles Peskined223b522018-06-11 18:12:58 +02002821 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2822 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002823 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002824 ipad[i] ^= 0x36;
2825 memset( ipad + key_length, 0x36, block_size - key_length );
2826
2827 /* Copy the key material from ipad to opad, flipping the requisite bits,
2828 * and filling the rest of opad with the requisite constant. */
2829 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002830 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2831 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002832
Gilles Peskine01126fa2018-07-12 17:04:55 +02002833 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002835 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002836
Gilles Peskine01126fa2018-07-12 17:04:55 +02002837 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002838
2839cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002840 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002841
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002842 return( status );
2843}
John Durkop6ba40d12020-11-10 08:50:04 -08002844#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002845
Gilles Peskine89167cb2018-07-08 20:12:23 +02002846static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002847 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002848 psa_algorithm_t alg,
2849 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002851 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002852 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002853 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002855 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002856 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002857 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002858 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002859
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002860 /* A context must be freshly initialized before it can be set up. */
2861 if( operation->alg != 0 )
2862 {
2863 return( PSA_ERROR_BAD_STATE );
2864 }
2865
Gilles Peskined911eb72018-08-14 15:18:45 +02002866 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002867 if( status != PSA_SUCCESS )
2868 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002869 if( is_sign )
2870 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002871
Ronald Cron5c522922020-11-14 16:35:34 +01002872 status = psa_get_and_lock_transparent_key_slot_with_policy(
2873 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002874 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002876 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002877
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002879 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 {
2881 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002882 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002883 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002884 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002885 if( cipher_info == NULL )
2886 {
2887 status = PSA_ERROR_NOT_SUPPORTED;
2888 goto exit;
2889 }
2890 operation->mac_size = cipher_info->block_size;
2891 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2892 status = mbedtls_to_psa_error( ret );
2893 }
2894 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002895#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002896#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002897 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002898 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002899 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002900 if( hash_alg == 0 )
2901 {
2902 status = PSA_ERROR_NOT_SUPPORTED;
2903 goto exit;
2904 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002905
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002906 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002907 /* Sanity check. This shouldn't fail on a valid configuration. */
2908 if( operation->mac_size == 0 ||
2909 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2910 {
2911 status = PSA_ERROR_NOT_SUPPORTED;
2912 goto exit;
2913 }
2914
Gilles Peskine8e338702019-07-30 20:06:31 +02002915 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002916 {
2917 status = PSA_ERROR_INVALID_ARGUMENT;
2918 goto exit;
2919 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002920
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002922 slot->key.data,
2923 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002924 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002925 }
2926 else
John Durkopd0321952020-10-29 21:37:36 -07002927#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002928 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002929 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002932
Gilles Peskined911eb72018-08-14 15:18:45 +02002933 if( truncated == 0 )
2934 {
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002935 /* The "normal" case: untruncated algorithm. Re-validate the
2936 * key policy with explicit MAC length set in the algorithm
2937 * when the algorithm policy is at-least-this-length to catch
2938 * a corner case due to the default MAC length being unknown
2939 * at key loading time. */
2940 if( PSA_ALG_IS_MAC( slot->attr.policy.alg ) &&
2941 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg ) == full_length_alg ) &&
2942 ( slot->attr.policy.alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2943 {
2944 /* validate policy length */
2945 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg ) > operation->mac_size )
2946 status = PSA_ERROR_NOT_PERMITTED;
2947 }
2948
2949 if( PSA_ALG_IS_MAC( slot->attr.policy.alg2 ) &&
2950 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg2 ) == full_length_alg ) &&
2951 ( slot->attr.policy.alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2952 {
2953 /* validate policy length */
2954 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg2 ) > operation->mac_size )
2955 status = PSA_ERROR_NOT_PERMITTED;
2956 }
Gilles Peskined911eb72018-08-14 15:18:45 +02002957 }
2958 else if( truncated < 4 )
2959 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002960 /* A very short MAC is too short for security since it can be
2961 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2962 * so we make this our minimum, even though 32 bits is still
2963 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002964 status = PSA_ERROR_NOT_SUPPORTED;
2965 }
2966 else if( truncated > operation->mac_size )
2967 {
2968 /* It's impossible to "truncate" to a larger length. */
2969 status = PSA_ERROR_INVALID_ARGUMENT;
2970 }
2971 else
2972 operation->mac_size = truncated;
2973
Gilles Peskinefbfac682018-07-08 20:51:54 +02002974exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002976 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002977 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002979 else
2980 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002981 operation->key_set = 1;
2982 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002983
Ronald Cron5c522922020-11-14 16:35:34 +01002984 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002985
Ronald Cron5c522922020-11-14 16:35:34 +01002986 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987}
2988
Gilles Peskine89167cb2018-07-08 20:12:23 +02002989psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002990 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002991 psa_algorithm_t alg )
2992{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002993 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002994}
2995
2996psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002997 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002998 psa_algorithm_t alg )
2999{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003000 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003001}
3002
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3004 const uint8_t *input,
3005 size_t input_length )
3006{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003007 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003009 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003011 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012 operation->has_input = 1;
3013
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003015 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003016 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3018 input, input_length );
3019 status = mbedtls_to_psa_error( ret );
3020 }
3021 else
3022#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003023#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003024 if( PSA_ALG_IS_HMAC( operation->alg ) )
3025 {
3026 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3027 input_length );
3028 }
3029 else
John Durkopd0321952020-10-29 21:37:36 -07003030#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 {
3032 /* This shouldn't happen if `operation` was initialized by
3033 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003034 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003035 }
3036
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 if( status != PSA_SUCCESS )
3038 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003039 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040}
3041
John Durkop6ba40d12020-11-10 08:50:04 -08003042#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003043static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3044 uint8_t *mac,
3045 size_t mac_size )
3046{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003047 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003048 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3049 size_t hash_size = 0;
3050 size_t block_size = psa_get_hash_block_size( hash_alg );
3051 psa_status_t status;
3052
Gilles Peskine01126fa2018-07-12 17:04:55 +02003053 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3054 if( status != PSA_SUCCESS )
3055 return( status );
3056 /* From here on, tmp needs to be wiped. */
3057
3058 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3059 if( status != PSA_SUCCESS )
3060 goto exit;
3061
3062 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3063 if( status != PSA_SUCCESS )
3064 goto exit;
3065
3066 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3067 if( status != PSA_SUCCESS )
3068 goto exit;
3069
Gilles Peskined911eb72018-08-14 15:18:45 +02003070 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3071 if( status != PSA_SUCCESS )
3072 goto exit;
3073
3074 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003075
3076exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003077 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003078 return( status );
3079}
John Durkop6ba40d12020-11-10 08:50:04 -08003080#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003081
mohammad16036df908f2018-04-02 08:34:15 -07003082static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003083 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003084 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003086 if( ! operation->key_set )
3087 return( PSA_ERROR_BAD_STATE );
3088 if( operation->iv_required && ! operation->iv_set )
3089 return( PSA_ERROR_BAD_STATE );
3090
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 if( mac_size < operation->mac_size )
3092 return( PSA_ERROR_BUFFER_TOO_SMALL );
3093
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003095 if( operation->alg == PSA_ALG_CMAC )
3096 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003097 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003098 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3099 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003100 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003101 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003102 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003103 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003104 else
3105#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 if( PSA_ALG_IS_HMAC( operation->alg ) )
3108 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003109 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003110 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003111 }
3112 else
John Durkopd0321952020-10-29 21:37:36 -07003113#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 {
3115 /* This shouldn't happen if `operation` was initialized by
3116 * a setup function. */
3117 return( PSA_ERROR_BAD_STATE );
3118 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119}
3120
Gilles Peskineacd4be32018-07-08 19:56:25 +02003121psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3122 uint8_t *mac,
3123 size_t mac_size,
3124 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003125{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003126 psa_status_t status;
3127
Jaeden Amero252ef282019-02-15 14:05:35 +00003128 if( operation->alg == 0 )
3129 {
3130 return( PSA_ERROR_BAD_STATE );
3131 }
3132
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003133 /* Fill the output buffer with something that isn't a valid mac
3134 * (barring an attack on the mac and deliberately-crafted input),
3135 * in case the caller doesn't check the return status properly. */
3136 *mac_length = mac_size;
3137 /* If mac_size is 0 then mac may be NULL and then the
3138 * call to memset would have undefined behavior. */
3139 if( mac_size != 0 )
3140 memset( mac, '!', mac_size );
3141
Gilles Peskine89167cb2018-07-08 20:12:23 +02003142 if( ! operation->is_sign )
3143 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003144 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003145 }
mohammad16036df908f2018-04-02 08:34:15 -07003146
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003147 status = psa_mac_finish_internal( operation, mac, mac_size );
3148
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003149 if( status == PSA_SUCCESS )
3150 {
3151 status = psa_mac_abort( operation );
3152 if( status == PSA_SUCCESS )
3153 *mac_length = operation->mac_size;
3154 else
3155 memset( mac, '!', mac_size );
3156 }
3157 else
3158 psa_mac_abort( operation );
3159 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003160}
3161
Gilles Peskineacd4be32018-07-08 19:56:25 +02003162psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3163 const uint8_t *mac,
3164 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165{
Gilles Peskine828ed142018-06-18 23:25:51 +02003166 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003167 psa_status_t status;
3168
Jaeden Amero252ef282019-02-15 14:05:35 +00003169 if( operation->alg == 0 )
3170 {
3171 return( PSA_ERROR_BAD_STATE );
3172 }
3173
Gilles Peskine89167cb2018-07-08 20:12:23 +02003174 if( operation->is_sign )
3175 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003176 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003177 }
3178 if( operation->mac_size != mac_length )
3179 {
3180 status = PSA_ERROR_INVALID_SIGNATURE;
3181 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003182 }
mohammad16036df908f2018-04-02 08:34:15 -07003183
3184 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003185 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003186 if( status != PSA_SUCCESS )
3187 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003188
3189 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3190 status = PSA_ERROR_INVALID_SIGNATURE;
3191
3192cleanup:
3193 if( status == PSA_SUCCESS )
3194 status = psa_mac_abort( operation );
3195 else
3196 psa_mac_abort( operation );
3197
Gilles Peskine3f108122018-12-07 18:14:53 +01003198 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003199
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003200 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201}
3202
3203
Gilles Peskine20035e32018-02-03 22:44:14 +01003204
Gilles Peskine20035e32018-02-03 22:44:14 +01003205/****************************************************************/
3206/* Asymmetric cryptography */
3207/****************************************************************/
3208
John Durkop6ba40d12020-11-10 08:50:04 -08003209#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3210 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003211/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003212 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003213static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3214 size_t hash_length,
3215 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003216{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003217 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003218 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003219 *md_alg = mbedtls_md_get_type( md_info );
3220
3221 /* The Mbed TLS RSA module uses an unsigned int for hash length
3222 * parameters. Validate that it fits so that we don't risk an
3223 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003224#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003225 if( hash_length > UINT_MAX )
3226 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003227#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003228
John Durkop0e005192020-10-31 22:06:54 -07003229#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003230 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3231 * must be correct. */
3232 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3233 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003234 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003235 if( md_info == NULL )
3236 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003237 if( mbedtls_md_get_size( md_info ) != hash_length )
3238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003239 }
John Durkop0e005192020-10-31 22:06:54 -07003240#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003241
John Durkop0e005192020-10-31 22:06:54 -07003242#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003243 /* PSS requires a hash internally. */
3244 if( PSA_ALG_IS_RSA_PSS( alg ) )
3245 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003246 if( md_info == NULL )
3247 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003248 }
John Durkop0e005192020-10-31 22:06:54 -07003249#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003250
Gilles Peskine61b91d42018-06-08 16:09:36 +02003251 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003252}
3253
Gilles Peskine2b450e32018-06-27 15:42:46 +02003254static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3255 psa_algorithm_t alg,
3256 const uint8_t *hash,
3257 size_t hash_length,
3258 uint8_t *signature,
3259 size_t signature_size,
3260 size_t *signature_length )
3261{
3262 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003264 mbedtls_md_type_t md_alg;
3265
3266 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3267 if( status != PSA_SUCCESS )
3268 return( status );
3269
Gilles Peskine630a18a2018-06-29 17:49:35 +02003270 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003271 return( PSA_ERROR_BUFFER_TOO_SMALL );
3272
John Durkop0e005192020-10-31 22:06:54 -07003273#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003274 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3275 {
3276 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3277 MBEDTLS_MD_NONE );
3278 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003279 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003280 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003281 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003282 md_alg,
3283 (unsigned int) hash_length,
3284 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003285 signature );
3286 }
3287 else
John Durkop0e005192020-10-31 22:06:54 -07003288#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3289#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003290 if( PSA_ALG_IS_RSA_PSS( alg ) )
3291 {
3292 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3293 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003294 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003295 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003296 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003297 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003298 (unsigned int) hash_length,
3299 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003300 signature );
3301 }
3302 else
John Durkop0e005192020-10-31 22:06:54 -07003303#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003304 {
3305 return( PSA_ERROR_INVALID_ARGUMENT );
3306 }
3307
3308 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003309 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003310 return( mbedtls_to_psa_error( ret ) );
3311}
3312
3313static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3314 psa_algorithm_t alg,
3315 const uint8_t *hash,
3316 size_t hash_length,
3317 const uint8_t *signature,
3318 size_t signature_length )
3319{
3320 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003321 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003322 mbedtls_md_type_t md_alg;
3323
3324 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3325 if( status != PSA_SUCCESS )
3326 return( status );
3327
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003328 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3329 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003330
John Durkop0e005192020-10-31 22:06:54 -07003331#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003332 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3333 {
3334 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3335 MBEDTLS_MD_NONE );
3336 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003337 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003338 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003339 MBEDTLS_RSA_PUBLIC,
3340 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003341 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003342 hash,
3343 signature );
3344 }
3345 else
John Durkop0e005192020-10-31 22:06:54 -07003346#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3347#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003348 if( PSA_ALG_IS_RSA_PSS( alg ) )
3349 {
3350 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3351 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003352 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003353 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003354 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003355 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003356 (unsigned int) hash_length,
3357 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003358 signature );
3359 }
3360 else
John Durkop0e005192020-10-31 22:06:54 -07003361#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003362 {
3363 return( PSA_ERROR_INVALID_ARGUMENT );
3364 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003365
3366 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3367 * the rest of the signature is invalid". This has little use in
3368 * practice and PSA doesn't report this distinction. */
3369 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3370 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003371 return( mbedtls_to_psa_error( ret ) );
3372}
John Durkop6ba40d12020-11-10 08:50:04 -08003373#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3374 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003375
John Durkop6ba40d12020-11-10 08:50:04 -08003376#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3377 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3379 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3380 * (even though these functions don't modify it). */
3381static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3382 psa_algorithm_t alg,
3383 const uint8_t *hash,
3384 size_t hash_length,
3385 uint8_t *signature,
3386 size_t signature_size,
3387 size_t *signature_length )
3388{
Janos Follath24eed8d2019-11-22 13:21:35 +00003389 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003390 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003391 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003392 mbedtls_mpi_init( &r );
3393 mbedtls_mpi_init( &s );
3394
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003395 if( signature_size < 2 * curve_bytes )
3396 {
3397 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3398 goto cleanup;
3399 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003400
John Durkop0ea39e02020-10-13 19:58:20 -07003401#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003402 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3403 {
3404 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3405 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3406 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003407 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3408 &ecp->d, hash,
3409 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003410 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003411 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003412 }
3413 else
John Durkop0ea39e02020-10-13 19:58:20 -07003414#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003415 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003416 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003417 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3418 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003419 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003420 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003421 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003422
3423 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3424 signature,
3425 curve_bytes ) );
3426 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3427 signature + curve_bytes,
3428 curve_bytes ) );
3429
3430cleanup:
3431 mbedtls_mpi_free( &r );
3432 mbedtls_mpi_free( &s );
3433 if( ret == 0 )
3434 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003435 return( mbedtls_to_psa_error( ret ) );
3436}
3437
3438static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3439 const uint8_t *hash,
3440 size_t hash_length,
3441 const uint8_t *signature,
3442 size_t signature_length )
3443{
Janos Follath24eed8d2019-11-22 13:21:35 +00003444 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003445 mbedtls_mpi r, s;
3446 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3447 mbedtls_mpi_init( &r );
3448 mbedtls_mpi_init( &s );
3449
3450 if( signature_length != 2 * curve_bytes )
3451 return( PSA_ERROR_INVALID_SIGNATURE );
3452
3453 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3454 signature,
3455 curve_bytes ) );
3456 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3457 signature + curve_bytes,
3458 curve_bytes ) );
3459
Steven Cooremanacda8342020-07-24 23:09:52 +02003460 /* Check whether the public part is loaded. If not, load it. */
3461 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3462 {
3463 MBEDTLS_MPI_CHK(
3464 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003465 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003466 }
3467
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003468 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3469 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470
3471cleanup:
3472 mbedtls_mpi_free( &r );
3473 mbedtls_mpi_free( &s );
3474 return( mbedtls_to_psa_error( ret ) );
3475}
John Durkop6ba40d12020-11-10 08:50:04 -08003476#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3477 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003478
Ronald Croncf56a0a2020-08-04 09:51:30 +02003479psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003480 psa_algorithm_t alg,
3481 const uint8_t *hash,
3482 size_t hash_length,
3483 uint8_t *signature,
3484 size_t signature_size,
3485 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003486{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003487 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003488 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003489 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003490
3491 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003492 /* Immediately reject a zero-length signature buffer. This guarantees
3493 * that signature must be a valid pointer. (On the other hand, the hash
3494 * buffer can in principle be empty since it doesn't actually have
3495 * to be a hash.) */
3496 if( signature_size == 0 )
3497 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003498
Ronald Cron5c522922020-11-14 16:35:34 +01003499 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3500 PSA_KEY_USAGE_SIGN_HASH,
3501 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003502 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003503 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003504 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003505 {
3506 status = PSA_ERROR_INVALID_ARGUMENT;
3507 goto exit;
3508 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003509
Steven Cooremancd84cb42020-07-16 20:28:36 +02003510 /* Try any of the available accelerators first */
3511 status = psa_driver_wrapper_sign_hash( slot,
3512 alg,
3513 hash,
3514 hash_length,
3515 signature,
3516 signature_size,
3517 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003518 if( status != PSA_ERROR_NOT_SUPPORTED ||
3519 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003520 goto exit;
3521
Steven Cooreman7a250572020-07-17 16:43:05 +02003522 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003523#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3524 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003525 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003526 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003527 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003528
Ronald Cron90857082020-11-25 14:58:33 +01003529 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3530 slot->key.data,
3531 slot->key.bytes,
3532 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003533 if( status != PSA_SUCCESS )
3534 goto exit;
3535
Steven Cooremana2371e52020-07-28 14:30:39 +02003536 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003537 alg,
3538 hash, hash_length,
3539 signature, signature_size,
3540 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003541
Steven Cooremana2371e52020-07-28 14:30:39 +02003542 mbedtls_rsa_free( rsa );
3543 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003544 }
3545 else
John Durkop6ba40d12020-11-10 08:50:04 -08003546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3547 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003548 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003549 {
John Durkop9814fa22020-11-04 12:28:15 -08003550#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3551 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003552 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003553#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003554 PSA_ALG_IS_ECDSA( alg )
3555#else
3556 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3557#endif
3558 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003559 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003560 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003561 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003562 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003563 slot->key.data,
3564 slot->key.bytes,
3565 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003566 if( status != PSA_SUCCESS )
3567 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003568 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003569 alg,
3570 hash, hash_length,
3571 signature, signature_size,
3572 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003573 mbedtls_ecp_keypair_free( ecp );
3574 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003575 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003576 else
John Durkop9814fa22020-11-04 12:28:15 -08003577#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3578 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003579 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003580 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003581 }
itayzafrir5c753392018-05-08 11:18:38 +03003582 }
3583 else
itayzafrir5c753392018-05-08 11:18:38 +03003584 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003585 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003586 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003587
3588exit:
3589 /* Fill the unused part of the output buffer (the whole buffer on error,
3590 * the trailing part on success) with something that isn't a valid mac
3591 * (barring an attack on the mac and deliberately-crafted input),
3592 * in case the caller doesn't check the return status properly. */
3593 if( status == PSA_SUCCESS )
3594 memset( signature + *signature_length, '!',
3595 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003596 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003597 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003598 /* If signature_size is 0 then we have nothing to do. We must not call
3599 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003600
Ronald Cron5c522922020-11-14 16:35:34 +01003601 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003602
Ronald Cron5c522922020-11-14 16:35:34 +01003603 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003604}
3605
Ronald Croncf56a0a2020-08-04 09:51:30 +02003606psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003607 psa_algorithm_t alg,
3608 const uint8_t *hash,
3609 size_t hash_length,
3610 const uint8_t *signature,
3611 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003612{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003613 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003614 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003615 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003616
Ronald Cron5c522922020-11-14 16:35:34 +01003617 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3618 PSA_KEY_USAGE_VERIFY_HASH,
3619 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003620 if( status != PSA_SUCCESS )
3621 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003622
Steven Cooreman55ae2172020-07-17 19:46:15 +02003623 /* Try any of the available accelerators first */
3624 status = psa_driver_wrapper_verify_hash( slot,
3625 alg,
3626 hash,
3627 hash_length,
3628 signature,
3629 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003630 if( status != PSA_ERROR_NOT_SUPPORTED ||
3631 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003632 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003633
John Durkop6ba40d12020-11-10 08:50:04 -08003634#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3635 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003636 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003638 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003639
Ronald Cron90857082020-11-25 14:58:33 +01003640 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3641 slot->key.data,
3642 slot->key.bytes,
3643 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003644 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003645 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003646
Steven Cooremana2371e52020-07-28 14:30:39 +02003647 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003648 alg,
3649 hash, hash_length,
3650 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003651 mbedtls_rsa_free( rsa );
3652 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003653 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654 }
3655 else
John Durkop6ba40d12020-11-10 08:50:04 -08003656#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3657 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003658 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003659 {
John Durkop9814fa22020-11-04 12:28:15 -08003660#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3661 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003662 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003663 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003664 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003665 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003666 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003667 slot->key.data,
3668 slot->key.bytes,
3669 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003670 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003671 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003672 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003673 hash, hash_length,
3674 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003675 mbedtls_ecp_keypair_free( ecp );
3676 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003677 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003678 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003679 else
John Durkop9814fa22020-11-04 12:28:15 -08003680#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3681 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003682 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003683 status = PSA_ERROR_INVALID_ARGUMENT;
3684 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003685 }
itayzafrir5c753392018-05-08 11:18:38 +03003686 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003687 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003688 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003689 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003690 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003691
3692exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003693 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003694
Ronald Cron5c522922020-11-14 16:35:34 +01003695 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003696}
3697
John Durkop0e005192020-10-31 22:06:54 -07003698#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003699static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3700 mbedtls_rsa_context *rsa )
3701{
3702 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3703 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3704 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3705 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3706}
John Durkop0e005192020-10-31 22:06:54 -07003707#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003708
Ronald Croncf56a0a2020-08-04 09:51:30 +02003709psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003710 psa_algorithm_t alg,
3711 const uint8_t *input,
3712 size_t input_length,
3713 const uint8_t *salt,
3714 size_t salt_length,
3715 uint8_t *output,
3716 size_t output_size,
3717 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003719 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003720 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003721 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003722
Darryl Green5cc689a2018-07-24 15:34:10 +01003723 (void) input;
3724 (void) input_length;
3725 (void) salt;
3726 (void) output;
3727 (void) output_size;
3728
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003729 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003730
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003731 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3732 return( PSA_ERROR_INVALID_ARGUMENT );
3733
Ronald Cron5c522922020-11-14 16:35:34 +01003734 status = psa_get_and_lock_transparent_key_slot_with_policy(
3735 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003736 if( status != PSA_SUCCESS )
3737 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003738 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3739 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003740 {
3741 status = PSA_ERROR_INVALID_ARGUMENT;
3742 goto exit;
3743 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003744
John Durkop6ba40d12020-11-10 08:50:04 -08003745#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3746 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003747 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003748 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003749 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003750 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3751 slot->key.data,
3752 slot->key.bytes,
3753 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003754 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003755 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003756
3757 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003758 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003759 status = PSA_ERROR_BUFFER_TOO_SMALL;
3760 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003761 }
John Durkop0e005192020-10-31 22:06:54 -07003762#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003763 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003764 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003765 status = mbedtls_to_psa_error(
3766 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003767 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003768 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003769 MBEDTLS_RSA_PUBLIC,
3770 input_length,
3771 input,
3772 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003773 }
3774 else
John Durkop0e005192020-10-31 22:06:54 -07003775#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3776#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003777 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003778 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003779 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003780 status = mbedtls_to_psa_error(
3781 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003782 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003783 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003784 MBEDTLS_RSA_PUBLIC,
3785 salt, salt_length,
3786 input_length,
3787 input,
3788 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003789 }
3790 else
John Durkop0e005192020-10-31 22:06:54 -07003791#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003792 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003793 status = PSA_ERROR_INVALID_ARGUMENT;
3794 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003796rsa_exit:
3797 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003798 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003799
Steven Cooremana2371e52020-07-28 14:30:39 +02003800 mbedtls_rsa_free( rsa );
3801 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003803 else
John Durkop9814fa22020-11-04 12:28:15 -08003804#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003805 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003806 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003807 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003808 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003809
3810exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003811 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003812
Ronald Cron5c522922020-11-14 16:35:34 +01003813 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003814}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003815
Ronald Croncf56a0a2020-08-04 09:51:30 +02003816psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003817 psa_algorithm_t alg,
3818 const uint8_t *input,
3819 size_t input_length,
3820 const uint8_t *salt,
3821 size_t salt_length,
3822 uint8_t *output,
3823 size_t output_size,
3824 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003826 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003827 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003828 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003829
Darryl Green5cc689a2018-07-24 15:34:10 +01003830 (void) input;
3831 (void) input_length;
3832 (void) salt;
3833 (void) output;
3834 (void) output_size;
3835
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003836 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003837
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003838 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3839 return( PSA_ERROR_INVALID_ARGUMENT );
3840
Ronald Cron5c522922020-11-14 16:35:34 +01003841 status = psa_get_and_lock_transparent_key_slot_with_policy(
3842 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003843 if( status != PSA_SUCCESS )
3844 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003845 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003846 {
3847 status = PSA_ERROR_INVALID_ARGUMENT;
3848 goto exit;
3849 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850
John Durkop6ba40d12020-11-10 08:50:04 -08003851#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3852 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003853 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003854 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003855 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003856 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3857 slot->key.data,
3858 slot->key.bytes,
3859 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003860 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003861 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862
Steven Cooremana2371e52020-07-28 14:30:39 +02003863 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003864 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003865 status = PSA_ERROR_INVALID_ARGUMENT;
3866 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003867 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003868
John Durkop0e005192020-10-31 22:06:54 -07003869#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003870 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003871 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003872 status = mbedtls_to_psa_error(
3873 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003874 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003875 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003876 MBEDTLS_RSA_PRIVATE,
3877 output_length,
3878 input,
3879 output,
3880 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 }
3882 else
John Durkop0e005192020-10-31 22:06:54 -07003883#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3884#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003885 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003887 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003888 status = mbedtls_to_psa_error(
3889 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003890 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003891 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003892 MBEDTLS_RSA_PRIVATE,
3893 salt, salt_length,
3894 output_length,
3895 input,
3896 output,
3897 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898 }
3899 else
John Durkop0e005192020-10-31 22:06:54 -07003900#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003901 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003902 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003903 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003904
Steven Cooreman4fed4552020-08-03 14:46:03 +02003905rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003906 mbedtls_rsa_free( rsa );
3907 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003909 else
John Durkop6ba40d12020-11-10 08:50:04 -08003910#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3911 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003912 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003913 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003915
3916exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003917 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003918
Ronald Cron5c522922020-11-14 16:35:34 +01003919 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003920}
Gilles Peskine20035e32018-02-03 22:44:14 +01003921
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003922
3923
mohammad1603503973b2018-03-12 15:59:30 +02003924/****************************************************************/
3925/* Symmetric cryptography */
3926/****************************************************************/
3927
Gilles Peskinee553c652018-06-04 16:22:46 +02003928static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003929 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003930 psa_algorithm_t alg,
3931 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003932{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003933 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003934 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003935 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003936 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003937 size_t key_bits;
3938 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003939 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3940 PSA_KEY_USAGE_ENCRYPT :
3941 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003942
Steven Cooremand3feccd2020-09-01 15:56:14 +02003943 /* A context must be freshly initialized before it can be set up. */
3944 if( operation->alg != 0 )
3945 return( PSA_ERROR_BAD_STATE );
3946
Steven Cooremana07b9972020-09-10 14:54:14 +02003947 /* The requested algorithm must be one that can be processed by cipher. */
3948 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003949 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003950
Steven Cooremanef8575e2020-09-11 11:44:50 +02003951 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003952 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003953 if( status != PSA_SUCCESS )
3954 goto exit;
3955
3956 /* Initialize the operation struct members, except for alg. The alg member
3957 * is used to indicate to psa_cipher_abort that there are resources to free,
3958 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003959 operation->key_set = 0;
3960 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003961 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003962 operation->iv_size = 0;
3963 operation->block_size = 0;
3964 if( alg == PSA_ALG_ECB_NO_PADDING )
3965 operation->iv_required = 0;
3966 else
3967 operation->iv_required = 1;
3968
Steven Cooremana07b9972020-09-10 14:54:14 +02003969 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003970 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003971 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003972 slot,
3973 alg );
3974 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003975 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003976 slot,
3977 alg );
3978
Steven Cooremanef8575e2020-09-11 11:44:50 +02003979 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003980 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003981 /* Once the driver context is initialised, it needs to be freed using
3982 * psa_cipher_abort. Indicate this through setting alg. */
3983 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003984 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003985
Steven Cooremand3feccd2020-09-01 15:56:14 +02003986 if( status != PSA_ERROR_NOT_SUPPORTED ||
3987 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3988 goto exit;
3989
Steven Cooremana07b9972020-09-10 14:54:14 +02003990 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003991 * available for the given algorithm & key. */
3992 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003993
Steven Cooremana07b9972020-09-10 14:54:14 +02003994 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003995 * psa_cipher_abort. Indicate there is something to be freed through setting
3996 * alg, and indicate the operation is being done using mbedtls crypto through
3997 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003998 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003999 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004000
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004001 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004002 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004003 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004004 {
4005 status = PSA_ERROR_NOT_SUPPORTED;
4006 goto exit;
4007 }
mohammad1603503973b2018-03-12 15:59:30 +02004008
mohammad1603503973b2018-03-12 15:59:30 +02004009 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004010 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004011 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004012
David Brown12ca5032021-02-11 11:02:00 -07004013#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004014 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004015 {
4016 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004017 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004018 memcpy( keys, slot->key.data, 16 );
4019 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004020 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4021 keys,
4022 192, cipher_operation );
4023 }
4024 else
4025#endif
4026 {
4027 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004028 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004029 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004030 }
Moran Peker41deec42018-04-04 15:43:05 +03004031 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004032 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004033
David Brown63ca2602021-01-26 11:51:12 -07004034#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4035 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004036 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004037 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004038 case PSA_ALG_CBC_NO_PADDING:
4039 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4040 MBEDTLS_PADDING_NONE );
4041 break;
4042 case PSA_ALG_CBC_PKCS7:
4043 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4044 MBEDTLS_PADDING_PKCS7 );
4045 break;
4046 default:
4047 /* The algorithm doesn't involve padding. */
4048 ret = 0;
4049 break;
4050 }
4051 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004052 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004053#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004054
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004055 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004056 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004057 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4058 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004059 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004060 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004061 }
David Brown1bfe4d72021-02-16 12:54:35 -07004062#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004063 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004064 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004065 operation->iv_size = 12;
4066#endif
mohammad1603503973b2018-03-12 15:59:30 +02004067
Steven Cooreman7df02922020-09-09 15:28:49 +02004068 status = PSA_SUCCESS;
4069
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004070exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004071 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004072 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004073 if( status == PSA_SUCCESS )
4074 {
4075 /* Update operation flags for both driver and software implementations */
4076 operation->key_set = 1;
4077 }
4078 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004079 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080
Ronald Cron5c522922020-11-14 16:35:34 +01004081 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082
Ronald Cron5c522922020-11-14 16:35:34 +01004083 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004084}
4085
Gilles Peskinefe119512018-07-08 21:39:34 +02004086psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004087 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004088 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004089{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004090 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004091}
4092
Gilles Peskinefe119512018-07-08 21:39:34 +02004093psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004094 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004095 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004096{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004097 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004098}
4099
Gilles Peskinefe119512018-07-08 21:39:34 +02004100psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004101 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004102 size_t iv_size,
4103 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004104{
itayzafrir534bd7c2018-08-02 13:56:32 +03004105 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004106 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004107 if( operation->iv_set || ! operation->iv_required )
4108 {
4109 return( PSA_ERROR_BAD_STATE );
4110 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004111
Steven Cooremanef8575e2020-09-11 11:44:50 +02004112 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004113 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004114 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004115 iv,
4116 iv_size,
4117 iv_length );
4118 goto exit;
4119 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004120
Moran Peker41deec42018-04-04 15:43:05 +03004121 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004122 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004123 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004124 goto exit;
4125 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004126 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004127 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004128 if( ret != 0 )
4129 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004130 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004131 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004132 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004133
mohammad16038481e742018-03-18 13:57:31 +02004134 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004135 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004136
Moran Peker395db872018-05-31 14:07:14 +03004137exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004138 if( status == PSA_SUCCESS )
4139 operation->iv_set = 1;
4140 else
Moran Peker395db872018-05-31 14:07:14 +03004141 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004142 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004143}
4144
Gilles Peskinefe119512018-07-08 21:39:34 +02004145psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004146 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004147 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004148{
itayzafrir534bd7c2018-08-02 13:56:32 +03004149 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004150 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004151 if( operation->iv_set || ! operation->iv_required )
4152 {
4153 return( PSA_ERROR_BAD_STATE );
4154 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004155
Steven Cooremanef8575e2020-09-11 11:44:50 +02004156 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004157 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004158 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004159 iv,
4160 iv_length );
4161 goto exit;
4162 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004163
Moran Pekera28258c2018-05-29 16:25:04 +03004164 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004165 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004166 status = PSA_ERROR_INVALID_ARGUMENT;
4167 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004168 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004169 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4170 status = mbedtls_to_psa_error( ret );
4171exit:
4172 if( status == PSA_SUCCESS )
4173 operation->iv_set = 1;
4174 else
mohammad1603503973b2018-03-12 15:59:30 +02004175 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004176 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004177}
4178
Steven Cooreman177deba2020-09-07 17:14:14 +02004179/* Process input for which the algorithm is set to ECB mode. This requires
4180 * manual processing, since the PSA API is defined as being able to process
4181 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4182 * underlying mbedtls_cipher_update only takes full blocks. */
4183static psa_status_t psa_cipher_update_ecb_internal(
4184 mbedtls_cipher_context_t *ctx,
4185 const uint8_t *input,
4186 size_t input_length,
4187 uint8_t *output,
4188 size_t output_size,
4189 size_t *output_length )
4190{
4191 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4192 size_t block_size = ctx->cipher_info->block_size;
4193 size_t internal_output_length = 0;
4194 *output_length = 0;
4195
4196 if( input_length == 0 )
4197 {
4198 status = PSA_SUCCESS;
4199 goto exit;
4200 }
4201
4202 if( ctx->unprocessed_len > 0 )
4203 {
4204 /* Fill up to block size, and run the block if there's a full one. */
4205 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4206
4207 if( input_length < bytes_to_copy )
4208 bytes_to_copy = input_length;
4209
4210 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4211 input, bytes_to_copy );
4212 input_length -= bytes_to_copy;
4213 input += bytes_to_copy;
4214 ctx->unprocessed_len += bytes_to_copy;
4215
4216 if( ctx->unprocessed_len == block_size )
4217 {
4218 status = mbedtls_to_psa_error(
4219 mbedtls_cipher_update( ctx,
4220 ctx->unprocessed_data,
4221 block_size,
4222 output, &internal_output_length ) );
4223
4224 if( status != PSA_SUCCESS )
4225 goto exit;
4226
4227 output += internal_output_length;
4228 output_size -= internal_output_length;
4229 *output_length += internal_output_length;
4230 ctx->unprocessed_len = 0;
4231 }
4232 }
4233
4234 while( input_length >= block_size )
4235 {
4236 /* Run all full blocks we have, one by one */
4237 status = mbedtls_to_psa_error(
4238 mbedtls_cipher_update( ctx, input,
4239 block_size,
4240 output, &internal_output_length ) );
4241
4242 if( status != PSA_SUCCESS )
4243 goto exit;
4244
4245 input_length -= block_size;
4246 input += block_size;
4247
4248 output += internal_output_length;
4249 output_size -= internal_output_length;
4250 *output_length += internal_output_length;
4251 }
4252
4253 if( input_length > 0 )
4254 {
4255 /* Save unprocessed bytes for later processing */
4256 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4257 input, input_length );
4258 ctx->unprocessed_len += input_length;
4259 }
4260
4261 status = PSA_SUCCESS;
4262
4263exit:
4264 return( status );
4265}
4266
Gilles Peskinee553c652018-06-04 16:22:46 +02004267psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4268 const uint8_t *input,
4269 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004270 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004271 size_t output_size,
4272 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004273{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004275 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004276 if( operation->alg == 0 )
4277 {
4278 return( PSA_ERROR_BAD_STATE );
4279 }
4280 if( operation->iv_required && ! operation->iv_set )
4281 {
4282 return( PSA_ERROR_BAD_STATE );
4283 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004284
Steven Cooremanef8575e2020-09-11 11:44:50 +02004285 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004286 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004287 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004288 input,
4289 input_length,
4290 output,
4291 output_size,
4292 output_length );
4293 goto exit;
4294 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004295
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004296 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004297 {
4298 /* Take the unprocessed partial block left over from previous
4299 * update calls, if any, plus the input to this call. Remove
4300 * the last partial block, if any. You get the data that will be
4301 * output in this call. */
4302 expected_output_size =
4303 ( operation->ctx.cipher.unprocessed_len + input_length )
4304 / operation->block_size * operation->block_size;
4305 }
4306 else
4307 {
4308 expected_output_size = input_length;
4309 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004310
Gilles Peskine89d789c2018-06-04 17:17:16 +02004311 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004312 {
4313 status = PSA_ERROR_BUFFER_TOO_SMALL;
4314 goto exit;
4315 }
mohammad160382759612018-03-12 18:16:40 +02004316
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004317 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4318 {
4319 /* mbedtls_cipher_update has an API inconsistency: it will only
4320 * process a single block at a time in ECB mode. Abstract away that
4321 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004322 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4323 input,
4324 input_length,
4325 output,
4326 output_size,
4327 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004328 }
4329 else
4330 {
4331 status = mbedtls_to_psa_error(
4332 mbedtls_cipher_update( &operation->ctx.cipher, input,
4333 input_length, output, output_length ) );
4334 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004335exit:
4336 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004337 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004338 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004339}
4340
Gilles Peskinee553c652018-06-04 16:22:46 +02004341psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4342 uint8_t *output,
4343 size_t output_size,
4344 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004345{
David Saadab4ecc272019-02-14 13:48:10 +02004346 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004347 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004348 if( operation->alg == 0 )
4349 {
4350 return( PSA_ERROR_BAD_STATE );
4351 }
4352 if( operation->iv_required && ! operation->iv_set )
4353 {
4354 return( PSA_ERROR_BAD_STATE );
4355 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004356
Steven Cooremanef8575e2020-09-11 11:44:50 +02004357 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004358 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004359 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004360 output,
4361 output_size,
4362 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004363 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004364 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004365
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004366 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004367 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004368 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004369 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004370 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004371 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004372 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004373 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004374 }
4375
Steven Cooremanef8575e2020-09-11 11:44:50 +02004376 status = mbedtls_to_psa_error(
4377 mbedtls_cipher_finish( &operation->ctx.cipher,
4378 temp_output_buffer,
4379 output_length ) );
4380 if( status != PSA_SUCCESS )
4381 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004382
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004383 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004384 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004385 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004386 memcpy( output, temp_output_buffer, *output_length );
4387 else
Janos Follath315b51c2018-07-09 16:04:51 +01004388 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004389
Steven Cooremanef8575e2020-09-11 11:44:50 +02004390exit:
4391 if( operation->mbedtls_in_use == 1 )
4392 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004393
Steven Cooremanef8575e2020-09-11 11:44:50 +02004394 if( status == PSA_SUCCESS )
4395 return( psa_cipher_abort( operation ) );
4396 else
4397 {
4398 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004399 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004400
Steven Cooremanef8575e2020-09-11 11:44:50 +02004401 return( status );
4402 }
mohammad1603503973b2018-03-12 15:59:30 +02004403}
4404
Gilles Peskinee553c652018-06-04 16:22:46 +02004405psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4406{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004407 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004408 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004409 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004410 * in use. It's ok to call abort on such an object, and there's
4411 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004412 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004413 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004414
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004415 /* Sanity check (shouldn't happen: operation->alg should
4416 * always have been initialized to a valid value). */
4417 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4418 return( PSA_ERROR_BAD_STATE );
4419
Steven Cooremanef8575e2020-09-11 11:44:50 +02004420 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004421 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004422 else
4423 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004424
Moran Peker41deec42018-04-04 15:43:05 +03004425 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004426 operation->key_set = 0;
4427 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004428 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004429 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004430 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004431 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004432
Moran Peker395db872018-05-31 14:07:14 +03004433 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004434}
4435
Gilles Peskinea0655c32018-04-30 17:06:50 +02004436
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004437
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004438
mohammad16035955c982018-04-26 00:53:03 +03004439/****************************************************************/
4440/* AEAD */
4441/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004442
Gilles Peskineedf9a652018-08-17 18:11:56 +02004443typedef struct
4444{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004445 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004446 const mbedtls_cipher_info_t *cipher_info;
4447 union
4448 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004449 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004450#if defined(MBEDTLS_CCM_C)
4451 mbedtls_ccm_context ccm;
4452#endif /* MBEDTLS_CCM_C */
4453#if defined(MBEDTLS_GCM_C)
4454 mbedtls_gcm_context gcm;
4455#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004456#if defined(MBEDTLS_CHACHAPOLY_C)
4457 mbedtls_chachapoly_context chachapoly;
4458#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004459 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004460 psa_algorithm_t core_alg;
4461 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004462 uint8_t tag_length;
4463} aead_operation_t;
4464
Ronald Cronf95a2b12020-10-22 15:24:49 +02004465#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4466
Gilles Peskine30a9e412019-01-14 18:36:12 +01004467static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004468{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004469 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004470 {
4471#if defined(MBEDTLS_CCM_C)
4472 case PSA_ALG_CCM:
4473 mbedtls_ccm_free( &operation->ctx.ccm );
4474 break;
4475#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004476#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004477 case PSA_ALG_GCM:
4478 mbedtls_gcm_free( &operation->ctx.gcm );
4479 break;
4480#endif /* MBEDTLS_GCM_C */
4481 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004482
Ronald Cron5c522922020-11-14 16:35:34 +01004483 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004484}
4485
4486static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004487 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004488 psa_key_usage_t usage,
4489 psa_algorithm_t alg )
4490{
4491 psa_status_t status;
4492 size_t key_bits;
4493 mbedtls_cipher_id_t cipher_id;
4494
Ronald Cron5c522922020-11-14 16:35:34 +01004495 status = psa_get_and_lock_transparent_key_slot_with_policy(
4496 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004497 if( status != PSA_SUCCESS )
4498 return( status );
4499
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004500 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004501
4502 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004503 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004504 &cipher_id );
4505 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004506 {
4507 status = PSA_ERROR_NOT_SUPPORTED;
4508 goto cleanup;
4509 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004510
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004511 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004512 {
4513#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004514 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004515 operation->core_alg = PSA_ALG_CCM;
4516 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004517 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4518 * The call to mbedtls_ccm_encrypt_and_tag or
4519 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004520 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004521 {
4522 status = PSA_ERROR_INVALID_ARGUMENT;
4523 goto cleanup;
4524 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004525 mbedtls_ccm_init( &operation->ctx.ccm );
4526 status = mbedtls_to_psa_error(
4527 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004528 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004529 (unsigned int) key_bits ) );
4530 if( status != 0 )
4531 goto cleanup;
4532 break;
4533#endif /* MBEDTLS_CCM_C */
4534
4535#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004536 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004537 operation->core_alg = PSA_ALG_GCM;
4538 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004539 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4540 * The call to mbedtls_gcm_crypt_and_tag or
4541 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004542 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004543 {
4544 status = PSA_ERROR_INVALID_ARGUMENT;
4545 goto cleanup;
4546 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004547 mbedtls_gcm_init( &operation->ctx.gcm );
4548 status = mbedtls_to_psa_error(
4549 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004550 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004551 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004552 if( status != 0 )
4553 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554 break;
4555#endif /* MBEDTLS_GCM_C */
4556
Gilles Peskine26869f22019-05-06 15:25:00 +02004557#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004558 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004559 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4560 operation->full_tag_length = 16;
4561 /* We only support the default tag length. */
4562 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004563 {
4564 status = PSA_ERROR_NOT_SUPPORTED;
4565 goto cleanup;
4566 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004567 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4568 status = mbedtls_to_psa_error(
4569 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004570 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004571 if( status != 0 )
4572 goto cleanup;
4573 break;
4574#endif /* MBEDTLS_CHACHAPOLY_C */
4575
Gilles Peskineedf9a652018-08-17 18:11:56 +02004576 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004577 status = PSA_ERROR_NOT_SUPPORTED;
4578 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004579 }
4580
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004581 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4582 {
4583 status = PSA_ERROR_INVALID_ARGUMENT;
4584 goto cleanup;
4585 }
4586 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004587
Gilles Peskineedf9a652018-08-17 18:11:56 +02004588 return( PSA_SUCCESS );
4589
4590cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004591 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004592 return( status );
4593}
4594
Ronald Croncf56a0a2020-08-04 09:51:30 +02004595psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004596 psa_algorithm_t alg,
4597 const uint8_t *nonce,
4598 size_t nonce_length,
4599 const uint8_t *additional_data,
4600 size_t additional_data_length,
4601 const uint8_t *plaintext,
4602 size_t plaintext_length,
4603 uint8_t *ciphertext,
4604 size_t ciphertext_size,
4605 size_t *ciphertext_length )
4606{
mohammad16035955c982018-04-26 00:53:03 +03004607 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004608 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004609 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004610
mohammad1603f08a5502018-06-03 15:05:47 +03004611 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004612
Ronald Croncf56a0a2020-08-04 09:51:30 +02004613 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004614 if( status != PSA_SUCCESS )
4615 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004616
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617 /* For all currently supported modes, the tag is at the end of the
4618 * ciphertext. */
4619 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4620 {
4621 status = PSA_ERROR_BUFFER_TOO_SMALL;
4622 goto exit;
4623 }
4624 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004625
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004626#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004627 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004628 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004629 status = mbedtls_to_psa_error(
4630 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4631 MBEDTLS_GCM_ENCRYPT,
4632 plaintext_length,
4633 nonce, nonce_length,
4634 additional_data, additional_data_length,
4635 plaintext, ciphertext,
4636 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004637 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004638 else
4639#endif /* MBEDTLS_GCM_C */
4640#if defined(MBEDTLS_CCM_C)
4641 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004642 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004643 status = mbedtls_to_psa_error(
4644 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4645 plaintext_length,
4646 nonce, nonce_length,
4647 additional_data,
4648 additional_data_length,
4649 plaintext, ciphertext,
4650 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004651 }
mohammad16035c8845f2018-05-09 05:40:09 -07004652 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004653#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004654#if defined(MBEDTLS_CHACHAPOLY_C)
4655 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4656 {
4657 if( nonce_length != 12 || operation.tag_length != 16 )
4658 {
4659 status = PSA_ERROR_NOT_SUPPORTED;
4660 goto exit;
4661 }
4662 status = mbedtls_to_psa_error(
4663 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4664 plaintext_length,
4665 nonce,
4666 additional_data,
4667 additional_data_length,
4668 plaintext,
4669 ciphertext,
4670 tag ) );
4671 }
4672 else
4673#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004674 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004675 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004676 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004677 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004678
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4680 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004681
Gilles Peskineedf9a652018-08-17 18:11:56 +02004682exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004683 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004684 if( status == PSA_SUCCESS )
4685 *ciphertext_length = plaintext_length + operation.tag_length;
4686 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004687}
4688
Gilles Peskineee652a32018-06-01 19:23:52 +02004689/* Locate the tag in a ciphertext buffer containing the encrypted data
4690 * followed by the tag. Return the length of the part preceding the tag in
4691 * *plaintext_length. This is the size of the plaintext in modes where
4692 * the encrypted data has the same size as the plaintext, such as
4693 * CCM and GCM. */
4694static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4695 const uint8_t *ciphertext,
4696 size_t ciphertext_length,
4697 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004698 const uint8_t **p_tag )
4699{
4700 size_t payload_length;
4701 if( tag_length > ciphertext_length )
4702 return( PSA_ERROR_INVALID_ARGUMENT );
4703 payload_length = ciphertext_length - tag_length;
4704 if( payload_length > plaintext_size )
4705 return( PSA_ERROR_BUFFER_TOO_SMALL );
4706 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004707 return( PSA_SUCCESS );
4708}
4709
Ronald Croncf56a0a2020-08-04 09:51:30 +02004710psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004711 psa_algorithm_t alg,
4712 const uint8_t *nonce,
4713 size_t nonce_length,
4714 const uint8_t *additional_data,
4715 size_t additional_data_length,
4716 const uint8_t *ciphertext,
4717 size_t ciphertext_length,
4718 uint8_t *plaintext,
4719 size_t plaintext_size,
4720 size_t *plaintext_length )
4721{
mohammad16035955c982018-04-26 00:53:03 +03004722 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004723 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004724 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004725
Gilles Peskineee652a32018-06-01 19:23:52 +02004726 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004727
Ronald Croncf56a0a2020-08-04 09:51:30 +02004728 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004729 if( status != PSA_SUCCESS )
4730 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004731
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004732 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4733 ciphertext, ciphertext_length,
4734 plaintext_size, &tag );
4735 if( status != PSA_SUCCESS )
4736 goto exit;
4737
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004738#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004739 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004740 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004741 status = mbedtls_to_psa_error(
4742 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4743 ciphertext_length - operation.tag_length,
4744 nonce, nonce_length,
4745 additional_data,
4746 additional_data_length,
4747 tag, operation.tag_length,
4748 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004749 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004750 else
4751#endif /* MBEDTLS_GCM_C */
4752#if defined(MBEDTLS_CCM_C)
4753 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004754 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004755 status = mbedtls_to_psa_error(
4756 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4757 ciphertext_length - operation.tag_length,
4758 nonce, nonce_length,
4759 additional_data,
4760 additional_data_length,
4761 ciphertext, plaintext,
4762 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004763 }
mohammad160339574652018-06-01 04:39:53 -07004764 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004765#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004766#if defined(MBEDTLS_CHACHAPOLY_C)
4767 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4768 {
4769 if( nonce_length != 12 || operation.tag_length != 16 )
4770 {
4771 status = PSA_ERROR_NOT_SUPPORTED;
4772 goto exit;
4773 }
4774 status = mbedtls_to_psa_error(
4775 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4776 ciphertext_length - operation.tag_length,
4777 nonce,
4778 additional_data,
4779 additional_data_length,
4780 tag,
4781 ciphertext,
4782 plaintext ) );
4783 }
4784 else
4785#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004786 {
mohammad1603554faad2018-06-03 15:07:38 +03004787 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004788 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004789
Gilles Peskineedf9a652018-08-17 18:11:56 +02004790 if( status != PSA_SUCCESS && plaintext_size != 0 )
4791 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004792
Gilles Peskineedf9a652018-08-17 18:11:56 +02004793exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004794 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004795 if( status == PSA_SUCCESS )
4796 *plaintext_length = ciphertext_length - operation.tag_length;
4797 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004798}
4799
Gilles Peskinea0655c32018-04-30 17:06:50 +02004800
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004801
Gilles Peskine20035e32018-02-03 22:44:14 +01004802/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004803/* Generators */
4804/****************************************************************/
4805
John Durkop07cc04a2020-11-16 22:08:34 -08004806#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4807 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4808 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4809#define AT_LEAST_ONE_BUILTIN_KDF
4810#endif
4811
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004812#define HKDF_STATE_INIT 0 /* no input yet */
4813#define HKDF_STATE_STARTED 1 /* got salt */
4814#define HKDF_STATE_KEYED 2 /* got key */
4815#define HKDF_STATE_OUTPUT 3 /* output started */
4816
Gilles Peskinecbe66502019-05-16 16:59:18 +02004817static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004818 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004819{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4821 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004822 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004823 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004824}
4825
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004826psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004827{
4828 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004830 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004831 {
4832 /* The object has (apparently) been initialized but it is not
4833 * in use. It's ok to call abort on such an object, and there's
4834 * nothing to do. */
4835 }
4836 else
John Durkopd0321952020-10-29 21:37:36 -07004837#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004838 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004839 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004840 mbedtls_free( operation->ctx.hkdf.info );
4841 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004842 }
John Durkop07cc04a2020-11-16 22:08:34 -08004843 else
4844#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4845#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4846 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4847 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004849 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004850 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004851 if( operation->ctx.tls12_prf.seed != NULL )
4852 {
4853 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4854 operation->ctx.tls12_prf.seed_length );
4855 mbedtls_free( operation->ctx.tls12_prf.seed );
4856 }
4857
4858 if( operation->ctx.tls12_prf.label != NULL )
4859 {
4860 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4861 operation->ctx.tls12_prf.label_length );
4862 mbedtls_free( operation->ctx.tls12_prf.label );
4863 }
4864
4865 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4866
4867 /* We leave the fields Ai and output_block to be erased safely by the
4868 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004869 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004870 else
John Durkop07cc04a2020-11-16 22:08:34 -08004871#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4872 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004873 {
4874 status = PSA_ERROR_BAD_STATE;
4875 }
Janos Follath083036a2019-06-11 10:22:26 +01004876 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004877 return( status );
4878}
4879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004880psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004881 size_t *capacity)
4882{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004883 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004884 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004885 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004886 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004887 }
4888
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004889 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004890 return( PSA_SUCCESS );
4891}
4892
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004894 size_t capacity )
4895{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004899 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004901 return( PSA_SUCCESS );
4902}
4903
John Durkopd0321952020-10-29 21:37:36 -07004904#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004905/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004906 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004907static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004908 psa_algorithm_t hash_alg,
4909 uint8_t *output,
4910 size_t output_length )
4911{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004912 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004913 psa_status_t status;
4914
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004915 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4916 return( PSA_ERROR_BAD_STATE );
4917 hkdf->state = HKDF_STATE_OUTPUT;
4918
Gilles Peskinebef7f142018-07-12 17:22:21 +02004919 while( output_length != 0 )
4920 {
4921 /* Copy what remains of the current block */
4922 uint8_t n = hash_length - hkdf->offset_in_block;
4923 if( n > output_length )
4924 n = (uint8_t) output_length;
4925 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4926 output += n;
4927 output_length -= n;
4928 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004929 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004930 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004931 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004932 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004933 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004934 * object was corrupted or if this function is called directly
4935 * inside the library. */
4936 if( hkdf->block_number == 0xff )
4937 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004938
4939 /* We need a new block */
4940 ++hkdf->block_number;
4941 hkdf->offset_in_block = 0;
4942 status = psa_hmac_setup_internal( &hkdf->hmac,
4943 hkdf->prk, hash_length,
4944 hash_alg );
4945 if( status != PSA_SUCCESS )
4946 return( status );
4947 if( hkdf->block_number != 1 )
4948 {
4949 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4950 hkdf->output_block,
4951 hash_length );
4952 if( status != PSA_SUCCESS )
4953 return( status );
4954 }
4955 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4956 hkdf->info,
4957 hkdf->info_length );
4958 if( status != PSA_SUCCESS )
4959 return( status );
4960 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4961 &hkdf->block_number, 1 );
4962 if( status != PSA_SUCCESS )
4963 return( status );
4964 status = psa_hmac_finish_internal( &hkdf->hmac,
4965 hkdf->output_block,
4966 sizeof( hkdf->output_block ) );
4967 if( status != PSA_SUCCESS )
4968 return( status );
4969 }
4970
4971 return( PSA_SUCCESS );
4972}
John Durkop07cc04a2020-11-16 22:08:34 -08004973#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004974
John Durkop07cc04a2020-11-16 22:08:34 -08004975#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4976 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004977static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4978 psa_tls12_prf_key_derivation_t *tls12_prf,
4979 psa_algorithm_t alg )
4980{
4981 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004982 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004983 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4984 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004985
Janos Follath7742fee2019-06-17 12:58:10 +01004986 /* We can't be wanting more output after block 0xff, otherwise
4987 * the capacity check in psa_key_derivation_output_bytes() would have
4988 * prevented this call. It could happen only if the operation
4989 * object was corrupted or if this function is called directly
4990 * inside the library. */
4991 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004992 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004993
4994 /* We need a new block */
4995 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004996 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004997
4998 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4999 *
5000 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5001 *
5002 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5003 * HMAC_hash(secret, A(2) + seed) +
5004 * HMAC_hash(secret, A(3) + seed) + ...
5005 *
5006 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005007 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005008 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005009 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005010 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005011 * is currently extracted as `output_block` and where i is
5012 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005013 */
5014
Janos Follathea29bfb2019-06-19 12:21:20 +01005015 /* Save the hash context before using it, to preserve the hash state with
5016 * only the inner padding in it. We need this, because inner padding depends
5017 * on the key (secret in the RFC's terminology). */
5018 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5019 if( status != PSA_SUCCESS )
5020 goto cleanup;
5021
5022 /* Calculate A(i) where i = tls12_prf->block_number. */
5023 if( tls12_prf->block_number == 1 )
5024 {
5025 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5026 * the variable seed and in this instance means it in the context of the
5027 * P_hash function, where seed = label + seed.) */
5028 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5029 tls12_prf->label, tls12_prf->label_length );
5030 if( status != PSA_SUCCESS )
5031 goto cleanup;
5032 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5033 tls12_prf->seed, tls12_prf->seed_length );
5034 if( status != PSA_SUCCESS )
5035 goto cleanup;
5036 }
5037 else
5038 {
5039 /* A(i) = HMAC_hash(secret, A(i-1)) */
5040 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5041 tls12_prf->Ai, hash_length );
5042 if( status != PSA_SUCCESS )
5043 goto cleanup;
5044 }
5045
5046 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5047 tls12_prf->Ai, hash_length );
5048 if( status != PSA_SUCCESS )
5049 goto cleanup;
5050 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5051 if( status != PSA_SUCCESS )
5052 goto cleanup;
5053
5054 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5055 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5056 tls12_prf->Ai, hash_length );
5057 if( status != PSA_SUCCESS )
5058 goto cleanup;
5059 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5060 tls12_prf->label, tls12_prf->label_length );
5061 if( status != PSA_SUCCESS )
5062 goto cleanup;
5063 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5064 tls12_prf->seed, tls12_prf->seed_length );
5065 if( status != PSA_SUCCESS )
5066 goto cleanup;
5067 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5068 tls12_prf->output_block, hash_length );
5069 if( status != PSA_SUCCESS )
5070 goto cleanup;
5071 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5072 if( status != PSA_SUCCESS )
5073 goto cleanup;
5074
Janos Follath7742fee2019-06-17 12:58:10 +01005075
5076cleanup:
5077
Janos Follathea29bfb2019-06-19 12:21:20 +01005078 cleanup_status = psa_hash_abort( &backup );
5079 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5080 status = cleanup_status;
5081
5082 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005083}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005084
Janos Follath844eb0e2019-06-19 12:10:49 +01005085static psa_status_t psa_key_derivation_tls12_prf_read(
5086 psa_tls12_prf_key_derivation_t *tls12_prf,
5087 psa_algorithm_t alg,
5088 uint8_t *output,
5089 size_t output_length )
5090{
5091 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005092 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005093 psa_status_t status;
5094 uint8_t offset, length;
5095
5096 while( output_length != 0 )
5097 {
5098 /* Check if we have fully processed the current block. */
5099 if( tls12_prf->left_in_block == 0 )
5100 {
5101 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5102 alg );
5103 if( status != PSA_SUCCESS )
5104 return( status );
5105
5106 continue;
5107 }
5108
5109 if( tls12_prf->left_in_block > output_length )
5110 length = (uint8_t) output_length;
5111 else
5112 length = tls12_prf->left_in_block;
5113
5114 offset = hash_length - tls12_prf->left_in_block;
5115 memcpy( output, tls12_prf->output_block + offset, length );
5116 output += length;
5117 output_length -= length;
5118 tls12_prf->left_in_block -= length;
5119 }
5120
5121 return( PSA_SUCCESS );
5122}
John Durkop07cc04a2020-11-16 22:08:34 -08005123#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5124 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005125
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005126psa_status_t psa_key_derivation_output_bytes(
5127 psa_key_derivation_operation_t *operation,
5128 uint8_t *output,
5129 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005130{
5131 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005132 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005133
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005134 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005135 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005136 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005137 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005138 }
5139
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005140 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005141 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005145 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005146 goto exit;
5147 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005148 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005149 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005151 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005152 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5153 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005155 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005156 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005157 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159
John Durkopd0321952020-10-29 21:37:36 -07005160#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005161 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005162 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005163 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005165 output, output_length );
5166 }
Janos Follathadbec812019-06-14 11:05:39 +01005167 else
John Durkop07cc04a2020-11-16 22:08:34 -08005168#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5169#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5170 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005171 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005172 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005173 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005175 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005176 output_length );
5177 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005178 else
John Durkop07cc04a2020-11-16 22:08:34 -08005179#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5180 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005181 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005182 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005183 return( PSA_ERROR_BAD_STATE );
5184 }
5185
5186exit:
5187 if( status != PSA_SUCCESS )
5188 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005189 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005190 * This allows us to differentiate between exhausted operations and
5191 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5192 * operations. */
5193 psa_algorithm_t alg = operation->alg;
5194 psa_key_derivation_abort( operation );
5195 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005196 memset( output, '!', output_length );
5197 }
5198 return( status );
5199}
5200
David Brown7807bf72021-02-09 16:28:23 -07005201#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005202static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5203{
5204 if( data_size >= 8 )
5205 mbedtls_des_key_set_parity( data );
5206 if( data_size >= 16 )
5207 mbedtls_des_key_set_parity( data + 8 );
5208 if( data_size >= 24 )
5209 mbedtls_des_key_set_parity( data + 16 );
5210}
David Brown7807bf72021-02-09 16:28:23 -07005211#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005212
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005213static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005214 psa_key_slot_t *slot,
5215 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005216 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217{
5218 uint8_t *data = NULL;
5219 size_t bytes = PSA_BITS_TO_BYTES( bits );
5220 psa_status_t status;
5221
Gilles Peskine8e338702019-07-30 20:06:31 +02005222 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 return( PSA_ERROR_INVALID_ARGUMENT );
5224 if( bits % 8 != 0 )
5225 return( PSA_ERROR_INVALID_ARGUMENT );
5226 data = mbedtls_calloc( 1, bytes );
5227 if( data == NULL )
5228 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5229
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005231 if( status != PSA_SUCCESS )
5232 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005233#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005234 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005235 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005236#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005237
5238 status = psa_allocate_buffer_to_slot( slot, bytes );
5239 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005240 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005241
Ronald Cronbf33c932020-11-28 18:06:53 +01005242 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005243 psa_key_attributes_t attributes = {
5244 .core = slot->attr
5245 };
5246
Ronald Cronbf33c932020-11-28 18:06:53 +01005247 status = psa_driver_wrapper_import_key( &attributes,
5248 data, bytes,
5249 slot->key.data,
5250 slot->key.bytes,
5251 &slot->key.bytes, &bits );
5252 if( bits != slot->attr.bits )
5253 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005254
5255exit:
5256 mbedtls_free( data );
5257 return( status );
5258}
5259
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005260psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005262 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005263{
5264 psa_status_t status;
5265 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005266 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005267
Ronald Cron81709fc2020-11-14 12:10:32 +01005268 *key = MBEDTLS_SVC_KEY_ID_INIT;
5269
Gilles Peskine0f84d622019-09-12 19:03:13 +02005270 /* Reject any attempt to create a zero-length key so that we don't
5271 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5272 if( psa_get_key_bits( attributes ) == 0 )
5273 return( PSA_ERROR_INVALID_ARGUMENT );
5274
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005275 if( ! operation->can_output_key )
5276 return( PSA_ERROR_NOT_PERMITTED );
5277
Ronald Cron81709fc2020-11-14 12:10:32 +01005278 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5279 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5281 if( driver != NULL )
5282 {
5283 /* Deriving a key in a secure element is not implemented yet. */
5284 status = PSA_ERROR_NOT_SUPPORTED;
5285 }
5286#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005287 if( status == PSA_SUCCESS )
5288 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005289 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005290 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005292 }
5293 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005294 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005295 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005296 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005297
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005298 return( status );
5299}
5300
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301
5302
5303/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005304/* Key derivation */
5305/****************************************************************/
5306
Steven Cooreman932ffb72021-02-15 12:14:32 +01005307#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005308static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005310 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005311{
John Durkop07cc04a2020-11-16 22:08:34 -08005312 int is_kdf_alg_supported;
5313
Janos Follath5fe19732019-06-20 15:09:30 +01005314 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5315 * initialisers for this union leave some bytes unspecified.) */
5316 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5317
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005319#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005320 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5321 is_kdf_alg_supported = 1;
5322 else
5323#endif
5324#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5325 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5326 is_kdf_alg_supported = 1;
5327 else
5328#endif
5329#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5330 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5331 is_kdf_alg_supported = 1;
5332 else
5333#endif
5334 is_kdf_alg_supported = 0;
5335
5336 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005337 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005338 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005339 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005340 if( hash_size == 0 )
5341 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005342 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5343 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005344 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005345 {
5346 return( PSA_ERROR_NOT_SUPPORTED );
5347 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005348 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005349 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350 }
John Durkop07cc04a2020-11-16 22:08:34 -08005351
5352 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005353}
John Durkop07cc04a2020-11-16 22:08:34 -08005354#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005355
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005356psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005357 psa_algorithm_t alg )
5358{
5359 psa_status_t status;
5360
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005362 return( PSA_ERROR_BAD_STATE );
5363
Gilles Peskine6843c292019-01-18 16:44:49 +01005364 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5365 return( PSA_ERROR_INVALID_ARGUMENT );
5366 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005367 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005368#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005369 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005370 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005371#else
5372 return( PSA_ERROR_NOT_SUPPORTED );
5373#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005374 }
5375 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5376 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005377#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005378 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005379#else
5380 return( PSA_ERROR_NOT_SUPPORTED );
5381#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005382 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005383 else
5384 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005385
5386 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005388 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005389}
5390
John Durkopd0321952020-10-29 21:37:36 -07005391#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005392static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005393 psa_algorithm_t hash_alg,
5394 psa_key_derivation_step_t step,
5395 const uint8_t *data,
5396 size_t data_length )
5397{
5398 psa_status_t status;
5399 switch( step )
5400 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005401 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005402 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005403 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005404 status = psa_hmac_setup_internal( &hkdf->hmac,
5405 data, data_length,
5406 hash_alg );
5407 if( status != PSA_SUCCESS )
5408 return( status );
5409 hkdf->state = HKDF_STATE_STARTED;
5410 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005411 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005412 /* If no salt was provided, use an empty salt. */
5413 if( hkdf->state == HKDF_STATE_INIT )
5414 {
5415 status = psa_hmac_setup_internal( &hkdf->hmac,
5416 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005417 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005418 if( status != PSA_SUCCESS )
5419 return( status );
5420 hkdf->state = HKDF_STATE_STARTED;
5421 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005422 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005423 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005424 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5425 data, data_length );
5426 if( status != PSA_SUCCESS )
5427 return( status );
5428 status = psa_hmac_finish_internal( &hkdf->hmac,
5429 hkdf->prk,
5430 sizeof( hkdf->prk ) );
5431 if( status != PSA_SUCCESS )
5432 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005433 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005434 hkdf->block_number = 0;
5435 hkdf->state = HKDF_STATE_KEYED;
5436 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005437 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005438 if( hkdf->state == HKDF_STATE_OUTPUT )
5439 return( PSA_ERROR_BAD_STATE );
5440 if( hkdf->info_set )
5441 return( PSA_ERROR_BAD_STATE );
5442 hkdf->info_length = data_length;
5443 if( data_length != 0 )
5444 {
5445 hkdf->info = mbedtls_calloc( 1, data_length );
5446 if( hkdf->info == NULL )
5447 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5448 memcpy( hkdf->info, data, data_length );
5449 }
5450 hkdf->info_set = 1;
5451 return( PSA_SUCCESS );
5452 default:
5453 return( PSA_ERROR_INVALID_ARGUMENT );
5454 }
5455}
John Durkop07cc04a2020-11-16 22:08:34 -08005456#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005457
John Durkop07cc04a2020-11-16 22:08:34 -08005458#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5459 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005460static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5461 const uint8_t *data,
5462 size_t data_length )
5463{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005464 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005465 return( PSA_ERROR_BAD_STATE );
5466
Janos Follathd6dce9f2019-07-04 09:11:38 +01005467 if( data_length != 0 )
5468 {
5469 prf->seed = mbedtls_calloc( 1, data_length );
5470 if( prf->seed == NULL )
5471 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005472
Janos Follathd6dce9f2019-07-04 09:11:38 +01005473 memcpy( prf->seed, data, data_length );
5474 prf->seed_length = data_length;
5475 }
Janos Follathf08e2652019-06-13 09:05:41 +01005476
Gilles Peskine43f958b2020-12-13 14:55:14 +01005477 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005478
5479 return( PSA_SUCCESS );
5480}
5481
Janos Follath81550542019-06-13 14:26:34 +01005482static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5483 psa_algorithm_t hash_alg,
5484 const uint8_t *data,
5485 size_t data_length )
5486{
5487 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005488 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005489 return( PSA_ERROR_BAD_STATE );
5490
5491 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5492 if( status != PSA_SUCCESS )
5493 return( status );
5494
Gilles Peskine43f958b2020-12-13 14:55:14 +01005495 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005496
5497 return( PSA_SUCCESS );
5498}
5499
Janos Follath63028dd2019-06-13 09:15:47 +01005500static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5501 const uint8_t *data,
5502 size_t data_length )
5503{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005504 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005505 return( PSA_ERROR_BAD_STATE );
5506
Janos Follathd6dce9f2019-07-04 09:11:38 +01005507 if( data_length != 0 )
5508 {
5509 prf->label = mbedtls_calloc( 1, data_length );
5510 if( prf->label == NULL )
5511 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005512
Janos Follathd6dce9f2019-07-04 09:11:38 +01005513 memcpy( prf->label, data, data_length );
5514 prf->label_length = data_length;
5515 }
Janos Follath63028dd2019-06-13 09:15:47 +01005516
Gilles Peskine43f958b2020-12-13 14:55:14 +01005517 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005518
5519 return( PSA_SUCCESS );
5520}
5521
Janos Follathb03233e2019-06-11 15:30:30 +01005522static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5523 psa_algorithm_t hash_alg,
5524 psa_key_derivation_step_t step,
5525 const uint8_t *data,
5526 size_t data_length )
5527{
Janos Follathb03233e2019-06-11 15:30:30 +01005528 switch( step )
5529 {
Janos Follathf08e2652019-06-13 09:05:41 +01005530 case PSA_KEY_DERIVATION_INPUT_SEED:
5531 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005532 case PSA_KEY_DERIVATION_INPUT_SECRET:
5533 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005534 case PSA_KEY_DERIVATION_INPUT_LABEL:
5535 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005536 default:
5537 return( PSA_ERROR_INVALID_ARGUMENT );
5538 }
5539}
John Durkop07cc04a2020-11-16 22:08:34 -08005540#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5541 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5542
5543#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5544static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5545 psa_tls12_prf_key_derivation_t *prf,
5546 psa_algorithm_t hash_alg,
5547 const uint8_t *data,
5548 size_t data_length )
5549{
5550 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005551 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005552 uint8_t *cur = pms;
5553
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005554 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005555 return( PSA_ERROR_INVALID_ARGUMENT );
5556
5557 /* Quoting RFC 4279, Section 2:
5558 *
5559 * The premaster secret is formed as follows: if the PSK is N octets
5560 * long, concatenate a uint16 with the value N, N zero octets, a second
5561 * uint16 with the value N, and the PSK itself.
5562 */
5563
5564 *cur++ = ( data_length >> 8 ) & 0xff;
5565 *cur++ = ( data_length >> 0 ) & 0xff;
5566 memset( cur, 0, data_length );
5567 cur += data_length;
5568 *cur++ = pms[0];
5569 *cur++ = pms[1];
5570 memcpy( cur, data, data_length );
5571 cur += data_length;
5572
5573 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5574
5575 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5576 return( status );
5577}
Janos Follath6660f0e2019-06-17 08:44:03 +01005578
5579static psa_status_t psa_tls12_prf_psk_to_ms_input(
5580 psa_tls12_prf_key_derivation_t *prf,
5581 psa_algorithm_t hash_alg,
5582 psa_key_derivation_step_t step,
5583 const uint8_t *data,
5584 size_t data_length )
5585{
5586 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005587 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005588 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5589 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005590 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005591
5592 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5593}
John Durkop07cc04a2020-11-16 22:08:34 -08005594#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005595
Gilles Peskineb8965192019-09-24 16:21:10 +02005596/** Check whether the given key type is acceptable for the given
5597 * input step of a key derivation.
5598 *
5599 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5600 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5601 * Both secret and non-secret inputs can alternatively have the type
5602 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5603 * that the input was passed as a buffer rather than via a key object.
5604 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005605static int psa_key_derivation_check_input_type(
5606 psa_key_derivation_step_t step,
5607 psa_key_type_t key_type )
5608{
5609 switch( step )
5610 {
5611 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005612 if( key_type == PSA_KEY_TYPE_DERIVE )
5613 return( PSA_SUCCESS );
5614 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005615 return( PSA_SUCCESS );
5616 break;
5617 case PSA_KEY_DERIVATION_INPUT_LABEL:
5618 case PSA_KEY_DERIVATION_INPUT_SALT:
5619 case PSA_KEY_DERIVATION_INPUT_INFO:
5620 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005621 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5622 return( PSA_SUCCESS );
5623 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005624 return( PSA_SUCCESS );
5625 break;
5626 }
5627 return( PSA_ERROR_INVALID_ARGUMENT );
5628}
5629
Janos Follathb80a94e2019-06-12 15:54:46 +01005630static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005631 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005632 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005633 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005634 const uint8_t *data,
5635 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005636{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005637 psa_status_t status;
5638 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5639
5640 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005641 if( status != PSA_SUCCESS )
5642 goto exit;
5643
John Durkopd0321952020-10-29 21:37:36 -07005644#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005645 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005646 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005647 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005648 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005649 step, data, data_length );
5650 }
John Durkop07cc04a2020-11-16 22:08:34 -08005651 else
5652#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5653#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5654 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005655 {
Janos Follathb03233e2019-06-11 15:30:30 +01005656 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5657 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5658 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005659 }
John Durkop07cc04a2020-11-16 22:08:34 -08005660 else
5661#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5662#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5663 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005664 {
5665 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5666 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5667 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005668 }
5669 else
John Durkop07cc04a2020-11-16 22:08:34 -08005670#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005671 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005672 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005673 (void) data;
5674 (void) data_length;
5675 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005676 return( PSA_ERROR_BAD_STATE );
5677 }
5678
Gilles Peskine224b0d62019-09-23 18:13:17 +02005679exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005680 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005681 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005682 return( status );
5683}
5684
Janos Follath51f4a0f2019-06-14 11:35:55 +01005685psa_status_t psa_key_derivation_input_bytes(
5686 psa_key_derivation_operation_t *operation,
5687 psa_key_derivation_step_t step,
5688 const uint8_t *data,
5689 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005690{
Gilles Peskineb8965192019-09-24 16:21:10 +02005691 return( psa_key_derivation_input_internal( operation, step,
5692 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005693 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005694}
5695
Janos Follath51f4a0f2019-06-14 11:35:55 +01005696psa_status_t psa_key_derivation_input_key(
5697 psa_key_derivation_operation_t *operation,
5698 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005699 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005700{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005701 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005702 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005703 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005704
Ronald Cron5c522922020-11-14 16:35:34 +01005705 status = psa_get_and_lock_transparent_key_slot_with_policy(
5706 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005707 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005708 {
5709 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005710 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005711 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005712
5713 /* Passing a key object as a SECRET input unlocks the permission
5714 * to output to a key object. */
5715 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5716 operation->can_output_key = 1;
5717
Ronald Cronf95a2b12020-10-22 15:24:49 +02005718 status = psa_key_derivation_input_internal( operation,
5719 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005720 slot->key.data,
5721 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005722
Ronald Cron5c522922020-11-14 16:35:34 +01005723 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005724
Ronald Cron5c522922020-11-14 16:35:34 +01005725 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005726}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005727
5728
5729
5730/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005731/* Key agreement */
5732/****************************************************************/
5733
John Durkop6ba40d12020-11-10 08:50:04 -08005734#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005735static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5736 size_t peer_key_length,
5737 const mbedtls_ecp_keypair *our_key,
5738 uint8_t *shared_secret,
5739 size_t shared_secret_size,
5740 size_t *shared_secret_length )
5741{
Steven Cooremand4867872020-08-05 16:31:39 +02005742 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005743 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005744 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005745 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005746 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005747 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005748
Ronald Cron90857082020-11-25 14:58:33 +01005749 status = mbedtls_psa_ecp_load_representation(
5750 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005751 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005752 peer_key,
5753 peer_key_length,
5754 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005755 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005756 goto exit;
5757
Jaeden Amero97271b32019-01-10 19:38:51 +00005758 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005759 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005760 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005761 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005762 status = mbedtls_to_psa_error(
5763 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5764 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005765 goto exit;
5766
Jaeden Amero97271b32019-01-10 19:38:51 +00005767 status = mbedtls_to_psa_error(
5768 mbedtls_ecdh_calc_secret( &ecdh,
5769 shared_secret_length,
5770 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005771 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005772 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005773 if( status != PSA_SUCCESS )
5774 goto exit;
5775 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5776 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005777
5778exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005779 if( status != PSA_SUCCESS )
5780 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005781 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005782 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005783 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005784
Jaeden Amero97271b32019-01-10 19:38:51 +00005785 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005786}
John Durkop6ba40d12020-11-10 08:50:04 -08005787#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005788
Gilles Peskine01d718c2018-09-18 12:01:02 +02005789#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5790
Gilles Peskine0216fe12019-04-11 21:23:21 +02005791static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5792 psa_key_slot_t *private_key,
5793 const uint8_t *peer_key,
5794 size_t peer_key_length,
5795 uint8_t *shared_secret,
5796 size_t shared_secret_size,
5797 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005798{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005799 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005800 {
John Durkop6ba40d12020-11-10 08:50:04 -08005801#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005802 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005803 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005804 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005805 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005806 psa_status_t status = mbedtls_psa_ecp_load_representation(
5807 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005808 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005809 private_key->key.data,
5810 private_key->key.bytes,
5811 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005812 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005813 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005814 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005815 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005816 shared_secret, shared_secret_size,
5817 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005818 mbedtls_ecp_keypair_free( ecp );
5819 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005820 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005821#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005822 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005823 (void) private_key;
5824 (void) peer_key;
5825 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005826 (void) shared_secret;
5827 (void) shared_secret_size;
5828 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005829 return( PSA_ERROR_NOT_SUPPORTED );
5830 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005831}
5832
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005833/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005834 * to potentially free embedded data structures and wipe confidential data.
5835 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005836static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005837 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005838 psa_key_slot_t *private_key,
5839 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005840 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005841{
5842 psa_status_t status;
5843 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5844 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005845 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005846
5847 /* Step 1: run the secret agreement algorithm to generate the shared
5848 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005849 status = psa_key_agreement_raw_internal( ka_alg,
5850 private_key,
5851 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005852 shared_secret,
5853 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005854 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005855 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005856 goto exit;
5857
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005858 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005859 * the shared secret. A shared secret is permitted wherever a key
5860 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005861 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005862 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005863 shared_secret,
5864 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005865exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005866 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005867 return( status );
5868}
5869
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005870psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005871 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005872 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005873 const uint8_t *peer_key,
5874 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005876 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005877 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005878 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005881 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005882 status = psa_get_and_lock_transparent_key_slot_with_policy(
5883 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005884 if( status != PSA_SUCCESS )
5885 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005886 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005887 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005888 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005889 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005890 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005891 else
5892 {
5893 /* If a private key has been added as SECRET, we allow the derived
5894 * key material to be used as a key in PSA Crypto. */
5895 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5896 operation->can_output_key = 1;
5897 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005898
Ronald Cron5c522922020-11-14 16:35:34 +01005899 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005900
Ronald Cron5c522922020-11-14 16:35:34 +01005901 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005902}
5903
Gilles Peskinebe697d82019-05-16 18:00:41 +02005904psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005905 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005906 const uint8_t *peer_key,
5907 size_t peer_key_length,
5908 uint8_t *output,
5909 size_t output_size,
5910 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005911{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005912 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005913 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005914 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005915
5916 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5917 {
5918 status = PSA_ERROR_INVALID_ARGUMENT;
5919 goto exit;
5920 }
Ronald Cron5c522922020-11-14 16:35:34 +01005921 status = psa_get_and_lock_transparent_key_slot_with_policy(
5922 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005923 if( status != PSA_SUCCESS )
5924 goto exit;
5925
5926 status = psa_key_agreement_raw_internal( alg, slot,
5927 peer_key, peer_key_length,
5928 output, output_size,
5929 output_length );
5930
5931exit:
5932 if( status != PSA_SUCCESS )
5933 {
5934 /* If an error happens and is not handled properly, the output
5935 * may be used as a key to protect sensitive data. Arrange for such
5936 * a key to be random, which is likely to result in decryption or
5937 * verification errors. This is better than filling the buffer with
5938 * some constant data such as zeros, which would result in the data
5939 * being protected with a reproducible, easily knowable key.
5940 */
5941 psa_generate_random( output, output_size );
5942 *output_length = output_size;
5943 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005944
Ronald Cron5c522922020-11-14 16:35:34 +01005945 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005946
Ronald Cron5c522922020-11-14 16:35:34 +01005947 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005948}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005949
5950
Gilles Peskine30524eb2020-11-13 17:02:26 +01005951
Gilles Peskine86a440b2018-11-12 18:39:40 +01005952/****************************************************************/
5953/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005954/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005955
Gilles Peskine30524eb2020-11-13 17:02:26 +01005956/** Initialize the PSA random generator.
5957 */
5958static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5959{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005960#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5961 memset( rng, 0, sizeof( *rng ) );
5962#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5963
Gilles Peskine30524eb2020-11-13 17:02:26 +01005964 /* Set default configuration if
5965 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5966 if( rng->entropy_init == NULL )
5967 rng->entropy_init = mbedtls_entropy_init;
5968 if( rng->entropy_free == NULL )
5969 rng->entropy_free = mbedtls_entropy_free;
5970
5971 rng->entropy_init( &rng->entropy );
5972#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5973 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5974 /* The PSA entropy injection feature depends on using NV seed as an entropy
5975 * source. Add NV seed as an entropy source for PSA entropy injection. */
5976 mbedtls_entropy_add_source( &rng->entropy,
5977 mbedtls_nv_seed_poll, NULL,
5978 MBEDTLS_ENTROPY_BLOCK_SIZE,
5979 MBEDTLS_ENTROPY_SOURCE_STRONG );
5980#endif
5981
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005982 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005983#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005984}
5985
5986/** Deinitialize the PSA random generator.
5987 */
5988static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5989{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005990#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5991 memset( rng, 0, sizeof( *rng ) );
5992#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005993 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005994 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005995#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005996}
5997
5998/** Seed the PSA random generator.
5999 */
6000static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6001{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006002#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6003 /* Do nothing: the external RNG seeds itself. */
6004 (void) rng;
6005 return( PSA_SUCCESS );
6006#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006007 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006008 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6009 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006010 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006011#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006012}
6013
Gilles Peskinee59236f2018-01-27 23:32:46 +01006014psa_status_t psa_generate_random( uint8_t *output,
6015 size_t output_size )
6016{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006017 GUARD_MODULE_INITIALIZED;
6018
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006019#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6020
6021 size_t output_length = 0;
6022 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6023 output, output_size,
6024 &output_length );
6025 if( status != PSA_SUCCESS )
6026 return( status );
6027 /* Breaking up a request into smaller chunks is currently not supported
6028 * for the extrernal RNG interface. */
6029 if( output_length != output_size )
6030 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6031 return( PSA_SUCCESS );
6032
6033#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6034
Gilles Peskine71ddab92021-01-04 21:01:07 +01006035 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006036 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006037 size_t request_size =
6038 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6039 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6040 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006041 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6042 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006043 if( ret != 0 )
6044 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006045 output_size -= request_size;
6046 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006047 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006048 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006049#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006050}
6051
Gilles Peskine8814fc42020-12-14 15:33:44 +01006052/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006053 *
6054 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6055 * `psa_generate_random(...)`. The state parameter is ignored since the
6056 * PSA API doesn't support passing an explicit state.
6057 *
6058 * In the non-external case, psa_generate_random() calls an
6059 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6060 * and semantics as mbedtls_psa_get_random(). As an optimization,
6061 * instead of doing this back-and-forth between the PSA API and the
6062 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6063 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006064 */
6065#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6066int mbedtls_psa_get_random( void *p_rng,
6067 unsigned char *output,
6068 size_t output_size )
6069{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006070 /* This function takes a pointer to the RNG state because that's what
6071 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6072 * its own state internally and doesn't let the caller access that state.
6073 * So we just ignore the state parameter, and in practice we'll pass
6074 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006075 (void) p_rng;
6076 psa_status_t status = psa_generate_random( output, output_size );
6077 if( status == PSA_SUCCESS )
6078 return( 0 );
6079 else
6080 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6081}
6082#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6083
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006084#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6085#include "mbedtls/entropy_poll.h"
6086
Gilles Peskine7228da22019-07-15 11:06:15 +02006087psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006088 size_t seed_size )
6089{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006090 if( global_data.initialized )
6091 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006092
6093 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6094 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6095 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6096 return( PSA_ERROR_INVALID_ARGUMENT );
6097
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006098 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006099}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006100#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006101
Ronald Cron3772afe2021-02-08 16:10:05 +01006102/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006103 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006104 * \param type The key type
6105 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006106 *
6107 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006108 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006109 * \retval #PSA_ERROR_INVALID_ARGUMENT
6110 * The size in bits of the key is not valid.
6111 * \retval #PSA_ERROR_NOT_SUPPORTED
6112 * The type and/or the size in bits of the key or the combination of
6113 * the two is not supported.
6114 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006115static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6116 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006117{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006119
Gilles Peskinee59236f2018-01-27 23:32:46 +01006120 if( key_type_is_raw_bytes( type ) )
6121 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006122 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006123 if( status != PSA_SUCCESS )
6124 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006125 }
6126 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006127#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006128 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6129 {
6130 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6131 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006132
Ronald Cron01b2aba2020-10-05 09:42:02 +02006133 /* Accept only byte-aligned keys, for the same reasons as
6134 * in psa_import_rsa_key(). */
6135 if( bits % 8 != 0 )
6136 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006137 }
6138 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006139#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006140
Ronald Cron5c4d3862020-12-07 11:07:24 +01006141#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006142 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6143 {
Ronald Crond81ab562021-02-16 09:01:16 +01006144 /* To avoid empty block, return successfully here. */
6145 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006146 }
6147 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006148#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006149 {
6150 return( PSA_ERROR_NOT_SUPPORTED );
6151 }
6152
6153 return( PSA_SUCCESS );
6154}
6155
Ronald Cron55ed0592020-10-05 10:30:40 +02006156psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006157 const psa_key_attributes_t *attributes,
6158 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006159{
6160 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006161 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006162
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006163 if( ( attributes->domain_parameters == NULL ) &&
6164 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006165 return( PSA_ERROR_INVALID_ARGUMENT );
6166
Ronald Cron01b2aba2020-10-05 09:42:02 +02006167 if( key_type_is_raw_bytes( type ) )
6168 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006169 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006170 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006171 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006172
David Brown12ca5032021-02-11 11:02:00 -07006173#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006174 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006175 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006176#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006177 }
6178 else
6179
John Durkop07cc04a2020-11-16 22:08:34 -08006180#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006181 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006182 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006183 return( mbedtls_psa_rsa_generate_key( attributes,
6184 key_buffer,
6185 key_buffer_size,
6186 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187 }
6188 else
John Durkop07cc04a2020-11-16 22:08:34 -08006189#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006190
John Durkop9814fa22020-11-04 12:28:15 -08006191#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006192 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006193 {
Ronald Cron7023db52020-11-20 18:17:42 +01006194 return( mbedtls_psa_ecp_generate_key( attributes,
6195 key_buffer,
6196 key_buffer_size,
6197 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198 }
6199 else
John Durkop9814fa22020-11-04 12:28:15 -08006200#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006201 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006202 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006204 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006205
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006206 return( PSA_SUCCESS );
6207}
Darryl Green0c6575a2018-11-07 16:05:30 +00006208
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006209psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006210 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006211{
6212 psa_status_t status;
6213 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006214 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006215 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006216
Ronald Cron81709fc2020-11-14 12:10:32 +01006217 *key = MBEDTLS_SVC_KEY_ID_INIT;
6218
Gilles Peskine0f84d622019-09-12 19:03:13 +02006219 /* Reject any attempt to create a zero-length key so that we don't
6220 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6221 if( psa_get_key_bits( attributes ) == 0 )
6222 return( PSA_ERROR_INVALID_ARGUMENT );
6223
Ronald Cron81709fc2020-11-14 12:10:32 +01006224 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6225 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006226 if( status != PSA_SUCCESS )
6227 goto exit;
6228
Ronald Cron977c2472020-10-13 08:32:21 +02006229 /* In the case of a transparent key or an opaque key stored in local
6230 * storage (thus not in the case of generating a key in a secure element
6231 * or cryptoprocessor with storage), we have to allocate a buffer to
6232 * hold the generated key material. */
6233 if( slot->key.data == NULL )
6234 {
6235 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6236 PSA_KEY_LOCATION_LOCAL_STORAGE )
6237 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006238 status = psa_validate_key_type_and_size_for_key_generation(
6239 attributes->core.type, attributes->core.bits );
6240 if( status != PSA_SUCCESS )
6241 goto exit;
6242
6243 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6244 attributes->core.type,
6245 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006246 }
6247 else
6248 {
6249 status = psa_driver_wrapper_get_key_buffer_size(
6250 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006251 if( status != PSA_SUCCESS )
6252 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006253 }
Ronald Cron977c2472020-10-13 08:32:21 +02006254
6255 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6256 if( status != PSA_SUCCESS )
6257 goto exit;
6258 }
6259
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006260 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006261 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006262
Ronald Cron2b56bc82020-10-05 10:02:26 +02006263 if( status != PSA_SUCCESS )
6264 psa_remove_key_data_from_memory( slot );
6265
Gilles Peskine11792082019-08-06 18:36:36 +02006266exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006267 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006268 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006269 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006270 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006271
Darryl Green0c6575a2018-11-07 16:05:30 +00006272 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273}
6274
Gilles Peskinee59236f2018-01-27 23:32:46 +01006275/****************************************************************/
6276/* Module setup */
6277/****************************************************************/
6278
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006279#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006280psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6281 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6282 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6283{
6284 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6285 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006286 global_data.rng.entropy_init = entropy_init;
6287 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006288 return( PSA_SUCCESS );
6289}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006290#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006291
Gilles Peskinee59236f2018-01-27 23:32:46 +01006292void mbedtls_psa_crypto_free( void )
6293{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006294 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006295 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6296 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006297 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006298 }
6299 /* Wipe all remaining data, including configuration.
6300 * In particular, this sets all state indicator to the value
6301 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006302 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006303#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006304 /* Unregister all secure element drivers, so that we restart from
6305 * a pristine state. */
6306 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006307#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006308}
6309
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006310#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6311/** Recover a transaction that was interrupted by a power failure.
6312 *
6313 * This function is called during initialization, before psa_crypto_init()
6314 * returns. If this function returns a failure status, the initialization
6315 * fails.
6316 */
6317static psa_status_t psa_crypto_recover_transaction(
6318 const psa_crypto_transaction_t *transaction )
6319{
6320 switch( transaction->unknown.type )
6321 {
6322 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6323 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006324 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006325 * is implemented.
6326 * https://github.com/ARMmbed/mbed-crypto/issues/218
6327 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006328 default:
6329 /* We found an unsupported transaction in the storage.
6330 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006331 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006332 }
6333}
6334#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6335
Gilles Peskinee59236f2018-01-27 23:32:46 +01006336psa_status_t psa_crypto_init( void )
6337{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006338 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006339
Gilles Peskinec6b69072018-11-20 21:42:52 +01006340 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006341 if( global_data.initialized != 0 )
6342 return( PSA_SUCCESS );
6343
Gilles Peskine30524eb2020-11-13 17:02:26 +01006344 /* Initialize and seed the random generator. */
6345 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006346 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006347 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006348 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006349 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006350 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351
Gilles Peskine66fb1262018-12-10 16:29:04 +01006352 status = psa_initialize_key_slots( );
6353 if( status != PSA_SUCCESS )
6354 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006355
Gilles Peskined9348f22019-10-01 15:22:29 +02006356#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6357 status = psa_init_all_se_drivers( );
6358 if( status != PSA_SUCCESS )
6359 goto exit;
6360#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6361
Gilles Peskine4b734222019-07-24 15:56:31 +02006362#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006363 status = psa_crypto_load_transaction( );
6364 if( status == PSA_SUCCESS )
6365 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006366 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6367 if( status != PSA_SUCCESS )
6368 goto exit;
6369 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006370 }
6371 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6372 {
6373 /* There's no transaction to complete. It's all good. */
6374 status = PSA_SUCCESS;
6375 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006376#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006377
Gilles Peskinec6b69072018-11-20 21:42:52 +01006378 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006379 global_data.initialized = 1;
6380
6381exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006382 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006384 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006385}
6386
6387#endif /* MBEDTLS_PSA_CRYPTO_C */