blob: 4f67ddf9c655b9e16e50da9e9ab0026788f35a6a [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
Gilles Peskine8e338702019-07-30 20:06:31 +0200893 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100894 {
895 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200896 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100897 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100898
899 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 if( alg != 0 )
901 {
902 status = psa_key_policy_permits( &slot->attr.policy, alg );
903 if( status != PSA_SUCCESS )
904 goto error;
905 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100906
Darryl Green06fd18d2018-07-16 11:21:11 +0100907 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200908
909error:
910 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +0100911 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200912
913 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100914}
Darryl Green940d72c2018-07-13 13:18:51 +0100915
Ronald Cron5c522922020-11-14 16:35:34 +0100916/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200917 *
918 * A transparent key is a key for which the key material is directly
919 * available, as opposed to a key in a secure element.
920 *
Ronald Cron5c522922020-11-14 16:35:34 +0100921 * This is a temporary function to use instead of
922 * psa_get_and_lock_key_slot_with_policy() until secure element support is
923 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200924 *
Ronald Cron5c522922020-11-14 16:35:34 +0100925 * On success, the returned key slot is locked. It is the responsibility of the
926 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927 */
928#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +0100929static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
930 mbedtls_svc_key_id_t key,
931 psa_key_slot_t **p_slot,
932 psa_key_usage_t usage,
933 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200934{
Ronald Cron5c522922020-11-14 16:35:34 +0100935 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
936 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200937 if( status != PSA_SUCCESS )
938 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200939
Gilles Peskineadad8132019-07-25 11:31:23 +0200940 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200941 {
Ronald Cron5c522922020-11-14 16:35:34 +0100942 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +0200943 *p_slot = NULL;
944 return( PSA_ERROR_NOT_SUPPORTED );
945 }
Ronald Cronf95a2b12020-10-22 15:24:49 +0200946
Gilles Peskine28f8f302019-07-24 13:30:31 +0200947 return( PSA_SUCCESS );
948}
949#else /* MBEDTLS_PSA_CRYPTO_SE_C */
950/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +0100951#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
952 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200953#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
954
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100955/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100956static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000957{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100958 /* Data pointer will always be either a valid pointer or NULL in an
959 * initialized slot, so we can just free it. */
960 if( slot->key.data != NULL )
961 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
962
963 mbedtls_free( slot->key.data );
964 slot->key.data = NULL;
965 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +0000966
967 return( PSA_SUCCESS );
968}
969
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100970/** Completely wipe a slot in memory, including its policy.
971 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100972psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100973{
974 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +0100975
976 /*
977 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +0100978 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +0100979 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
980 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +0100981 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +0100982 */
Ronald Cron5c522922020-11-14 16:35:34 +0100983 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +0100984 {
985#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +0100986 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +0100987#endif
Ronald Cronddd3d052020-10-30 14:07:07 +0100988 status = PSA_ERROR_CORRUPTION_DETECTED;
989 }
990
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200991 /* Multipart operations may still be using the key. This is safe
992 * because all multipart operation objects are independent from
993 * the key slot: if they need to access the key after the setup
994 * phase, they have a copy of the key. Note that this means that
995 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100996 /* At this point, key material and other type-specific content has
997 * been wiped. Clear remaining metadata. We can call memset and not
998 * zeroize because the metadata is not particularly sensitive. */
999 memset( slot, 0, sizeof( *slot ) );
1000 return( status );
1001}
1002
Ronald Croncf56a0a2020-08-04 09:51:30 +02001003psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001004{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001005 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001006 psa_status_t status; /* status of the last operation */
1007 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001008#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1009 psa_se_drv_table_entry_t *driver;
1010#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001011
Ronald Croncf56a0a2020-08-04 09:51:30 +02001012 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001013 return( PSA_SUCCESS );
1014
Ronald Cronf2911112020-10-29 17:51:10 +01001015 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001016 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001017 * key, this will load the key description from persistent memory if not
1018 * done yet. We cannot avoid this loading as without it we don't know if
1019 * the key is operated by an SE or not and this information is needed by
1020 * the current implementation.
1021 */
Ronald Cron5c522922020-11-14 16:35:34 +01001022 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001023 if( status != PSA_SUCCESS )
1024 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001025
Ronald Cronf2911112020-10-29 17:51:10 +01001026 /*
1027 * If the key slot containing the key description is under access by the
1028 * library (apart from the present access), the key cannot be destroyed
1029 * yet. For the time being, just return in error. Eventually (to be
1030 * implemented), the key should be destroyed when all accesses have
1031 * stopped.
1032 */
Ronald Cron5c522922020-11-14 16:35:34 +01001033 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001034 {
Ronald Cron5c522922020-11-14 16:35:34 +01001035 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001036 return( PSA_ERROR_GENERIC_ERROR );
1037 }
1038
Gilles Peskine354f7672019-07-12 23:46:38 +02001039#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001040 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001041 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001042 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001043 /* For a key in a secure element, we need to do three things:
1044 * remove the key file in internal storage, destroy the
1045 * key inside the secure element, and update the driver's
1046 * persistent data. Start a transaction that will encompass these
1047 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001048 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001049 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001050 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001051 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001052 status = psa_crypto_save_transaction( );
1053 if( status != PSA_SUCCESS )
1054 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001055 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 /* We should still try to destroy the key in the secure
1057 * element and the key metadata in storage. This is especially
1058 * important if the error is that the storage is full.
1059 * But how to do it exactly without risking an inconsistent
1060 * state after a reset?
1061 * https://github.com/ARMmbed/mbed-crypto/issues/215
1062 */
1063 overall_status = status;
1064 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001065 }
1066
Ronald Cronea0f8a62020-11-25 17:52:23 +01001067 status = psa_destroy_se_key( driver,
1068 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001069 if( overall_status == PSA_SUCCESS )
1070 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001071 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001072#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1073
Darryl Greend49a4992018-06-18 17:27:26 +01001074#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001075 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001076 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001077 status = psa_destroy_persistent_key( slot->attr.id );
1078 if( overall_status == PSA_SUCCESS )
1079 overall_status = status;
1080
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001081 /* TODO: other slots may have a copy of the same key. We should
1082 * invalidate them.
1083 * https://github.com/ARMmbed/mbed-crypto/issues/214
1084 */
Darryl Greend49a4992018-06-18 17:27:26 +01001085 }
1086#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001087
Gilles Peskinefc762652019-07-22 19:30:34 +02001088#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1089 if( driver != NULL )
1090 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001091 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094 status = psa_crypto_stop_transaction( );
1095 if( overall_status == PSA_SUCCESS )
1096 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001097 }
1098#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1099
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001100#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1101exit:
1102#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001103 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001104 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1105 if( overall_status == PSA_SUCCESS )
1106 overall_status = status;
1107 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001108}
1109
John Durkop07cc04a2020-11-16 22:08:34 -08001110#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001111 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001112static psa_status_t psa_get_rsa_public_exponent(
1113 const mbedtls_rsa_context *rsa,
1114 psa_key_attributes_t *attributes )
1115{
1116 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001117 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001118 uint8_t *buffer = NULL;
1119 size_t buflen;
1120 mbedtls_mpi_init( &mpi );
1121
1122 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1123 if( ret != 0 )
1124 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001125 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1126 {
1127 /* It's the default value, which is reported as an empty string,
1128 * so there's nothing to do. */
1129 goto exit;
1130 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001131
1132 buflen = mbedtls_mpi_size( &mpi );
1133 buffer = mbedtls_calloc( 1, buflen );
1134 if( buffer == NULL )
1135 {
1136 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1137 goto exit;
1138 }
1139 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1140 if( ret != 0 )
1141 goto exit;
1142 attributes->domain_parameters = buffer;
1143 attributes->domain_parameters_size = buflen;
1144
1145exit:
1146 mbedtls_mpi_free( &mpi );
1147 if( ret != 0 )
1148 mbedtls_free( buffer );
1149 return( mbedtls_to_psa_error( ret ) );
1150}
John Durkop07cc04a2020-11-16 22:08:34 -08001151#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001152 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001153
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001154/** Retrieve all the publicly-accessible attributes of a key.
1155 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001156psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001157 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001158{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001159 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001160 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001161 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001162
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001163 psa_reset_key_attributes( attributes );
1164
Ronald Cron5c522922020-11-14 16:35:34 +01001165 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001166 if( status != PSA_SUCCESS )
1167 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001168
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001169 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001170 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1171 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1172
1173#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1174 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001175 psa_set_key_slot_number( attributes,
1176 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001177#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001178
Gilles Peskine8e338702019-07-30 20:06:31 +02001179 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001180 {
John Durkop07cc04a2020-11-16 22:08:34 -08001181#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001182 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001183 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001184 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001185#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001186 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001187 * is not yet implemented.
1188 * https://github.com/ARMmbed/mbed-crypto/issues/216
1189 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001190 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001191 break;
1192#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001193 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001194 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001195
Ronald Cron90857082020-11-25 14:58:33 +01001196 status = mbedtls_psa_rsa_load_representation(
1197 slot->attr.type,
1198 slot->key.data,
1199 slot->key.bytes,
1200 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001201 if( status != PSA_SUCCESS )
1202 break;
1203
Steven Cooremana2371e52020-07-28 14:30:39 +02001204 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001205 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001206 mbedtls_rsa_free( rsa );
1207 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001208 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001209 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001210#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001211 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 default:
1213 /* Nothing else to do. */
1214 break;
1215 }
1216
1217 if( status != PSA_SUCCESS )
1218 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001219
Ronald Cron5c522922020-11-14 16:35:34 +01001220 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001221
Ronald Cron5c522922020-11-14 16:35:34 +01001222 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001223}
1224
Gilles Peskinec8000c02019-08-02 20:15:51 +02001225#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1226psa_status_t psa_get_key_slot_number(
1227 const psa_key_attributes_t *attributes,
1228 psa_key_slot_number_t *slot_number )
1229{
1230 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1231 {
1232 *slot_number = attributes->slot_number;
1233 return( PSA_SUCCESS );
1234 }
1235 else
1236 return( PSA_ERROR_INVALID_ARGUMENT );
1237}
1238#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1239
Ronald Crond18b5f82020-11-25 16:46:05 +01001240static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1241 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001242 uint8_t *data,
1243 size_t data_size,
1244 size_t *data_length )
1245{
Ronald Crond18b5f82020-11-25 16:46:05 +01001246 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001247 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001248 memcpy( data, key_buffer, key_buffer_size );
1249 memset( data + key_buffer_size, 0,
1250 data_size - key_buffer_size );
1251 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001252 return( PSA_SUCCESS );
1253}
1254
Ronald Cron7285cda2020-11-26 14:46:37 +01001255psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001256 const psa_key_attributes_t *attributes,
1257 const uint8_t *key_buffer, size_t key_buffer_size,
1258 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001259{
Ronald Crond18b5f82020-11-25 16:46:05 +01001260 psa_key_type_t type = attributes->core.type;
1261
Ronald Crond18b5f82020-11-25 16:46:05 +01001262 if( key_type_is_raw_bytes( type ) ||
1263 PSA_KEY_TYPE_IS_RSA( type ) ||
1264 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001265 {
1266 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001267 key_buffer, key_buffer_size,
1268 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001269 }
1270 else
1271 {
1272 /* This shouldn't happen in the reference implementation, but
1273 it is valid for a special-purpose implementation to omit
1274 support for exporting certain key types. */
1275 return( PSA_ERROR_NOT_SUPPORTED );
1276 }
1277}
1278
1279psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1280 uint8_t *data,
1281 size_t data_size,
1282 size_t *data_length )
1283{
1284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1285 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1286 psa_key_slot_t *slot;
1287
Ronald Crone907e552021-01-18 13:22:38 +01001288 /* Reject a zero-length output buffer now, since this can never be a
1289 * valid key representation. This way we know that data must be a valid
1290 * pointer and we can do things like memset(data, ..., data_size). */
1291 if( data_size == 0 )
1292 return( PSA_ERROR_BUFFER_TOO_SMALL );
1293
Ronald Cron9486f9d2020-11-26 13:36:23 +01001294 /* Set the key to empty now, so that even when there are errors, we always
1295 * set data_length to a value between 0 and data_size. On error, setting
1296 * the key to empty is a good choice because an empty key representation is
1297 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001298 *data_length = 0;
1299
Ronald Cron9486f9d2020-11-26 13:36:23 +01001300 /* Export requires the EXPORT flag. There is an exception for public keys,
1301 * which don't require any flag, but
1302 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1303 */
1304 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1305 PSA_KEY_USAGE_EXPORT, 0 );
1306 if( status != PSA_SUCCESS )
1307 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001308
Ronald Crond18b5f82020-11-25 16:46:05 +01001309 psa_key_attributes_t attributes = {
1310 .core = slot->attr
1311 };
Ronald Cron67227982020-11-26 15:16:05 +01001312 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001313 slot->key.data, slot->key.bytes,
1314 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001315
Ronald Cron9486f9d2020-11-26 13:36:23 +01001316 unlock_status = psa_unlock_key_slot( slot );
1317
1318 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1319}
1320
Ronald Cron7285cda2020-11-26 14:46:37 +01001321psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001322 const psa_key_attributes_t *attributes,
1323 const uint8_t *key_buffer,
1324 size_t key_buffer_size,
1325 uint8_t *data,
1326 size_t data_size,
1327 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001328{
Ronald Crond18b5f82020-11-25 16:46:05 +01001329 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001330
Ronald Crond18b5f82020-11-25 16:46:05 +01001331 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001332 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001333 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001334 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001335 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001336 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001337 key_buffer, key_buffer_size,
1338 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001339 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001340
Ronald Crond18b5f82020-11-25 16:46:05 +01001341 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001342 {
John Durkop0e005192020-10-31 22:06:54 -07001343#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1344 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001345 return( mbedtls_psa_rsa_export_public_key( attributes,
1346 key_buffer,
1347 key_buffer_size,
1348 data,
1349 data_size,
1350 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001351#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001352 /* We don't know how to convert a private RSA key to public. */
1353 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001354#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1355 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001356 }
1357 else
Moran Pekera998bc62018-04-16 18:16:20 +03001358 {
John Durkop6ba40d12020-11-10 08:50:04 -08001359#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1360 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001361 return( mbedtls_psa_ecp_export_public_key( attributes,
1362 key_buffer,
1363 key_buffer_size,
1364 data,
1365 data_size,
1366 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001367#else
1368 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001369 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001370#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1371 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001372 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001373 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001374 else
1375 {
1376 /* This shouldn't happen in the reference implementation, but
1377 it is valid for a special-purpose implementation to omit
1378 support for exporting certain key types. */
1379 return( PSA_ERROR_NOT_SUPPORTED );
1380 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381}
Ronald Crond18b5f82020-11-25 16:46:05 +01001382
Ronald Croncf56a0a2020-08-04 09:51:30 +02001383psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001384 uint8_t *data,
1385 size_t data_size,
1386 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001387{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001388 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001389 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001390 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001391
Ronald Crone907e552021-01-18 13:22:38 +01001392 /* Reject a zero-length output buffer now, since this can never be a
1393 * valid key representation. This way we know that data must be a valid
1394 * pointer and we can do things like memset(data, ..., data_size). */
1395 if( data_size == 0 )
1396 return( PSA_ERROR_BUFFER_TOO_SMALL );
1397
Darryl Greendd8fb772018-11-07 16:00:44 +00001398 /* Set the key to empty now, so that even when there are errors, we always
1399 * set data_length to a value between 0 and data_size. On error, setting
1400 * the key to empty is a good choice because an empty key representation is
1401 * unlikely to be accepted anywhere. */
1402 *data_length = 0;
1403
1404 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001405 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001406 if( status != PSA_SUCCESS )
1407 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001408
Ronald Cron9486f9d2020-11-26 13:36:23 +01001409 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1410 {
1411 status = PSA_ERROR_INVALID_ARGUMENT;
1412 goto exit;
1413 }
1414
Ronald Crond18b5f82020-11-25 16:46:05 +01001415 psa_key_attributes_t attributes = {
1416 .core = slot->attr
1417 };
Ronald Cron67227982020-11-26 15:16:05 +01001418 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001419 &attributes, slot->key.data, slot->key.bytes,
1420 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001421
1422exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001423 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001424
Ronald Cron5c522922020-11-14 16:35:34 +01001425 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001426}
1427
Gilles Peskine91e8c332019-08-02 19:19:39 +02001428#if defined(static_assert)
1429static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1430 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001431static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001432 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001433static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001434 "One or more key attribute flag is listed as both internal-only and external-only" );
1435#endif
1436
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001437/** Validate that a key policy is internally well-formed.
1438 *
1439 * This function only rejects invalid policies. It does not validate the
1440 * consistency of the policy with respect to other attributes of the key
1441 * such as the key type.
1442 */
1443static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001444{
1445 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001446 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001447 PSA_KEY_USAGE_ENCRYPT |
1448 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001449 PSA_KEY_USAGE_SIGN_HASH |
1450 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001451 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1452 return( PSA_ERROR_INVALID_ARGUMENT );
1453
Gilles Peskine4747d192019-04-17 15:05:45 +02001454 return( PSA_SUCCESS );
1455}
1456
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001457/** Validate the internal consistency of key attributes.
1458 *
1459 * This function only rejects invalid attribute values. If does not
1460 * validate the consistency of the attributes with any key data that may
1461 * be involved in the creation of the key.
1462 *
1463 * Call this function early in the key creation process.
1464 *
1465 * \param[in] attributes Key attributes for the new key.
1466 * \param[out] p_drv On any return, the driver for the key, if any.
1467 * NULL for a transparent key.
1468 *
1469 */
1470static psa_status_t psa_validate_key_attributes(
1471 const psa_key_attributes_t *attributes,
1472 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001473{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001474 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001475 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001476 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001477
Ronald Cron54b90082020-10-29 15:26:43 +01001478 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001479 if( status != PSA_SUCCESS )
1480 return( status );
1481
Ronald Crond2ed4812020-07-17 16:11:30 +02001482 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001483 if( status != PSA_SUCCESS )
1484 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485
Ronald Cron65f38a32020-10-23 17:11:13 +02001486 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1487 {
1488 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1489 return( PSA_ERROR_INVALID_ARGUMENT );
1490 }
1491 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001492 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001493 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001494 if( status != PSA_SUCCESS )
1495 return( status );
1496 }
1497
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001498 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001499 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001500 return( status );
1501
1502 /* Refuse to create overly large keys.
1503 * Note that this doesn't trigger on import if the attributes don't
1504 * explicitly specify a size (so psa_get_key_bits returns 0), so
1505 * psa_import_key() needs its own checks. */
1506 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1507 return( PSA_ERROR_NOT_SUPPORTED );
1508
Gilles Peskine91e8c332019-08-02 19:19:39 +02001509 /* Reject invalid flags. These should not be reachable through the API. */
1510 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1511 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1512 return( PSA_ERROR_INVALID_ARGUMENT );
1513
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001514 return( PSA_SUCCESS );
1515}
1516
Gilles Peskine4747d192019-04-17 15:05:45 +02001517/** Prepare a key slot to receive key material.
1518 *
1519 * This function allocates a key slot and sets its metadata.
1520 *
1521 * If this function fails, call psa_fail_key_creation().
1522 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001523 * This function is intended to be used as follows:
1524 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001525 * it with the specified attributes, and in case of a volatile key assign it
1526 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001527 * -# Populate the slot with the key material.
1528 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1529 * In case of failure at any step, stop the sequence and call
1530 * psa_fail_key_creation().
1531 *
Ronald Cron5c522922020-11-14 16:35:34 +01001532 * On success, the key slot is locked. It is the responsibility of the caller
1533 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001534 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001535 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001536 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001537 * \param[out] p_slot On success, a pointer to the prepared slot.
1538 * \param[out] p_drv On any return, the driver for the key, if any.
1539 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001540 *
1541 * \retval #PSA_SUCCESS
1542 * The key slot is ready to receive key material.
1543 * \return If this function fails, the key slot is an invalid state.
1544 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001545 */
1546static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001548 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001549 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001550 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001551{
1552 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001553 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 psa_key_slot_t *slot;
1555
Gilles Peskinedf179142019-07-15 22:02:14 +02001556 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001557 *p_drv = NULL;
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559 status = psa_validate_key_attributes( attributes, p_drv );
1560 if( status != PSA_SUCCESS )
1561 return( status );
1562
Ronald Cronc4d1b512020-07-31 11:26:37 +02001563 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001564 if( status != PSA_SUCCESS )
1565 return( status );
1566 slot = *p_slot;
1567
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001568 /* We're storing the declared bit-size of the key. It's up to each
1569 * creation mechanism to verify that this information is correct.
1570 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001571 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001572 * is optional (import, copy). In case of a volatile key, assign it the
1573 * volatile key identifier associated to the slot returned to contain its
1574 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001575
1576 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001577 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1578 {
1579#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1580 slot->attr.id = volatile_key_id;
1581#else
1582 slot->attr.id.key_id = volatile_key_id;
1583#endif
1584 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001585
Gilles Peskine91e8c332019-08-02 19:19:39 +02001586 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001587 * external-only flags, query `attributes`. Thanks to the check
1588 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001589 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001590 * may have set. */
1591 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001592
Gilles Peskinecbaff462019-07-12 23:46:04 +02001593#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001594 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001595 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001596 * create the key file in internal storage, create the
1597 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001598 * persistent data. This is done by starting a transaction that will
1599 * encompass these three actions.
1600 * For registering a volatile key, we just need to find an appropriate
1601 * slot number inside the SE. Since the key is designated volatile, creating
1602 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001603 /* The first thing to do is to find a slot number for the new key.
1604 * We save the slot number in persistent storage as part of the
1605 * transaction data. It will be needed to recover if the power
1606 * fails during the key creation process, to clean up on the secure
1607 * element side after restarting. Obtaining a slot number from the
1608 * secure element driver updates its persistent state, but we do not yet
1609 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001610 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001611 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001612 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001613 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001614 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001615 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001616 if( status != PSA_SUCCESS )
1617 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001618
1619 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001620 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001621 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1622 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001623 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001624 psa_crypto_transaction.key.id = slot->attr.id;
1625 status = psa_crypto_save_transaction( );
1626 if( status != PSA_SUCCESS )
1627 {
1628 (void) psa_crypto_stop_transaction( );
1629 return( status );
1630 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001631 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001632
1633 status = psa_copy_key_material_into_slot(
1634 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001635 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001636
1637 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1638 {
1639 /* Key registration only makes sense with a secure element. */
1640 return( PSA_ERROR_INVALID_ARGUMENT );
1641 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001642#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1643
Ronald Cronc4d1b512020-07-31 11:26:37 +02001644 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001645}
Gilles Peskine4747d192019-04-17 15:05:45 +02001646
1647/** Finalize the creation of a key once its key material has been set.
1648 *
1649 * This entails writing the key to persistent storage.
1650 *
1651 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001652 * See the documentation of psa_start_key_creation() for the intended use
1653 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 *
Ronald Cron5c522922020-11-14 16:35:34 +01001655 * If the finalization succeeds, the function unlocks the key slot (it was
1656 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1657 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001658 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 * \param[in,out] slot Pointer to the slot with key material.
1660 * \param[in] driver The secure element driver for the key,
1661 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001662 * \param[out] key On success, identifier of the key. Note that the
1663 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001664 *
1665 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001666 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001667 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1668 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1669 * \retval #PSA_ERROR_ALREADY_EXISTS
1670 * \retval #PSA_ERROR_DATA_INVALID
1671 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001672 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001673 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001674 * \return If this function fails, the key slot is an invalid state.
1675 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001676 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001677static psa_status_t psa_finish_key_creation(
1678 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001679 psa_se_drv_table_entry_t *driver,
1680 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001681{
1682 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001683 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001684 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001685
1686#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001687 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001689#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1690 if( driver != NULL )
1691 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001692 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001693 psa_key_slot_number_t slot_number =
1694 psa_key_slot_get_slot_number( slot ) ;
1695
Gilles Peskineb46bef22019-07-30 21:32:04 +02001696#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001697 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001698 sizeof( data.slot_number ),
1699 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001700#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001701 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001702 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 (uint8_t*) &data,
1704 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001705 }
1706 else
1707#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1708 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001709 /* Key material is saved in export representation in the slot, so
1710 * just pass the slot buffer for storage. */
1711 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001712 slot->key.data,
1713 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001714 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001715 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001716#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1717
Gilles Peskinecbaff462019-07-12 23:46:04 +02001718#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001719 /* Finish the transaction for a key creation. This does not
1720 * happen when registering an existing key. Detect this case
1721 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 * creation of a persistent key in a secure element requires a transaction,
1723 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001724 if( driver != NULL &&
1725 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001726 {
1727 status = psa_save_se_persistent_data( driver );
1728 if( status != PSA_SUCCESS )
1729 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001730 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001731 return( status );
1732 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001733 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001734 }
1735#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1736
Ronald Cron50972942020-11-14 11:28:25 +01001737 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001738 {
1739 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001740 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001741 if( status != PSA_SUCCESS )
1742 *key = MBEDTLS_SVC_KEY_ID_INIT;
1743 }
Ronald Cron50972942020-11-14 11:28:25 +01001744
Gilles Peskine4747d192019-04-17 15:05:45 +02001745 return( status );
1746}
1747
1748/** Abort the creation of a key.
1749 *
1750 * You may call this function after calling psa_start_key_creation(),
1751 * or after psa_finish_key_creation() fails. In other circumstances, this
1752 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001753 * See the documentation of psa_start_key_creation() for the intended use
1754 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001755 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001756 * \param[in,out] slot Pointer to the slot with key material.
1757 * \param[in] driver The secure element driver for the key,
1758 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001759 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001760static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001761 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001762{
Gilles Peskine011e4282019-06-26 18:34:38 +02001763 (void) driver;
1764
Gilles Peskine4747d192019-04-17 15:05:45 +02001765 if( slot == NULL )
1766 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001767
1768#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001769 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001770 * element, and the failure happened later (when saving metadata
1771 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001772 * element.
1773 * https://github.com/ARMmbed/mbed-crypto/issues/217
1774 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001775
Gilles Peskined7729582019-08-05 15:55:54 +02001776 /* Abort the ongoing transaction if any (there may not be one if
1777 * the creation process failed before starting one, or if the
1778 * key creation is a registration of a key in a secure element).
1779 * Earlier functions must already have done what it takes to undo any
1780 * partial creation. All that's left is to update the transaction data
1781 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001782 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001783#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1784
Gilles Peskine4747d192019-04-17 15:05:45 +02001785 psa_wipe_key_slot( slot );
1786}
1787
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001788/** Validate optional attributes during key creation.
1789 *
1790 * Some key attributes are optional during key creation. If they are
1791 * specified in the attributes structure, check that they are consistent
1792 * with the data in the slot.
1793 *
1794 * This function should be called near the end of key creation, after
1795 * the slot in memory is fully populated but before saving persistent data.
1796 */
1797static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001798 const psa_key_slot_t *slot,
1799 const psa_key_attributes_t *attributes )
1800{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001801 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001802 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001803 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001804 return( PSA_ERROR_INVALID_ARGUMENT );
1805 }
1806
1807 if( attributes->domain_parameters_size != 0 )
1808 {
John Durkop0e005192020-10-31 22:06:54 -07001809#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1810 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001811 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001813 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001814 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001815 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001816
Ronald Cron90857082020-11-25 14:58:33 +01001817 psa_status_t status = mbedtls_psa_rsa_load_representation(
1818 slot->attr.type,
1819 slot->key.data,
1820 slot->key.bytes,
1821 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001822 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001823 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001824
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001825 mbedtls_mpi_init( &actual );
1826 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001827 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001829 mbedtls_rsa_free( rsa );
1830 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001831 if( ret != 0 )
1832 goto rsa_exit;
1833 ret = mbedtls_mpi_read_binary( &required,
1834 attributes->domain_parameters,
1835 attributes->domain_parameters_size );
1836 if( ret != 0 )
1837 goto rsa_exit;
1838 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1839 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1840 rsa_exit:
1841 mbedtls_mpi_free( &actual );
1842 mbedtls_mpi_free( &required );
1843 if( ret != 0)
1844 return( mbedtls_to_psa_error( ret ) );
1845 }
1846 else
John Durkop9814fa22020-11-04 12:28:15 -08001847#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1848 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849 {
1850 return( PSA_ERROR_INVALID_ARGUMENT );
1851 }
1852 }
1853
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001854 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001855 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001856 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001857 return( PSA_ERROR_INVALID_ARGUMENT );
1858 }
1859
1860 return( PSA_SUCCESS );
1861}
1862
Gilles Peskine4747d192019-04-17 15:05:45 +02001863psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001864 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001865 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001866 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001867{
1868 psa_status_t status;
1869 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001870 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001871 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001872
Ronald Cron81709fc2020-11-14 12:10:32 +01001873 *key = MBEDTLS_SVC_KEY_ID_INIT;
1874
Gilles Peskine0f84d622019-09-12 19:03:13 +02001875 /* Reject zero-length symmetric keys (including raw data key objects).
1876 * This also rejects any key which might be encoded as an empty string,
1877 * which is never valid. */
1878 if( data_length == 0 )
1879 return( PSA_ERROR_INVALID_ARGUMENT );
1880
Gilles Peskinedf179142019-07-15 22:02:14 +02001881 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001882 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001883 if( status != PSA_SUCCESS )
1884 goto exit;
1885
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001886 /* In the case of a transparent key or an opaque key stored in local
1887 * storage (thus not in the case of generating a key in a secure element
1888 * or cryptoprocessor with storage), we have to allocate a buffer to
1889 * hold the generated key material. */
1890 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001891 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001892 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001893 if( status != PSA_SUCCESS )
1894 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001895 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001896
1897 bits = slot->attr.bits;
1898 status = psa_driver_wrapper_import_key( attributes,
1899 data, data_length,
1900 slot->key.data,
1901 slot->key.bytes,
1902 &slot->key.bytes, &bits );
1903 if( status != PSA_SUCCESS )
1904 goto exit;
1905
1906 if( slot->attr.bits == 0 )
1907 slot->attr.bits = (psa_key_bits_t) bits;
1908 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01001909 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01001910 status = PSA_ERROR_INVALID_ARGUMENT;
1911 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001912 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01001913
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001914 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001915 if( status != PSA_SUCCESS )
1916 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001917
Ronald Cron81709fc2020-11-14 12:10:32 +01001918 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001920 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02001921 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001922
Gilles Peskine4747d192019-04-17 15:05:45 +02001923 return( status );
1924}
1925
Gilles Peskined7729582019-08-05 15:55:54 +02001926#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1927psa_status_t mbedtls_psa_register_se_key(
1928 const psa_key_attributes_t *attributes )
1929{
1930 psa_status_t status;
1931 psa_key_slot_t *slot = NULL;
1932 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02001933 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02001934
1935 /* Leaving attributes unspecified is not currently supported.
1936 * It could make sense to query the key type and size from the
1937 * secure element, but not all secure elements support this
1938 * and the driver HAL doesn't currently support it. */
1939 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1940 return( PSA_ERROR_NOT_SUPPORTED );
1941 if( psa_get_key_bits( attributes ) == 0 )
1942 return( PSA_ERROR_NOT_SUPPORTED );
1943
1944 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001945 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02001946 if( status != PSA_SUCCESS )
1947 goto exit;
1948
Ronald Cron81709fc2020-11-14 12:10:32 +01001949 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02001950
1951exit:
1952 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02001953 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001954
Gilles Peskined7729582019-08-05 15:55:54 +02001955 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001956 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02001957 return( status );
1958}
1959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1960
Gilles Peskinef603c712019-01-19 13:40:11 +01001961static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001963{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001964 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001965 source->key.data,
1966 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02001968 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01001969
Steven Cooreman398aee52020-10-13 14:35:45 +02001970 target->attr.type = source->attr.type;
1971 target->attr.bits = source->attr.bits;
1972
1973 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01001974}
1975
Ronald Croncf56a0a2020-08-04 09:51:30 +02001976psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001977 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001978 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01001979{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001980 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001981 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01001982 psa_key_slot_t *source_slot = NULL;
1983 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001985 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001986
Ronald Cron81709fc2020-11-14 12:10:32 +01001987 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
1988
Ronald Cron5c522922020-11-14 16:35:34 +01001989 status = psa_get_and_lock_transparent_key_slot_with_policy(
1990 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001991 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001992 goto exit;
1993
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001994 status = psa_validate_optional_attributes( source_slot,
1995 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996 if( status != PSA_SUCCESS )
1997 goto exit;
1998
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001999 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002000 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 if( status != PSA_SUCCESS )
2002 goto exit;
2003
Ronald Cron81709fc2020-11-14 12:10:32 +01002004 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2005 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006 if( status != PSA_SUCCESS )
2007 goto exit;
2008
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002009#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2010 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002011 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002012 /* Copying to a secure element is not implemented yet. */
2013 status = PSA_ERROR_NOT_SUPPORTED;
2014 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002015 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002016#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002017
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002019 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002020 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002021
Ronald Cron81709fc2020-11-14 12:10:32 +01002022 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002023exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002024 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002025 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002026
Ronald Cron5c522922020-11-14 16:35:34 +01002027 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002028
Ronald Cron5c522922020-11-14 16:35:34 +01002029 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002030}
2031
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002032
2033
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002034/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002035/* Message digests */
2036/****************************************************************/
2037
John Durkop07cc04a2020-11-16 22:08:34 -08002038#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002039 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2040 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002041 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002042static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002043{
2044 switch( alg )
2045 {
John Durkopee4e6602020-11-27 08:48:46 -08002046#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002047 case PSA_ALG_MD2:
2048 return( &mbedtls_md2_info );
2049#endif
John Durkopee4e6602020-11-27 08:48:46 -08002050#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002051 case PSA_ALG_MD4:
2052 return( &mbedtls_md4_info );
2053#endif
John Durkopee4e6602020-11-27 08:48:46 -08002054#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002055 case PSA_ALG_MD5:
2056 return( &mbedtls_md5_info );
2057#endif
John Durkopee4e6602020-11-27 08:48:46 -08002058#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002059 case PSA_ALG_RIPEMD160:
2060 return( &mbedtls_ripemd160_info );
2061#endif
John Durkopee4e6602020-11-27 08:48:46 -08002062#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002063 case PSA_ALG_SHA_1:
2064 return( &mbedtls_sha1_info );
2065#endif
John Durkopee4e6602020-11-27 08:48:46 -08002066#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002067 case PSA_ALG_SHA_224:
2068 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002069#endif
2070#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002071 case PSA_ALG_SHA_256:
2072 return( &mbedtls_sha256_info );
2073#endif
John Durkopee4e6602020-11-27 08:48:46 -08002074#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002075 case PSA_ALG_SHA_384:
2076 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002077#endif
John Durkopee4e6602020-11-27 08:48:46 -08002078#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002079 case PSA_ALG_SHA_512:
2080 return( &mbedtls_sha512_info );
2081#endif
2082 default:
2083 return( NULL );
2084 }
2085}
John Durkop07cc04a2020-11-16 22:08:34 -08002086#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002087 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2088 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2089 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002090
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002091psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2092{
2093 switch( operation->alg )
2094 {
Gilles Peskine81736312018-06-26 15:04:31 +02002095 case 0:
2096 /* The object has (apparently) been initialized but it is not
2097 * in use. It's ok to call abort on such an object, and there's
2098 * nothing to do. */
2099 break;
John Durkopee4e6602020-11-27 08:48:46 -08002100#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002101 case PSA_ALG_MD2:
2102 mbedtls_md2_free( &operation->ctx.md2 );
2103 break;
2104#endif
John Durkopee4e6602020-11-27 08:48:46 -08002105#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002106 case PSA_ALG_MD4:
2107 mbedtls_md4_free( &operation->ctx.md4 );
2108 break;
2109#endif
John Durkopee4e6602020-11-27 08:48:46 -08002110#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111 case PSA_ALG_MD5:
2112 mbedtls_md5_free( &operation->ctx.md5 );
2113 break;
2114#endif
John Durkopee4e6602020-11-27 08:48:46 -08002115#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 case PSA_ALG_RIPEMD160:
2117 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2118 break;
2119#endif
John Durkopee4e6602020-11-27 08:48:46 -08002120#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002121 case PSA_ALG_SHA_1:
2122 mbedtls_sha1_free( &operation->ctx.sha1 );
2123 break;
2124#endif
John Durkop6ca23272020-12-03 06:01:32 -08002125#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002126 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002127 mbedtls_sha256_free( &operation->ctx.sha256 );
2128 break;
2129#endif
2130#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002131 case PSA_ALG_SHA_256:
2132 mbedtls_sha256_free( &operation->ctx.sha256 );
2133 break;
2134#endif
John Durkop6ca23272020-12-03 06:01:32 -08002135#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002136 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002137 mbedtls_sha512_free( &operation->ctx.sha512 );
2138 break;
2139#endif
2140#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002141 case PSA_ALG_SHA_512:
2142 mbedtls_sha512_free( &operation->ctx.sha512 );
2143 break;
2144#endif
2145 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002146 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002147 }
2148 operation->alg = 0;
2149 return( PSA_SUCCESS );
2150}
2151
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002152psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002153 psa_algorithm_t alg )
2154{
Janos Follath24eed8d2019-11-22 13:21:35 +00002155 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002156
2157 /* A context must be freshly initialized before it can be set up. */
2158 if( operation->alg != 0 )
2159 {
2160 return( PSA_ERROR_BAD_STATE );
2161 }
2162
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163 switch( alg )
2164 {
John Durkopee4e6602020-11-27 08:48:46 -08002165#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002166 case PSA_ALG_MD2:
2167 mbedtls_md2_init( &operation->ctx.md2 );
2168 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2169 break;
2170#endif
John Durkopee4e6602020-11-27 08:48:46 -08002171#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002172 case PSA_ALG_MD4:
2173 mbedtls_md4_init( &operation->ctx.md4 );
2174 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2175 break;
2176#endif
John Durkopee4e6602020-11-27 08:48:46 -08002177#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002178 case PSA_ALG_MD5:
2179 mbedtls_md5_init( &operation->ctx.md5 );
2180 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2181 break;
2182#endif
John Durkopee4e6602020-11-27 08:48:46 -08002183#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184 case PSA_ALG_RIPEMD160:
2185 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2186 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2187 break;
2188#endif
John Durkopee4e6602020-11-27 08:48:46 -08002189#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190 case PSA_ALG_SHA_1:
2191 mbedtls_sha1_init( &operation->ctx.sha1 );
2192 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2193 break;
2194#endif
John Durkopee4e6602020-11-27 08:48:46 -08002195#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 case PSA_ALG_SHA_224:
2197 mbedtls_sha256_init( &operation->ctx.sha256 );
2198 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2199 break;
John Durkopee4e6602020-11-27 08:48:46 -08002200#endif
2201#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 case PSA_ALG_SHA_256:
2203 mbedtls_sha256_init( &operation->ctx.sha256 );
2204 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2205 break;
2206#endif
John Durkopee4e6602020-11-27 08:48:46 -08002207#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002208 case PSA_ALG_SHA_384:
2209 mbedtls_sha512_init( &operation->ctx.sha512 );
2210 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2211 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002212#endif
John Durkopee4e6602020-11-27 08:48:46 -08002213#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214 case PSA_ALG_SHA_512:
2215 mbedtls_sha512_init( &operation->ctx.sha512 );
2216 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2217 break;
2218#endif
2219 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002220 return( PSA_ALG_IS_HASH( alg ) ?
2221 PSA_ERROR_NOT_SUPPORTED :
2222 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002223 }
2224 if( ret == 0 )
2225 operation->alg = alg;
2226 else
2227 psa_hash_abort( operation );
2228 return( mbedtls_to_psa_error( ret ) );
2229}
2230
2231psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2232 const uint8_t *input,
2233 size_t input_length )
2234{
Janos Follath24eed8d2019-11-22 13:21:35 +00002235 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002236
2237 /* Don't require hash implementations to behave correctly on a
2238 * zero-length input, which may have an invalid pointer. */
2239 if( input_length == 0 )
2240 return( PSA_SUCCESS );
2241
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 switch( operation->alg )
2243 {
John Durkopee4e6602020-11-27 08:48:46 -08002244#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245 case PSA_ALG_MD2:
2246 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2247 input, input_length );
2248 break;
2249#endif
John Durkopee4e6602020-11-27 08:48:46 -08002250#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_MD4:
2252 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2253 input, input_length );
2254 break;
2255#endif
John Durkopee4e6602020-11-27 08:48:46 -08002256#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002257 case PSA_ALG_MD5:
2258 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2259 input, input_length );
2260 break;
2261#endif
John Durkopee4e6602020-11-27 08:48:46 -08002262#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002263 case PSA_ALG_RIPEMD160:
2264 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2265 input, input_length );
2266 break;
2267#endif
John Durkopee4e6602020-11-27 08:48:46 -08002268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 case PSA_ALG_SHA_1:
2270 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2271 input, input_length );
2272 break;
2273#endif
John Durkop6ca23272020-12-03 06:01:32 -08002274#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002276 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2277 input, input_length );
2278 break;
2279#endif
2280#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002281 case PSA_ALG_SHA_256:
2282 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2283 input, input_length );
2284 break;
2285#endif
John Durkop6ca23272020-12-03 06:01:32 -08002286#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002287 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002288 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2289 input, input_length );
2290 break;
2291#endif
2292#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002293 case PSA_ALG_SHA_512:
2294 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2295 input, input_length );
2296 break;
2297#endif
2298 default:
John Durkopee4e6602020-11-27 08:48:46 -08002299 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002300 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002302
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303 if( ret != 0 )
2304 psa_hash_abort( operation );
2305 return( mbedtls_to_psa_error( ret ) );
2306}
2307
2308psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2309 uint8_t *hash,
2310 size_t hash_size,
2311 size_t *hash_length )
2312{
itayzafrir40835d42018-08-02 13:14:17 +03002313 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002314 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002315 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316
2317 /* Fill the output buffer with something that isn't a valid hash
2318 * (barring an attack on the hash and deliberately-crafted input),
2319 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002320 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002321 /* If hash_size is 0 then hash may be NULL and then the
2322 * call to memset would have undefined behavior. */
2323 if( hash_size != 0 )
2324 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325
2326 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002327 {
2328 status = PSA_ERROR_BUFFER_TOO_SMALL;
2329 goto exit;
2330 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331
2332 switch( operation->alg )
2333 {
John Durkopee4e6602020-11-27 08:48:46 -08002334#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335 case PSA_ALG_MD2:
2336 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2337 break;
2338#endif
John Durkopee4e6602020-11-27 08:48:46 -08002339#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 case PSA_ALG_MD4:
2341 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2342 break;
2343#endif
John Durkopee4e6602020-11-27 08:48:46 -08002344#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002345 case PSA_ALG_MD5:
2346 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2347 break;
2348#endif
John Durkopee4e6602020-11-27 08:48:46 -08002349#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 case PSA_ALG_RIPEMD160:
2351 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2352 break;
2353#endif
John Durkopee4e6602020-11-27 08:48:46 -08002354#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002355 case PSA_ALG_SHA_1:
2356 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2357 break;
2358#endif
John Durkop6ca23272020-12-03 06:01:32 -08002359#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002361 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2362 break;
2363#endif
2364#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 case PSA_ALG_SHA_256:
2366 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2367 break;
2368#endif
John Durkop6ca23272020-12-03 06:01:32 -08002369#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002371 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2372 break;
2373#endif
2374#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 case PSA_ALG_SHA_512:
2376 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2377 break;
2378#endif
2379 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002380 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002381 }
itayzafrir40835d42018-08-02 13:14:17 +03002382 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383
itayzafrir40835d42018-08-02 13:14:17 +03002384exit:
2385 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002387 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 return( psa_hash_abort( operation ) );
2389 }
2390 else
2391 {
2392 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002393 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 }
2395}
2396
Gilles Peskine2d277862018-06-18 15:41:12 +02002397psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2398 const uint8_t *hash,
2399 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400{
2401 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2402 size_t actual_hash_length;
2403 psa_status_t status = psa_hash_finish( operation,
2404 actual_hash, sizeof( actual_hash ),
2405 &actual_hash_length );
2406 if( status != PSA_SUCCESS )
2407 return( status );
2408 if( actual_hash_length != hash_length )
2409 return( PSA_ERROR_INVALID_SIGNATURE );
2410 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2411 return( PSA_ERROR_INVALID_SIGNATURE );
2412 return( PSA_SUCCESS );
2413}
2414
Gilles Peskine0a749c82019-11-28 19:33:58 +01002415psa_status_t psa_hash_compute( psa_algorithm_t alg,
2416 const uint8_t *input, size_t input_length,
2417 uint8_t *hash, size_t hash_size,
2418 size_t *hash_length )
2419{
2420 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2422
2423 *hash_length = hash_size;
2424 status = psa_hash_setup( &operation, alg );
2425 if( status != PSA_SUCCESS )
2426 goto exit;
2427 status = psa_hash_update( &operation, input, input_length );
2428 if( status != PSA_SUCCESS )
2429 goto exit;
2430 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2431 if( status != PSA_SUCCESS )
2432 goto exit;
2433
2434exit:
2435 if( status == PSA_SUCCESS )
2436 status = psa_hash_abort( &operation );
2437 else
2438 psa_hash_abort( &operation );
2439 return( status );
2440}
2441
2442psa_status_t psa_hash_compare( psa_algorithm_t alg,
2443 const uint8_t *input, size_t input_length,
2444 const uint8_t *hash, size_t hash_length )
2445{
2446 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2447 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2448
2449 status = psa_hash_setup( &operation, alg );
2450 if( status != PSA_SUCCESS )
2451 goto exit;
2452 status = psa_hash_update( &operation, input, input_length );
2453 if( status != PSA_SUCCESS )
2454 goto exit;
2455 status = psa_hash_verify( &operation, hash, hash_length );
2456 if( status != PSA_SUCCESS )
2457 goto exit;
2458
2459exit:
2460 if( status == PSA_SUCCESS )
2461 status = psa_hash_abort( &operation );
2462 else
2463 psa_hash_abort( &operation );
2464 return( status );
2465}
2466
Gilles Peskineeb35d782019-01-22 17:56:16 +01002467psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2468 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469{
2470 if( target_operation->alg != 0 )
2471 return( PSA_ERROR_BAD_STATE );
2472
2473 switch( source_operation->alg )
2474 {
2475 case 0:
2476 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002478 case PSA_ALG_MD2:
2479 mbedtls_md2_clone( &target_operation->ctx.md2,
2480 &source_operation->ctx.md2 );
2481 break;
2482#endif
John Durkopee4e6602020-11-27 08:48:46 -08002483#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002484 case PSA_ALG_MD4:
2485 mbedtls_md4_clone( &target_operation->ctx.md4,
2486 &source_operation->ctx.md4 );
2487 break;
2488#endif
John Durkopee4e6602020-11-27 08:48:46 -08002489#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002490 case PSA_ALG_MD5:
2491 mbedtls_md5_clone( &target_operation->ctx.md5,
2492 &source_operation->ctx.md5 );
2493 break;
2494#endif
John Durkopee4e6602020-11-27 08:48:46 -08002495#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002496 case PSA_ALG_RIPEMD160:
2497 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2498 &source_operation->ctx.ripemd160 );
2499 break;
2500#endif
John Durkopee4e6602020-11-27 08:48:46 -08002501#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002502 case PSA_ALG_SHA_1:
2503 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2504 &source_operation->ctx.sha1 );
2505 break;
2506#endif
John Durkop6ca23272020-12-03 06:01:32 -08002507#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002508 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002509 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2510 &source_operation->ctx.sha256 );
2511 break;
2512#endif
2513#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002514 case PSA_ALG_SHA_256:
2515 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2516 &source_operation->ctx.sha256 );
2517 break;
2518#endif
John Durkop6ca23272020-12-03 06:01:32 -08002519#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002520 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002521 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2522 &source_operation->ctx.sha512 );
2523 break;
2524#endif
2525#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002526 case PSA_ALG_SHA_512:
2527 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2528 &source_operation->ctx.sha512 );
2529 break;
2530#endif
2531 default:
2532 return( PSA_ERROR_NOT_SUPPORTED );
2533 }
2534
2535 target_operation->alg = source_operation->alg;
2536 return( PSA_SUCCESS );
2537}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538
2539
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002540/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002541/* MAC */
2542/****************************************************************/
2543
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002544static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 psa_algorithm_t alg,
2546 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002547 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002548 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002553 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002554 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002555
Gilles Peskine8c9def32018-02-08 10:02:12 +01002556 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2557 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002558 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002560 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 mode = MBEDTLS_MODE_STREAM;
2562 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563 case PSA_ALG_CTR:
2564 mode = MBEDTLS_MODE_CTR;
2565 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002566 case PSA_ALG_CFB:
2567 mode = MBEDTLS_MODE_CFB;
2568 break;
2569 case PSA_ALG_OFB:
2570 mode = MBEDTLS_MODE_OFB;
2571 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002572 case PSA_ALG_ECB_NO_PADDING:
2573 mode = MBEDTLS_MODE_ECB;
2574 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002575 case PSA_ALG_CBC_NO_PADDING:
2576 mode = MBEDTLS_MODE_CBC;
2577 break;
2578 case PSA_ALG_CBC_PKCS7:
2579 mode = MBEDTLS_MODE_CBC;
2580 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002581 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002582 mode = MBEDTLS_MODE_CCM;
2583 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002584 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002585 mode = MBEDTLS_MODE_GCM;
2586 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002587 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002588 mode = MBEDTLS_MODE_CHACHAPOLY;
2589 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002590 default:
2591 return( NULL );
2592 }
2593 }
2594 else if( alg == PSA_ALG_CMAC )
2595 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002596 else
2597 return( NULL );
2598
2599 switch( key_type )
2600 {
2601 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002602 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002603 break;
2604 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002605 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2606 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002607 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002608 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002609 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002610 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002611 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2612 * but two-key Triple-DES is functionally three-key Triple-DES
2613 * with K1=K3, so that's how we present it to mbedtls. */
2614 if( key_bits == 128 )
2615 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002616 break;
2617 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002618 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002619 break;
2620 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002621 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002622 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002623 case PSA_KEY_TYPE_CHACHA20:
2624 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2625 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002626 default:
2627 return( NULL );
2628 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002629 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002630 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631
Jaeden Amero23bbb752018-06-26 14:16:54 +01002632 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2633 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002634}
2635
John Durkop6ba40d12020-11-10 08:50:04 -08002636#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002637static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002638{
Gilles Peskine2d277862018-06-18 15:41:12 +02002639 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002640 {
2641 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002642 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002643 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002644 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002645 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002646 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002647 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002648 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002649 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002650 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002651 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002652 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002653 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002654 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002655 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002656 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002657 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002658 return( 128 );
2659 default:
2660 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002661 }
2662}
John Durkop07cc04a2020-11-16 22:08:34 -08002663#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002664
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002665/* Initialize the MAC operation structure. Once this function has been
2666 * called, psa_mac_abort can run and will do the right thing. */
2667static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2668 psa_algorithm_t alg )
2669{
2670 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2671
2672 operation->alg = alg;
2673 operation->key_set = 0;
2674 operation->iv_set = 0;
2675 operation->iv_required = 0;
2676 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002677 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002678
2679#if defined(MBEDTLS_CMAC_C)
2680 if( alg == PSA_ALG_CMAC )
2681 {
2682 operation->iv_required = 0;
2683 mbedtls_cipher_init( &operation->ctx.cmac );
2684 status = PSA_SUCCESS;
2685 }
2686 else
2687#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002688#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002689 if( PSA_ALG_IS_HMAC( operation->alg ) )
2690 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002691 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2692 operation->ctx.hmac.hash_ctx.alg = 0;
2693 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002694 }
2695 else
John Durkopd0321952020-10-29 21:37:36 -07002696#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002697 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002698 if( ! PSA_ALG_IS_MAC( alg ) )
2699 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002700 }
2701
2702 if( status != PSA_SUCCESS )
2703 memset( operation, 0, sizeof( *operation ) );
2704 return( status );
2705}
2706
John Durkop6ba40d12020-11-10 08:50:04 -08002707#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002708static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2709{
Gilles Peskine3f108122018-12-07 18:14:53 +01002710 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002711 return( psa_hash_abort( &hmac->hash_ctx ) );
2712}
John Durkop6ba40d12020-11-10 08:50:04 -08002713#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2716{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002717 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002719 /* The object has (apparently) been initialized but it is not
2720 * in use. It's ok to call abort on such an object, and there's
2721 * nothing to do. */
2722 return( PSA_SUCCESS );
2723 }
2724 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002726 if( operation->alg == PSA_ALG_CMAC )
2727 {
2728 mbedtls_cipher_free( &operation->ctx.cmac );
2729 }
2730 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002731#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002732#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002733 if( PSA_ALG_IS_HMAC( operation->alg ) )
2734 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002735 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002736 }
2737 else
John Durkopd0321952020-10-29 21:37:36 -07002738#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002739 {
2740 /* Sanity check (shouldn't happen: operation->alg should
2741 * always have been initialized to a valid value). */
2742 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002743 }
Moran Peker41deec42018-04-04 15:43:05 +03002744
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 operation->alg = 0;
2746 operation->key_set = 0;
2747 operation->iv_set = 0;
2748 operation->iv_required = 0;
2749 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002750 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002751
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002753
2754bad_state:
2755 /* If abort is called on an uninitialized object, we can't trust
2756 * anything. Wipe the object in case it contains confidential data.
2757 * This may result in a memory leak if a pointer gets overwritten,
2758 * but it's too late to do anything about this. */
2759 memset( operation, 0, sizeof( *operation ) );
2760 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002761}
2762
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002763#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002764static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002766 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767 const mbedtls_cipher_info_t *cipher_info )
2768{
Janos Follath24eed8d2019-11-22 13:21:35 +00002769 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002770
2771 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772
2773 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2774 if( ret != 0 )
2775 return( ret );
2776
2777 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002778 slot->key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779 key_bits );
2780 return( ret );
2781}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002782#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002783
John Durkop6ba40d12020-11-10 08:50:04 -08002784#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2786 const uint8_t *key,
2787 size_t key_length,
2788 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002790 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002791 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002792 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002793 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794 psa_status_t status;
2795
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002796 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2797 * overflow below. This should never trigger if the hash algorithm
2798 * is implemented correctly. */
2799 /* The size checks against the ipad and opad buffers cannot be written
2800 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2801 * because that triggers -Wlogical-op on GCC 7.3. */
2802 if( block_size > sizeof( ipad ) )
2803 return( PSA_ERROR_NOT_SUPPORTED );
2804 if( block_size > sizeof( hmac->opad ) )
2805 return( PSA_ERROR_NOT_SUPPORTED );
2806 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002807 return( PSA_ERROR_NOT_SUPPORTED );
2808
Gilles Peskined223b522018-06-11 18:12:58 +02002809 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002810 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002811 status = psa_hash_compute( hash_alg, key, key_length,
2812 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002813 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002814 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002815 }
Gilles Peskine96889972018-07-12 17:07:03 +02002816 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2817 * but it is permitted. It is common when HMAC is used in HKDF, for
2818 * example. Don't call `memcpy` in the 0-length because `key` could be
2819 * an invalid pointer which would make the behavior undefined. */
2820 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002821 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002822
Gilles Peskined223b522018-06-11 18:12:58 +02002823 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2824 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002825 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002826 ipad[i] ^= 0x36;
2827 memset( ipad + key_length, 0x36, block_size - key_length );
2828
2829 /* Copy the key material from ipad to opad, flipping the requisite bits,
2830 * and filling the rest of opad with the requisite constant. */
2831 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002832 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2833 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002834
Gilles Peskine01126fa2018-07-12 17:04:55 +02002835 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002836 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002837 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002838
Gilles Peskine01126fa2018-07-12 17:04:55 +02002839 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002840
2841cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002842 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002843
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002844 return( status );
2845}
John Durkop6ba40d12020-11-10 08:50:04 -08002846#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002847
Gilles Peskine89167cb2018-07-08 20:12:23 +02002848static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002849 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002850 psa_algorithm_t alg,
2851 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002852{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002854 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002855 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002857 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002858 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002859 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002860 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002861
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002862 /* A context must be freshly initialized before it can be set up. */
2863 if( operation->alg != 0 )
2864 {
2865 return( PSA_ERROR_BAD_STATE );
2866 }
2867
Gilles Peskined911eb72018-08-14 15:18:45 +02002868 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002869 if( status != PSA_SUCCESS )
2870 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002871 if( is_sign )
2872 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873
Ronald Cron5c522922020-11-14 16:35:34 +01002874 status = psa_get_and_lock_transparent_key_slot_with_policy(
2875 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002876 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002877 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002878 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002879
Gilles Peskine8c9def32018-02-08 10:02:12 +01002880#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002881 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002882 {
2883 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002884 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002885 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002886 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002887 if( cipher_info == NULL )
2888 {
2889 status = PSA_ERROR_NOT_SUPPORTED;
2890 goto exit;
2891 }
2892 operation->mac_size = cipher_info->block_size;
2893 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2894 status = mbedtls_to_psa_error( ret );
2895 }
2896 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002898#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02002899 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002900 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002901 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002902 if( hash_alg == 0 )
2903 {
2904 status = PSA_ERROR_NOT_SUPPORTED;
2905 goto exit;
2906 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002907
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002908 operation->mac_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002909 /* Sanity check. This shouldn't fail on a valid configuration. */
2910 if( operation->mac_size == 0 ||
2911 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2912 {
2913 status = PSA_ERROR_NOT_SUPPORTED;
2914 goto exit;
2915 }
2916
Gilles Peskine8e338702019-07-30 20:06:31 +02002917 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002918 {
2919 status = PSA_ERROR_INVALID_ARGUMENT;
2920 goto exit;
2921 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002922
Gilles Peskine01126fa2018-07-12 17:04:55 +02002923 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002924 slot->key.data,
2925 slot->key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002926 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002927 }
2928 else
John Durkopd0321952020-10-29 21:37:36 -07002929#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002930 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002931 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002932 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002934
Gilles Peskined911eb72018-08-14 15:18:45 +02002935 if( truncated == 0 )
2936 {
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002937 /* The "normal" case: untruncated algorithm. Re-validate the
2938 * key policy with explicit MAC length set in the algorithm
2939 * when the algorithm policy is at-least-this-length to catch
2940 * a corner case due to the default MAC length being unknown
2941 * at key loading time. */
2942 if( PSA_ALG_IS_MAC( slot->attr.policy.alg ) &&
Steven Cooreman328f11c2021-03-02 11:44:51 +01002943 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg ) ==
2944 full_length_alg ) &&
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002945 ( slot->attr.policy.alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2946 {
2947 /* validate policy length */
Steven Cooreman328f11c2021-03-02 11:44:51 +01002948 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg ) >
2949 operation->mac_size )
2950 {
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002951 status = PSA_ERROR_NOT_PERMITTED;
Steven Cooreman328f11c2021-03-02 11:44:51 +01002952 }
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002953 }
2954
2955 if( PSA_ALG_IS_MAC( slot->attr.policy.alg2 ) &&
Steven Cooreman328f11c2021-03-02 11:44:51 +01002956 ( PSA_ALG_FULL_LENGTH_MAC( slot->attr.policy.alg2 ) ==
2957 full_length_alg ) &&
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002958 ( slot->attr.policy.alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) )
2959 {
2960 /* validate policy length */
Steven Cooreman328f11c2021-03-02 11:44:51 +01002961 if( PSA_MAC_TRUNCATED_LENGTH( slot->attr.policy.alg2 ) >
2962 operation->mac_size )
2963 {
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002964 status = PSA_ERROR_NOT_PERMITTED;
Steven Cooreman328f11c2021-03-02 11:44:51 +01002965 }
Steven Cooremanae3f13b2021-02-23 12:19:42 +01002966 }
Gilles Peskined911eb72018-08-14 15:18:45 +02002967 }
2968 else if( truncated < 4 )
2969 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002970 /* A very short MAC is too short for security since it can be
2971 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2972 * so we make this our minimum, even though 32 bits is still
2973 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002974 status = PSA_ERROR_NOT_SUPPORTED;
2975 }
2976 else if( truncated > operation->mac_size )
2977 {
2978 /* It's impossible to "truncate" to a larger length. */
2979 status = PSA_ERROR_INVALID_ARGUMENT;
2980 }
2981 else
2982 operation->mac_size = truncated;
2983
Gilles Peskinefbfac682018-07-08 20:51:54 +02002984exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002985 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002986 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002987 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002989 else
2990 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002991 operation->key_set = 1;
2992 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002993
Ronald Cron5c522922020-11-14 16:35:34 +01002994 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002995
Ronald Cron5c522922020-11-14 16:35:34 +01002996 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997}
2998
Gilles Peskine89167cb2018-07-08 20:12:23 +02002999psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003000 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003001 psa_algorithm_t alg )
3002{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003003 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003004}
3005
3006psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003007 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003008 psa_algorithm_t alg )
3009{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003010 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003011}
3012
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3014 const uint8_t *input,
3015 size_t input_length )
3016{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003017 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003019 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003021 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022 operation->has_input = 1;
3023
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003026 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003027 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3028 input, input_length );
3029 status = mbedtls_to_psa_error( ret );
3030 }
3031 else
3032#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003033#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 if( PSA_ALG_IS_HMAC( operation->alg ) )
3035 {
3036 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3037 input_length );
3038 }
3039 else
John Durkopd0321952020-10-29 21:37:36 -07003040#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003041 {
3042 /* This shouldn't happen if `operation` was initialized by
3043 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003044 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003045 }
3046
Gilles Peskinefbfac682018-07-08 20:51:54 +02003047 if( status != PSA_SUCCESS )
3048 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003049 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050}
3051
John Durkop6ba40d12020-11-10 08:50:04 -08003052#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003053static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3054 uint8_t *mac,
3055 size_t mac_size )
3056{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003057 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003058 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3059 size_t hash_size = 0;
3060 size_t block_size = psa_get_hash_block_size( hash_alg );
3061 psa_status_t status;
3062
Gilles Peskine01126fa2018-07-12 17:04:55 +02003063 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3064 if( status != PSA_SUCCESS )
3065 return( status );
3066 /* From here on, tmp needs to be wiped. */
3067
3068 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3069 if( status != PSA_SUCCESS )
3070 goto exit;
3071
3072 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3073 if( status != PSA_SUCCESS )
3074 goto exit;
3075
3076 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3077 if( status != PSA_SUCCESS )
3078 goto exit;
3079
Gilles Peskined911eb72018-08-14 15:18:45 +02003080 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3081 if( status != PSA_SUCCESS )
3082 goto exit;
3083
3084 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003085
3086exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003087 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003088 return( status );
3089}
John Durkop6ba40d12020-11-10 08:50:04 -08003090#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003091
mohammad16036df908f2018-04-02 08:34:15 -07003092static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003093 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003094 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003096 if( ! operation->key_set )
3097 return( PSA_ERROR_BAD_STATE );
3098 if( operation->iv_required && ! operation->iv_set )
3099 return( PSA_ERROR_BAD_STATE );
3100
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101 if( mac_size < operation->mac_size )
3102 return( PSA_ERROR_BUFFER_TOO_SMALL );
3103
Gilles Peskine8c9def32018-02-08 10:02:12 +01003104#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003105 if( operation->alg == PSA_ALG_CMAC )
3106 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003107 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003108 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3109 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003110 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003111 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003112 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003113 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 else
3115#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003117 if( PSA_ALG_IS_HMAC( operation->alg ) )
3118 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003119 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003120 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003121 }
3122 else
John Durkopd0321952020-10-29 21:37:36 -07003123#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 {
3125 /* This shouldn't happen if `operation` was initialized by
3126 * a setup function. */
3127 return( PSA_ERROR_BAD_STATE );
3128 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003129}
3130
Gilles Peskineacd4be32018-07-08 19:56:25 +02003131psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3132 uint8_t *mac,
3133 size_t mac_size,
3134 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003135{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003136 psa_status_t status;
3137
Jaeden Amero252ef282019-02-15 14:05:35 +00003138 if( operation->alg == 0 )
3139 {
3140 return( PSA_ERROR_BAD_STATE );
3141 }
3142
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003143 /* Fill the output buffer with something that isn't a valid mac
3144 * (barring an attack on the mac and deliberately-crafted input),
3145 * in case the caller doesn't check the return status properly. */
3146 *mac_length = mac_size;
3147 /* If mac_size is 0 then mac may be NULL and then the
3148 * call to memset would have undefined behavior. */
3149 if( mac_size != 0 )
3150 memset( mac, '!', mac_size );
3151
Gilles Peskine89167cb2018-07-08 20:12:23 +02003152 if( ! operation->is_sign )
3153 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003154 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155 }
mohammad16036df908f2018-04-02 08:34:15 -07003156
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003157 status = psa_mac_finish_internal( operation, mac, mac_size );
3158
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003159 if( status == PSA_SUCCESS )
3160 {
3161 status = psa_mac_abort( operation );
3162 if( status == PSA_SUCCESS )
3163 *mac_length = operation->mac_size;
3164 else
3165 memset( mac, '!', mac_size );
3166 }
3167 else
3168 psa_mac_abort( operation );
3169 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003170}
3171
Gilles Peskineacd4be32018-07-08 19:56:25 +02003172psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3173 const uint8_t *mac,
3174 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175{
Gilles Peskine828ed142018-06-18 23:25:51 +02003176 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003177 psa_status_t status;
3178
Jaeden Amero252ef282019-02-15 14:05:35 +00003179 if( operation->alg == 0 )
3180 {
3181 return( PSA_ERROR_BAD_STATE );
3182 }
3183
Gilles Peskine89167cb2018-07-08 20:12:23 +02003184 if( operation->is_sign )
3185 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003186 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003187 }
3188 if( operation->mac_size != mac_length )
3189 {
3190 status = PSA_ERROR_INVALID_SIGNATURE;
3191 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003192 }
mohammad16036df908f2018-04-02 08:34:15 -07003193
3194 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003195 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003196 if( status != PSA_SUCCESS )
3197 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003198
3199 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3200 status = PSA_ERROR_INVALID_SIGNATURE;
3201
3202cleanup:
3203 if( status == PSA_SUCCESS )
3204 status = psa_mac_abort( operation );
3205 else
3206 psa_mac_abort( operation );
3207
Gilles Peskine3f108122018-12-07 18:14:53 +01003208 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003209
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003210 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003211}
3212
3213
Gilles Peskine20035e32018-02-03 22:44:14 +01003214
Gilles Peskine20035e32018-02-03 22:44:14 +01003215/****************************************************************/
3216/* Asymmetric cryptography */
3217/****************************************************************/
3218
John Durkop6ba40d12020-11-10 08:50:04 -08003219#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3220 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003221/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003222 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003223static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3224 size_t hash_length,
3225 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003226{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003227 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003228 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003229 *md_alg = mbedtls_md_get_type( md_info );
3230
3231 /* The Mbed TLS RSA module uses an unsigned int for hash length
3232 * parameters. Validate that it fits so that we don't risk an
3233 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003234#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003235 if( hash_length > UINT_MAX )
3236 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003237#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003238
John Durkop0e005192020-10-31 22:06:54 -07003239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003240 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3241 * must be correct. */
3242 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3243 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003244 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003245 if( md_info == NULL )
3246 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003247 if( mbedtls_md_get_size( md_info ) != hash_length )
3248 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003249 }
John Durkop0e005192020-10-31 22:06:54 -07003250#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003251
John Durkop0e005192020-10-31 22:06:54 -07003252#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003253 /* PSS requires a hash internally. */
3254 if( PSA_ALG_IS_RSA_PSS( alg ) )
3255 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003256 if( md_info == NULL )
3257 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003258 }
John Durkop0e005192020-10-31 22:06:54 -07003259#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003260
Gilles Peskine61b91d42018-06-08 16:09:36 +02003261 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003262}
3263
Gilles Peskine2b450e32018-06-27 15:42:46 +02003264static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3265 psa_algorithm_t alg,
3266 const uint8_t *hash,
3267 size_t hash_length,
3268 uint8_t *signature,
3269 size_t signature_size,
3270 size_t *signature_length )
3271{
3272 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003274 mbedtls_md_type_t md_alg;
3275
3276 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3277 if( status != PSA_SUCCESS )
3278 return( status );
3279
Gilles Peskine630a18a2018-06-29 17:49:35 +02003280 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003281 return( PSA_ERROR_BUFFER_TOO_SMALL );
3282
John Durkop0e005192020-10-31 22:06:54 -07003283#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003284 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3285 {
3286 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3287 MBEDTLS_MD_NONE );
3288 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003289 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003290 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003291 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003292 md_alg,
3293 (unsigned int) hash_length,
3294 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003295 signature );
3296 }
3297 else
John Durkop0e005192020-10-31 22:06:54 -07003298#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3299#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003300 if( PSA_ALG_IS_RSA_PSS( alg ) )
3301 {
3302 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3303 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003304 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003305 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003306 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003307 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003308 (unsigned int) hash_length,
3309 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003310 signature );
3311 }
3312 else
John Durkop0e005192020-10-31 22:06:54 -07003313#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003314 {
3315 return( PSA_ERROR_INVALID_ARGUMENT );
3316 }
3317
3318 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003319 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003320 return( mbedtls_to_psa_error( ret ) );
3321}
3322
3323static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3324 psa_algorithm_t alg,
3325 const uint8_t *hash,
3326 size_t hash_length,
3327 const uint8_t *signature,
3328 size_t signature_length )
3329{
3330 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003331 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003332 mbedtls_md_type_t md_alg;
3333
3334 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3335 if( status != PSA_SUCCESS )
3336 return( status );
3337
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003338 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3339 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003340
John Durkop0e005192020-10-31 22:06:54 -07003341#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003342 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3343 {
3344 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3345 MBEDTLS_MD_NONE );
3346 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003347 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003348 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003349 MBEDTLS_RSA_PUBLIC,
3350 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003351 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003352 hash,
3353 signature );
3354 }
3355 else
John Durkop0e005192020-10-31 22:06:54 -07003356#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3357#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003358 if( PSA_ALG_IS_RSA_PSS( alg ) )
3359 {
3360 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3361 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003362 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003363 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003364 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003365 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003366 (unsigned int) hash_length,
3367 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003368 signature );
3369 }
3370 else
John Durkop0e005192020-10-31 22:06:54 -07003371#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003372 {
3373 return( PSA_ERROR_INVALID_ARGUMENT );
3374 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003375
3376 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3377 * the rest of the signature is invalid". This has little use in
3378 * practice and PSA doesn't report this distinction. */
3379 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3380 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003381 return( mbedtls_to_psa_error( ret ) );
3382}
John Durkop6ba40d12020-11-10 08:50:04 -08003383#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3384 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003385
John Durkop6ba40d12020-11-10 08:50:04 -08003386#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3387 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003388/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3389 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3390 * (even though these functions don't modify it). */
3391static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3392 psa_algorithm_t alg,
3393 const uint8_t *hash,
3394 size_t hash_length,
3395 uint8_t *signature,
3396 size_t signature_size,
3397 size_t *signature_length )
3398{
Janos Follath24eed8d2019-11-22 13:21:35 +00003399 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003400 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003401 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003402 mbedtls_mpi_init( &r );
3403 mbedtls_mpi_init( &s );
3404
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003405 if( signature_size < 2 * curve_bytes )
3406 {
3407 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3408 goto cleanup;
3409 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003410
John Durkop0ea39e02020-10-13 19:58:20 -07003411#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003412 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3413 {
3414 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3415 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3416 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003417 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3418 &ecp->d, hash,
3419 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003420 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003421 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003422 }
3423 else
John Durkop0ea39e02020-10-13 19:58:20 -07003424#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003425 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003426 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003427 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3428 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003429 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003430 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003431 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003432
3433 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3434 signature,
3435 curve_bytes ) );
3436 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3437 signature + curve_bytes,
3438 curve_bytes ) );
3439
3440cleanup:
3441 mbedtls_mpi_free( &r );
3442 mbedtls_mpi_free( &s );
3443 if( ret == 0 )
3444 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003445 return( mbedtls_to_psa_error( ret ) );
3446}
3447
3448static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3449 const uint8_t *hash,
3450 size_t hash_length,
3451 const uint8_t *signature,
3452 size_t signature_length )
3453{
Janos Follath24eed8d2019-11-22 13:21:35 +00003454 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003455 mbedtls_mpi r, s;
3456 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3457 mbedtls_mpi_init( &r );
3458 mbedtls_mpi_init( &s );
3459
3460 if( signature_length != 2 * curve_bytes )
3461 return( PSA_ERROR_INVALID_SIGNATURE );
3462
3463 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3464 signature,
3465 curve_bytes ) );
3466 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3467 signature + curve_bytes,
3468 curve_bytes ) );
3469
Steven Cooremanacda8342020-07-24 23:09:52 +02003470 /* Check whether the public part is loaded. If not, load it. */
3471 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3472 {
3473 MBEDTLS_MPI_CHK(
3474 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003475 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003476 }
3477
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003478 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3479 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003480
3481cleanup:
3482 mbedtls_mpi_free( &r );
3483 mbedtls_mpi_free( &s );
3484 return( mbedtls_to_psa_error( ret ) );
3485}
John Durkop6ba40d12020-11-10 08:50:04 -08003486#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3487 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003488
Ronald Croncf56a0a2020-08-04 09:51:30 +02003489psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003490 psa_algorithm_t alg,
3491 const uint8_t *hash,
3492 size_t hash_length,
3493 uint8_t *signature,
3494 size_t signature_size,
3495 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003496{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003497 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003498 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003499 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003500
3501 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003502 /* Immediately reject a zero-length signature buffer. This guarantees
3503 * that signature must be a valid pointer. (On the other hand, the hash
3504 * buffer can in principle be empty since it doesn't actually have
3505 * to be a hash.) */
3506 if( signature_size == 0 )
3507 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003508
Ronald Cron5c522922020-11-14 16:35:34 +01003509 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3510 PSA_KEY_USAGE_SIGN_HASH,
3511 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003512 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003513 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003514 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003515 {
3516 status = PSA_ERROR_INVALID_ARGUMENT;
3517 goto exit;
3518 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003519
Steven Cooremancd84cb42020-07-16 20:28:36 +02003520 /* Try any of the available accelerators first */
3521 status = psa_driver_wrapper_sign_hash( slot,
3522 alg,
3523 hash,
3524 hash_length,
3525 signature,
3526 signature_size,
3527 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003528 if( status != PSA_ERROR_NOT_SUPPORTED ||
3529 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003530 goto exit;
3531
Steven Cooreman7a250572020-07-17 16:43:05 +02003532 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003533#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3534 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003535 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003536 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003537 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003538
Ronald Cron90857082020-11-25 14:58:33 +01003539 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3540 slot->key.data,
3541 slot->key.bytes,
3542 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003543 if( status != PSA_SUCCESS )
3544 goto exit;
3545
Steven Cooremana2371e52020-07-28 14:30:39 +02003546 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003547 alg,
3548 hash, hash_length,
3549 signature, signature_size,
3550 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003551
Steven Cooremana2371e52020-07-28 14:30:39 +02003552 mbedtls_rsa_free( rsa );
3553 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003554 }
3555 else
John Durkop6ba40d12020-11-10 08:50:04 -08003556#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3557 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003558 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003559 {
John Durkop9814fa22020-11-04 12:28:15 -08003560#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3561 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003562 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003563#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003564 PSA_ALG_IS_ECDSA( alg )
3565#else
3566 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3567#endif
3568 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003569 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003570 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003571 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003572 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003573 slot->key.data,
3574 slot->key.bytes,
3575 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003576 if( status != PSA_SUCCESS )
3577 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003578 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003579 alg,
3580 hash, hash_length,
3581 signature, signature_size,
3582 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003583 mbedtls_ecp_keypair_free( ecp );
3584 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003585 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003586 else
John Durkop9814fa22020-11-04 12:28:15 -08003587#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3588 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003589 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003590 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003591 }
itayzafrir5c753392018-05-08 11:18:38 +03003592 }
3593 else
itayzafrir5c753392018-05-08 11:18:38 +03003594 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003595 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003596 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003597
3598exit:
3599 /* Fill the unused part of the output buffer (the whole buffer on error,
3600 * the trailing part on success) with something that isn't a valid mac
3601 * (barring an attack on the mac and deliberately-crafted input),
3602 * in case the caller doesn't check the return status properly. */
3603 if( status == PSA_SUCCESS )
3604 memset( signature + *signature_length, '!',
3605 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003606 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003607 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003608 /* If signature_size is 0 then we have nothing to do. We must not call
3609 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003610
Ronald Cron5c522922020-11-14 16:35:34 +01003611 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003612
Ronald Cron5c522922020-11-14 16:35:34 +01003613 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003614}
3615
Ronald Croncf56a0a2020-08-04 09:51:30 +02003616psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003617 psa_algorithm_t alg,
3618 const uint8_t *hash,
3619 size_t hash_length,
3620 const uint8_t *signature,
3621 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003622{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003623 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003624 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003625 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003626
Ronald Cron5c522922020-11-14 16:35:34 +01003627 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3628 PSA_KEY_USAGE_VERIFY_HASH,
3629 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003630 if( status != PSA_SUCCESS )
3631 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003632
Steven Cooreman55ae2172020-07-17 19:46:15 +02003633 /* Try any of the available accelerators first */
3634 status = psa_driver_wrapper_verify_hash( slot,
3635 alg,
3636 hash,
3637 hash_length,
3638 signature,
3639 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003640 if( status != PSA_ERROR_NOT_SUPPORTED ||
3641 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003642 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003643
John Durkop6ba40d12020-11-10 08:50:04 -08003644#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3645 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003646 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003647 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003648 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003649
Ronald Cron90857082020-11-25 14:58:33 +01003650 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3651 slot->key.data,
3652 slot->key.bytes,
3653 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003654 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003655 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003656
Steven Cooremana2371e52020-07-28 14:30:39 +02003657 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003658 alg,
3659 hash, hash_length,
3660 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003661 mbedtls_rsa_free( rsa );
3662 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003663 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664 }
3665 else
John Durkop6ba40d12020-11-10 08:50:04 -08003666#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3667 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003668 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003669 {
John Durkop9814fa22020-11-04 12:28:15 -08003670#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3671 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003672 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003673 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003674 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003675 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003676 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003677 slot->key.data,
3678 slot->key.bytes,
3679 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003680 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003681 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003682 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003683 hash, hash_length,
3684 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003685 mbedtls_ecp_keypair_free( ecp );
3686 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003687 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003688 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003689 else
John Durkop9814fa22020-11-04 12:28:15 -08003690#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3691 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003692 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003693 status = PSA_ERROR_INVALID_ARGUMENT;
3694 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003695 }
itayzafrir5c753392018-05-08 11:18:38 +03003696 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003697 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003698 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003699 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003700 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003701
3702exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003703 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003704
Ronald Cron5c522922020-11-14 16:35:34 +01003705 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003706}
3707
John Durkop0e005192020-10-31 22:06:54 -07003708#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003709static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3710 mbedtls_rsa_context *rsa )
3711{
3712 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3713 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3714 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3715 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3716}
John Durkop0e005192020-10-31 22:06:54 -07003717#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003718
Ronald Croncf56a0a2020-08-04 09:51:30 +02003719psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003720 psa_algorithm_t alg,
3721 const uint8_t *input,
3722 size_t input_length,
3723 const uint8_t *salt,
3724 size_t salt_length,
3725 uint8_t *output,
3726 size_t output_size,
3727 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003729 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003730 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003731 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003732
Darryl Green5cc689a2018-07-24 15:34:10 +01003733 (void) input;
3734 (void) input_length;
3735 (void) salt;
3736 (void) output;
3737 (void) output_size;
3738
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003739 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003740
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003741 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3742 return( PSA_ERROR_INVALID_ARGUMENT );
3743
Ronald Cron5c522922020-11-14 16:35:34 +01003744 status = psa_get_and_lock_transparent_key_slot_with_policy(
3745 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003746 if( status != PSA_SUCCESS )
3747 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003748 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3749 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003750 {
3751 status = PSA_ERROR_INVALID_ARGUMENT;
3752 goto exit;
3753 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003754
John Durkop6ba40d12020-11-10 08:50:04 -08003755#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3756 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003757 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003758 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003759 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003760 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3761 slot->key.data,
3762 slot->key.bytes,
3763 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003764 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003765 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003766
3767 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003768 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003769 status = PSA_ERROR_BUFFER_TOO_SMALL;
3770 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003771 }
John Durkop0e005192020-10-31 22:06:54 -07003772#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003773 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003774 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003775 status = mbedtls_to_psa_error(
3776 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003777 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003778 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003779 MBEDTLS_RSA_PUBLIC,
3780 input_length,
3781 input,
3782 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003783 }
3784 else
John Durkop0e005192020-10-31 22:06:54 -07003785#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3786#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003787 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003789 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003790 status = mbedtls_to_psa_error(
3791 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003792 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003793 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003794 MBEDTLS_RSA_PUBLIC,
3795 salt, salt_length,
3796 input_length,
3797 input,
3798 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799 }
3800 else
John Durkop0e005192020-10-31 22:06:54 -07003801#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003802 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003803 status = PSA_ERROR_INVALID_ARGUMENT;
3804 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003805 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003806rsa_exit:
3807 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003808 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003809
Steven Cooremana2371e52020-07-28 14:30:39 +02003810 mbedtls_rsa_free( rsa );
3811 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003812 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003813 else
John Durkop9814fa22020-11-04 12:28:15 -08003814#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003815 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003816 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003817 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003818 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003819
3820exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003821 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003822
Ronald Cron5c522922020-11-14 16:35:34 +01003823 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003824}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825
Ronald Croncf56a0a2020-08-04 09:51:30 +02003826psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003827 psa_algorithm_t alg,
3828 const uint8_t *input,
3829 size_t input_length,
3830 const uint8_t *salt,
3831 size_t salt_length,
3832 uint8_t *output,
3833 size_t output_size,
3834 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003835{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003836 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003837 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003838 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003839
Darryl Green5cc689a2018-07-24 15:34:10 +01003840 (void) input;
3841 (void) input_length;
3842 (void) salt;
3843 (void) output;
3844 (void) output_size;
3845
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003846 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003847
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003848 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3849 return( PSA_ERROR_INVALID_ARGUMENT );
3850
Ronald Cron5c522922020-11-14 16:35:34 +01003851 status = psa_get_and_lock_transparent_key_slot_with_policy(
3852 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003853 if( status != PSA_SUCCESS )
3854 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003855 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003856 {
3857 status = PSA_ERROR_INVALID_ARGUMENT;
3858 goto exit;
3859 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003860
John Durkop6ba40d12020-11-10 08:50:04 -08003861#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3862 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003863 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003865 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003866 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3867 slot->key.data,
3868 slot->key.bytes,
3869 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003870 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003871 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872
Steven Cooremana2371e52020-07-28 14:30:39 +02003873 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003874 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003875 status = PSA_ERROR_INVALID_ARGUMENT;
3876 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003877 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878
John Durkop0e005192020-10-31 22:06:54 -07003879#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003880 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003881 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003882 status = mbedtls_to_psa_error(
3883 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003884 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003885 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003886 MBEDTLS_RSA_PRIVATE,
3887 output_length,
3888 input,
3889 output,
3890 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891 }
3892 else
John Durkop0e005192020-10-31 22:06:54 -07003893#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3894#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003895 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003897 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003898 status = mbedtls_to_psa_error(
3899 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003900 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003901 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003902 MBEDTLS_RSA_PRIVATE,
3903 salt, salt_length,
3904 output_length,
3905 input,
3906 output,
3907 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908 }
3909 else
John Durkop0e005192020-10-31 22:06:54 -07003910#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003911 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003912 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003913 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003914
Steven Cooreman4fed4552020-08-03 14:46:03 +02003915rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003916 mbedtls_rsa_free( rsa );
3917 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003919 else
John Durkop6ba40d12020-11-10 08:50:04 -08003920#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3921 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003922 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003923 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003925
3926exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003927 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003928
Ronald Cron5c522922020-11-14 16:35:34 +01003929 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003930}
Gilles Peskine20035e32018-02-03 22:44:14 +01003931
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003932
3933
mohammad1603503973b2018-03-12 15:59:30 +02003934/****************************************************************/
3935/* Symmetric cryptography */
3936/****************************************************************/
3937
Gilles Peskinee553c652018-06-04 16:22:46 +02003938static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003939 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003940 psa_algorithm_t alg,
3941 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003942{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003943 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003944 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003945 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003946 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003947 size_t key_bits;
3948 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003949 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3950 PSA_KEY_USAGE_ENCRYPT :
3951 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003952
Steven Cooremand3feccd2020-09-01 15:56:14 +02003953 /* A context must be freshly initialized before it can be set up. */
3954 if( operation->alg != 0 )
3955 return( PSA_ERROR_BAD_STATE );
3956
Steven Cooremana07b9972020-09-10 14:54:14 +02003957 /* The requested algorithm must be one that can be processed by cipher. */
3958 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003959 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003960
Steven Cooremanef8575e2020-09-11 11:44:50 +02003961 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003962 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003963 if( status != PSA_SUCCESS )
3964 goto exit;
3965
3966 /* Initialize the operation struct members, except for alg. The alg member
3967 * is used to indicate to psa_cipher_abort that there are resources to free,
3968 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003969 operation->key_set = 0;
3970 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003971 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003972 operation->iv_size = 0;
3973 operation->block_size = 0;
3974 if( alg == PSA_ALG_ECB_NO_PADDING )
3975 operation->iv_required = 0;
3976 else
3977 operation->iv_required = 1;
3978
Steven Cooremana07b9972020-09-10 14:54:14 +02003979 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003980 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003981 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003982 slot,
3983 alg );
3984 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003985 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003986 slot,
3987 alg );
3988
Steven Cooremanef8575e2020-09-11 11:44:50 +02003989 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003990 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003991 /* Once the driver context is initialised, it needs to be freed using
3992 * psa_cipher_abort. Indicate this through setting alg. */
3993 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003994 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003995
Steven Cooremand3feccd2020-09-01 15:56:14 +02003996 if( status != PSA_ERROR_NOT_SUPPORTED ||
3997 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3998 goto exit;
3999
Steven Cooremana07b9972020-09-10 14:54:14 +02004000 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004001 * available for the given algorithm & key. */
4002 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004003
Steven Cooremana07b9972020-09-10 14:54:14 +02004004 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004005 * psa_cipher_abort. Indicate there is something to be freed through setting
4006 * alg, and indicate the operation is being done using mbedtls crypto through
4007 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004008 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004009 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004010
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004011 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004012 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004013 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004014 {
4015 status = PSA_ERROR_NOT_SUPPORTED;
4016 goto exit;
4017 }
mohammad1603503973b2018-03-12 15:59:30 +02004018
mohammad1603503973b2018-03-12 15:59:30 +02004019 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004020 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004021 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004022
David Brown12ca5032021-02-11 11:02:00 -07004023#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004024 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004025 {
4026 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004027 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004028 memcpy( keys, slot->key.data, 16 );
4029 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004030 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4031 keys,
4032 192, cipher_operation );
4033 }
4034 else
4035#endif
4036 {
4037 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004038 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004039 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004040 }
Moran Peker41deec42018-04-04 15:43:05 +03004041 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004042 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004043
David Brown63ca2602021-01-26 11:51:12 -07004044#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4045 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004046 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004047 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004048 case PSA_ALG_CBC_NO_PADDING:
4049 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4050 MBEDTLS_PADDING_NONE );
4051 break;
4052 case PSA_ALG_CBC_PKCS7:
4053 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4054 MBEDTLS_PADDING_PKCS7 );
4055 break;
4056 default:
4057 /* The algorithm doesn't involve padding. */
4058 ret = 0;
4059 break;
4060 }
4061 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004062 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004063#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004064
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004065 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004066 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004067 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4068 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004069 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004070 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004071 }
David Brown1bfe4d72021-02-16 12:54:35 -07004072#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004073 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004074 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004075 operation->iv_size = 12;
4076#endif
mohammad1603503973b2018-03-12 15:59:30 +02004077
Steven Cooreman7df02922020-09-09 15:28:49 +02004078 status = PSA_SUCCESS;
4079
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004080exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004081 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004082 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004083 if( status == PSA_SUCCESS )
4084 {
4085 /* Update operation flags for both driver and software implementations */
4086 operation->key_set = 1;
4087 }
4088 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004089 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004090
Ronald Cron5c522922020-11-14 16:35:34 +01004091 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004092
Ronald Cron5c522922020-11-14 16:35:34 +01004093 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004094}
4095
Gilles Peskinefe119512018-07-08 21:39:34 +02004096psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004097 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004098 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004099{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004100 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004101}
4102
Gilles Peskinefe119512018-07-08 21:39:34 +02004103psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004104 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004105 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004106{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004107 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004108}
4109
Gilles Peskinefe119512018-07-08 21:39:34 +02004110psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004111 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004112 size_t iv_size,
4113 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004114{
itayzafrir534bd7c2018-08-02 13:56:32 +03004115 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004116 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004117 if( operation->iv_set || ! operation->iv_required )
4118 {
4119 return( PSA_ERROR_BAD_STATE );
4120 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004121
Steven Cooremanef8575e2020-09-11 11:44:50 +02004122 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004123 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004124 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004125 iv,
4126 iv_size,
4127 iv_length );
4128 goto exit;
4129 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004130
Moran Peker41deec42018-04-04 15:43:05 +03004131 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004132 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004133 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004134 goto exit;
4135 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004136 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004137 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004138 if( ret != 0 )
4139 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004140 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004141 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004142 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004143
mohammad16038481e742018-03-18 13:57:31 +02004144 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004145 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004146
Moran Peker395db872018-05-31 14:07:14 +03004147exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 if( status == PSA_SUCCESS )
4149 operation->iv_set = 1;
4150 else
Moran Peker395db872018-05-31 14:07:14 +03004151 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004152 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004153}
4154
Gilles Peskinefe119512018-07-08 21:39:34 +02004155psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004156 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004157 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004158{
itayzafrir534bd7c2018-08-02 13:56:32 +03004159 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004161 if( operation->iv_set || ! operation->iv_required )
4162 {
4163 return( PSA_ERROR_BAD_STATE );
4164 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004165
Steven Cooremanef8575e2020-09-11 11:44:50 +02004166 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004167 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004168 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004169 iv,
4170 iv_length );
4171 goto exit;
4172 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004173
Moran Pekera28258c2018-05-29 16:25:04 +03004174 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004175 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004176 status = PSA_ERROR_INVALID_ARGUMENT;
4177 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004178 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004179 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4180 status = mbedtls_to_psa_error( ret );
4181exit:
4182 if( status == PSA_SUCCESS )
4183 operation->iv_set = 1;
4184 else
mohammad1603503973b2018-03-12 15:59:30 +02004185 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004186 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004187}
4188
Steven Cooreman177deba2020-09-07 17:14:14 +02004189/* Process input for which the algorithm is set to ECB mode. This requires
4190 * manual processing, since the PSA API is defined as being able to process
4191 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4192 * underlying mbedtls_cipher_update only takes full blocks. */
4193static psa_status_t psa_cipher_update_ecb_internal(
4194 mbedtls_cipher_context_t *ctx,
4195 const uint8_t *input,
4196 size_t input_length,
4197 uint8_t *output,
4198 size_t output_size,
4199 size_t *output_length )
4200{
4201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4202 size_t block_size = ctx->cipher_info->block_size;
4203 size_t internal_output_length = 0;
4204 *output_length = 0;
4205
4206 if( input_length == 0 )
4207 {
4208 status = PSA_SUCCESS;
4209 goto exit;
4210 }
4211
4212 if( ctx->unprocessed_len > 0 )
4213 {
4214 /* Fill up to block size, and run the block if there's a full one. */
4215 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4216
4217 if( input_length < bytes_to_copy )
4218 bytes_to_copy = input_length;
4219
4220 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4221 input, bytes_to_copy );
4222 input_length -= bytes_to_copy;
4223 input += bytes_to_copy;
4224 ctx->unprocessed_len += bytes_to_copy;
4225
4226 if( ctx->unprocessed_len == block_size )
4227 {
4228 status = mbedtls_to_psa_error(
4229 mbedtls_cipher_update( ctx,
4230 ctx->unprocessed_data,
4231 block_size,
4232 output, &internal_output_length ) );
4233
4234 if( status != PSA_SUCCESS )
4235 goto exit;
4236
4237 output += internal_output_length;
4238 output_size -= internal_output_length;
4239 *output_length += internal_output_length;
4240 ctx->unprocessed_len = 0;
4241 }
4242 }
4243
4244 while( input_length >= block_size )
4245 {
4246 /* Run all full blocks we have, one by one */
4247 status = mbedtls_to_psa_error(
4248 mbedtls_cipher_update( ctx, input,
4249 block_size,
4250 output, &internal_output_length ) );
4251
4252 if( status != PSA_SUCCESS )
4253 goto exit;
4254
4255 input_length -= block_size;
4256 input += block_size;
4257
4258 output += internal_output_length;
4259 output_size -= internal_output_length;
4260 *output_length += internal_output_length;
4261 }
4262
4263 if( input_length > 0 )
4264 {
4265 /* Save unprocessed bytes for later processing */
4266 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4267 input, input_length );
4268 ctx->unprocessed_len += input_length;
4269 }
4270
4271 status = PSA_SUCCESS;
4272
4273exit:
4274 return( status );
4275}
4276
Gilles Peskinee553c652018-06-04 16:22:46 +02004277psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4278 const uint8_t *input,
4279 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004280 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004281 size_t output_size,
4282 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004283{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004284 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004285 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004286 if( operation->alg == 0 )
4287 {
4288 return( PSA_ERROR_BAD_STATE );
4289 }
4290 if( operation->iv_required && ! operation->iv_set )
4291 {
4292 return( PSA_ERROR_BAD_STATE );
4293 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004294
Steven Cooremanef8575e2020-09-11 11:44:50 +02004295 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004296 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004297 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004298 input,
4299 input_length,
4300 output,
4301 output_size,
4302 output_length );
4303 goto exit;
4304 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004305
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004306 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004307 {
4308 /* Take the unprocessed partial block left over from previous
4309 * update calls, if any, plus the input to this call. Remove
4310 * the last partial block, if any. You get the data that will be
4311 * output in this call. */
4312 expected_output_size =
4313 ( operation->ctx.cipher.unprocessed_len + input_length )
4314 / operation->block_size * operation->block_size;
4315 }
4316 else
4317 {
4318 expected_output_size = input_length;
4319 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004320
Gilles Peskine89d789c2018-06-04 17:17:16 +02004321 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004322 {
4323 status = PSA_ERROR_BUFFER_TOO_SMALL;
4324 goto exit;
4325 }
mohammad160382759612018-03-12 18:16:40 +02004326
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004327 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4328 {
4329 /* mbedtls_cipher_update has an API inconsistency: it will only
4330 * process a single block at a time in ECB mode. Abstract away that
4331 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004332 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4333 input,
4334 input_length,
4335 output,
4336 output_size,
4337 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004338 }
4339 else
4340 {
4341 status = mbedtls_to_psa_error(
4342 mbedtls_cipher_update( &operation->ctx.cipher, input,
4343 input_length, output, output_length ) );
4344 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004345exit:
4346 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004347 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004348 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004349}
4350
Gilles Peskinee553c652018-06-04 16:22:46 +02004351psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4352 uint8_t *output,
4353 size_t output_size,
4354 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004355{
David Saadab4ecc272019-02-14 13:48:10 +02004356 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004357 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004358 if( operation->alg == 0 )
4359 {
4360 return( PSA_ERROR_BAD_STATE );
4361 }
4362 if( operation->iv_required && ! operation->iv_set )
4363 {
4364 return( PSA_ERROR_BAD_STATE );
4365 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004366
Steven Cooremanef8575e2020-09-11 11:44:50 +02004367 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004368 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004369 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004370 output,
4371 output_size,
4372 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004373 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004374 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004375
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004376 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004377 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004378 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004379 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004380 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004381 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004382 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004383 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004384 }
4385
Steven Cooremanef8575e2020-09-11 11:44:50 +02004386 status = mbedtls_to_psa_error(
4387 mbedtls_cipher_finish( &operation->ctx.cipher,
4388 temp_output_buffer,
4389 output_length ) );
4390 if( status != PSA_SUCCESS )
4391 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004392
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004393 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004394 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004395 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004396 memcpy( output, temp_output_buffer, *output_length );
4397 else
Janos Follath315b51c2018-07-09 16:04:51 +01004398 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004399
Steven Cooremanef8575e2020-09-11 11:44:50 +02004400exit:
4401 if( operation->mbedtls_in_use == 1 )
4402 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004403
Steven Cooremanef8575e2020-09-11 11:44:50 +02004404 if( status == PSA_SUCCESS )
4405 return( psa_cipher_abort( operation ) );
4406 else
4407 {
4408 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004409 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004410
Steven Cooremanef8575e2020-09-11 11:44:50 +02004411 return( status );
4412 }
mohammad1603503973b2018-03-12 15:59:30 +02004413}
4414
Gilles Peskinee553c652018-06-04 16:22:46 +02004415psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4416{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004417 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004418 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004419 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004420 * in use. It's ok to call abort on such an object, and there's
4421 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004422 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004423 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004424
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004425 /* Sanity check (shouldn't happen: operation->alg should
4426 * always have been initialized to a valid value). */
4427 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4428 return( PSA_ERROR_BAD_STATE );
4429
Steven Cooremanef8575e2020-09-11 11:44:50 +02004430 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004431 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004432 else
4433 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004434
Moran Peker41deec42018-04-04 15:43:05 +03004435 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004436 operation->key_set = 0;
4437 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004438 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004439 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004440 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004441 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004442
Moran Peker395db872018-05-31 14:07:14 +03004443 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004444}
4445
Gilles Peskinea0655c32018-04-30 17:06:50 +02004446
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004447
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004448
mohammad16035955c982018-04-26 00:53:03 +03004449/****************************************************************/
4450/* AEAD */
4451/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004452
Gilles Peskineedf9a652018-08-17 18:11:56 +02004453typedef struct
4454{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004455 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004456 const mbedtls_cipher_info_t *cipher_info;
4457 union
4458 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004459 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004460#if defined(MBEDTLS_CCM_C)
4461 mbedtls_ccm_context ccm;
4462#endif /* MBEDTLS_CCM_C */
4463#if defined(MBEDTLS_GCM_C)
4464 mbedtls_gcm_context gcm;
4465#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004466#if defined(MBEDTLS_CHACHAPOLY_C)
4467 mbedtls_chachapoly_context chachapoly;
4468#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004469 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004470 psa_algorithm_t core_alg;
4471 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004472 uint8_t tag_length;
4473} aead_operation_t;
4474
Ronald Cronf95a2b12020-10-22 15:24:49 +02004475#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4476
Gilles Peskine30a9e412019-01-14 18:36:12 +01004477static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004478{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004479 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004480 {
4481#if defined(MBEDTLS_CCM_C)
4482 case PSA_ALG_CCM:
4483 mbedtls_ccm_free( &operation->ctx.ccm );
4484 break;
4485#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004486#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004487 case PSA_ALG_GCM:
4488 mbedtls_gcm_free( &operation->ctx.gcm );
4489 break;
4490#endif /* MBEDTLS_GCM_C */
4491 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004492
Ronald Cron5c522922020-11-14 16:35:34 +01004493 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004494}
4495
4496static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004497 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004498 psa_key_usage_t usage,
4499 psa_algorithm_t alg )
4500{
4501 psa_status_t status;
4502 size_t key_bits;
4503 mbedtls_cipher_id_t cipher_id;
4504
Ronald Cron5c522922020-11-14 16:35:34 +01004505 status = psa_get_and_lock_transparent_key_slot_with_policy(
4506 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004507 if( status != PSA_SUCCESS )
4508 return( status );
4509
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004510 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004511
4512 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004513 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004514 &cipher_id );
4515 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004516 {
4517 status = PSA_ERROR_NOT_SUPPORTED;
4518 goto cleanup;
4519 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004521 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004522 {
4523#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004524 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004525 operation->core_alg = PSA_ALG_CCM;
4526 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004527 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4528 * The call to mbedtls_ccm_encrypt_and_tag or
4529 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004530 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004531 {
4532 status = PSA_ERROR_INVALID_ARGUMENT;
4533 goto cleanup;
4534 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004535 mbedtls_ccm_init( &operation->ctx.ccm );
4536 status = mbedtls_to_psa_error(
4537 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004538 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004539 (unsigned int) key_bits ) );
4540 if( status != 0 )
4541 goto cleanup;
4542 break;
4543#endif /* MBEDTLS_CCM_C */
4544
4545#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004546 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004547 operation->core_alg = PSA_ALG_GCM;
4548 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004549 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4550 * The call to mbedtls_gcm_crypt_and_tag or
4551 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004552 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004553 {
4554 status = PSA_ERROR_INVALID_ARGUMENT;
4555 goto cleanup;
4556 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004557 mbedtls_gcm_init( &operation->ctx.gcm );
4558 status = mbedtls_to_psa_error(
4559 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004560 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004561 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004562 if( status != 0 )
4563 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004564 break;
4565#endif /* MBEDTLS_GCM_C */
4566
Gilles Peskine26869f22019-05-06 15:25:00 +02004567#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004568 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004569 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4570 operation->full_tag_length = 16;
4571 /* We only support the default tag length. */
4572 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004573 {
4574 status = PSA_ERROR_NOT_SUPPORTED;
4575 goto cleanup;
4576 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004577 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4578 status = mbedtls_to_psa_error(
4579 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004580 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004581 if( status != 0 )
4582 goto cleanup;
4583 break;
4584#endif /* MBEDTLS_CHACHAPOLY_C */
4585
Gilles Peskineedf9a652018-08-17 18:11:56 +02004586 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004587 status = PSA_ERROR_NOT_SUPPORTED;
4588 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004589 }
4590
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004591 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4592 {
4593 status = PSA_ERROR_INVALID_ARGUMENT;
4594 goto cleanup;
4595 }
4596 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004597
Gilles Peskineedf9a652018-08-17 18:11:56 +02004598 return( PSA_SUCCESS );
4599
4600cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004601 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004602 return( status );
4603}
4604
Ronald Croncf56a0a2020-08-04 09:51:30 +02004605psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004606 psa_algorithm_t alg,
4607 const uint8_t *nonce,
4608 size_t nonce_length,
4609 const uint8_t *additional_data,
4610 size_t additional_data_length,
4611 const uint8_t *plaintext,
4612 size_t plaintext_length,
4613 uint8_t *ciphertext,
4614 size_t ciphertext_size,
4615 size_t *ciphertext_length )
4616{
mohammad16035955c982018-04-26 00:53:03 +03004617 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004618 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004619 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004620
mohammad1603f08a5502018-06-03 15:05:47 +03004621 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004622
Ronald Croncf56a0a2020-08-04 09:51:30 +02004623 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004624 if( status != PSA_SUCCESS )
4625 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004626
Gilles Peskineedf9a652018-08-17 18:11:56 +02004627 /* For all currently supported modes, the tag is at the end of the
4628 * ciphertext. */
4629 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4630 {
4631 status = PSA_ERROR_BUFFER_TOO_SMALL;
4632 goto exit;
4633 }
4634 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004635
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004636#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004637 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004638 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004639 status = mbedtls_to_psa_error(
4640 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4641 MBEDTLS_GCM_ENCRYPT,
4642 plaintext_length,
4643 nonce, nonce_length,
4644 additional_data, additional_data_length,
4645 plaintext, ciphertext,
4646 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004647 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004648 else
4649#endif /* MBEDTLS_GCM_C */
4650#if defined(MBEDTLS_CCM_C)
4651 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004652 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004653 status = mbedtls_to_psa_error(
4654 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4655 plaintext_length,
4656 nonce, nonce_length,
4657 additional_data,
4658 additional_data_length,
4659 plaintext, ciphertext,
4660 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004661 }
mohammad16035c8845f2018-05-09 05:40:09 -07004662 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004663#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004664#if defined(MBEDTLS_CHACHAPOLY_C)
4665 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4666 {
4667 if( nonce_length != 12 || operation.tag_length != 16 )
4668 {
4669 status = PSA_ERROR_NOT_SUPPORTED;
4670 goto exit;
4671 }
4672 status = mbedtls_to_psa_error(
4673 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4674 plaintext_length,
4675 nonce,
4676 additional_data,
4677 additional_data_length,
4678 plaintext,
4679 ciphertext,
4680 tag ) );
4681 }
4682 else
4683#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004684 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004685 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004686 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004687 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004688
Gilles Peskineedf9a652018-08-17 18:11:56 +02004689 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4690 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004691
Gilles Peskineedf9a652018-08-17 18:11:56 +02004692exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004693 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004694 if( status == PSA_SUCCESS )
4695 *ciphertext_length = plaintext_length + operation.tag_length;
4696 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004697}
4698
Gilles Peskineee652a32018-06-01 19:23:52 +02004699/* Locate the tag in a ciphertext buffer containing the encrypted data
4700 * followed by the tag. Return the length of the part preceding the tag in
4701 * *plaintext_length. This is the size of the plaintext in modes where
4702 * the encrypted data has the same size as the plaintext, such as
4703 * CCM and GCM. */
4704static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4705 const uint8_t *ciphertext,
4706 size_t ciphertext_length,
4707 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004708 const uint8_t **p_tag )
4709{
4710 size_t payload_length;
4711 if( tag_length > ciphertext_length )
4712 return( PSA_ERROR_INVALID_ARGUMENT );
4713 payload_length = ciphertext_length - tag_length;
4714 if( payload_length > plaintext_size )
4715 return( PSA_ERROR_BUFFER_TOO_SMALL );
4716 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004717 return( PSA_SUCCESS );
4718}
4719
Ronald Croncf56a0a2020-08-04 09:51:30 +02004720psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004721 psa_algorithm_t alg,
4722 const uint8_t *nonce,
4723 size_t nonce_length,
4724 const uint8_t *additional_data,
4725 size_t additional_data_length,
4726 const uint8_t *ciphertext,
4727 size_t ciphertext_length,
4728 uint8_t *plaintext,
4729 size_t plaintext_size,
4730 size_t *plaintext_length )
4731{
mohammad16035955c982018-04-26 00:53:03 +03004732 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004733 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004735
Gilles Peskineee652a32018-06-01 19:23:52 +02004736 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004737
Ronald Croncf56a0a2020-08-04 09:51:30 +02004738 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004739 if( status != PSA_SUCCESS )
4740 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004741
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004742 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4743 ciphertext, ciphertext_length,
4744 plaintext_size, &tag );
4745 if( status != PSA_SUCCESS )
4746 goto exit;
4747
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004748#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004749 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004750 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004751 status = mbedtls_to_psa_error(
4752 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4753 ciphertext_length - operation.tag_length,
4754 nonce, nonce_length,
4755 additional_data,
4756 additional_data_length,
4757 tag, operation.tag_length,
4758 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004759 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004760 else
4761#endif /* MBEDTLS_GCM_C */
4762#if defined(MBEDTLS_CCM_C)
4763 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004764 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004765 status = mbedtls_to_psa_error(
4766 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4767 ciphertext_length - operation.tag_length,
4768 nonce, nonce_length,
4769 additional_data,
4770 additional_data_length,
4771 ciphertext, plaintext,
4772 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004773 }
mohammad160339574652018-06-01 04:39:53 -07004774 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004775#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004776#if defined(MBEDTLS_CHACHAPOLY_C)
4777 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4778 {
4779 if( nonce_length != 12 || operation.tag_length != 16 )
4780 {
4781 status = PSA_ERROR_NOT_SUPPORTED;
4782 goto exit;
4783 }
4784 status = mbedtls_to_psa_error(
4785 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4786 ciphertext_length - operation.tag_length,
4787 nonce,
4788 additional_data,
4789 additional_data_length,
4790 tag,
4791 ciphertext,
4792 plaintext ) );
4793 }
4794 else
4795#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004796 {
mohammad1603554faad2018-06-03 15:07:38 +03004797 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004798 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004799
Gilles Peskineedf9a652018-08-17 18:11:56 +02004800 if( status != PSA_SUCCESS && plaintext_size != 0 )
4801 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004802
Gilles Peskineedf9a652018-08-17 18:11:56 +02004803exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004804 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004805 if( status == PSA_SUCCESS )
4806 *plaintext_length = ciphertext_length - operation.tag_length;
4807 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004808}
4809
Gilles Peskinea0655c32018-04-30 17:06:50 +02004810
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004811
Gilles Peskine20035e32018-02-03 22:44:14 +01004812/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813/* Generators */
4814/****************************************************************/
4815
John Durkop07cc04a2020-11-16 22:08:34 -08004816#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4817 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4818 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4819#define AT_LEAST_ONE_BUILTIN_KDF
4820#endif
4821
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004822#define HKDF_STATE_INIT 0 /* no input yet */
4823#define HKDF_STATE_STARTED 1 /* got salt */
4824#define HKDF_STATE_KEYED 2 /* got key */
4825#define HKDF_STATE_OUTPUT 3 /* output started */
4826
Gilles Peskinecbe66502019-05-16 16:59:18 +02004827static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004828 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4831 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004833 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004834}
4835
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004836psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004837{
4838 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004839 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004840 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004841 {
4842 /* The object has (apparently) been initialized but it is not
4843 * in use. It's ok to call abort on such an object, and there's
4844 * nothing to do. */
4845 }
4846 else
John Durkopd0321952020-10-29 21:37:36 -07004847#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004848 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004849 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004850 mbedtls_free( operation->ctx.hkdf.info );
4851 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004852 }
John Durkop07cc04a2020-11-16 22:08:34 -08004853 else
4854#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4855#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4856 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4857 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004859 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004860 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004861 if( operation->ctx.tls12_prf.seed != NULL )
4862 {
4863 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4864 operation->ctx.tls12_prf.seed_length );
4865 mbedtls_free( operation->ctx.tls12_prf.seed );
4866 }
4867
4868 if( operation->ctx.tls12_prf.label != NULL )
4869 {
4870 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4871 operation->ctx.tls12_prf.label_length );
4872 mbedtls_free( operation->ctx.tls12_prf.label );
4873 }
4874
4875 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4876
4877 /* We leave the fields Ai and output_block to be erased safely by the
4878 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004879 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004880 else
John Durkop07cc04a2020-11-16 22:08:34 -08004881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4882 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004883 {
4884 status = PSA_ERROR_BAD_STATE;
4885 }
Janos Follath083036a2019-06-11 10:22:26 +01004886 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004887 return( status );
4888}
4889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004891 size_t *capacity)
4892{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004894 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004896 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004897 }
4898
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004900 return( PSA_SUCCESS );
4901}
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004904 size_t capacity )
4905{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004907 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004911 return( PSA_SUCCESS );
4912}
4913
John Durkopd0321952020-10-29 21:37:36 -07004914#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004916 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004917static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004918 psa_algorithm_t hash_alg,
4919 uint8_t *output,
4920 size_t output_length )
4921{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004922 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004923 psa_status_t status;
4924
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004925 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4926 return( PSA_ERROR_BAD_STATE );
4927 hkdf->state = HKDF_STATE_OUTPUT;
4928
Gilles Peskinebef7f142018-07-12 17:22:21 +02004929 while( output_length != 0 )
4930 {
4931 /* Copy what remains of the current block */
4932 uint8_t n = hash_length - hkdf->offset_in_block;
4933 if( n > output_length )
4934 n = (uint8_t) output_length;
4935 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4936 output += n;
4937 output_length -= n;
4938 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004939 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004940 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004941 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004942 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004943 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004944 * object was corrupted or if this function is called directly
4945 * inside the library. */
4946 if( hkdf->block_number == 0xff )
4947 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004948
4949 /* We need a new block */
4950 ++hkdf->block_number;
4951 hkdf->offset_in_block = 0;
4952 status = psa_hmac_setup_internal( &hkdf->hmac,
4953 hkdf->prk, hash_length,
4954 hash_alg );
4955 if( status != PSA_SUCCESS )
4956 return( status );
4957 if( hkdf->block_number != 1 )
4958 {
4959 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4960 hkdf->output_block,
4961 hash_length );
4962 if( status != PSA_SUCCESS )
4963 return( status );
4964 }
4965 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4966 hkdf->info,
4967 hkdf->info_length );
4968 if( status != PSA_SUCCESS )
4969 return( status );
4970 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4971 &hkdf->block_number, 1 );
4972 if( status != PSA_SUCCESS )
4973 return( status );
4974 status = psa_hmac_finish_internal( &hkdf->hmac,
4975 hkdf->output_block,
4976 sizeof( hkdf->output_block ) );
4977 if( status != PSA_SUCCESS )
4978 return( status );
4979 }
4980
4981 return( PSA_SUCCESS );
4982}
John Durkop07cc04a2020-11-16 22:08:34 -08004983#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004984
John Durkop07cc04a2020-11-16 22:08:34 -08004985#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4986 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004987static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4988 psa_tls12_prf_key_derivation_t *tls12_prf,
4989 psa_algorithm_t alg )
4990{
4991 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004992 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004993 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4994 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004995
Janos Follath7742fee2019-06-17 12:58:10 +01004996 /* We can't be wanting more output after block 0xff, otherwise
4997 * the capacity check in psa_key_derivation_output_bytes() would have
4998 * prevented this call. It could happen only if the operation
4999 * object was corrupted or if this function is called directly
5000 * inside the library. */
5001 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005002 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005003
5004 /* We need a new block */
5005 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005006 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005007
5008 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5009 *
5010 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5011 *
5012 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5013 * HMAC_hash(secret, A(2) + seed) +
5014 * HMAC_hash(secret, A(3) + seed) + ...
5015 *
5016 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005017 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005018 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005019 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005020 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005021 * is currently extracted as `output_block` and where i is
5022 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005023 */
5024
Janos Follathea29bfb2019-06-19 12:21:20 +01005025 /* Save the hash context before using it, to preserve the hash state with
5026 * only the inner padding in it. We need this, because inner padding depends
5027 * on the key (secret in the RFC's terminology). */
5028 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5029 if( status != PSA_SUCCESS )
5030 goto cleanup;
5031
5032 /* Calculate A(i) where i = tls12_prf->block_number. */
5033 if( tls12_prf->block_number == 1 )
5034 {
5035 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5036 * the variable seed and in this instance means it in the context of the
5037 * P_hash function, where seed = label + seed.) */
5038 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5039 tls12_prf->label, tls12_prf->label_length );
5040 if( status != PSA_SUCCESS )
5041 goto cleanup;
5042 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5043 tls12_prf->seed, tls12_prf->seed_length );
5044 if( status != PSA_SUCCESS )
5045 goto cleanup;
5046 }
5047 else
5048 {
5049 /* A(i) = HMAC_hash(secret, A(i-1)) */
5050 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5051 tls12_prf->Ai, hash_length );
5052 if( status != PSA_SUCCESS )
5053 goto cleanup;
5054 }
5055
5056 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5057 tls12_prf->Ai, hash_length );
5058 if( status != PSA_SUCCESS )
5059 goto cleanup;
5060 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5061 if( status != PSA_SUCCESS )
5062 goto cleanup;
5063
5064 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5065 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5066 tls12_prf->Ai, hash_length );
5067 if( status != PSA_SUCCESS )
5068 goto cleanup;
5069 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5070 tls12_prf->label, tls12_prf->label_length );
5071 if( status != PSA_SUCCESS )
5072 goto cleanup;
5073 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5074 tls12_prf->seed, tls12_prf->seed_length );
5075 if( status != PSA_SUCCESS )
5076 goto cleanup;
5077 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5078 tls12_prf->output_block, hash_length );
5079 if( status != PSA_SUCCESS )
5080 goto cleanup;
5081 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5082 if( status != PSA_SUCCESS )
5083 goto cleanup;
5084
Janos Follath7742fee2019-06-17 12:58:10 +01005085
5086cleanup:
5087
Janos Follathea29bfb2019-06-19 12:21:20 +01005088 cleanup_status = psa_hash_abort( &backup );
5089 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5090 status = cleanup_status;
5091
5092 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005093}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005094
Janos Follath844eb0e2019-06-19 12:10:49 +01005095static psa_status_t psa_key_derivation_tls12_prf_read(
5096 psa_tls12_prf_key_derivation_t *tls12_prf,
5097 psa_algorithm_t alg,
5098 uint8_t *output,
5099 size_t output_length )
5100{
5101 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005102 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005103 psa_status_t status;
5104 uint8_t offset, length;
5105
5106 while( output_length != 0 )
5107 {
5108 /* Check if we have fully processed the current block. */
5109 if( tls12_prf->left_in_block == 0 )
5110 {
5111 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5112 alg );
5113 if( status != PSA_SUCCESS )
5114 return( status );
5115
5116 continue;
5117 }
5118
5119 if( tls12_prf->left_in_block > output_length )
5120 length = (uint8_t) output_length;
5121 else
5122 length = tls12_prf->left_in_block;
5123
5124 offset = hash_length - tls12_prf->left_in_block;
5125 memcpy( output, tls12_prf->output_block + offset, length );
5126 output += length;
5127 output_length -= length;
5128 tls12_prf->left_in_block -= length;
5129 }
5130
5131 return( PSA_SUCCESS );
5132}
John Durkop07cc04a2020-11-16 22:08:34 -08005133#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5134 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005135
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005136psa_status_t psa_key_derivation_output_bytes(
5137 psa_key_derivation_operation_t *operation,
5138 uint8_t *output,
5139 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005140{
5141 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005142 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005143
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005144 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005145 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005146 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005147 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005148 }
5149
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005150 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005151 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005152 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005153 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005154 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005155 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005156 goto exit;
5157 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005158 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005159 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005160 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005161 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005162 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5163 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005164 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005165 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005166 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005167 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005169
John Durkopd0321952020-10-29 21:37:36 -07005170#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005171 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005172 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005173 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005174 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005175 output, output_length );
5176 }
Janos Follathadbec812019-06-14 11:05:39 +01005177 else
John Durkop07cc04a2020-11-16 22:08:34 -08005178#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5179#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5180 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005181 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005182 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005183 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005185 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005186 output_length );
5187 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005188 else
John Durkop07cc04a2020-11-16 22:08:34 -08005189#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5190 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005191 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005192 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005193 return( PSA_ERROR_BAD_STATE );
5194 }
5195
5196exit:
5197 if( status != PSA_SUCCESS )
5198 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005199 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 * This allows us to differentiate between exhausted operations and
5201 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5202 * operations. */
5203 psa_algorithm_t alg = operation->alg;
5204 psa_key_derivation_abort( operation );
5205 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005206 memset( output, '!', output_length );
5207 }
5208 return( status );
5209}
5210
David Brown7807bf72021-02-09 16:28:23 -07005211#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005212static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5213{
5214 if( data_size >= 8 )
5215 mbedtls_des_key_set_parity( data );
5216 if( data_size >= 16 )
5217 mbedtls_des_key_set_parity( data + 8 );
5218 if( data_size >= 24 )
5219 mbedtls_des_key_set_parity( data + 16 );
5220}
David Brown7807bf72021-02-09 16:28:23 -07005221#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005222
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005223static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005224 psa_key_slot_t *slot,
5225 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227{
5228 uint8_t *data = NULL;
5229 size_t bytes = PSA_BITS_TO_BYTES( bits );
5230 psa_status_t status;
5231
Gilles Peskine8e338702019-07-30 20:06:31 +02005232 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005233 return( PSA_ERROR_INVALID_ARGUMENT );
5234 if( bits % 8 != 0 )
5235 return( PSA_ERROR_INVALID_ARGUMENT );
5236 data = mbedtls_calloc( 1, bytes );
5237 if( data == NULL )
5238 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5239
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005240 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005241 if( status != PSA_SUCCESS )
5242 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005243#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005244 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005245 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005246#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005247
5248 status = psa_allocate_buffer_to_slot( slot, bytes );
5249 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005250 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005251
Ronald Cronbf33c932020-11-28 18:06:53 +01005252 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005253 psa_key_attributes_t attributes = {
5254 .core = slot->attr
5255 };
5256
Ronald Cronbf33c932020-11-28 18:06:53 +01005257 status = psa_driver_wrapper_import_key( &attributes,
5258 data, bytes,
5259 slot->key.data,
5260 slot->key.bytes,
5261 &slot->key.bytes, &bits );
5262 if( bits != slot->attr.bits )
5263 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264
5265exit:
5266 mbedtls_free( data );
5267 return( status );
5268}
5269
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005270psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005272 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005273{
5274 psa_status_t status;
5275 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005276 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005277
Ronald Cron81709fc2020-11-14 12:10:32 +01005278 *key = MBEDTLS_SVC_KEY_ID_INIT;
5279
Gilles Peskine0f84d622019-09-12 19:03:13 +02005280 /* Reject any attempt to create a zero-length key so that we don't
5281 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5282 if( psa_get_key_bits( attributes ) == 0 )
5283 return( PSA_ERROR_INVALID_ARGUMENT );
5284
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005285 if( ! operation->can_output_key )
5286 return( PSA_ERROR_NOT_PERMITTED );
5287
Ronald Cron81709fc2020-11-14 12:10:32 +01005288 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5289 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5291 if( driver != NULL )
5292 {
5293 /* Deriving a key in a secure element is not implemented yet. */
5294 status = PSA_ERROR_NOT_SUPPORTED;
5295 }
5296#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005297 if( status == PSA_SUCCESS )
5298 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005299 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005300 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005301 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005302 }
5303 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005304 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005305 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005306 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005307
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005308 return( status );
5309}
5310
Gilles Peskineeab56e42018-07-12 17:12:33 +02005311
5312
5313/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005314/* Key derivation */
5315/****************************************************************/
5316
Steven Cooreman932ffb72021-02-15 12:14:32 +01005317#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005318static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005320 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321{
John Durkop07cc04a2020-11-16 22:08:34 -08005322 int is_kdf_alg_supported;
5323
Janos Follath5fe19732019-06-20 15:09:30 +01005324 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5325 * initialisers for this union leave some bytes unspecified.) */
5326 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5327
Gilles Peskine969c5d62019-01-16 15:53:06 +01005328 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005329#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005330 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5331 is_kdf_alg_supported = 1;
5332 else
5333#endif
5334#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5335 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5336 is_kdf_alg_supported = 1;
5337 else
5338#endif
5339#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5340 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5341 is_kdf_alg_supported = 1;
5342 else
5343#endif
5344 is_kdf_alg_supported = 0;
5345
5346 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005347 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005348 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005349 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350 if( hash_size == 0 )
5351 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005352 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5353 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005354 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005355 {
5356 return( PSA_ERROR_NOT_SUPPORTED );
5357 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005358 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005359 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005360 }
John Durkop07cc04a2020-11-16 22:08:34 -08005361
5362 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005363}
John Durkop07cc04a2020-11-16 22:08:34 -08005364#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005365
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005366psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005367 psa_algorithm_t alg )
5368{
5369 psa_status_t status;
5370
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005371 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005372 return( PSA_ERROR_BAD_STATE );
5373
Gilles Peskine6843c292019-01-18 16:44:49 +01005374 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5375 return( PSA_ERROR_INVALID_ARGUMENT );
5376 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005377 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005378#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005379 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005381#else
5382 return( PSA_ERROR_NOT_SUPPORTED );
5383#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005384 }
5385 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5386 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005387#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005389#else
5390 return( PSA_ERROR_NOT_SUPPORTED );
5391#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005392 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005393 else
5394 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395
5396 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005398 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005399}
5400
John Durkopd0321952020-10-29 21:37:36 -07005401#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005402static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005403 psa_algorithm_t hash_alg,
5404 psa_key_derivation_step_t step,
5405 const uint8_t *data,
5406 size_t data_length )
5407{
5408 psa_status_t status;
5409 switch( step )
5410 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005411 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005412 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005413 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005414 status = psa_hmac_setup_internal( &hkdf->hmac,
5415 data, data_length,
5416 hash_alg );
5417 if( status != PSA_SUCCESS )
5418 return( status );
5419 hkdf->state = HKDF_STATE_STARTED;
5420 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005421 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005422 /* If no salt was provided, use an empty salt. */
5423 if( hkdf->state == HKDF_STATE_INIT )
5424 {
5425 status = psa_hmac_setup_internal( &hkdf->hmac,
5426 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005427 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428 if( status != PSA_SUCCESS )
5429 return( status );
5430 hkdf->state = HKDF_STATE_STARTED;
5431 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005432 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005434 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5435 data, data_length );
5436 if( status != PSA_SUCCESS )
5437 return( status );
5438 status = psa_hmac_finish_internal( &hkdf->hmac,
5439 hkdf->prk,
5440 sizeof( hkdf->prk ) );
5441 if( status != PSA_SUCCESS )
5442 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005443 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005444 hkdf->block_number = 0;
5445 hkdf->state = HKDF_STATE_KEYED;
5446 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005447 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005448 if( hkdf->state == HKDF_STATE_OUTPUT )
5449 return( PSA_ERROR_BAD_STATE );
5450 if( hkdf->info_set )
5451 return( PSA_ERROR_BAD_STATE );
5452 hkdf->info_length = data_length;
5453 if( data_length != 0 )
5454 {
5455 hkdf->info = mbedtls_calloc( 1, data_length );
5456 if( hkdf->info == NULL )
5457 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5458 memcpy( hkdf->info, data, data_length );
5459 }
5460 hkdf->info_set = 1;
5461 return( PSA_SUCCESS );
5462 default:
5463 return( PSA_ERROR_INVALID_ARGUMENT );
5464 }
5465}
John Durkop07cc04a2020-11-16 22:08:34 -08005466#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005467
John Durkop07cc04a2020-11-16 22:08:34 -08005468#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5469 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005470static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5471 const uint8_t *data,
5472 size_t data_length )
5473{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005474 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005475 return( PSA_ERROR_BAD_STATE );
5476
Janos Follathd6dce9f2019-07-04 09:11:38 +01005477 if( data_length != 0 )
5478 {
5479 prf->seed = mbedtls_calloc( 1, data_length );
5480 if( prf->seed == NULL )
5481 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005482
Janos Follathd6dce9f2019-07-04 09:11:38 +01005483 memcpy( prf->seed, data, data_length );
5484 prf->seed_length = data_length;
5485 }
Janos Follathf08e2652019-06-13 09:05:41 +01005486
Gilles Peskine43f958b2020-12-13 14:55:14 +01005487 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005488
5489 return( PSA_SUCCESS );
5490}
5491
Janos Follath81550542019-06-13 14:26:34 +01005492static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5493 psa_algorithm_t hash_alg,
5494 const uint8_t *data,
5495 size_t data_length )
5496{
5497 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005498 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005499 return( PSA_ERROR_BAD_STATE );
5500
5501 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5502 if( status != PSA_SUCCESS )
5503 return( status );
5504
Gilles Peskine43f958b2020-12-13 14:55:14 +01005505 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005506
5507 return( PSA_SUCCESS );
5508}
5509
Janos Follath63028dd2019-06-13 09:15:47 +01005510static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5511 const uint8_t *data,
5512 size_t data_length )
5513{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005514 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005515 return( PSA_ERROR_BAD_STATE );
5516
Janos Follathd6dce9f2019-07-04 09:11:38 +01005517 if( data_length != 0 )
5518 {
5519 prf->label = mbedtls_calloc( 1, data_length );
5520 if( prf->label == NULL )
5521 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005522
Janos Follathd6dce9f2019-07-04 09:11:38 +01005523 memcpy( prf->label, data, data_length );
5524 prf->label_length = data_length;
5525 }
Janos Follath63028dd2019-06-13 09:15:47 +01005526
Gilles Peskine43f958b2020-12-13 14:55:14 +01005527 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005528
5529 return( PSA_SUCCESS );
5530}
5531
Janos Follathb03233e2019-06-11 15:30:30 +01005532static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5533 psa_algorithm_t hash_alg,
5534 psa_key_derivation_step_t step,
5535 const uint8_t *data,
5536 size_t data_length )
5537{
Janos Follathb03233e2019-06-11 15:30:30 +01005538 switch( step )
5539 {
Janos Follathf08e2652019-06-13 09:05:41 +01005540 case PSA_KEY_DERIVATION_INPUT_SEED:
5541 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005542 case PSA_KEY_DERIVATION_INPUT_SECRET:
5543 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005544 case PSA_KEY_DERIVATION_INPUT_LABEL:
5545 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005546 default:
5547 return( PSA_ERROR_INVALID_ARGUMENT );
5548 }
5549}
John Durkop07cc04a2020-11-16 22:08:34 -08005550#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5551 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5552
5553#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5554static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5555 psa_tls12_prf_key_derivation_t *prf,
5556 psa_algorithm_t hash_alg,
5557 const uint8_t *data,
5558 size_t data_length )
5559{
5560 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005561 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005562 uint8_t *cur = pms;
5563
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005564 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005565 return( PSA_ERROR_INVALID_ARGUMENT );
5566
5567 /* Quoting RFC 4279, Section 2:
5568 *
5569 * The premaster secret is formed as follows: if the PSK is N octets
5570 * long, concatenate a uint16 with the value N, N zero octets, a second
5571 * uint16 with the value N, and the PSK itself.
5572 */
5573
5574 *cur++ = ( data_length >> 8 ) & 0xff;
5575 *cur++ = ( data_length >> 0 ) & 0xff;
5576 memset( cur, 0, data_length );
5577 cur += data_length;
5578 *cur++ = pms[0];
5579 *cur++ = pms[1];
5580 memcpy( cur, data, data_length );
5581 cur += data_length;
5582
5583 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5584
5585 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5586 return( status );
5587}
Janos Follath6660f0e2019-06-17 08:44:03 +01005588
5589static psa_status_t psa_tls12_prf_psk_to_ms_input(
5590 psa_tls12_prf_key_derivation_t *prf,
5591 psa_algorithm_t hash_alg,
5592 psa_key_derivation_step_t step,
5593 const uint8_t *data,
5594 size_t data_length )
5595{
5596 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005597 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005598 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5599 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005600 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005601
5602 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5603}
John Durkop07cc04a2020-11-16 22:08:34 -08005604#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005605
Gilles Peskineb8965192019-09-24 16:21:10 +02005606/** Check whether the given key type is acceptable for the given
5607 * input step of a key derivation.
5608 *
5609 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5610 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5611 * Both secret and non-secret inputs can alternatively have the type
5612 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5613 * that the input was passed as a buffer rather than via a key object.
5614 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005615static int psa_key_derivation_check_input_type(
5616 psa_key_derivation_step_t step,
5617 psa_key_type_t key_type )
5618{
5619 switch( step )
5620 {
5621 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005622 if( key_type == PSA_KEY_TYPE_DERIVE )
5623 return( PSA_SUCCESS );
5624 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005625 return( PSA_SUCCESS );
5626 break;
5627 case PSA_KEY_DERIVATION_INPUT_LABEL:
5628 case PSA_KEY_DERIVATION_INPUT_SALT:
5629 case PSA_KEY_DERIVATION_INPUT_INFO:
5630 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005631 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5632 return( PSA_SUCCESS );
5633 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005634 return( PSA_SUCCESS );
5635 break;
5636 }
5637 return( PSA_ERROR_INVALID_ARGUMENT );
5638}
5639
Janos Follathb80a94e2019-06-12 15:54:46 +01005640static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005641 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005642 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005643 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005644 const uint8_t *data,
5645 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005646{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005647 psa_status_t status;
5648 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5649
5650 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005651 if( status != PSA_SUCCESS )
5652 goto exit;
5653
John Durkopd0321952020-10-29 21:37:36 -07005654#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005655 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005656 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005657 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005658 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005659 step, data, data_length );
5660 }
John Durkop07cc04a2020-11-16 22:08:34 -08005661 else
5662#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5663#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5664 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665 {
Janos Follathb03233e2019-06-11 15:30:30 +01005666 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5667 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5668 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005669 }
John Durkop07cc04a2020-11-16 22:08:34 -08005670 else
5671#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5672#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5673 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005674 {
5675 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5676 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5677 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678 }
5679 else
John Durkop07cc04a2020-11-16 22:08:34 -08005680#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005681 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005682 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005683 (void) data;
5684 (void) data_length;
5685 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005686 return( PSA_ERROR_BAD_STATE );
5687 }
5688
Gilles Peskine224b0d62019-09-23 18:13:17 +02005689exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005690 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005691 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005692 return( status );
5693}
5694
Janos Follath51f4a0f2019-06-14 11:35:55 +01005695psa_status_t psa_key_derivation_input_bytes(
5696 psa_key_derivation_operation_t *operation,
5697 psa_key_derivation_step_t step,
5698 const uint8_t *data,
5699 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005700{
Gilles Peskineb8965192019-09-24 16:21:10 +02005701 return( psa_key_derivation_input_internal( operation, step,
5702 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005703 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005704}
5705
Janos Follath51f4a0f2019-06-14 11:35:55 +01005706psa_status_t psa_key_derivation_input_key(
5707 psa_key_derivation_operation_t *operation,
5708 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005709 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005710{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005711 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005712 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005713 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005714
Ronald Cron5c522922020-11-14 16:35:34 +01005715 status = psa_get_and_lock_transparent_key_slot_with_policy(
5716 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005717 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005718 {
5719 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005720 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005721 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005722
5723 /* Passing a key object as a SECRET input unlocks the permission
5724 * to output to a key object. */
5725 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5726 operation->can_output_key = 1;
5727
Ronald Cronf95a2b12020-10-22 15:24:49 +02005728 status = psa_key_derivation_input_internal( operation,
5729 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005730 slot->key.data,
5731 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005732
Ronald Cron5c522922020-11-14 16:35:34 +01005733 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005734
Ronald Cron5c522922020-11-14 16:35:34 +01005735 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005737
5738
5739
5740/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005741/* Key agreement */
5742/****************************************************************/
5743
John Durkop6ba40d12020-11-10 08:50:04 -08005744#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005745static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5746 size_t peer_key_length,
5747 const mbedtls_ecp_keypair *our_key,
5748 uint8_t *shared_secret,
5749 size_t shared_secret_size,
5750 size_t *shared_secret_length )
5751{
Steven Cooremand4867872020-08-05 16:31:39 +02005752 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005753 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005754 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005755 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005756 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005757 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005758
Ronald Cron90857082020-11-25 14:58:33 +01005759 status = mbedtls_psa_ecp_load_representation(
5760 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005761 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005762 peer_key,
5763 peer_key_length,
5764 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005765 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005766 goto exit;
5767
Jaeden Amero97271b32019-01-10 19:38:51 +00005768 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005769 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005770 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005771 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005772 status = mbedtls_to_psa_error(
5773 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5774 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005775 goto exit;
5776
Jaeden Amero97271b32019-01-10 19:38:51 +00005777 status = mbedtls_to_psa_error(
5778 mbedtls_ecdh_calc_secret( &ecdh,
5779 shared_secret_length,
5780 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005781 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005782 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005783 if( status != PSA_SUCCESS )
5784 goto exit;
5785 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5786 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005787
5788exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005789 if( status != PSA_SUCCESS )
5790 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005791 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005792 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005793 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005794
Jaeden Amero97271b32019-01-10 19:38:51 +00005795 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005796}
John Durkop6ba40d12020-11-10 08:50:04 -08005797#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005798
Gilles Peskine01d718c2018-09-18 12:01:02 +02005799#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5800
Gilles Peskine0216fe12019-04-11 21:23:21 +02005801static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5802 psa_key_slot_t *private_key,
5803 const uint8_t *peer_key,
5804 size_t peer_key_length,
5805 uint8_t *shared_secret,
5806 size_t shared_secret_size,
5807 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005809 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005810 {
John Durkop6ba40d12020-11-10 08:50:04 -08005811#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005812 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005813 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005814 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005815 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005816 psa_status_t status = mbedtls_psa_ecp_load_representation(
5817 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005818 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005819 private_key->key.data,
5820 private_key->key.bytes,
5821 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005822 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005823 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005824 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005825 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005826 shared_secret, shared_secret_size,
5827 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005828 mbedtls_ecp_keypair_free( ecp );
5829 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005830 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005831#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005832 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005833 (void) private_key;
5834 (void) peer_key;
5835 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005836 (void) shared_secret;
5837 (void) shared_secret_size;
5838 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005839 return( PSA_ERROR_NOT_SUPPORTED );
5840 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005841}
5842
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005843/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005844 * to potentially free embedded data structures and wipe confidential data.
5845 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005846static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005847 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005848 psa_key_slot_t *private_key,
5849 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005850 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005851{
5852 psa_status_t status;
5853 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5854 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005855 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005856
5857 /* Step 1: run the secret agreement algorithm to generate the shared
5858 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005859 status = psa_key_agreement_raw_internal( ka_alg,
5860 private_key,
5861 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005862 shared_secret,
5863 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005864 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005865 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005866 goto exit;
5867
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005868 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005869 * the shared secret. A shared secret is permitted wherever a key
5870 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005871 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005872 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005873 shared_secret,
5874 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005875exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005876 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005877 return( status );
5878}
5879
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005880psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005881 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005882 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005883 const uint8_t *peer_key,
5884 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005885{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005886 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005887 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005888 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005889
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005890 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005891 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005892 status = psa_get_and_lock_transparent_key_slot_with_policy(
5893 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005894 if( status != PSA_SUCCESS )
5895 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005896 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005897 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005898 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005899 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005900 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005901 else
5902 {
5903 /* If a private key has been added as SECRET, we allow the derived
5904 * key material to be used as a key in PSA Crypto. */
5905 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5906 operation->can_output_key = 1;
5907 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005908
Ronald Cron5c522922020-11-14 16:35:34 +01005909 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005910
Ronald Cron5c522922020-11-14 16:35:34 +01005911 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005912}
5913
Gilles Peskinebe697d82019-05-16 18:00:41 +02005914psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005915 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005916 const uint8_t *peer_key,
5917 size_t peer_key_length,
5918 uint8_t *output,
5919 size_t output_size,
5920 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005921{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005922 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005923 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005924 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005925
5926 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5927 {
5928 status = PSA_ERROR_INVALID_ARGUMENT;
5929 goto exit;
5930 }
Ronald Cron5c522922020-11-14 16:35:34 +01005931 status = psa_get_and_lock_transparent_key_slot_with_policy(
5932 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005933 if( status != PSA_SUCCESS )
5934 goto exit;
5935
5936 status = psa_key_agreement_raw_internal( alg, slot,
5937 peer_key, peer_key_length,
5938 output, output_size,
5939 output_length );
5940
5941exit:
5942 if( status != PSA_SUCCESS )
5943 {
5944 /* If an error happens and is not handled properly, the output
5945 * may be used as a key to protect sensitive data. Arrange for such
5946 * a key to be random, which is likely to result in decryption or
5947 * verification errors. This is better than filling the buffer with
5948 * some constant data such as zeros, which would result in the data
5949 * being protected with a reproducible, easily knowable key.
5950 */
5951 psa_generate_random( output, output_size );
5952 *output_length = output_size;
5953 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005954
Ronald Cron5c522922020-11-14 16:35:34 +01005955 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005956
Ronald Cron5c522922020-11-14 16:35:34 +01005957 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005958}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005959
5960
Gilles Peskine30524eb2020-11-13 17:02:26 +01005961
Gilles Peskine86a440b2018-11-12 18:39:40 +01005962/****************************************************************/
5963/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005964/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005965
Gilles Peskine30524eb2020-11-13 17:02:26 +01005966/** Initialize the PSA random generator.
5967 */
5968static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5969{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005970#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5971 memset( rng, 0, sizeof( *rng ) );
5972#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5973
Gilles Peskine30524eb2020-11-13 17:02:26 +01005974 /* Set default configuration if
5975 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5976 if( rng->entropy_init == NULL )
5977 rng->entropy_init = mbedtls_entropy_init;
5978 if( rng->entropy_free == NULL )
5979 rng->entropy_free = mbedtls_entropy_free;
5980
5981 rng->entropy_init( &rng->entropy );
5982#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5983 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5984 /* The PSA entropy injection feature depends on using NV seed as an entropy
5985 * source. Add NV seed as an entropy source for PSA entropy injection. */
5986 mbedtls_entropy_add_source( &rng->entropy,
5987 mbedtls_nv_seed_poll, NULL,
5988 MBEDTLS_ENTROPY_BLOCK_SIZE,
5989 MBEDTLS_ENTROPY_SOURCE_STRONG );
5990#endif
5991
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005992 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005993#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005994}
5995
5996/** Deinitialize the PSA random generator.
5997 */
5998static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5999{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006000#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6001 memset( rng, 0, sizeof( *rng ) );
6002#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006003 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006004 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006005#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006006}
6007
6008/** Seed the PSA random generator.
6009 */
6010static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6011{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006012#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6013 /* Do nothing: the external RNG seeds itself. */
6014 (void) rng;
6015 return( PSA_SUCCESS );
6016#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006017 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006018 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6019 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006020 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006021#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006022}
6023
Gilles Peskinee59236f2018-01-27 23:32:46 +01006024psa_status_t psa_generate_random( uint8_t *output,
6025 size_t output_size )
6026{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006027 GUARD_MODULE_INITIALIZED;
6028
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006029#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6030
6031 size_t output_length = 0;
6032 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6033 output, output_size,
6034 &output_length );
6035 if( status != PSA_SUCCESS )
6036 return( status );
6037 /* Breaking up a request into smaller chunks is currently not supported
6038 * for the extrernal RNG interface. */
6039 if( output_length != output_size )
6040 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6041 return( PSA_SUCCESS );
6042
6043#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6044
Gilles Peskine71ddab92021-01-04 21:01:07 +01006045 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006046 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006047 size_t request_size =
6048 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6049 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6050 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006051 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6052 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006053 if( ret != 0 )
6054 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006055 output_size -= request_size;
6056 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006057 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006058 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006059#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006060}
6061
Gilles Peskine8814fc42020-12-14 15:33:44 +01006062/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006063 *
6064 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6065 * `psa_generate_random(...)`. The state parameter is ignored since the
6066 * PSA API doesn't support passing an explicit state.
6067 *
6068 * In the non-external case, psa_generate_random() calls an
6069 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6070 * and semantics as mbedtls_psa_get_random(). As an optimization,
6071 * instead of doing this back-and-forth between the PSA API and the
6072 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6073 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006074 */
6075#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6076int mbedtls_psa_get_random( void *p_rng,
6077 unsigned char *output,
6078 size_t output_size )
6079{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006080 /* This function takes a pointer to the RNG state because that's what
6081 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6082 * its own state internally and doesn't let the caller access that state.
6083 * So we just ignore the state parameter, and in practice we'll pass
6084 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006085 (void) p_rng;
6086 psa_status_t status = psa_generate_random( output, output_size );
6087 if( status == PSA_SUCCESS )
6088 return( 0 );
6089 else
6090 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6091}
6092#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6093
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006094#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6095#include "mbedtls/entropy_poll.h"
6096
Gilles Peskine7228da22019-07-15 11:06:15 +02006097psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006098 size_t seed_size )
6099{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006100 if( global_data.initialized )
6101 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006102
6103 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6104 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6105 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6106 return( PSA_ERROR_INVALID_ARGUMENT );
6107
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006108 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006109}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006110#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006111
Ronald Cron3772afe2021-02-08 16:10:05 +01006112/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006113 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006114 * \param type The key type
6115 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006116 *
6117 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006118 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006119 * \retval #PSA_ERROR_INVALID_ARGUMENT
6120 * The size in bits of the key is not valid.
6121 * \retval #PSA_ERROR_NOT_SUPPORTED
6122 * The type and/or the size in bits of the key or the combination of
6123 * the two is not supported.
6124 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006125static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6126 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006128 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006129
Gilles Peskinee59236f2018-01-27 23:32:46 +01006130 if( key_type_is_raw_bytes( type ) )
6131 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006132 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006133 if( status != PSA_SUCCESS )
6134 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006135 }
6136 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006137#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006138 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6139 {
6140 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6141 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006142
Ronald Cron01b2aba2020-10-05 09:42:02 +02006143 /* Accept only byte-aligned keys, for the same reasons as
6144 * in psa_import_rsa_key(). */
6145 if( bits % 8 != 0 )
6146 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006147 }
6148 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006149#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006150
Ronald Cron5c4d3862020-12-07 11:07:24 +01006151#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006152 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6153 {
Ronald Crond81ab562021-02-16 09:01:16 +01006154 /* To avoid empty block, return successfully here. */
6155 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006156 }
6157 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006158#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006159 {
6160 return( PSA_ERROR_NOT_SUPPORTED );
6161 }
6162
6163 return( PSA_SUCCESS );
6164}
6165
Ronald Cron55ed0592020-10-05 10:30:40 +02006166psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006167 const psa_key_attributes_t *attributes,
6168 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006169{
6170 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006171 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006172
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006173 if( ( attributes->domain_parameters == NULL ) &&
6174 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006175 return( PSA_ERROR_INVALID_ARGUMENT );
6176
Ronald Cron01b2aba2020-10-05 09:42:02 +02006177 if( key_type_is_raw_bytes( type ) )
6178 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006179 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006180 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006181 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006182
David Brown12ca5032021-02-11 11:02:00 -07006183#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006184 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006185 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006186#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187 }
6188 else
6189
John Durkop07cc04a2020-11-16 22:08:34 -08006190#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006191 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006192 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006193 return( mbedtls_psa_rsa_generate_key( attributes,
6194 key_buffer,
6195 key_buffer_size,
6196 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006197 }
6198 else
John Durkop07cc04a2020-11-16 22:08:34 -08006199#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200
John Durkop9814fa22020-11-04 12:28:15 -08006201#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006202 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 {
Ronald Cron7023db52020-11-20 18:17:42 +01006204 return( mbedtls_psa_ecp_generate_key( attributes,
6205 key_buffer,
6206 key_buffer_size,
6207 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006208 }
6209 else
John Durkop9814fa22020-11-04 12:28:15 -08006210#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006211 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006212 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006213 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006214 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006215
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006216 return( PSA_SUCCESS );
6217}
Darryl Green0c6575a2018-11-07 16:05:30 +00006218
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006219psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006220 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006221{
6222 psa_status_t status;
6223 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006224 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006225 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006226
Ronald Cron81709fc2020-11-14 12:10:32 +01006227 *key = MBEDTLS_SVC_KEY_ID_INIT;
6228
Gilles Peskine0f84d622019-09-12 19:03:13 +02006229 /* Reject any attempt to create a zero-length key so that we don't
6230 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6231 if( psa_get_key_bits( attributes ) == 0 )
6232 return( PSA_ERROR_INVALID_ARGUMENT );
6233
Ronald Cron81709fc2020-11-14 12:10:32 +01006234 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6235 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006236 if( status != PSA_SUCCESS )
6237 goto exit;
6238
Ronald Cron977c2472020-10-13 08:32:21 +02006239 /* In the case of a transparent key or an opaque key stored in local
6240 * storage (thus not in the case of generating a key in a secure element
6241 * or cryptoprocessor with storage), we have to allocate a buffer to
6242 * hold the generated key material. */
6243 if( slot->key.data == NULL )
6244 {
6245 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6246 PSA_KEY_LOCATION_LOCAL_STORAGE )
6247 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006248 status = psa_validate_key_type_and_size_for_key_generation(
6249 attributes->core.type, attributes->core.bits );
6250 if( status != PSA_SUCCESS )
6251 goto exit;
6252
6253 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6254 attributes->core.type,
6255 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006256 }
6257 else
6258 {
6259 status = psa_driver_wrapper_get_key_buffer_size(
6260 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006261 if( status != PSA_SUCCESS )
6262 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006263 }
Ronald Cron977c2472020-10-13 08:32:21 +02006264
6265 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6266 if( status != PSA_SUCCESS )
6267 goto exit;
6268 }
6269
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006270 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006271 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006272
Ronald Cron2b56bc82020-10-05 10:02:26 +02006273 if( status != PSA_SUCCESS )
6274 psa_remove_key_data_from_memory( slot );
6275
Gilles Peskine11792082019-08-06 18:36:36 +02006276exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006277 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006278 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006279 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006280 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006281
Darryl Green0c6575a2018-11-07 16:05:30 +00006282 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006283}
6284
Gilles Peskinee59236f2018-01-27 23:32:46 +01006285/****************************************************************/
6286/* Module setup */
6287/****************************************************************/
6288
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006289#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006290psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6291 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6292 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6293{
6294 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6295 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006296 global_data.rng.entropy_init = entropy_init;
6297 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006298 return( PSA_SUCCESS );
6299}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006300#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006301
Gilles Peskinee59236f2018-01-27 23:32:46 +01006302void mbedtls_psa_crypto_free( void )
6303{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006304 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006305 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6306 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006307 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006308 }
6309 /* Wipe all remaining data, including configuration.
6310 * In particular, this sets all state indicator to the value
6311 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006312 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006313#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006314 /* Unregister all secure element drivers, so that we restart from
6315 * a pristine state. */
6316 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006317#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006318}
6319
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006320#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6321/** Recover a transaction that was interrupted by a power failure.
6322 *
6323 * This function is called during initialization, before psa_crypto_init()
6324 * returns. If this function returns a failure status, the initialization
6325 * fails.
6326 */
6327static psa_status_t psa_crypto_recover_transaction(
6328 const psa_crypto_transaction_t *transaction )
6329{
6330 switch( transaction->unknown.type )
6331 {
6332 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6333 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006334 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006335 * is implemented.
6336 * https://github.com/ARMmbed/mbed-crypto/issues/218
6337 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006338 default:
6339 /* We found an unsupported transaction in the storage.
6340 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006341 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006342 }
6343}
6344#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6345
Gilles Peskinee59236f2018-01-27 23:32:46 +01006346psa_status_t psa_crypto_init( void )
6347{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006348 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006349
Gilles Peskinec6b69072018-11-20 21:42:52 +01006350 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006351 if( global_data.initialized != 0 )
6352 return( PSA_SUCCESS );
6353
Gilles Peskine30524eb2020-11-13 17:02:26 +01006354 /* Initialize and seed the random generator. */
6355 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006356 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006357 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006358 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006359 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006360 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006361
Gilles Peskine66fb1262018-12-10 16:29:04 +01006362 status = psa_initialize_key_slots( );
6363 if( status != PSA_SUCCESS )
6364 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006365
Gilles Peskined9348f22019-10-01 15:22:29 +02006366#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6367 status = psa_init_all_se_drivers( );
6368 if( status != PSA_SUCCESS )
6369 goto exit;
6370#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6371
Gilles Peskine4b734222019-07-24 15:56:31 +02006372#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006373 status = psa_crypto_load_transaction( );
6374 if( status == PSA_SUCCESS )
6375 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006376 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6377 if( status != PSA_SUCCESS )
6378 goto exit;
6379 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006380 }
6381 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6382 {
6383 /* There's no transaction to complete. It's all good. */
6384 status = PSA_SUCCESS;
6385 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006386#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006387
Gilles Peskinec6b69072018-11-20 21:42:52 +01006388 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389 global_data.initialized = 1;
6390
6391exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006392 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006393 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006394 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006395}
6396
6397#endif /* MBEDTLS_PSA_CRYPTO_C */