blob: f466e6900a9184effe089ec89a25bedd44226298 [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 Cooreman5ad4bf72021-03-02 15:11:57 +0100548/** Return the output MAC length of a MAC algorithm, in bytes
549 *
550 * \param[in] algorithm The specific MAC algorithm
551 * \param[in] key_type The key type of the key to be used with the
552 * \p algorithm.
553 * \param[out] length The calculated output length of the given MAC
554 * \p algorithm when used with a key corresponding to
555 * the given \p key_type
556 *
557 * \retval #PSA_SUCCESS
558 * The \p length has been successfully calculated
559 * \retval #PSA_ERROR_NOT_SUPPORTED
560 * \p algorithm is a MAC algorithm, but ubsupported by this PSA core.
561 * \retval #PSA_ERROR_INVALID_ARGUMENT
562 * \p algorithm is not a valid, specific MAC algorithm or \p key_type
563 * describes a key incompatible with the specified \p algorithm.
564 */
565static psa_status_t psa_get_mac_output_length( psa_algorithm_t algorithm,
566 psa_key_type_t key_type,
567 size_t *length )
568{
569 if( !PSA_ALG_IS_MAC( algorithm ) || PSA_ALG_IS_WILDCARD( algorithm ) )
570 return( PSA_ERROR_INVALID_ARGUMENT );
571
572 size_t default_length = 0;
573
574 if( PSA_ALG_FULL_LENGTH_MAC( algorithm ) == PSA_ALG_CMAC ||
575 PSA_ALG_FULL_LENGTH_MAC( algorithm ) == PSA_ALG_CBC_MAC )
576 {
577 default_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
578 /* CMAC and CBC-MAC are only defined on block ciphers */
579 if( default_length == 0 )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 }
582 else if( PSA_ALG_IS_HMAC( algorithm ) )
583 {
584 /* HMAC output length is dictated by the underlying hash operation */
585 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( algorithm );
586 default_length = PSA_HASH_LENGTH( hash_alg );
587
588 if( hash_alg == 0 || default_length == 0 )
589 return( PSA_ERROR_INVALID_ARGUMENT );
590 }
591 else
592 return( PSA_ERROR_NOT_SUPPORTED );
593
594 /* Output the expected (potentially truncated) length as long as it can
595 * actually be output by the algorithm */
596 if( PSA_ALG_FULL_LENGTH_MAC( algorithm ) == algorithm )
597 {
598 *length = default_length;
599 return( PSA_SUCCESS );
600 }
601 else if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) <= default_length )
602 {
603 *length = PSA_MAC_TRUNCATED_LENGTH( algorithm );
604 return( PSA_SUCCESS );
605 }
606 else
607 return( PSA_ERROR_INVALID_ARGUMENT );
608}
609
Steven Cooreman75b74362020-07-28 14:30:13 +0200610/** Try to allocate a buffer to an empty key slot.
611 *
612 * \param[in,out] slot Key slot to attach buffer to.
613 * \param[in] buffer_length Requested size of the buffer.
614 *
615 * \retval #PSA_SUCCESS
616 * The buffer has been successfully allocated.
617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
618 * Not enough memory was available for allocation.
619 * \retval #PSA_ERROR_ALREADY_EXISTS
620 * Trying to allocate a buffer to a non-empty key slot.
621 */
622static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
623 size_t buffer_length )
624{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100625 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200626 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200627
Ronald Cronea0f8a62020-11-25 17:52:23 +0100628 slot->key.data = mbedtls_calloc( 1, buffer_length );
629 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200630 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200631
Ronald Cronea0f8a62020-11-25 17:52:23 +0100632 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200633 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200634}
635
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200636psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
637 const uint8_t* data,
638 size_t data_length )
639{
640 psa_status_t status = psa_allocate_buffer_to_slot( slot,
641 data_length );
642 if( status != PSA_SUCCESS )
643 return( status );
644
Ronald Cronea0f8a62020-11-25 17:52:23 +0100645 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200646 return( PSA_SUCCESS );
647}
648
Ronald Crona0fe59f2020-11-29 11:14:53 +0100649psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 const psa_key_attributes_t *attributes,
651 const uint8_t *data, size_t data_length,
652 uint8_t *key_buffer, size_t key_buffer_size,
653 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
656 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200658 /* zero-length keys are never supported. */
659 if( data_length == 0 )
660 return( PSA_ERROR_NOT_SUPPORTED );
661
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100662 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200665
Steven Cooreman75b74362020-07-28 14:30:13 +0200666 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
667 if( data_length > SIZE_MAX / 8 )
668 return( PSA_ERROR_NOT_SUPPORTED );
669
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200670 /* Enforce a size limit, and in particular ensure that the bit
671 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100672 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200673 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200674
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100675 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200676 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200677 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200678
Ronald Crondd04d422020-11-28 15:14:42 +0100679 /* Copy the key material. */
680 memcpy( key_buffer, data, data_length );
681 *key_buffer_length = data_length;
682 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200683
Steven Cooreman40120f62020-10-29 11:42:22 +0100684 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100686 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200687 {
John Durkop9814fa22020-11-04 12:28:15 -0800688#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
689 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100690 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200691 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100692 return( mbedtls_psa_ecp_import_key( attributes,
693 data, data_length,
694 key_buffer, key_buffer_size,
695 key_buffer_length,
696 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100697 }
John Durkop9814fa22020-11-04 12:28:15 -0800698#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
699 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700700#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
701 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100702 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200703 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100704 return( mbedtls_psa_rsa_import_key( attributes,
705 data, data_length,
706 key_buffer, key_buffer_size,
707 key_buffer_length,
708 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200709 }
John Durkop9814fa22020-11-04 12:28:15 -0800710#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
711 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100712 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100713
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100715}
716
Gilles Peskinef603c712019-01-19 13:40:11 +0100717/** Calculate the intersection of two algorithm usage policies.
718 *
719 * Return 0 (which allows no operation) on incompatibility.
720 */
721static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100722 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100723 psa_algorithm_t alg1,
724 psa_algorithm_t alg2 )
725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100727 if( alg1 == alg2 )
728 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100729 /* If the policies are from the same hash-and-sign family, check
730 * if one is a wildcard. If so the other has the specific algorithm. */
731 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
732 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
733 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100734 {
Steven Cooreman03488022021-02-18 12:07:20 +0100735 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
736 return( alg2 );
737 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
738 return( alg1 );
739 }
740 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100741 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100742 * restrictive tag length. */
743 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
744 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
745 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
746 {
747 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
748 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
749 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100750
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100751 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100752 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
753 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100754 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100755 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100756 }
757 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100758 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100759 ( alg1_len <= alg2_len ) )
760 {
761 return( alg2 );
762 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100763 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100764 ( alg2_len <= alg1_len ) )
765 {
766 return( alg1 );
767 }
768 }
769 /* If the policies are from the same MAC family, check whether one
770 * of them is a minimum-MAC-length policy. Calculate the most
771 * restrictive tag length. */
772 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
773 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
774 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
775 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100776 /* Calculate the actual requested output length for both sides. In case
777 * of at-least-this-length wildcard algorithms, the requested output
778 * length is the shortest allowed length. */
779 size_t alg1_len = 0;
780 size_t alg2_len = 0;
781 if( PSA_SUCCESS != psa_get_mac_output_length(
782 PSA_ALG_TRUNCATED_MAC( alg1,
783 PSA_MAC_TRUNCATED_LENGTH( alg1 ) ),
784 key_type,
785 &alg1_len ) )
786 {
787 return( 0 );
788 }
789 if( PSA_SUCCESS != psa_get_mac_output_length(
790 PSA_ALG_TRUNCATED_MAC( alg2,
791 PSA_MAC_TRUNCATED_LENGTH( alg2 ) ),
792 key_type,
793 &alg2_len ) )
794 {
795 return( 0 );
796 }
797
Steven Cooreman03488022021-02-18 12:07:20 +0100798 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799
Steven Cooremana1d83222021-02-25 10:20:29 +0100800 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100801 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
802 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100803 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100804 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100805 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100806 /* If only one is a wildcard, return specific algorithm if compatible. */
807 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100808 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100809 if( alg1_len <= alg2_len )
810 return( alg2 );
811 else
812 return( 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100814 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100815 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100816 if( alg2_len <= alg1_len )
817 return( alg1 );
818 else
819 return( 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100820 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100821 /* If none of them are wildcards, check whether we can match
822 * default-length with exact-length, and return exact-length in that
823 * case. */
824 if( alg1_len == alg2_len )
825 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100826 }
827 /* If the policies are incompatible, allow nothing. */
828 return( 0 );
829}
830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831static int psa_key_algorithm_permits( psa_key_type_t key_type,
832 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 psa_algorithm_t requested_alg )
834{
Gilles Peskine549ea862019-05-22 11:45:59 +0200835 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200836 if( requested_alg == policy_alg )
837 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100838 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
839 * and requested_alg is the same hash-and-sign family with any hash,
840 * then requested_alg is compliant with policy_alg. */
841 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
842 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843 {
Steven Cooreman03488022021-02-18 12:07:20 +0100844 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
845 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
846 }
847 /* If policy_alg is a wildcard AEAD algorithm of the same base as
848 * the requested algorithm, check the requested tag length to be
849 * equal-length or longer than the wildcard-specified length. */
850 if( PSA_ALG_IS_AEAD( policy_alg ) &&
851 PSA_ALG_IS_AEAD( requested_alg ) &&
852 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
853 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100854 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100855 {
856 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
857 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
858 }
Steven Cooreman03488022021-02-18 12:07:20 +0100859 if( PSA_ALG_IS_MAC( policy_alg ) &&
860 PSA_ALG_IS_MAC( requested_alg ) &&
861 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100862 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100863 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100864 size_t actual_output_length;
865 size_t default_output_length;
866 if( PSA_SUCCESS != psa_get_mac_output_length(
867 requested_alg,
868 key_type,
869 &actual_output_length ) )
870 {
871 return( 0 );
872 }
873 if( PSA_SUCCESS != psa_get_mac_output_length(
874 PSA_ALG_FULL_LENGTH_MAC( requested_alg ),
875 key_type,
876 &default_output_length ) )
877 {
878 return( 0 );
879 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100880
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100881 /* If the policy is default-length, only allow an algorithm with
882 * a declared exact-length matching the default. */
883 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
884 return( actual_output_length == default_output_length );
885
886 /* If the requested algorithm is default-length, allow it if the policy
887 * is exactly the default length. */
888 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
889 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
890 {
891 return( 1 );
892 }
893
894 /* If policy_alg is a wildcard MAC algorithm of the same base as
895 * the requested algorithm, check the requested tag length to be
896 * equal-length or longer than the wildcard-specified length. */
897 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
898 {
899 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
900 actual_output_length );
901 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902 }
Steven Cooremance48e852020-10-05 16:02:45 +0200903 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200904 * a key derivation with that key agreement should also be allowed. This
905 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200906 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
907 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
908 {
909 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
910 policy_alg );
911 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100912 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200913 return( 0 );
914}
915
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100916/** Test whether a policy permits an algorithm.
917 *
918 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100919 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100920 * \note This function requires providing the key type for which the policy is
921 * being validated, since some algorithm policy definitions (e.g. MAC)
922 * have different properties depending on what kind of cipher it is
923 * combined with.
924 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100925 * \retval PSA_SUCCESS When \p alg is a specific algorithm
926 * allowed by the \p policy.
927 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
928 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
929 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100930 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100932 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100934{
Steven Cooremand788fab2021-02-25 11:29:17 +0100935 /* '0' is not a valid algorithm */
936 if( alg == 0 )
937 return( PSA_ERROR_INVALID_ARGUMENT );
938
Steven Cooreman7e39f052021-02-23 14:18:32 +0100939 /* A requested algorithm cannot be a wildcard. */
940 if( PSA_ALG_IS_WILDCARD( alg ) )
941 return( PSA_ERROR_INVALID_ARGUMENT );
942
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100943 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
944 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100945 return( PSA_SUCCESS );
946 else
947 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100948}
949
Gilles Peskinef603c712019-01-19 13:40:11 +0100950/** Restrict a key policy based on a constraint.
951 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100952 * \note This function requires providing the key type for which the policy is
953 * being restricted, since some algorithm policy definitions (e.g. MAC)
954 * have different properties depending on what kind of cipher it is
955 * combined with.
956 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100957 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100958 * \param[in,out] policy The policy to restrict.
959 * \param[in] constraint The policy constraint to apply.
960 *
961 * \retval #PSA_SUCCESS
962 * \c *policy contains the intersection of the original value of
963 * \c *policy and \c *constraint.
964 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100965 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100966 * \c *policy is unchanged.
967 */
968static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100969 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100970 psa_key_policy_t *policy,
971 const psa_key_policy_t *constraint )
972{
973 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100974 psa_key_policy_algorithm_intersection( key_type, policy->alg,
975 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200976 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100977 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
978 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100979 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
980 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
982 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100983 policy->usage &= constraint->usage;
984 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200985 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100986 return( PSA_SUCCESS );
987}
988
Ronald Cron5c522922020-11-14 16:35:34 +0100989/** Get the description of a key given its identifier and policy constraints
990 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 *
Ronald Cron5c522922020-11-14 16:35:34 +0100992 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100993 * nonzero, the key must allow operations with this algorithm. If \p alg is
994 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100995 *
Ronald Cron5c522922020-11-14 16:35:34 +0100996 * In case of a persistent key, the function loads the description of the key
997 * into a key slot if not already done.
998 *
999 * On success, the returned key slot is locked. It is the responsibility of
1000 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001 */
Ronald Cron5c522922020-11-14 16:35:34 +01001002static psa_status_t psa_get_and_lock_key_slot_with_policy(
1003 mbedtls_svc_key_id_t key,
1004 psa_key_slot_t **p_slot,
1005 psa_key_usage_t usage,
1006 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001007{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1009 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001010
Ronald Cron5c522922020-11-14 16:35:34 +01001011 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001012 if( status != PSA_SUCCESS )
1013 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001015
1016 /* Enforce that usage policy for the key slot contains all the flags
1017 * required by the usage parameter. There is one exception: public
1018 * keys can always be exported, so we treat public key objects as
1019 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001020 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001021 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001022
Gilles Peskine8e338702019-07-30 20:06:31 +02001023 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +01001024 {
1025 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001027 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001028
1029 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001030 if( alg != 0 )
1031 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001032 status = psa_key_policy_permits( &slot->attr.policy,
1033 slot->attr.type,
1034 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001035 if( status != PSA_SUCCESS )
1036 goto error;
1037 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001038
Darryl Green06fd18d2018-07-16 11:21:11 +01001039 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001040
1041error:
1042 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001043 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001044
1045 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001046}
Darryl Green940d72c2018-07-13 13:18:51 +01001047
Ronald Cron5c522922020-11-14 16:35:34 +01001048/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001049 *
1050 * A transparent key is a key for which the key material is directly
1051 * available, as opposed to a key in a secure element.
1052 *
Ronald Cron5c522922020-11-14 16:35:34 +01001053 * This is a temporary function to use instead of
1054 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1055 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056 *
Ronald Cron5c522922020-11-14 16:35:34 +01001057 * On success, the returned key slot is locked. It is the responsibility of the
1058 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001059 */
1060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001061static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1062 mbedtls_svc_key_id_t key,
1063 psa_key_slot_t **p_slot,
1064 psa_key_usage_t usage,
1065 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001066{
Ronald Cron5c522922020-11-14 16:35:34 +01001067 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1068 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001069 if( status != PSA_SUCCESS )
1070 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001071
Gilles Peskineadad8132019-07-25 11:31:23 +02001072 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001073 {
Ronald Cron5c522922020-11-14 16:35:34 +01001074 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075 *p_slot = NULL;
1076 return( PSA_ERROR_NOT_SUPPORTED );
1077 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001078
Gilles Peskine28f8f302019-07-24 13:30:31 +02001079 return( PSA_SUCCESS );
1080}
1081#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1082/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001083#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1084 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001087/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001088static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001089{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001090 /* Data pointer will always be either a valid pointer or NULL in an
1091 * initialized slot, so we can just free it. */
1092 if( slot->key.data != NULL )
1093 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1094
1095 mbedtls_free( slot->key.data );
1096 slot->key.data = NULL;
1097 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001098
1099 return( PSA_SUCCESS );
1100}
1101
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001102/** Completely wipe a slot in memory, including its policy.
1103 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001104psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001105{
1106 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001107
1108 /*
1109 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001110 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001111 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1112 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001113 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001114 */
Ronald Cron5c522922020-11-14 16:35:34 +01001115 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001116 {
1117#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001118 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001119#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001120 status = PSA_ERROR_CORRUPTION_DETECTED;
1121 }
1122
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001123 /* Multipart operations may still be using the key. This is safe
1124 * because all multipart operation objects are independent from
1125 * the key slot: if they need to access the key after the setup
1126 * phase, they have a copy of the key. Note that this means that
1127 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128 /* At this point, key material and other type-specific content has
1129 * been wiped. Clear remaining metadata. We can call memset and not
1130 * zeroize because the metadata is not particularly sensitive. */
1131 memset( slot, 0, sizeof( *slot ) );
1132 return( status );
1133}
1134
Ronald Croncf56a0a2020-08-04 09:51:30 +02001135psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001137 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 psa_status_t status; /* status of the last operation */
1139 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1141 psa_se_drv_table_entry_t *driver;
1142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Ronald Croncf56a0a2020-08-04 09:51:30 +02001144 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001145 return( PSA_SUCCESS );
1146
Ronald Cronf2911112020-10-29 17:51:10 +01001147 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001148 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001149 * key, this will load the key description from persistent memory if not
1150 * done yet. We cannot avoid this loading as without it we don't know if
1151 * the key is operated by an SE or not and this information is needed by
1152 * the current implementation.
1153 */
Ronald Cron5c522922020-11-14 16:35:34 +01001154 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001155 if( status != PSA_SUCCESS )
1156 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001157
Ronald Cronf2911112020-10-29 17:51:10 +01001158 /*
1159 * If the key slot containing the key description is under access by the
1160 * library (apart from the present access), the key cannot be destroyed
1161 * yet. For the time being, just return in error. Eventually (to be
1162 * implemented), the key should be destroyed when all accesses have
1163 * stopped.
1164 */
Ronald Cron5c522922020-11-14 16:35:34 +01001165 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001166 {
Ronald Cron5c522922020-11-14 16:35:34 +01001167 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001168 return( PSA_ERROR_GENERIC_ERROR );
1169 }
1170
Gilles Peskine354f7672019-07-12 23:46:38 +02001171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001172 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001173 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001174 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001175 /* For a key in a secure element, we need to do three things:
1176 * remove the key file in internal storage, destroy the
1177 * key inside the secure element, and update the driver's
1178 * persistent data. Start a transaction that will encompass these
1179 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001180 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001181 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001182 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001183 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001184 status = psa_crypto_save_transaction( );
1185 if( status != PSA_SUCCESS )
1186 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001187 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 /* We should still try to destroy the key in the secure
1189 * element and the key metadata in storage. This is especially
1190 * important if the error is that the storage is full.
1191 * But how to do it exactly without risking an inconsistent
1192 * state after a reset?
1193 * https://github.com/ARMmbed/mbed-crypto/issues/215
1194 */
1195 overall_status = status;
1196 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001197 }
1198
Ronald Cronea0f8a62020-11-25 17:52:23 +01001199 status = psa_destroy_se_key( driver,
1200 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 if( overall_status == PSA_SUCCESS )
1202 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001203 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1205
Darryl Greend49a4992018-06-18 17:27:26 +01001206#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001207 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001208 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001209 status = psa_destroy_persistent_key( slot->attr.id );
1210 if( overall_status == PSA_SUCCESS )
1211 overall_status = status;
1212
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001213 /* TODO: other slots may have a copy of the same key. We should
1214 * invalidate them.
1215 * https://github.com/ARMmbed/mbed-crypto/issues/214
1216 */
Darryl Greend49a4992018-06-18 17:27:26 +01001217 }
1218#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001219
Gilles Peskinefc762652019-07-22 19:30:34 +02001220#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1221 if( driver != NULL )
1222 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001223 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224 if( overall_status == PSA_SUCCESS )
1225 overall_status = status;
1226 status = psa_crypto_stop_transaction( );
1227 if( overall_status == PSA_SUCCESS )
1228 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001229 }
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1231
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233exit:
1234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001235 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001236 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1237 if( overall_status == PSA_SUCCESS )
1238 overall_status = status;
1239 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240}
1241
John Durkop07cc04a2020-11-16 22:08:34 -08001242#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001243 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001244static psa_status_t psa_get_rsa_public_exponent(
1245 const mbedtls_rsa_context *rsa,
1246 psa_key_attributes_t *attributes )
1247{
1248 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001250 uint8_t *buffer = NULL;
1251 size_t buflen;
1252 mbedtls_mpi_init( &mpi );
1253
1254 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1255 if( ret != 0 )
1256 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001257 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1258 {
1259 /* It's the default value, which is reported as an empty string,
1260 * so there's nothing to do. */
1261 goto exit;
1262 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001263
1264 buflen = mbedtls_mpi_size( &mpi );
1265 buffer = mbedtls_calloc( 1, buflen );
1266 if( buffer == NULL )
1267 {
1268 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1269 goto exit;
1270 }
1271 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1272 if( ret != 0 )
1273 goto exit;
1274 attributes->domain_parameters = buffer;
1275 attributes->domain_parameters_size = buflen;
1276
1277exit:
1278 mbedtls_mpi_free( &mpi );
1279 if( ret != 0 )
1280 mbedtls_free( buffer );
1281 return( mbedtls_to_psa_error( ret ) );
1282}
John Durkop07cc04a2020-11-16 22:08:34 -08001283#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001284 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001285
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001286/** Retrieve all the publicly-accessible attributes of a key.
1287 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001288psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001289 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001290{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001292 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001293 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001295 psa_reset_key_attributes( attributes );
1296
Ronald Cron5c522922020-11-14 16:35:34 +01001297 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001298 if( status != PSA_SUCCESS )
1299 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001300
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001301 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001302 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1303 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1304
1305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1306 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001307 psa_set_key_slot_number( attributes,
1308 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001310
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 {
John Durkop07cc04a2020-11-16 22:08:34 -08001313#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001314 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001315 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001318 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001319 * is not yet implemented.
1320 * https://github.com/ARMmbed/mbed-crypto/issues/216
1321 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001323 break;
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001325 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001326 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001327
Ronald Cron90857082020-11-25 14:58:33 +01001328 status = mbedtls_psa_rsa_load_representation(
1329 slot->attr.type,
1330 slot->key.data,
1331 slot->key.bytes,
1332 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001333 if( status != PSA_SUCCESS )
1334 break;
1335
Steven Cooremana2371e52020-07-28 14:30:39 +02001336 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001337 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001338 mbedtls_rsa_free( rsa );
1339 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001340 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001341 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001342#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001343 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001344 default:
1345 /* Nothing else to do. */
1346 break;
1347 }
1348
1349 if( status != PSA_SUCCESS )
1350 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001351
Ronald Cron5c522922020-11-14 16:35:34 +01001352 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001353
Ronald Cron5c522922020-11-14 16:35:34 +01001354 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355}
1356
Gilles Peskinec8000c02019-08-02 20:15:51 +02001357#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1358psa_status_t psa_get_key_slot_number(
1359 const psa_key_attributes_t *attributes,
1360 psa_key_slot_number_t *slot_number )
1361{
1362 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1363 {
1364 *slot_number = attributes->slot_number;
1365 return( PSA_SUCCESS );
1366 }
1367 else
1368 return( PSA_ERROR_INVALID_ARGUMENT );
1369}
1370#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1371
Ronald Crond18b5f82020-11-25 16:46:05 +01001372static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1373 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001374 uint8_t *data,
1375 size_t data_size,
1376 size_t *data_length )
1377{
Ronald Crond18b5f82020-11-25 16:46:05 +01001378 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001379 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 memcpy( data, key_buffer, key_buffer_size );
1381 memset( data + key_buffer_size, 0,
1382 data_size - key_buffer_size );
1383 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001384 return( PSA_SUCCESS );
1385}
1386
Ronald Cron7285cda2020-11-26 14:46:37 +01001387psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001388 const psa_key_attributes_t *attributes,
1389 const uint8_t *key_buffer, size_t key_buffer_size,
1390 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391{
Ronald Crond18b5f82020-11-25 16:46:05 +01001392 psa_key_type_t type = attributes->core.type;
1393
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 if( key_type_is_raw_bytes( type ) ||
1395 PSA_KEY_TYPE_IS_RSA( type ) ||
1396 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397 {
1398 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001399 key_buffer, key_buffer_size,
1400 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401 }
1402 else
1403 {
1404 /* This shouldn't happen in the reference implementation, but
1405 it is valid for a special-purpose implementation to omit
1406 support for exporting certain key types. */
1407 return( PSA_ERROR_NOT_SUPPORTED );
1408 }
1409}
1410
1411psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
1415{
1416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1417 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1418 psa_key_slot_t *slot;
1419
Ronald Crone907e552021-01-18 13:22:38 +01001420 /* Reject a zero-length output buffer now, since this can never be a
1421 * valid key representation. This way we know that data must be a valid
1422 * pointer and we can do things like memset(data, ..., data_size). */
1423 if( data_size == 0 )
1424 return( PSA_ERROR_BUFFER_TOO_SMALL );
1425
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426 /* Set the key to empty now, so that even when there are errors, we always
1427 * set data_length to a value between 0 and data_size. On error, setting
1428 * the key to empty is a good choice because an empty key representation is
1429 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001430 *data_length = 0;
1431
Ronald Cron9486f9d2020-11-26 13:36:23 +01001432 /* Export requires the EXPORT flag. There is an exception for public keys,
1433 * which don't require any flag, but
1434 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1435 */
1436 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1437 PSA_KEY_USAGE_EXPORT, 0 );
1438 if( status != PSA_SUCCESS )
1439 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001440
Ronald Crond18b5f82020-11-25 16:46:05 +01001441 psa_key_attributes_t attributes = {
1442 .core = slot->attr
1443 };
Ronald Cron67227982020-11-26 15:16:05 +01001444 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001445 slot->key.data, slot->key.bytes,
1446 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001447
Ronald Cron9486f9d2020-11-26 13:36:23 +01001448 unlock_status = psa_unlock_key_slot( slot );
1449
1450 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1451}
1452
Ronald Cron7285cda2020-11-26 14:46:37 +01001453psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 const psa_key_attributes_t *attributes,
1455 const uint8_t *key_buffer,
1456 size_t key_buffer_size,
1457 uint8_t *data,
1458 size_t data_size,
1459 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001460{
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001462
Ronald Crond18b5f82020-11-25 16:46:05 +01001463 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001464 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001466 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001468 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 key_buffer, key_buffer_size,
1470 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001471 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001472
Ronald Crond18b5f82020-11-25 16:46:05 +01001473 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 {
John Durkop0e005192020-10-31 22:06:54 -07001475#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1476 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001477 return( mbedtls_psa_rsa_export_public_key( attributes,
1478 key_buffer,
1479 key_buffer_size,
1480 data,
1481 data_size,
1482 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001483#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001484 /* We don't know how to convert a private RSA key to public. */
1485 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001486#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1487 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001488 }
1489 else
Moran Pekera998bc62018-04-16 18:16:20 +03001490 {
John Durkop6ba40d12020-11-10 08:50:04 -08001491#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1492 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001493 return( mbedtls_psa_ecp_export_public_key( attributes,
1494 key_buffer,
1495 key_buffer_size,
1496 data,
1497 data_size,
1498 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001499#else
1500 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001501 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001502#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1503 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001504 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001506 else
1507 {
1508 /* This shouldn't happen in the reference implementation, but
1509 it is valid for a special-purpose implementation to omit
1510 support for exporting certain key types. */
1511 return( PSA_ERROR_NOT_SUPPORTED );
1512 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513}
Ronald Crond18b5f82020-11-25 16:46:05 +01001514
Ronald Croncf56a0a2020-08-04 09:51:30 +02001515psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001516 uint8_t *data,
1517 size_t data_size,
1518 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001519{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001521 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001522 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001523
Ronald Crone907e552021-01-18 13:22:38 +01001524 /* Reject a zero-length output buffer now, since this can never be a
1525 * valid key representation. This way we know that data must be a valid
1526 * pointer and we can do things like memset(data, ..., data_size). */
1527 if( data_size == 0 )
1528 return( PSA_ERROR_BUFFER_TOO_SMALL );
1529
Darryl Greendd8fb772018-11-07 16:00:44 +00001530 /* Set the key to empty now, so that even when there are errors, we always
1531 * set data_length to a value between 0 and data_size. On error, setting
1532 * the key to empty is a good choice because an empty key representation is
1533 * unlikely to be accepted anywhere. */
1534 *data_length = 0;
1535
1536 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001537 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001538 if( status != PSA_SUCCESS )
1539 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001540
Ronald Cron9486f9d2020-11-26 13:36:23 +01001541 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1542 {
1543 status = PSA_ERROR_INVALID_ARGUMENT;
1544 goto exit;
1545 }
1546
Ronald Crond18b5f82020-11-25 16:46:05 +01001547 psa_key_attributes_t attributes = {
1548 .core = slot->attr
1549 };
Ronald Cron67227982020-11-26 15:16:05 +01001550 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001551 &attributes, slot->key.data, slot->key.bytes,
1552 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001553
1554exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001555 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001556
Ronald Cron5c522922020-11-14 16:35:34 +01001557 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001558}
1559
Gilles Peskine91e8c332019-08-02 19:19:39 +02001560#if defined(static_assert)
1561static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1562 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001563static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001564 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001565static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001566 "One or more key attribute flag is listed as both internal-only and external-only" );
1567#endif
1568
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569/** Validate that a key policy is internally well-formed.
1570 *
1571 * This function only rejects invalid policies. It does not validate the
1572 * consistency of the policy with respect to other attributes of the key
1573 * such as the key type.
1574 */
1575static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001576{
1577 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001578 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 PSA_KEY_USAGE_ENCRYPT |
1580 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001581 PSA_KEY_USAGE_SIGN_HASH |
1582 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001583 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1584 return( PSA_ERROR_INVALID_ARGUMENT );
1585
Gilles Peskine4747d192019-04-17 15:05:45 +02001586 return( PSA_SUCCESS );
1587}
1588
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001589/** Validate the internal consistency of key attributes.
1590 *
1591 * This function only rejects invalid attribute values. If does not
1592 * validate the consistency of the attributes with any key data that may
1593 * be involved in the creation of the key.
1594 *
1595 * Call this function early in the key creation process.
1596 *
1597 * \param[in] attributes Key attributes for the new key.
1598 * \param[out] p_drv On any return, the driver for the key, if any.
1599 * NULL for a transparent key.
1600 *
1601 */
1602static psa_status_t psa_validate_key_attributes(
1603 const psa_key_attributes_t *attributes,
1604 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001605{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001606 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001607 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001608 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001609
Ronald Cron54b90082020-10-29 15:26:43 +01001610 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001611 if( status != PSA_SUCCESS )
1612 return( status );
1613
Ronald Crond2ed4812020-07-17 16:11:30 +02001614 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001615 if( status != PSA_SUCCESS )
1616 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001617
Ronald Cron65f38a32020-10-23 17:11:13 +02001618 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1619 {
1620 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1621 return( PSA_ERROR_INVALID_ARGUMENT );
1622 }
1623 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001624 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001625 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001626 if( status != PSA_SUCCESS )
1627 return( status );
1628 }
1629
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001630 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001631 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001632 return( status );
1633
1634 /* Refuse to create overly large keys.
1635 * Note that this doesn't trigger on import if the attributes don't
1636 * explicitly specify a size (so psa_get_key_bits returns 0), so
1637 * psa_import_key() needs its own checks. */
1638 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1639 return( PSA_ERROR_NOT_SUPPORTED );
1640
Gilles Peskine91e8c332019-08-02 19:19:39 +02001641 /* Reject invalid flags. These should not be reachable through the API. */
1642 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1643 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1644 return( PSA_ERROR_INVALID_ARGUMENT );
1645
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646 return( PSA_SUCCESS );
1647}
1648
Gilles Peskine4747d192019-04-17 15:05:45 +02001649/** Prepare a key slot to receive key material.
1650 *
1651 * This function allocates a key slot and sets its metadata.
1652 *
1653 * If this function fails, call psa_fail_key_creation().
1654 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * This function is intended to be used as follows:
1656 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001657 * it with the specified attributes, and in case of a volatile key assign it
1658 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001659 * -# Populate the slot with the key material.
1660 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1661 * In case of failure at any step, stop the sequence and call
1662 * psa_fail_key_creation().
1663 *
Ronald Cron5c522922020-11-14 16:35:34 +01001664 * On success, the key slot is locked. It is the responsibility of the caller
1665 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001666 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001667 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001668 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001669 * \param[out] p_slot On success, a pointer to the prepared slot.
1670 * \param[out] p_drv On any return, the driver for the key, if any.
1671 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001672 *
1673 * \retval #PSA_SUCCESS
1674 * The key slot is ready to receive key material.
1675 * \return If this function fails, the key slot is an invalid state.
1676 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 */
1678static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001679 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001682 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001683{
1684 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001685 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 psa_key_slot_t *slot;
1687
Gilles Peskinedf179142019-07-15 22:02:14 +02001688 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001689 *p_drv = NULL;
1690
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001691 status = psa_validate_key_attributes( attributes, p_drv );
1692 if( status != PSA_SUCCESS )
1693 return( status );
1694
Ronald Cronc4d1b512020-07-31 11:26:37 +02001695 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 if( status != PSA_SUCCESS )
1697 return( status );
1698 slot = *p_slot;
1699
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001700 /* We're storing the declared bit-size of the key. It's up to each
1701 * creation mechanism to verify that this information is correct.
1702 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001704 * is optional (import, copy). In case of a volatile key, assign it the
1705 * volatile key identifier associated to the slot returned to contain its
1706 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001707
1708 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001709 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1710 {
1711#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1712 slot->attr.id = volatile_key_id;
1713#else
1714 slot->attr.id.key_id = volatile_key_id;
1715#endif
1716 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001719 * external-only flags, query `attributes`. Thanks to the check
1720 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001721 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001722 * may have set. */
1723 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001724
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001726 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001728 * create the key file in internal storage, create the
1729 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001730 * persistent data. This is done by starting a transaction that will
1731 * encompass these three actions.
1732 * For registering a volatile key, we just need to find an appropriate
1733 * slot number inside the SE. Since the key is designated volatile, creating
1734 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001735 /* The first thing to do is to find a slot number for the new key.
1736 * We save the slot number in persistent storage as part of the
1737 * transaction data. It will be needed to recover if the power
1738 * fails during the key creation process, to clean up on the secure
1739 * element side after restarting. Obtaining a slot number from the
1740 * secure element driver updates its persistent state, but we do not yet
1741 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001742 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001743 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001744 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001745 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001746 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001747 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001748 if( status != PSA_SUCCESS )
1749 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001750
1751 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001752 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001753 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1754 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001755 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 psa_crypto_transaction.key.id = slot->attr.id;
1757 status = psa_crypto_save_transaction( );
1758 if( status != PSA_SUCCESS )
1759 {
1760 (void) psa_crypto_stop_transaction( );
1761 return( status );
1762 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001763 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764
1765 status = psa_copy_key_material_into_slot(
1766 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001767 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001768
1769 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1770 {
1771 /* Key registration only makes sense with a secure element. */
1772 return( PSA_ERROR_INVALID_ARGUMENT );
1773 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1775
Ronald Cronc4d1b512020-07-31 11:26:37 +02001776 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001777}
Gilles Peskine4747d192019-04-17 15:05:45 +02001778
1779/** Finalize the creation of a key once its key material has been set.
1780 *
1781 * This entails writing the key to persistent storage.
1782 *
1783 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001784 * See the documentation of psa_start_key_creation() for the intended use
1785 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 *
Ronald Cron5c522922020-11-14 16:35:34 +01001787 * If the finalization succeeds, the function unlocks the key slot (it was
1788 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1789 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001790 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 * \param[in,out] slot Pointer to the slot with key material.
1792 * \param[in] driver The secure element driver for the key,
1793 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001794 * \param[out] key On success, identifier of the key. Note that the
1795 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001796 *
1797 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001798 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001799 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1800 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1801 * \retval #PSA_ERROR_ALREADY_EXISTS
1802 * \retval #PSA_ERROR_DATA_INVALID
1803 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001804 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001805 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001806 * \return If this function fails, the key slot is an invalid state.
1807 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001809static psa_status_t psa_finish_key_creation(
1810 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001811 psa_se_drv_table_entry_t *driver,
1812 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001813{
1814 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001815 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001817
1818#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001819 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001820 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001821#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1822 if( driver != NULL )
1823 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001824 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001825 psa_key_slot_number_t slot_number =
1826 psa_key_slot_get_slot_number( slot ) ;
1827
Gilles Peskineb46bef22019-07-30 21:32:04 +02001828#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001829 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001830 sizeof( data.slot_number ),
1831 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001832#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001833 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001834 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001835 (uint8_t*) &data,
1836 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001837 }
1838 else
1839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1840 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001841 /* Key material is saved in export representation in the slot, so
1842 * just pass the slot buffer for storage. */
1843 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001844 slot->key.data,
1845 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001846 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001847 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001848#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1849
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001851 /* Finish the transaction for a key creation. This does not
1852 * happen when registering an existing key. Detect this case
1853 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001854 * creation of a persistent key in a secure element requires a transaction,
1855 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001856 if( driver != NULL &&
1857 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001858 {
1859 status = psa_save_se_persistent_data( driver );
1860 if( status != PSA_SUCCESS )
1861 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001862 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001863 return( status );
1864 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001865 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001866 }
1867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868
Ronald Cron50972942020-11-14 11:28:25 +01001869 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001870 {
1871 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001872 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001873 if( status != PSA_SUCCESS )
1874 *key = MBEDTLS_SVC_KEY_ID_INIT;
1875 }
Ronald Cron50972942020-11-14 11:28:25 +01001876
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 return( status );
1878}
1879
1880/** Abort the creation of a key.
1881 *
1882 * You may call this function after calling psa_start_key_creation(),
1883 * or after psa_finish_key_creation() fails. In other circumstances, this
1884 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 * See the documentation of psa_start_key_creation() for the intended use
1886 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001888 * \param[in,out] slot Pointer to the slot with key material.
1889 * \param[in] driver The secure element driver for the key,
1890 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001892static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 (void) driver;
1896
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 if( slot == NULL )
1898 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001899
1900#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001901 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 * element, and the failure happened later (when saving metadata
1903 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001904 * element.
1905 * https://github.com/ARMmbed/mbed-crypto/issues/217
1906 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001907
Gilles Peskined7729582019-08-05 15:55:54 +02001908 /* Abort the ongoing transaction if any (there may not be one if
1909 * the creation process failed before starting one, or if the
1910 * key creation is a registration of a key in a secure element).
1911 * Earlier functions must already have done what it takes to undo any
1912 * partial creation. All that's left is to update the transaction data
1913 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001914 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001915#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1916
Gilles Peskine4747d192019-04-17 15:05:45 +02001917 psa_wipe_key_slot( slot );
1918}
1919
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001920/** Validate optional attributes during key creation.
1921 *
1922 * Some key attributes are optional during key creation. If they are
1923 * specified in the attributes structure, check that they are consistent
1924 * with the data in the slot.
1925 *
1926 * This function should be called near the end of key creation, after
1927 * the slot in memory is fully populated but before saving persistent data.
1928 */
1929static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 const psa_key_slot_t *slot,
1931 const psa_key_attributes_t *attributes )
1932{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001933 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001935 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 return( PSA_ERROR_INVALID_ARGUMENT );
1937 }
1938
1939 if( attributes->domain_parameters_size != 0 )
1940 {
John Durkop0e005192020-10-31 22:06:54 -07001941#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1942 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001943 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001945 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001946 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001948
Ronald Cron90857082020-11-25 14:58:33 +01001949 psa_status_t status = mbedtls_psa_rsa_load_representation(
1950 slot->attr.type,
1951 slot->key.data,
1952 slot->key.bytes,
1953 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001954 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001955 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001956
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 mbedtls_mpi_init( &actual );
1958 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001959 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001961 mbedtls_rsa_free( rsa );
1962 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( ret != 0 )
1964 goto rsa_exit;
1965 ret = mbedtls_mpi_read_binary( &required,
1966 attributes->domain_parameters,
1967 attributes->domain_parameters_size );
1968 if( ret != 0 )
1969 goto rsa_exit;
1970 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1971 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1972 rsa_exit:
1973 mbedtls_mpi_free( &actual );
1974 mbedtls_mpi_free( &required );
1975 if( ret != 0)
1976 return( mbedtls_to_psa_error( ret ) );
1977 }
1978 else
John Durkop9814fa22020-11-04 12:28:15 -08001979#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1980 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 {
1982 return( PSA_ERROR_INVALID_ARGUMENT );
1983 }
1984 }
1985
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001986 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001988 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 return( PSA_ERROR_INVALID_ARGUMENT );
1990 }
1991
1992 return( PSA_SUCCESS );
1993}
1994
Gilles Peskine4747d192019-04-17 15:05:45 +02001995psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001996 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001997 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001998 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001999{
2000 psa_status_t status;
2001 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002002 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004
Ronald Cron81709fc2020-11-14 12:10:32 +01002005 *key = MBEDTLS_SVC_KEY_ID_INIT;
2006
Gilles Peskine0f84d622019-09-12 19:03:13 +02002007 /* Reject zero-length symmetric keys (including raw data key objects).
2008 * This also rejects any key which might be encoded as an empty string,
2009 * which is never valid. */
2010 if( data_length == 0 )
2011 return( PSA_ERROR_INVALID_ARGUMENT );
2012
Gilles Peskinedf179142019-07-15 22:02:14 +02002013 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002014 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 if( status != PSA_SUCCESS )
2016 goto exit;
2017
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002018 /* In the case of a transparent key or an opaque key stored in local
2019 * storage (thus not in the case of generating a key in a secure element
2020 * or cryptoprocessor with storage), we have to allocate a buffer to
2021 * hold the generated key material. */
2022 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02002023 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002024 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002025 if( status != PSA_SUCCESS )
2026 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002027 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028
2029 bits = slot->attr.bits;
2030 status = psa_driver_wrapper_import_key( attributes,
2031 data, data_length,
2032 slot->key.data,
2033 slot->key.bytes,
2034 &slot->key.bytes, &bits );
2035 if( status != PSA_SUCCESS )
2036 goto exit;
2037
2038 if( slot->attr.bits == 0 )
2039 slot->attr.bits = (psa_key_bits_t) bits;
2040 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002041 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002042 status = PSA_ERROR_INVALID_ARGUMENT;
2043 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002044 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002045
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002046 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002047 if( status != PSA_SUCCESS )
2048 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049
Ronald Cron81709fc2020-11-14 12:10:32 +01002050 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002052 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002053 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002054
Gilles Peskine4747d192019-04-17 15:05:45 +02002055 return( status );
2056}
2057
Gilles Peskined7729582019-08-05 15:55:54 +02002058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2059psa_status_t mbedtls_psa_register_se_key(
2060 const psa_key_attributes_t *attributes )
2061{
2062 psa_status_t status;
2063 psa_key_slot_t *slot = NULL;
2064 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002066
2067 /* Leaving attributes unspecified is not currently supported.
2068 * It could make sense to query the key type and size from the
2069 * secure element, but not all secure elements support this
2070 * and the driver HAL doesn't currently support it. */
2071 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2072 return( PSA_ERROR_NOT_SUPPORTED );
2073 if( psa_get_key_bits( attributes ) == 0 )
2074 return( PSA_ERROR_NOT_SUPPORTED );
2075
2076 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002077 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002078 if( status != PSA_SUCCESS )
2079 goto exit;
2080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002082
2083exit:
2084 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002085 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002086
Gilles Peskined7729582019-08-05 15:55:54 +02002087 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002088 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002089 return( status );
2090}
2091#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2092
Gilles Peskinef603c712019-01-19 13:40:11 +01002093static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002095{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002096 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002097 source->key.data,
2098 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002099 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002100 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Steven Cooreman398aee52020-10-13 14:35:45 +02002102 target->attr.type = source->attr.type;
2103 target->attr.bits = source->attr.bits;
2104
2105 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002106}
2107
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114 psa_key_slot_t *source_slot = NULL;
2115 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002116 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002117 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002118
Ronald Cron81709fc2020-11-14 12:10:32 +01002119 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2120
Ronald Cron5c522922020-11-14 16:35:34 +01002121 status = psa_get_and_lock_transparent_key_slot_with_policy(
2122 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002123 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 goto exit;
2125
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002126 status = psa_validate_optional_attributes( source_slot,
2127 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 if( status != PSA_SUCCESS )
2129 goto exit;
2130
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002131 status = psa_restrict_key_policy( source_slot->attr.type,
2132 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002133 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 if( status != PSA_SUCCESS )
2135 goto exit;
2136
Ronald Cron81709fc2020-11-14 12:10:32 +01002137 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2138 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139 if( status != PSA_SUCCESS )
2140 goto exit;
2141
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2143 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002145 /* Copying to a secure element is not implemented yet. */
2146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002150
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002151 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002152 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002153 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002154
Ronald Cron81709fc2020-11-14 12:10:32 +01002155 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002157 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002158 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002159
Ronald Cron5c522922020-11-14 16:35:34 +01002160 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002161
Ronald Cron5c522922020-11-14 16:35:34 +01002162 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002163}
2164
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002165
2166
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002167/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002168/* Message digests */
2169/****************************************************************/
2170
John Durkop07cc04a2020-11-16 22:08:34 -08002171#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002172 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2173 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002174 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002175static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002176{
2177 switch( alg )
2178 {
John Durkopee4e6602020-11-27 08:48:46 -08002179#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002180 case PSA_ALG_MD2:
2181 return( &mbedtls_md2_info );
2182#endif
John Durkopee4e6602020-11-27 08:48:46 -08002183#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002184 case PSA_ALG_MD4:
2185 return( &mbedtls_md4_info );
2186#endif
John Durkopee4e6602020-11-27 08:48:46 -08002187#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002188 case PSA_ALG_MD5:
2189 return( &mbedtls_md5_info );
2190#endif
John Durkopee4e6602020-11-27 08:48:46 -08002191#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002192 case PSA_ALG_RIPEMD160:
2193 return( &mbedtls_ripemd160_info );
2194#endif
John Durkopee4e6602020-11-27 08:48:46 -08002195#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002196 case PSA_ALG_SHA_1:
2197 return( &mbedtls_sha1_info );
2198#endif
John Durkopee4e6602020-11-27 08:48:46 -08002199#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002200 case PSA_ALG_SHA_224:
2201 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002202#endif
2203#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002204 case PSA_ALG_SHA_256:
2205 return( &mbedtls_sha256_info );
2206#endif
John Durkopee4e6602020-11-27 08:48:46 -08002207#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002208 case PSA_ALG_SHA_384:
2209 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002210#endif
John Durkopee4e6602020-11-27 08:48:46 -08002211#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002212 case PSA_ALG_SHA_512:
2213 return( &mbedtls_sha512_info );
2214#endif
2215 default:
2216 return( NULL );
2217 }
2218}
John Durkop07cc04a2020-11-16 22:08:34 -08002219#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002220 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2221 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2222 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002223
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2225{
2226 switch( operation->alg )
2227 {
Gilles Peskine81736312018-06-26 15:04:31 +02002228 case 0:
2229 /* The object has (apparently) been initialized but it is not
2230 * in use. It's ok to call abort on such an object, and there's
2231 * nothing to do. */
2232 break;
John Durkopee4e6602020-11-27 08:48:46 -08002233#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 case PSA_ALG_MD2:
2235 mbedtls_md2_free( &operation->ctx.md2 );
2236 break;
2237#endif
John Durkopee4e6602020-11-27 08:48:46 -08002238#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_MD4:
2240 mbedtls_md4_free( &operation->ctx.md4 );
2241 break;
2242#endif
John Durkopee4e6602020-11-27 08:48:46 -08002243#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 case PSA_ALG_MD5:
2245 mbedtls_md5_free( &operation->ctx.md5 );
2246 break;
2247#endif
John Durkopee4e6602020-11-27 08:48:46 -08002248#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_RIPEMD160:
2250 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2251 break;
2252#endif
John Durkopee4e6602020-11-27 08:48:46 -08002253#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_1:
2255 mbedtls_sha1_free( &operation->ctx.sha1 );
2256 break;
2257#endif
John Durkop6ca23272020-12-03 06:01:32 -08002258#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002260 mbedtls_sha256_free( &operation->ctx.sha256 );
2261 break;
2262#endif
2263#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 case PSA_ALG_SHA_256:
2265 mbedtls_sha256_free( &operation->ctx.sha256 );
2266 break;
2267#endif
John Durkop6ca23272020-12-03 06:01:32 -08002268#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002270 mbedtls_sha512_free( &operation->ctx.sha512 );
2271 break;
2272#endif
2273#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274 case PSA_ALG_SHA_512:
2275 mbedtls_sha512_free( &operation->ctx.sha512 );
2276 break;
2277#endif
2278 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 }
2281 operation->alg = 0;
2282 return( PSA_SUCCESS );
2283}
2284
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002285psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286 psa_algorithm_t alg )
2287{
Janos Follath24eed8d2019-11-22 13:21:35 +00002288 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002289
2290 /* A context must be freshly initialized before it can be set up. */
2291 if( operation->alg != 0 )
2292 {
2293 return( PSA_ERROR_BAD_STATE );
2294 }
2295
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002296 switch( alg )
2297 {
John Durkopee4e6602020-11-27 08:48:46 -08002298#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002299 case PSA_ALG_MD2:
2300 mbedtls_md2_init( &operation->ctx.md2 );
2301 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2302 break;
2303#endif
John Durkopee4e6602020-11-27 08:48:46 -08002304#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002305 case PSA_ALG_MD4:
2306 mbedtls_md4_init( &operation->ctx.md4 );
2307 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2308 break;
2309#endif
John Durkopee4e6602020-11-27 08:48:46 -08002310#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002311 case PSA_ALG_MD5:
2312 mbedtls_md5_init( &operation->ctx.md5 );
2313 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2314 break;
2315#endif
John Durkopee4e6602020-11-27 08:48:46 -08002316#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002317 case PSA_ALG_RIPEMD160:
2318 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2319 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2320 break;
2321#endif
John Durkopee4e6602020-11-27 08:48:46 -08002322#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002323 case PSA_ALG_SHA_1:
2324 mbedtls_sha1_init( &operation->ctx.sha1 );
2325 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2326 break;
2327#endif
John Durkopee4e6602020-11-27 08:48:46 -08002328#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002329 case PSA_ALG_SHA_224:
2330 mbedtls_sha256_init( &operation->ctx.sha256 );
2331 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2332 break;
John Durkopee4e6602020-11-27 08:48:46 -08002333#endif
2334#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335 case PSA_ALG_SHA_256:
2336 mbedtls_sha256_init( &operation->ctx.sha256 );
2337 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2338 break;
2339#endif
John Durkopee4e6602020-11-27 08:48:46 -08002340#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 case PSA_ALG_SHA_384:
2342 mbedtls_sha512_init( &operation->ctx.sha512 );
2343 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2344 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002345#endif
John Durkopee4e6602020-11-27 08:48:46 -08002346#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 case PSA_ALG_SHA_512:
2348 mbedtls_sha512_init( &operation->ctx.sha512 );
2349 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2350 break;
2351#endif
2352 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002353 return( PSA_ALG_IS_HASH( alg ) ?
2354 PSA_ERROR_NOT_SUPPORTED :
2355 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002356 }
2357 if( ret == 0 )
2358 operation->alg = alg;
2359 else
2360 psa_hash_abort( operation );
2361 return( mbedtls_to_psa_error( ret ) );
2362}
2363
2364psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2365 const uint8_t *input,
2366 size_t input_length )
2367{
Janos Follath24eed8d2019-11-22 13:21:35 +00002368 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002369
2370 /* Don't require hash implementations to behave correctly on a
2371 * zero-length input, which may have an invalid pointer. */
2372 if( input_length == 0 )
2373 return( PSA_SUCCESS );
2374
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002375 switch( operation->alg )
2376 {
John Durkopee4e6602020-11-27 08:48:46 -08002377#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002378 case PSA_ALG_MD2:
2379 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2380 input, input_length );
2381 break;
2382#endif
John Durkopee4e6602020-11-27 08:48:46 -08002383#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002384 case PSA_ALG_MD4:
2385 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2386 input, input_length );
2387 break;
2388#endif
John Durkopee4e6602020-11-27 08:48:46 -08002389#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002390 case PSA_ALG_MD5:
2391 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2392 input, input_length );
2393 break;
2394#endif
John Durkopee4e6602020-11-27 08:48:46 -08002395#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002396 case PSA_ALG_RIPEMD160:
2397 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2398 input, input_length );
2399 break;
2400#endif
John Durkopee4e6602020-11-27 08:48:46 -08002401#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002402 case PSA_ALG_SHA_1:
2403 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2404 input, input_length );
2405 break;
2406#endif
John Durkop6ca23272020-12-03 06:01:32 -08002407#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002408 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002409 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2410 input, input_length );
2411 break;
2412#endif
2413#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002414 case PSA_ALG_SHA_256:
2415 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2416 input, input_length );
2417 break;
2418#endif
John Durkop6ca23272020-12-03 06:01:32 -08002419#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002420 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002421 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2422 input, input_length );
2423 break;
2424#endif
2425#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426 case PSA_ALG_SHA_512:
2427 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2428 input, input_length );
2429 break;
2430#endif
2431 default:
John Durkopee4e6602020-11-27 08:48:46 -08002432 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002433 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002434 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002435
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436 if( ret != 0 )
2437 psa_hash_abort( operation );
2438 return( mbedtls_to_psa_error( ret ) );
2439}
2440
2441psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2442 uint8_t *hash,
2443 size_t hash_size,
2444 size_t *hash_length )
2445{
itayzafrir40835d42018-08-02 13:14:17 +03002446 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002447 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002448 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002449
2450 /* Fill the output buffer with something that isn't a valid hash
2451 * (barring an attack on the hash and deliberately-crafted input),
2452 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002453 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002454 /* If hash_size is 0 then hash may be NULL and then the
2455 * call to memset would have undefined behavior. */
2456 if( hash_size != 0 )
2457 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458
2459 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002460 {
2461 status = PSA_ERROR_BUFFER_TOO_SMALL;
2462 goto exit;
2463 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002464
2465 switch( operation->alg )
2466 {
John Durkopee4e6602020-11-27 08:48:46 -08002467#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002468 case PSA_ALG_MD2:
2469 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2470 break;
2471#endif
John Durkopee4e6602020-11-27 08:48:46 -08002472#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002473 case PSA_ALG_MD4:
2474 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2475 break;
2476#endif
John Durkopee4e6602020-11-27 08:48:46 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478 case PSA_ALG_MD5:
2479 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2480 break;
2481#endif
John Durkopee4e6602020-11-27 08:48:46 -08002482#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_RIPEMD160:
2484 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2485 break;
2486#endif
John Durkopee4e6602020-11-27 08:48:46 -08002487#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 case PSA_ALG_SHA_1:
2489 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2490 break;
2491#endif
John Durkop6ca23272020-12-03 06:01:32 -08002492#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002493 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002494 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2495 break;
2496#endif
2497#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 case PSA_ALG_SHA_256:
2499 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2500 break;
2501#endif
John Durkop6ca23272020-12-03 06:01:32 -08002502#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002503 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002504 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2505 break;
2506#endif
2507#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508 case PSA_ALG_SHA_512:
2509 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2510 break;
2511#endif
2512 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002513 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002514 }
itayzafrir40835d42018-08-02 13:14:17 +03002515 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002516
itayzafrir40835d42018-08-02 13:14:17 +03002517exit:
2518 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002520 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002521 return( psa_hash_abort( operation ) );
2522 }
2523 else
2524 {
2525 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002526 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002527 }
2528}
2529
Gilles Peskine2d277862018-06-18 15:41:12 +02002530psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2531 const uint8_t *hash,
2532 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002533{
2534 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2535 size_t actual_hash_length;
2536 psa_status_t status = psa_hash_finish( operation,
2537 actual_hash, sizeof( actual_hash ),
2538 &actual_hash_length );
2539 if( status != PSA_SUCCESS )
2540 return( status );
2541 if( actual_hash_length != hash_length )
2542 return( PSA_ERROR_INVALID_SIGNATURE );
2543 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2544 return( PSA_ERROR_INVALID_SIGNATURE );
2545 return( PSA_SUCCESS );
2546}
2547
Gilles Peskine0a749c82019-11-28 19:33:58 +01002548psa_status_t psa_hash_compute( psa_algorithm_t alg,
2549 const uint8_t *input, size_t input_length,
2550 uint8_t *hash, size_t hash_size,
2551 size_t *hash_length )
2552{
2553 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2554 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2555
2556 *hash_length = hash_size;
2557 status = psa_hash_setup( &operation, alg );
2558 if( status != PSA_SUCCESS )
2559 goto exit;
2560 status = psa_hash_update( &operation, input, input_length );
2561 if( status != PSA_SUCCESS )
2562 goto exit;
2563 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2564 if( status != PSA_SUCCESS )
2565 goto exit;
2566
2567exit:
2568 if( status == PSA_SUCCESS )
2569 status = psa_hash_abort( &operation );
2570 else
2571 psa_hash_abort( &operation );
2572 return( status );
2573}
2574
2575psa_status_t psa_hash_compare( psa_algorithm_t alg,
2576 const uint8_t *input, size_t input_length,
2577 const uint8_t *hash, size_t hash_length )
2578{
2579 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2580 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2581
2582 status = psa_hash_setup( &operation, alg );
2583 if( status != PSA_SUCCESS )
2584 goto exit;
2585 status = psa_hash_update( &operation, input, input_length );
2586 if( status != PSA_SUCCESS )
2587 goto exit;
2588 status = psa_hash_verify( &operation, hash, hash_length );
2589 if( status != PSA_SUCCESS )
2590 goto exit;
2591
2592exit:
2593 if( status == PSA_SUCCESS )
2594 status = psa_hash_abort( &operation );
2595 else
2596 psa_hash_abort( &operation );
2597 return( status );
2598}
2599
Gilles Peskineeb35d782019-01-22 17:56:16 +01002600psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2601 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002602{
2603 if( target_operation->alg != 0 )
2604 return( PSA_ERROR_BAD_STATE );
2605
2606 switch( source_operation->alg )
2607 {
2608 case 0:
2609 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002610#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002611 case PSA_ALG_MD2:
2612 mbedtls_md2_clone( &target_operation->ctx.md2,
2613 &source_operation->ctx.md2 );
2614 break;
2615#endif
John Durkopee4e6602020-11-27 08:48:46 -08002616#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002617 case PSA_ALG_MD4:
2618 mbedtls_md4_clone( &target_operation->ctx.md4,
2619 &source_operation->ctx.md4 );
2620 break;
2621#endif
John Durkopee4e6602020-11-27 08:48:46 -08002622#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002623 case PSA_ALG_MD5:
2624 mbedtls_md5_clone( &target_operation->ctx.md5,
2625 &source_operation->ctx.md5 );
2626 break;
2627#endif
John Durkopee4e6602020-11-27 08:48:46 -08002628#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002629 case PSA_ALG_RIPEMD160:
2630 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2631 &source_operation->ctx.ripemd160 );
2632 break;
2633#endif
John Durkopee4e6602020-11-27 08:48:46 -08002634#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002635 case PSA_ALG_SHA_1:
2636 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2637 &source_operation->ctx.sha1 );
2638 break;
2639#endif
John Durkop6ca23272020-12-03 06:01:32 -08002640#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002641 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002642 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2643 &source_operation->ctx.sha256 );
2644 break;
2645#endif
2646#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002647 case PSA_ALG_SHA_256:
2648 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2649 &source_operation->ctx.sha256 );
2650 break;
2651#endif
John Durkop6ca23272020-12-03 06:01:32 -08002652#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002653 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002654 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2655 &source_operation->ctx.sha512 );
2656 break;
2657#endif
2658#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002659 case PSA_ALG_SHA_512:
2660 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2661 &source_operation->ctx.sha512 );
2662 break;
2663#endif
2664 default:
2665 return( PSA_ERROR_NOT_SUPPORTED );
2666 }
2667
2668 target_operation->alg = source_operation->alg;
2669 return( PSA_SUCCESS );
2670}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002671
2672
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002673/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674/* MAC */
2675/****************************************************************/
2676
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002677static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678 psa_algorithm_t alg,
2679 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002680 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002681 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002684 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002686 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002687 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002688
Gilles Peskine8c9def32018-02-08 10:02:12 +01002689 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2690 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002691 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002692 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002693 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 mode = MBEDTLS_MODE_STREAM;
2695 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002696 case PSA_ALG_CTR:
2697 mode = MBEDTLS_MODE_CTR;
2698 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002699 case PSA_ALG_CFB:
2700 mode = MBEDTLS_MODE_CFB;
2701 break;
2702 case PSA_ALG_OFB:
2703 mode = MBEDTLS_MODE_OFB;
2704 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002705 case PSA_ALG_ECB_NO_PADDING:
2706 mode = MBEDTLS_MODE_ECB;
2707 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002708 case PSA_ALG_CBC_NO_PADDING:
2709 mode = MBEDTLS_MODE_CBC;
2710 break;
2711 case PSA_ALG_CBC_PKCS7:
2712 mode = MBEDTLS_MODE_CBC;
2713 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002714 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 mode = MBEDTLS_MODE_CCM;
2716 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002717 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002718 mode = MBEDTLS_MODE_GCM;
2719 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002720 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002721 mode = MBEDTLS_MODE_CHACHAPOLY;
2722 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 default:
2724 return( NULL );
2725 }
2726 }
2727 else if( alg == PSA_ALG_CMAC )
2728 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002729 else
2730 return( NULL );
2731
2732 switch( key_type )
2733 {
2734 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002735 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002736 break;
2737 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002738 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2739 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002740 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002741 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002743 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002744 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2745 * but two-key Triple-DES is functionally three-key Triple-DES
2746 * with K1=K3, so that's how we present it to mbedtls. */
2747 if( key_bits == 128 )
2748 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 break;
2750 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002751 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752 break;
2753 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002754 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002755 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002756 case PSA_KEY_TYPE_CHACHA20:
2757 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2758 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759 default:
2760 return( NULL );
2761 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002762 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002763 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002764
Jaeden Amero23bbb752018-06-26 14:16:54 +01002765 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2766 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002767}
2768
John Durkop6ba40d12020-11-10 08:50:04 -08002769#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002770static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002771{
Gilles Peskine2d277862018-06-18 15:41:12 +02002772 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002773 {
2774 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002775 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002776 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002778 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002779 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002780 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002781 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002782 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002783 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002784 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002785 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002786 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002787 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002788 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002789 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002790 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002791 return( 128 );
2792 default:
2793 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002794 }
2795}
John Durkop07cc04a2020-11-16 22:08:34 -08002796#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002797
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002798/* Initialize the MAC operation structure. Once this function has been
2799 * called, psa_mac_abort can run and will do the right thing. */
2800static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2801 psa_algorithm_t alg )
2802{
2803 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2804
Steven Cooreman15472f82021-03-02 16:16:22 +01002805 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806 operation->key_set = 0;
2807 operation->iv_set = 0;
2808 operation->iv_required = 0;
2809 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002810 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811
2812#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002813 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814 {
2815 operation->iv_required = 0;
2816 mbedtls_cipher_init( &operation->ctx.cmac );
2817 status = PSA_SUCCESS;
2818 }
2819 else
2820#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002821#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002822 if( PSA_ALG_IS_HMAC( operation->alg ) )
2823 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002824 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2825 operation->ctx.hmac.hash_ctx.alg = 0;
2826 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002827 }
2828 else
John Durkopd0321952020-10-29 21:37:36 -07002829#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002830 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002831 if( ! PSA_ALG_IS_MAC( alg ) )
2832 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002833 }
2834
2835 if( status != PSA_SUCCESS )
2836 memset( operation, 0, sizeof( *operation ) );
2837 return( status );
2838}
2839
John Durkop6ba40d12020-11-10 08:50:04 -08002840#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002841static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2842{
Gilles Peskine3f108122018-12-07 18:14:53 +01002843 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002844 return( psa_hash_abort( &hmac->hash_ctx ) );
2845}
John Durkop6ba40d12020-11-10 08:50:04 -08002846#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2849{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002850 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002852 /* The object has (apparently) been initialized but it is not
2853 * in use. It's ok to call abort on such an object, and there's
2854 * nothing to do. */
2855 return( PSA_SUCCESS );
2856 }
2857 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002858#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002859 if( operation->alg == PSA_ALG_CMAC )
2860 {
2861 mbedtls_cipher_free( &operation->ctx.cmac );
2862 }
2863 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002864#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002865#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002866 if( PSA_ALG_IS_HMAC( operation->alg ) )
2867 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002868 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002869 }
2870 else
John Durkopd0321952020-10-29 21:37:36 -07002871#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002872 {
2873 /* Sanity check (shouldn't happen: operation->alg should
2874 * always have been initialized to a valid value). */
2875 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002876 }
Moran Peker41deec42018-04-04 15:43:05 +03002877
Gilles Peskine8c9def32018-02-08 10:02:12 +01002878 operation->alg = 0;
2879 operation->key_set = 0;
2880 operation->iv_set = 0;
2881 operation->iv_required = 0;
2882 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002883 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002884
Gilles Peskine8c9def32018-02-08 10:02:12 +01002885 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002886
2887bad_state:
2888 /* If abort is called on an uninitialized object, we can't trust
2889 * anything. Wipe the object in case it contains confidential data.
2890 * This may result in a memory leak if a pointer gets overwritten,
2891 * but it's too late to do anything about this. */
2892 memset( operation, 0, sizeof( *operation ) );
2893 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002894}
2895
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002896#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002897static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2898 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002899{
Janos Follath24eed8d2019-11-22 13:21:35 +00002900 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002901 const mbedtls_cipher_info_t *cipher_info =
2902 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2903 slot->attr.type, slot->attr.bits,
2904 NULL );
2905 if( cipher_info == NULL )
2906 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907
2908 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2909 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002910 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002911
2912 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002913 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002914 slot->attr.bits );
2915exit:
2916 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002918#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919
John Durkop6ba40d12020-11-10 08:50:04 -08002920#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2922 const uint8_t *key,
2923 size_t key_length,
2924 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002925{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002926 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002927 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002928 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002929 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002930 psa_status_t status;
2931
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002932 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2933 * overflow below. This should never trigger if the hash algorithm
2934 * is implemented correctly. */
2935 /* The size checks against the ipad and opad buffers cannot be written
2936 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2937 * because that triggers -Wlogical-op on GCC 7.3. */
2938 if( block_size > sizeof( ipad ) )
2939 return( PSA_ERROR_NOT_SUPPORTED );
2940 if( block_size > sizeof( hmac->opad ) )
2941 return( PSA_ERROR_NOT_SUPPORTED );
2942 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002943 return( PSA_ERROR_NOT_SUPPORTED );
2944
Gilles Peskined223b522018-06-11 18:12:58 +02002945 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002946 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002947 status = psa_hash_compute( hash_alg, key, key_length,
2948 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002950 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951 }
Gilles Peskine96889972018-07-12 17:07:03 +02002952 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2953 * but it is permitted. It is common when HMAC is used in HKDF, for
2954 * example. Don't call `memcpy` in the 0-length because `key` could be
2955 * an invalid pointer which would make the behavior undefined. */
2956 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002957 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958
Gilles Peskined223b522018-06-11 18:12:58 +02002959 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2960 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002961 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002962 ipad[i] ^= 0x36;
2963 memset( ipad + key_length, 0x36, block_size - key_length );
2964
2965 /* Copy the key material from ipad to opad, flipping the requisite bits,
2966 * and filling the rest of opad with the requisite constant. */
2967 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2969 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002973 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002974
Gilles Peskine01126fa2018-07-12 17:04:55 +02002975 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002976
2977cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002978 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002979
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002980 return( status );
2981}
John Durkop6ba40d12020-11-10 08:50:04 -08002982#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002983
Gilles Peskine89167cb2018-07-08 20:12:23 +02002984static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002985 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002986 psa_algorithm_t alg,
2987 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002990 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002991 psa_key_slot_t *slot;
Steven Cooreman15472f82021-03-02 16:16:22 +01002992 size_t output_length = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002993 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002994 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002995
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002996 /* A context must be freshly initialized before it can be set up. */
2997 if( operation->alg != 0 )
2998 {
2999 return( PSA_ERROR_BAD_STATE );
3000 }
3001
Steven Cooreman15472f82021-03-02 16:16:22 +01003002 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003003 if( status != PSA_SUCCESS )
3004 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003005 if( is_sign )
3006 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007
Ronald Cron5c522922020-11-14 16:35:34 +01003008 status = psa_get_and_lock_transparent_key_slot_with_policy(
3009 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003010 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003011 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01003012
3013 status = psa_get_mac_output_length( alg, slot->attr.type,
3014 &output_length );
3015 if( status != PSA_SUCCESS )
3016 goto exit;
3017
3018 operation->mac_size = (uint8_t) output_length;
3019
3020 if( operation->mac_size < 4 )
3021 {
3022 /* A very short MAC is too short for security since it can be
3023 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3024 * so we make this our minimum, even though 32 bits is still
3025 * too small for security. */
3026 status = PSA_ERROR_NOT_SUPPORTED;
3027 goto exit;
3028 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003029
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01003031 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003032 {
Steven Cooreman15472f82021-03-02 16:16:22 +01003033 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003034 }
3035 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003037#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003038 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003039 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003040 /* Sanity check. This shouldn't fail on a valid configuration. */
3041 if( operation->mac_size == 0 ||
3042 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3043 {
3044 status = PSA_ERROR_NOT_SUPPORTED;
3045 goto exit;
3046 }
3047
Gilles Peskine8e338702019-07-30 20:06:31 +02003048 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003049 {
3050 status = PSA_ERROR_INVALID_ARGUMENT;
3051 goto exit;
3052 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003053
Gilles Peskine01126fa2018-07-12 17:04:55 +02003054 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003055 slot->key.data,
3056 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003057 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003058 }
3059 else
John Durkopd0321952020-10-29 21:37:36 -07003060#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003061 {
3062 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003064
Gilles Peskinefbfac682018-07-08 20:51:54 +02003065exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003066 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003067 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003068 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003070 else
3071 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003072 operation->key_set = 1;
3073 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003074
Ronald Cron5c522922020-11-14 16:35:34 +01003075 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003076
Ronald Cron5c522922020-11-14 16:35:34 +01003077 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078}
3079
Gilles Peskine89167cb2018-07-08 20:12:23 +02003080psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003081 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003082 psa_algorithm_t alg )
3083{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003084 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003085}
3086
3087psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003088 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003089 psa_algorithm_t alg )
3090{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003091 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003092}
3093
Gilles Peskine8c9def32018-02-08 10:02:12 +01003094psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3095 const uint8_t *input,
3096 size_t input_length )
3097{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003098 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003099 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003100 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003101 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003103 operation->has_input = 1;
3104
Gilles Peskine8c9def32018-02-08 10:02:12 +01003105#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003106 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003107 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3109 input, input_length );
3110 status = mbedtls_to_psa_error( ret );
3111 }
3112 else
3113#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003114#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003115 if( PSA_ALG_IS_HMAC( operation->alg ) )
3116 {
3117 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3118 input_length );
3119 }
3120 else
John Durkopd0321952020-10-29 21:37:36 -07003121#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003122 {
3123 /* This shouldn't happen if `operation` was initialized by
3124 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003125 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003126 }
3127
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128 if( status != PSA_SUCCESS )
3129 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003130 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003131}
3132
John Durkop6ba40d12020-11-10 08:50:04 -08003133#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003134static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3135 uint8_t *mac,
3136 size_t mac_size )
3137{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003138 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003139 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3140 size_t hash_size = 0;
3141 size_t block_size = psa_get_hash_block_size( hash_alg );
3142 psa_status_t status;
3143
Gilles Peskine01126fa2018-07-12 17:04:55 +02003144 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3145 if( status != PSA_SUCCESS )
3146 return( status );
3147 /* From here on, tmp needs to be wiped. */
3148
3149 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3150 if( status != PSA_SUCCESS )
3151 goto exit;
3152
3153 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3154 if( status != PSA_SUCCESS )
3155 goto exit;
3156
3157 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3158 if( status != PSA_SUCCESS )
3159 goto exit;
3160
Gilles Peskined911eb72018-08-14 15:18:45 +02003161 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3162 if( status != PSA_SUCCESS )
3163 goto exit;
3164
3165 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166
3167exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003168 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003169 return( status );
3170}
John Durkop6ba40d12020-11-10 08:50:04 -08003171#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003172
mohammad16036df908f2018-04-02 08:34:15 -07003173static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003174 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003175 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003177 if( ! operation->key_set )
3178 return( PSA_ERROR_BAD_STATE );
3179 if( operation->iv_required && ! operation->iv_set )
3180 return( PSA_ERROR_BAD_STATE );
3181
Gilles Peskine8c9def32018-02-08 10:02:12 +01003182 if( mac_size < operation->mac_size )
3183 return( PSA_ERROR_BUFFER_TOO_SMALL );
3184
Gilles Peskine8c9def32018-02-08 10:02:12 +01003185#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003186 if( operation->alg == PSA_ALG_CMAC )
3187 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003188 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003189 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3190 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003191 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003192 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003193 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003194 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003195 else
3196#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003197#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003198 if( PSA_ALG_IS_HMAC( operation->alg ) )
3199 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003200 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003201 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003202 }
3203 else
John Durkopd0321952020-10-29 21:37:36 -07003204#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003205 {
3206 /* This shouldn't happen if `operation` was initialized by
3207 * a setup function. */
3208 return( PSA_ERROR_BAD_STATE );
3209 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003210}
3211
Gilles Peskineacd4be32018-07-08 19:56:25 +02003212psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3213 uint8_t *mac,
3214 size_t mac_size,
3215 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003216{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003217 psa_status_t status;
3218
Jaeden Amero252ef282019-02-15 14:05:35 +00003219 if( operation->alg == 0 )
3220 {
3221 return( PSA_ERROR_BAD_STATE );
3222 }
3223
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003224 /* Fill the output buffer with something that isn't a valid mac
3225 * (barring an attack on the mac and deliberately-crafted input),
3226 * in case the caller doesn't check the return status properly. */
3227 *mac_length = mac_size;
3228 /* If mac_size is 0 then mac may be NULL and then the
3229 * call to memset would have undefined behavior. */
3230 if( mac_size != 0 )
3231 memset( mac, '!', mac_size );
3232
Gilles Peskine89167cb2018-07-08 20:12:23 +02003233 if( ! operation->is_sign )
3234 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003235 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003236 }
mohammad16036df908f2018-04-02 08:34:15 -07003237
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003238 status = psa_mac_finish_internal( operation, mac, mac_size );
3239
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003240 if( status == PSA_SUCCESS )
3241 {
3242 status = psa_mac_abort( operation );
3243 if( status == PSA_SUCCESS )
3244 *mac_length = operation->mac_size;
3245 else
3246 memset( mac, '!', mac_size );
3247 }
3248 else
3249 psa_mac_abort( operation );
3250 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003251}
3252
Gilles Peskineacd4be32018-07-08 19:56:25 +02003253psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3254 const uint8_t *mac,
3255 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003256{
Gilles Peskine828ed142018-06-18 23:25:51 +02003257 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003258 psa_status_t status;
3259
Jaeden Amero252ef282019-02-15 14:05:35 +00003260 if( operation->alg == 0 )
3261 {
3262 return( PSA_ERROR_BAD_STATE );
3263 }
3264
Gilles Peskine89167cb2018-07-08 20:12:23 +02003265 if( operation->is_sign )
3266 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003267 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003268 }
3269 if( operation->mac_size != mac_length )
3270 {
3271 status = PSA_ERROR_INVALID_SIGNATURE;
3272 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003273 }
mohammad16036df908f2018-04-02 08:34:15 -07003274
3275 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003276 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003277 if( status != PSA_SUCCESS )
3278 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003279
3280 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3281 status = PSA_ERROR_INVALID_SIGNATURE;
3282
3283cleanup:
3284 if( status == PSA_SUCCESS )
3285 status = psa_mac_abort( operation );
3286 else
3287 psa_mac_abort( operation );
3288
Gilles Peskine3f108122018-12-07 18:14:53 +01003289 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003290
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003291 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003292}
3293
3294
Gilles Peskine20035e32018-02-03 22:44:14 +01003295
Gilles Peskine20035e32018-02-03 22:44:14 +01003296/****************************************************************/
3297/* Asymmetric cryptography */
3298/****************************************************************/
3299
John Durkop6ba40d12020-11-10 08:50:04 -08003300#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3301 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003302/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003303 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003304static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3305 size_t hash_length,
3306 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003307{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003308 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003309 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003310 *md_alg = mbedtls_md_get_type( md_info );
3311
3312 /* The Mbed TLS RSA module uses an unsigned int for hash length
3313 * parameters. Validate that it fits so that we don't risk an
3314 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003315#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003316 if( hash_length > UINT_MAX )
3317 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003318#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003319
John Durkop0e005192020-10-31 22:06:54 -07003320#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003321 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3322 * must be correct. */
3323 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3324 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003325 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003326 if( md_info == NULL )
3327 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 if( mbedtls_md_get_size( md_info ) != hash_length )
3329 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003330 }
John Durkop0e005192020-10-31 22:06:54 -07003331#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003332
John Durkop0e005192020-10-31 22:06:54 -07003333#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003334 /* PSS requires a hash internally. */
3335 if( PSA_ALG_IS_RSA_PSS( alg ) )
3336 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003337 if( md_info == NULL )
3338 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003339 }
John Durkop0e005192020-10-31 22:06:54 -07003340#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003341
Gilles Peskine61b91d42018-06-08 16:09:36 +02003342 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003343}
3344
Gilles Peskine2b450e32018-06-27 15:42:46 +02003345static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3346 psa_algorithm_t alg,
3347 const uint8_t *hash,
3348 size_t hash_length,
3349 uint8_t *signature,
3350 size_t signature_size,
3351 size_t *signature_length )
3352{
3353 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003354 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003355 mbedtls_md_type_t md_alg;
3356
3357 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3358 if( status != PSA_SUCCESS )
3359 return( status );
3360
Gilles Peskine630a18a2018-06-29 17:49:35 +02003361 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003362 return( PSA_ERROR_BUFFER_TOO_SMALL );
3363
John Durkop0e005192020-10-31 22:06:54 -07003364#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003365 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3366 {
3367 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3368 MBEDTLS_MD_NONE );
3369 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003370 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003371 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003372 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003373 md_alg,
3374 (unsigned int) hash_length,
3375 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003376 signature );
3377 }
3378 else
John Durkop0e005192020-10-31 22:06:54 -07003379#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3380#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003381 if( PSA_ALG_IS_RSA_PSS( alg ) )
3382 {
3383 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3384 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003385 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003386 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003388 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003389 (unsigned int) hash_length,
3390 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003391 signature );
3392 }
3393 else
John Durkop0e005192020-10-31 22:06:54 -07003394#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003395 {
3396 return( PSA_ERROR_INVALID_ARGUMENT );
3397 }
3398
3399 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003400 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003401 return( mbedtls_to_psa_error( ret ) );
3402}
3403
3404static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3405 psa_algorithm_t alg,
3406 const uint8_t *hash,
3407 size_t hash_length,
3408 const uint8_t *signature,
3409 size_t signature_length )
3410{
3411 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003412 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413 mbedtls_md_type_t md_alg;
3414
3415 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3416 if( status != PSA_SUCCESS )
3417 return( status );
3418
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003419 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3420 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003421
John Durkop0e005192020-10-31 22:06:54 -07003422#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003423 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3424 {
3425 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3426 MBEDTLS_MD_NONE );
3427 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003428 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003429 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003430 MBEDTLS_RSA_PUBLIC,
3431 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003432 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003433 hash,
3434 signature );
3435 }
3436 else
John Durkop0e005192020-10-31 22:06:54 -07003437#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3438#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003439 if( PSA_ALG_IS_RSA_PSS( alg ) )
3440 {
3441 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3442 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003443 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003444 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003446 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003447 (unsigned int) hash_length,
3448 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003449 signature );
3450 }
3451 else
John Durkop0e005192020-10-31 22:06:54 -07003452#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003453 {
3454 return( PSA_ERROR_INVALID_ARGUMENT );
3455 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003456
3457 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3458 * the rest of the signature is invalid". This has little use in
3459 * practice and PSA doesn't report this distinction. */
3460 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3461 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003462 return( mbedtls_to_psa_error( ret ) );
3463}
John Durkop6ba40d12020-11-10 08:50:04 -08003464#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3465 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003466
John Durkop6ba40d12020-11-10 08:50:04 -08003467#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3468 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003469/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3470 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3471 * (even though these functions don't modify it). */
3472static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3473 psa_algorithm_t alg,
3474 const uint8_t *hash,
3475 size_t hash_length,
3476 uint8_t *signature,
3477 size_t signature_size,
3478 size_t *signature_length )
3479{
Janos Follath24eed8d2019-11-22 13:21:35 +00003480 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003481 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003482 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003483 mbedtls_mpi_init( &r );
3484 mbedtls_mpi_init( &s );
3485
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003486 if( signature_size < 2 * curve_bytes )
3487 {
3488 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3489 goto cleanup;
3490 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003491
John Durkop0ea39e02020-10-13 19:58:20 -07003492#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003493 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3494 {
3495 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3496 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3497 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003498 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3499 &ecp->d, hash,
3500 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003501 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003502 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003503 }
3504 else
John Durkop0ea39e02020-10-13 19:58:20 -07003505#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003506 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003507 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003508 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3509 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003510 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003511 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003512 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003513
3514 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3515 signature,
3516 curve_bytes ) );
3517 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3518 signature + curve_bytes,
3519 curve_bytes ) );
3520
3521cleanup:
3522 mbedtls_mpi_free( &r );
3523 mbedtls_mpi_free( &s );
3524 if( ret == 0 )
3525 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003526 return( mbedtls_to_psa_error( ret ) );
3527}
3528
3529static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3530 const uint8_t *hash,
3531 size_t hash_length,
3532 const uint8_t *signature,
3533 size_t signature_length )
3534{
Janos Follath24eed8d2019-11-22 13:21:35 +00003535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003536 mbedtls_mpi r, s;
3537 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3538 mbedtls_mpi_init( &r );
3539 mbedtls_mpi_init( &s );
3540
3541 if( signature_length != 2 * curve_bytes )
3542 return( PSA_ERROR_INVALID_SIGNATURE );
3543
3544 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3545 signature,
3546 curve_bytes ) );
3547 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3548 signature + curve_bytes,
3549 curve_bytes ) );
3550
Steven Cooremanacda8342020-07-24 23:09:52 +02003551 /* Check whether the public part is loaded. If not, load it. */
3552 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3553 {
3554 MBEDTLS_MPI_CHK(
3555 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003556 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003557 }
3558
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003559 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3560 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003561
3562cleanup:
3563 mbedtls_mpi_free( &r );
3564 mbedtls_mpi_free( &s );
3565 return( mbedtls_to_psa_error( ret ) );
3566}
John Durkop6ba40d12020-11-10 08:50:04 -08003567#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3568 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003569
Ronald Croncf56a0a2020-08-04 09:51:30 +02003570psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003571 psa_algorithm_t alg,
3572 const uint8_t *hash,
3573 size_t hash_length,
3574 uint8_t *signature,
3575 size_t signature_size,
3576 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003577{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003579 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003580 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003581
3582 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003583 /* Immediately reject a zero-length signature buffer. This guarantees
3584 * that signature must be a valid pointer. (On the other hand, the hash
3585 * buffer can in principle be empty since it doesn't actually have
3586 * to be a hash.) */
3587 if( signature_size == 0 )
3588 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003589
Ronald Cron5c522922020-11-14 16:35:34 +01003590 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3591 PSA_KEY_USAGE_SIGN_HASH,
3592 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003593 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003594 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003596 {
3597 status = PSA_ERROR_INVALID_ARGUMENT;
3598 goto exit;
3599 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003600
Steven Cooremancd84cb42020-07-16 20:28:36 +02003601 /* Try any of the available accelerators first */
3602 status = psa_driver_wrapper_sign_hash( slot,
3603 alg,
3604 hash,
3605 hash_length,
3606 signature,
3607 signature_size,
3608 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003609 if( status != PSA_ERROR_NOT_SUPPORTED ||
3610 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003611 goto exit;
3612
Steven Cooreman7a250572020-07-17 16:43:05 +02003613 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003614#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3615 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003616 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003617 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003618 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003619
Ronald Cron90857082020-11-25 14:58:33 +01003620 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3621 slot->key.data,
3622 slot->key.bytes,
3623 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003624 if( status != PSA_SUCCESS )
3625 goto exit;
3626
Steven Cooremana2371e52020-07-28 14:30:39 +02003627 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003628 alg,
3629 hash, hash_length,
3630 signature, signature_size,
3631 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003632
Steven Cooremana2371e52020-07-28 14:30:39 +02003633 mbedtls_rsa_free( rsa );
3634 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003635 }
3636 else
John Durkop6ba40d12020-11-10 08:50:04 -08003637#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3638 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003639 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003640 {
John Durkop9814fa22020-11-04 12:28:15 -08003641#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3642 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003643 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003644#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003645 PSA_ALG_IS_ECDSA( alg )
3646#else
3647 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3648#endif
3649 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003650 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003651 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003652 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003653 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003654 slot->key.data,
3655 slot->key.bytes,
3656 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003657 if( status != PSA_SUCCESS )
3658 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003659 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003660 alg,
3661 hash, hash_length,
3662 signature, signature_size,
3663 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003664 mbedtls_ecp_keypair_free( ecp );
3665 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003666 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003667 else
John Durkop9814fa22020-11-04 12:28:15 -08003668#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3669 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003670 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003671 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003672 }
itayzafrir5c753392018-05-08 11:18:38 +03003673 }
3674 else
itayzafrir5c753392018-05-08 11:18:38 +03003675 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003676 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003677 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003678
3679exit:
3680 /* Fill the unused part of the output buffer (the whole buffer on error,
3681 * the trailing part on success) with something that isn't a valid mac
3682 * (barring an attack on the mac and deliberately-crafted input),
3683 * in case the caller doesn't check the return status properly. */
3684 if( status == PSA_SUCCESS )
3685 memset( signature + *signature_length, '!',
3686 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003687 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003688 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003689 /* If signature_size is 0 then we have nothing to do. We must not call
3690 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003691
Ronald Cron5c522922020-11-14 16:35:34 +01003692 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003693
Ronald Cron5c522922020-11-14 16:35:34 +01003694 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003695}
3696
Ronald Croncf56a0a2020-08-04 09:51:30 +02003697psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003698 psa_algorithm_t alg,
3699 const uint8_t *hash,
3700 size_t hash_length,
3701 const uint8_t *signature,
3702 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003703{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003704 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003705 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003706 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003707
Ronald Cron5c522922020-11-14 16:35:34 +01003708 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3709 PSA_KEY_USAGE_VERIFY_HASH,
3710 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003711 if( status != PSA_SUCCESS )
3712 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003713
Steven Cooreman55ae2172020-07-17 19:46:15 +02003714 /* Try any of the available accelerators first */
3715 status = psa_driver_wrapper_verify_hash( slot,
3716 alg,
3717 hash,
3718 hash_length,
3719 signature,
3720 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003721 if( status != PSA_ERROR_NOT_SUPPORTED ||
3722 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003723 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003724
John Durkop6ba40d12020-11-10 08:50:04 -08003725#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3726 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003727 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003729 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003730
Ronald Cron90857082020-11-25 14:58:33 +01003731 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3732 slot->key.data,
3733 slot->key.bytes,
3734 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003735 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003736 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003737
Steven Cooremana2371e52020-07-28 14:30:39 +02003738 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003739 alg,
3740 hash, hash_length,
3741 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003742 mbedtls_rsa_free( rsa );
3743 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003744 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003745 }
3746 else
John Durkop6ba40d12020-11-10 08:50:04 -08003747#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3748 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003749 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003750 {
John Durkop9814fa22020-11-04 12:28:15 -08003751#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3752 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003753 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003754 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003755 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003756 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003757 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003758 slot->key.data,
3759 slot->key.bytes,
3760 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003761 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003762 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003763 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003764 hash, hash_length,
3765 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003766 mbedtls_ecp_keypair_free( ecp );
3767 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003768 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003769 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003770 else
John Durkop9814fa22020-11-04 12:28:15 -08003771#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3772 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003773 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003774 status = PSA_ERROR_INVALID_ARGUMENT;
3775 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003776 }
itayzafrir5c753392018-05-08 11:18:38 +03003777 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003778 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003779 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003780 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003781 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003782
3783exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003784 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003785
Ronald Cron5c522922020-11-14 16:35:34 +01003786 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003787}
3788
John Durkop0e005192020-10-31 22:06:54 -07003789#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003790static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3791 mbedtls_rsa_context *rsa )
3792{
3793 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3794 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3795 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3796 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3797}
John Durkop0e005192020-10-31 22:06:54 -07003798#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003799
Ronald Croncf56a0a2020-08-04 09:51:30 +02003800psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003801 psa_algorithm_t alg,
3802 const uint8_t *input,
3803 size_t input_length,
3804 const uint8_t *salt,
3805 size_t salt_length,
3806 uint8_t *output,
3807 size_t output_size,
3808 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003809{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003810 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003811 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003812 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003813
Darryl Green5cc689a2018-07-24 15:34:10 +01003814 (void) input;
3815 (void) input_length;
3816 (void) salt;
3817 (void) output;
3818 (void) output_size;
3819
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003820 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003822 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3823 return( PSA_ERROR_INVALID_ARGUMENT );
3824
Ronald Cron5c522922020-11-14 16:35:34 +01003825 status = psa_get_and_lock_transparent_key_slot_with_policy(
3826 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003827 if( status != PSA_SUCCESS )
3828 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003829 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3830 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003831 {
3832 status = PSA_ERROR_INVALID_ARGUMENT;
3833 goto exit;
3834 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003835
John Durkop6ba40d12020-11-10 08:50:04 -08003836#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3837 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003838 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003839 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003840 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003841 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3842 slot->key.data,
3843 slot->key.bytes,
3844 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003845 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003846 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003847
3848 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003849 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003850 status = PSA_ERROR_BUFFER_TOO_SMALL;
3851 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003852 }
John Durkop0e005192020-10-31 22:06:54 -07003853#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003854 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003856 status = mbedtls_to_psa_error(
3857 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003858 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003859 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003860 MBEDTLS_RSA_PUBLIC,
3861 input_length,
3862 input,
3863 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003864 }
3865 else
John Durkop0e005192020-10-31 22:06:54 -07003866#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3867#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003868 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003870 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003871 status = mbedtls_to_psa_error(
3872 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003873 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003874 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003875 MBEDTLS_RSA_PUBLIC,
3876 salt, salt_length,
3877 input_length,
3878 input,
3879 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 }
3881 else
John Durkop0e005192020-10-31 22:06:54 -07003882#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003884 status = PSA_ERROR_INVALID_ARGUMENT;
3885 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003887rsa_exit:
3888 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003889 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003890
Steven Cooremana2371e52020-07-28 14:30:39 +02003891 mbedtls_rsa_free( rsa );
3892 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003893 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003894 else
John Durkop9814fa22020-11-04 12:28:15 -08003895#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003896 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003897 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003898 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003899 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003900
3901exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003902 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003903
Ronald Cron5c522922020-11-14 16:35:34 +01003904 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003905}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906
Ronald Croncf56a0a2020-08-04 09:51:30 +02003907psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003908 psa_algorithm_t alg,
3909 const uint8_t *input,
3910 size_t input_length,
3911 const uint8_t *salt,
3912 size_t salt_length,
3913 uint8_t *output,
3914 size_t output_size,
3915 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003916{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003917 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003918 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003919 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003920
Darryl Green5cc689a2018-07-24 15:34:10 +01003921 (void) input;
3922 (void) input_length;
3923 (void) salt;
3924 (void) output;
3925 (void) output_size;
3926
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003927 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003928
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003929 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3930 return( PSA_ERROR_INVALID_ARGUMENT );
3931
Ronald Cron5c522922020-11-14 16:35:34 +01003932 status = psa_get_and_lock_transparent_key_slot_with_policy(
3933 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003934 if( status != PSA_SUCCESS )
3935 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003936 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003937 {
3938 status = PSA_ERROR_INVALID_ARGUMENT;
3939 goto exit;
3940 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941
John Durkop6ba40d12020-11-10 08:50:04 -08003942#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3943 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003944 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003946 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003947 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3948 slot->key.data,
3949 slot->key.bytes,
3950 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003951 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003952 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003953
Steven Cooremana2371e52020-07-28 14:30:39 +02003954 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003955 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003956 status = PSA_ERROR_INVALID_ARGUMENT;
3957 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003958 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003959
John Durkop0e005192020-10-31 22:06:54 -07003960#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003961 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003963 status = mbedtls_to_psa_error(
3964 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003965 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003966 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003967 MBEDTLS_RSA_PRIVATE,
3968 output_length,
3969 input,
3970 output,
3971 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003972 }
3973 else
John Durkop0e005192020-10-31 22:06:54 -07003974#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3975#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003976 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003977 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003978 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003979 status = mbedtls_to_psa_error(
3980 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003981 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003982 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 MBEDTLS_RSA_PRIVATE,
3984 salt, salt_length,
3985 output_length,
3986 input,
3987 output,
3988 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989 }
3990 else
John Durkop0e005192020-10-31 22:06:54 -07003991#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003993 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003994 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003995
Steven Cooreman4fed4552020-08-03 14:46:03 +02003996rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003997 mbedtls_rsa_free( rsa );
3998 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003999 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004000 else
John Durkop6ba40d12020-11-10 08:50:04 -08004001#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4002 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004003 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004004 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004005 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004006
4007exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004008 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004009
Ronald Cron5c522922020-11-14 16:35:34 +01004010 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004011}
Gilles Peskine20035e32018-02-03 22:44:14 +01004012
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004013
4014
mohammad1603503973b2018-03-12 15:59:30 +02004015/****************************************************************/
4016/* Symmetric cryptography */
4017/****************************************************************/
4018
Gilles Peskinee553c652018-06-04 16:22:46 +02004019static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004020 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004021 psa_algorithm_t alg,
4022 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004023{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004024 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004025 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004026 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004027 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004028 size_t key_bits;
4029 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004030 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4031 PSA_KEY_USAGE_ENCRYPT :
4032 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004033
Steven Cooremand3feccd2020-09-01 15:56:14 +02004034 /* A context must be freshly initialized before it can be set up. */
4035 if( operation->alg != 0 )
4036 return( PSA_ERROR_BAD_STATE );
4037
Steven Cooremana07b9972020-09-10 14:54:14 +02004038 /* The requested algorithm must be one that can be processed by cipher. */
4039 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004040 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004041
Steven Cooremanef8575e2020-09-11 11:44:50 +02004042 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004043 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004044 if( status != PSA_SUCCESS )
4045 goto exit;
4046
4047 /* Initialize the operation struct members, except for alg. The alg member
4048 * is used to indicate to psa_cipher_abort that there are resources to free,
4049 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004050 operation->key_set = 0;
4051 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004052 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004053 operation->iv_size = 0;
4054 operation->block_size = 0;
4055 if( alg == PSA_ALG_ECB_NO_PADDING )
4056 operation->iv_required = 0;
4057 else
4058 operation->iv_required = 1;
4059
Steven Cooremana07b9972020-09-10 14:54:14 +02004060 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004061 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004062 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004063 slot,
4064 alg );
4065 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004066 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004067 slot,
4068 alg );
4069
Steven Cooremanef8575e2020-09-11 11:44:50 +02004070 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004071 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004072 /* Once the driver context is initialised, it needs to be freed using
4073 * psa_cipher_abort. Indicate this through setting alg. */
4074 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004075 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004076
Steven Cooremand3feccd2020-09-01 15:56:14 +02004077 if( status != PSA_ERROR_NOT_SUPPORTED ||
4078 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4079 goto exit;
4080
Steven Cooremana07b9972020-09-10 14:54:14 +02004081 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004082 * available for the given algorithm & key. */
4083 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004084
Steven Cooremana07b9972020-09-10 14:54:14 +02004085 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004086 * psa_cipher_abort. Indicate there is something to be freed through setting
4087 * alg, and indicate the operation is being done using mbedtls crypto through
4088 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004089 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004090 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004091
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004092 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004093 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004094 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004095 {
4096 status = PSA_ERROR_NOT_SUPPORTED;
4097 goto exit;
4098 }
mohammad1603503973b2018-03-12 15:59:30 +02004099
mohammad1603503973b2018-03-12 15:59:30 +02004100 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004101 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004102 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004103
David Brown12ca5032021-02-11 11:02:00 -07004104#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004105 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004106 {
4107 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004108 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004109 memcpy( keys, slot->key.data, 16 );
4110 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004111 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4112 keys,
4113 192, cipher_operation );
4114 }
4115 else
4116#endif
4117 {
4118 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004119 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004120 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004121 }
Moran Peker41deec42018-04-04 15:43:05 +03004122 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004123 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004124
David Brown63ca2602021-01-26 11:51:12 -07004125#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4126 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004127 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004128 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004129 case PSA_ALG_CBC_NO_PADDING:
4130 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4131 MBEDTLS_PADDING_NONE );
4132 break;
4133 case PSA_ALG_CBC_PKCS7:
4134 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4135 MBEDTLS_PADDING_PKCS7 );
4136 break;
4137 default:
4138 /* The algorithm doesn't involve padding. */
4139 ret = 0;
4140 break;
4141 }
4142 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004143 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004144#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004145
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004146 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004147 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004148 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4149 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004150 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004151 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004152 }
David Brown1bfe4d72021-02-16 12:54:35 -07004153#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004154 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004155 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004156 operation->iv_size = 12;
4157#endif
mohammad1603503973b2018-03-12 15:59:30 +02004158
Steven Cooreman7df02922020-09-09 15:28:49 +02004159 status = PSA_SUCCESS;
4160
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004161exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004162 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004163 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004164 if( status == PSA_SUCCESS )
4165 {
4166 /* Update operation flags for both driver and software implementations */
4167 operation->key_set = 1;
4168 }
4169 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004170 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004171
Ronald Cron5c522922020-11-14 16:35:34 +01004172 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004173
Ronald Cron5c522922020-11-14 16:35:34 +01004174 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004175}
4176
Gilles Peskinefe119512018-07-08 21:39:34 +02004177psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004178 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004179 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004180{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004181 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004182}
4183
Gilles Peskinefe119512018-07-08 21:39:34 +02004184psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004185 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004186 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004187{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004188 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004189}
4190
Gilles Peskinefe119512018-07-08 21:39:34 +02004191psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004192 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004193 size_t iv_size,
4194 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004195{
itayzafrir534bd7c2018-08-02 13:56:32 +03004196 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004197 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004198 if( operation->iv_set || ! operation->iv_required )
4199 {
4200 return( PSA_ERROR_BAD_STATE );
4201 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004202
Steven Cooremanef8575e2020-09-11 11:44:50 +02004203 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004204 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004205 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004206 iv,
4207 iv_size,
4208 iv_length );
4209 goto exit;
4210 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004211
Moran Peker41deec42018-04-04 15:43:05 +03004212 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004213 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004214 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004215 goto exit;
4216 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004217 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004218 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004219 if( ret != 0 )
4220 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004221 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004222 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004223 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004224
mohammad16038481e742018-03-18 13:57:31 +02004225 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004226 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004227
Moran Peker395db872018-05-31 14:07:14 +03004228exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004229 if( status == PSA_SUCCESS )
4230 operation->iv_set = 1;
4231 else
Moran Peker395db872018-05-31 14:07:14 +03004232 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004233 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004234}
4235
Gilles Peskinefe119512018-07-08 21:39:34 +02004236psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004237 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004238 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004239{
itayzafrir534bd7c2018-08-02 13:56:32 +03004240 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004242 if( operation->iv_set || ! operation->iv_required )
4243 {
4244 return( PSA_ERROR_BAD_STATE );
4245 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004246
Steven Cooremanef8575e2020-09-11 11:44:50 +02004247 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004248 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004249 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004250 iv,
4251 iv_length );
4252 goto exit;
4253 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004254
Moran Pekera28258c2018-05-29 16:25:04 +03004255 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004256 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004257 status = PSA_ERROR_INVALID_ARGUMENT;
4258 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004259 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004260 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4261 status = mbedtls_to_psa_error( ret );
4262exit:
4263 if( status == PSA_SUCCESS )
4264 operation->iv_set = 1;
4265 else
mohammad1603503973b2018-03-12 15:59:30 +02004266 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004267 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004268}
4269
Steven Cooreman177deba2020-09-07 17:14:14 +02004270/* Process input for which the algorithm is set to ECB mode. This requires
4271 * manual processing, since the PSA API is defined as being able to process
4272 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4273 * underlying mbedtls_cipher_update only takes full blocks. */
4274static psa_status_t psa_cipher_update_ecb_internal(
4275 mbedtls_cipher_context_t *ctx,
4276 const uint8_t *input,
4277 size_t input_length,
4278 uint8_t *output,
4279 size_t output_size,
4280 size_t *output_length )
4281{
4282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4283 size_t block_size = ctx->cipher_info->block_size;
4284 size_t internal_output_length = 0;
4285 *output_length = 0;
4286
4287 if( input_length == 0 )
4288 {
4289 status = PSA_SUCCESS;
4290 goto exit;
4291 }
4292
4293 if( ctx->unprocessed_len > 0 )
4294 {
4295 /* Fill up to block size, and run the block if there's a full one. */
4296 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4297
4298 if( input_length < bytes_to_copy )
4299 bytes_to_copy = input_length;
4300
4301 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4302 input, bytes_to_copy );
4303 input_length -= bytes_to_copy;
4304 input += bytes_to_copy;
4305 ctx->unprocessed_len += bytes_to_copy;
4306
4307 if( ctx->unprocessed_len == block_size )
4308 {
4309 status = mbedtls_to_psa_error(
4310 mbedtls_cipher_update( ctx,
4311 ctx->unprocessed_data,
4312 block_size,
4313 output, &internal_output_length ) );
4314
4315 if( status != PSA_SUCCESS )
4316 goto exit;
4317
4318 output += internal_output_length;
4319 output_size -= internal_output_length;
4320 *output_length += internal_output_length;
4321 ctx->unprocessed_len = 0;
4322 }
4323 }
4324
4325 while( input_length >= block_size )
4326 {
4327 /* Run all full blocks we have, one by one */
4328 status = mbedtls_to_psa_error(
4329 mbedtls_cipher_update( ctx, input,
4330 block_size,
4331 output, &internal_output_length ) );
4332
4333 if( status != PSA_SUCCESS )
4334 goto exit;
4335
4336 input_length -= block_size;
4337 input += block_size;
4338
4339 output += internal_output_length;
4340 output_size -= internal_output_length;
4341 *output_length += internal_output_length;
4342 }
4343
4344 if( input_length > 0 )
4345 {
4346 /* Save unprocessed bytes for later processing */
4347 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4348 input, input_length );
4349 ctx->unprocessed_len += input_length;
4350 }
4351
4352 status = PSA_SUCCESS;
4353
4354exit:
4355 return( status );
4356}
4357
Gilles Peskinee553c652018-06-04 16:22:46 +02004358psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4359 const uint8_t *input,
4360 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004361 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004362 size_t output_size,
4363 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004364{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004365 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004366 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004367 if( operation->alg == 0 )
4368 {
4369 return( PSA_ERROR_BAD_STATE );
4370 }
4371 if( operation->iv_required && ! operation->iv_set )
4372 {
4373 return( PSA_ERROR_BAD_STATE );
4374 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004375
Steven Cooremanef8575e2020-09-11 11:44:50 +02004376 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004377 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004378 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004379 input,
4380 input_length,
4381 output,
4382 output_size,
4383 output_length );
4384 goto exit;
4385 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004386
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004387 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004388 {
4389 /* Take the unprocessed partial block left over from previous
4390 * update calls, if any, plus the input to this call. Remove
4391 * the last partial block, if any. You get the data that will be
4392 * output in this call. */
4393 expected_output_size =
4394 ( operation->ctx.cipher.unprocessed_len + input_length )
4395 / operation->block_size * operation->block_size;
4396 }
4397 else
4398 {
4399 expected_output_size = input_length;
4400 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004401
Gilles Peskine89d789c2018-06-04 17:17:16 +02004402 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004403 {
4404 status = PSA_ERROR_BUFFER_TOO_SMALL;
4405 goto exit;
4406 }
mohammad160382759612018-03-12 18:16:40 +02004407
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004408 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4409 {
4410 /* mbedtls_cipher_update has an API inconsistency: it will only
4411 * process a single block at a time in ECB mode. Abstract away that
4412 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004413 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4414 input,
4415 input_length,
4416 output,
4417 output_size,
4418 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004419 }
4420 else
4421 {
4422 status = mbedtls_to_psa_error(
4423 mbedtls_cipher_update( &operation->ctx.cipher, input,
4424 input_length, output, output_length ) );
4425 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004426exit:
4427 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004428 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004429 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004430}
4431
Gilles Peskinee553c652018-06-04 16:22:46 +02004432psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4433 uint8_t *output,
4434 size_t output_size,
4435 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004436{
David Saadab4ecc272019-02-14 13:48:10 +02004437 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004438 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004439 if( operation->alg == 0 )
4440 {
4441 return( PSA_ERROR_BAD_STATE );
4442 }
4443 if( operation->iv_required && ! operation->iv_set )
4444 {
4445 return( PSA_ERROR_BAD_STATE );
4446 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004447
Steven Cooremanef8575e2020-09-11 11:44:50 +02004448 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004449 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004450 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004451 output,
4452 output_size,
4453 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004454 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004455 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004456
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004457 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004458 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004459 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004460 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004461 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004462 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004463 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004464 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004465 }
4466
Steven Cooremanef8575e2020-09-11 11:44:50 +02004467 status = mbedtls_to_psa_error(
4468 mbedtls_cipher_finish( &operation->ctx.cipher,
4469 temp_output_buffer,
4470 output_length ) );
4471 if( status != PSA_SUCCESS )
4472 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004473
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004474 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004475 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004476 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004477 memcpy( output, temp_output_buffer, *output_length );
4478 else
Janos Follath315b51c2018-07-09 16:04:51 +01004479 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004480
Steven Cooremanef8575e2020-09-11 11:44:50 +02004481exit:
4482 if( operation->mbedtls_in_use == 1 )
4483 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004484
Steven Cooremanef8575e2020-09-11 11:44:50 +02004485 if( status == PSA_SUCCESS )
4486 return( psa_cipher_abort( operation ) );
4487 else
4488 {
4489 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004490 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004491
Steven Cooremanef8575e2020-09-11 11:44:50 +02004492 return( status );
4493 }
mohammad1603503973b2018-03-12 15:59:30 +02004494}
4495
Gilles Peskinee553c652018-06-04 16:22:46 +02004496psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4497{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004498 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004499 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004500 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004501 * in use. It's ok to call abort on such an object, and there's
4502 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004503 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004504 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004505
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004506 /* Sanity check (shouldn't happen: operation->alg should
4507 * always have been initialized to a valid value). */
4508 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4509 return( PSA_ERROR_BAD_STATE );
4510
Steven Cooremanef8575e2020-09-11 11:44:50 +02004511 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004512 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004513 else
4514 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004515
Moran Peker41deec42018-04-04 15:43:05 +03004516 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004517 operation->key_set = 0;
4518 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004519 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004520 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004521 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004522 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004523
Moran Peker395db872018-05-31 14:07:14 +03004524 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004525}
4526
Gilles Peskinea0655c32018-04-30 17:06:50 +02004527
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004528
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004529
mohammad16035955c982018-04-26 00:53:03 +03004530/****************************************************************/
4531/* AEAD */
4532/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004533
Gilles Peskineedf9a652018-08-17 18:11:56 +02004534typedef struct
4535{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004536 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004537 const mbedtls_cipher_info_t *cipher_info;
4538 union
4539 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004540 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004541#if defined(MBEDTLS_CCM_C)
4542 mbedtls_ccm_context ccm;
4543#endif /* MBEDTLS_CCM_C */
4544#if defined(MBEDTLS_GCM_C)
4545 mbedtls_gcm_context gcm;
4546#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004547#if defined(MBEDTLS_CHACHAPOLY_C)
4548 mbedtls_chachapoly_context chachapoly;
4549#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004550 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004551 psa_algorithm_t core_alg;
4552 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553 uint8_t tag_length;
4554} aead_operation_t;
4555
Ronald Cronf95a2b12020-10-22 15:24:49 +02004556#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4557
Gilles Peskine30a9e412019-01-14 18:36:12 +01004558static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004559{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004560 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004561 {
4562#if defined(MBEDTLS_CCM_C)
4563 case PSA_ALG_CCM:
4564 mbedtls_ccm_free( &operation->ctx.ccm );
4565 break;
4566#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004567#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004568 case PSA_ALG_GCM:
4569 mbedtls_gcm_free( &operation->ctx.gcm );
4570 break;
4571#endif /* MBEDTLS_GCM_C */
4572 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004573
Ronald Cron5c522922020-11-14 16:35:34 +01004574 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004575}
4576
4577static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004578 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004579 psa_key_usage_t usage,
4580 psa_algorithm_t alg )
4581{
4582 psa_status_t status;
4583 size_t key_bits;
4584 mbedtls_cipher_id_t cipher_id;
4585
Ronald Cron5c522922020-11-14 16:35:34 +01004586 status = psa_get_and_lock_transparent_key_slot_with_policy(
4587 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004588 if( status != PSA_SUCCESS )
4589 return( status );
4590
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004591 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004592
4593 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004594 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004595 &cipher_id );
4596 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004597 {
4598 status = PSA_ERROR_NOT_SUPPORTED;
4599 goto cleanup;
4600 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004601
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004602 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004603 {
4604#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004605 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004606 operation->core_alg = PSA_ALG_CCM;
4607 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004608 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4609 * The call to mbedtls_ccm_encrypt_and_tag or
4610 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004611 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004612 {
4613 status = PSA_ERROR_INVALID_ARGUMENT;
4614 goto cleanup;
4615 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004616 mbedtls_ccm_init( &operation->ctx.ccm );
4617 status = mbedtls_to_psa_error(
4618 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004619 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004620 (unsigned int) key_bits ) );
4621 if( status != 0 )
4622 goto cleanup;
4623 break;
4624#endif /* MBEDTLS_CCM_C */
4625
4626#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004627 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004628 operation->core_alg = PSA_ALG_GCM;
4629 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004630 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4631 * The call to mbedtls_gcm_crypt_and_tag or
4632 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004633 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004634 {
4635 status = PSA_ERROR_INVALID_ARGUMENT;
4636 goto cleanup;
4637 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004638 mbedtls_gcm_init( &operation->ctx.gcm );
4639 status = mbedtls_to_psa_error(
4640 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004641 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004642 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004643 if( status != 0 )
4644 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004645 break;
4646#endif /* MBEDTLS_GCM_C */
4647
Gilles Peskine26869f22019-05-06 15:25:00 +02004648#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004649 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004650 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4651 operation->full_tag_length = 16;
4652 /* We only support the default tag length. */
4653 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004654 {
4655 status = PSA_ERROR_NOT_SUPPORTED;
4656 goto cleanup;
4657 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004658 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4659 status = mbedtls_to_psa_error(
4660 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004661 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004662 if( status != 0 )
4663 goto cleanup;
4664 break;
4665#endif /* MBEDTLS_CHACHAPOLY_C */
4666
Gilles Peskineedf9a652018-08-17 18:11:56 +02004667 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004668 status = PSA_ERROR_NOT_SUPPORTED;
4669 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004670 }
4671
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004672 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4673 {
4674 status = PSA_ERROR_INVALID_ARGUMENT;
4675 goto cleanup;
4676 }
4677 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004678
Gilles Peskineedf9a652018-08-17 18:11:56 +02004679 return( PSA_SUCCESS );
4680
4681cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004682 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004683 return( status );
4684}
4685
Ronald Croncf56a0a2020-08-04 09:51:30 +02004686psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004687 psa_algorithm_t alg,
4688 const uint8_t *nonce,
4689 size_t nonce_length,
4690 const uint8_t *additional_data,
4691 size_t additional_data_length,
4692 const uint8_t *plaintext,
4693 size_t plaintext_length,
4694 uint8_t *ciphertext,
4695 size_t ciphertext_size,
4696 size_t *ciphertext_length )
4697{
mohammad16035955c982018-04-26 00:53:03 +03004698 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004699 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004700 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004701
mohammad1603f08a5502018-06-03 15:05:47 +03004702 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004703
Ronald Croncf56a0a2020-08-04 09:51:30 +02004704 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004705 if( status != PSA_SUCCESS )
4706 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004707
Gilles Peskineedf9a652018-08-17 18:11:56 +02004708 /* For all currently supported modes, the tag is at the end of the
4709 * ciphertext. */
4710 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4711 {
4712 status = PSA_ERROR_BUFFER_TOO_SMALL;
4713 goto exit;
4714 }
4715 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004716
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004717#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004718 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004719 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004720 status = mbedtls_to_psa_error(
4721 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4722 MBEDTLS_GCM_ENCRYPT,
4723 plaintext_length,
4724 nonce, nonce_length,
4725 additional_data, additional_data_length,
4726 plaintext, ciphertext,
4727 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004728 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004729 else
4730#endif /* MBEDTLS_GCM_C */
4731#if defined(MBEDTLS_CCM_C)
4732 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004733 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004734 status = mbedtls_to_psa_error(
4735 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4736 plaintext_length,
4737 nonce, nonce_length,
4738 additional_data,
4739 additional_data_length,
4740 plaintext, ciphertext,
4741 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004742 }
mohammad16035c8845f2018-05-09 05:40:09 -07004743 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004744#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004745#if defined(MBEDTLS_CHACHAPOLY_C)
4746 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4747 {
4748 if( nonce_length != 12 || operation.tag_length != 16 )
4749 {
4750 status = PSA_ERROR_NOT_SUPPORTED;
4751 goto exit;
4752 }
4753 status = mbedtls_to_psa_error(
4754 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4755 plaintext_length,
4756 nonce,
4757 additional_data,
4758 additional_data_length,
4759 plaintext,
4760 ciphertext,
4761 tag ) );
4762 }
4763 else
4764#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004765 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004766 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004767 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004768 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004769
Gilles Peskineedf9a652018-08-17 18:11:56 +02004770 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4771 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004772
Gilles Peskineedf9a652018-08-17 18:11:56 +02004773exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004774 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004775 if( status == PSA_SUCCESS )
4776 *ciphertext_length = plaintext_length + operation.tag_length;
4777 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004778}
4779
Gilles Peskineee652a32018-06-01 19:23:52 +02004780/* Locate the tag in a ciphertext buffer containing the encrypted data
4781 * followed by the tag. Return the length of the part preceding the tag in
4782 * *plaintext_length. This is the size of the plaintext in modes where
4783 * the encrypted data has the same size as the plaintext, such as
4784 * CCM and GCM. */
4785static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4786 const uint8_t *ciphertext,
4787 size_t ciphertext_length,
4788 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004789 const uint8_t **p_tag )
4790{
4791 size_t payload_length;
4792 if( tag_length > ciphertext_length )
4793 return( PSA_ERROR_INVALID_ARGUMENT );
4794 payload_length = ciphertext_length - tag_length;
4795 if( payload_length > plaintext_size )
4796 return( PSA_ERROR_BUFFER_TOO_SMALL );
4797 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004798 return( PSA_SUCCESS );
4799}
4800
Ronald Croncf56a0a2020-08-04 09:51:30 +02004801psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004802 psa_algorithm_t alg,
4803 const uint8_t *nonce,
4804 size_t nonce_length,
4805 const uint8_t *additional_data,
4806 size_t additional_data_length,
4807 const uint8_t *ciphertext,
4808 size_t ciphertext_length,
4809 uint8_t *plaintext,
4810 size_t plaintext_size,
4811 size_t *plaintext_length )
4812{
mohammad16035955c982018-04-26 00:53:03 +03004813 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004814 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004815 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004816
Gilles Peskineee652a32018-06-01 19:23:52 +02004817 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004818
Ronald Croncf56a0a2020-08-04 09:51:30 +02004819 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004820 if( status != PSA_SUCCESS )
4821 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004822
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004823 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4824 ciphertext, ciphertext_length,
4825 plaintext_size, &tag );
4826 if( status != PSA_SUCCESS )
4827 goto exit;
4828
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004829#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004830 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004831 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004832 status = mbedtls_to_psa_error(
4833 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4834 ciphertext_length - operation.tag_length,
4835 nonce, nonce_length,
4836 additional_data,
4837 additional_data_length,
4838 tag, operation.tag_length,
4839 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004840 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004841 else
4842#endif /* MBEDTLS_GCM_C */
4843#if defined(MBEDTLS_CCM_C)
4844 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004845 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004846 status = mbedtls_to_psa_error(
4847 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4848 ciphertext_length - operation.tag_length,
4849 nonce, nonce_length,
4850 additional_data,
4851 additional_data_length,
4852 ciphertext, plaintext,
4853 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004854 }
mohammad160339574652018-06-01 04:39:53 -07004855 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004856#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004857#if defined(MBEDTLS_CHACHAPOLY_C)
4858 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4859 {
4860 if( nonce_length != 12 || operation.tag_length != 16 )
4861 {
4862 status = PSA_ERROR_NOT_SUPPORTED;
4863 goto exit;
4864 }
4865 status = mbedtls_to_psa_error(
4866 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4867 ciphertext_length - operation.tag_length,
4868 nonce,
4869 additional_data,
4870 additional_data_length,
4871 tag,
4872 ciphertext,
4873 plaintext ) );
4874 }
4875 else
4876#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004877 {
mohammad1603554faad2018-06-03 15:07:38 +03004878 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004879 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004880
Gilles Peskineedf9a652018-08-17 18:11:56 +02004881 if( status != PSA_SUCCESS && plaintext_size != 0 )
4882 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004883
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004885 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004886 if( status == PSA_SUCCESS )
4887 *plaintext_length = ciphertext_length - operation.tag_length;
4888 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004889}
4890
Gilles Peskinea0655c32018-04-30 17:06:50 +02004891
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004892
Gilles Peskine20035e32018-02-03 22:44:14 +01004893/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004894/* Generators */
4895/****************************************************************/
4896
John Durkop07cc04a2020-11-16 22:08:34 -08004897#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4898 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4899 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4900#define AT_LEAST_ONE_BUILTIN_KDF
4901#endif
4902
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004903#define HKDF_STATE_INIT 0 /* no input yet */
4904#define HKDF_STATE_STARTED 1 /* got salt */
4905#define HKDF_STATE_KEYED 2 /* got key */
4906#define HKDF_STATE_OUTPUT 3 /* output started */
4907
Gilles Peskinecbe66502019-05-16 16:59:18 +02004908static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004910{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4912 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004914 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915}
4916
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004918{
4919 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004922 {
4923 /* The object has (apparently) been initialized but it is not
4924 * in use. It's ok to call abort on such an object, and there's
4925 * nothing to do. */
4926 }
4927 else
John Durkopd0321952020-10-29 21:37:36 -07004928#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004930 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 mbedtls_free( operation->ctx.hkdf.info );
4932 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004933 }
John Durkop07cc04a2020-11-16 22:08:34 -08004934 else
4935#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4936#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4937 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4938 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004939 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004940 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004941 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004942 if( operation->ctx.tls12_prf.seed != NULL )
4943 {
4944 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4945 operation->ctx.tls12_prf.seed_length );
4946 mbedtls_free( operation->ctx.tls12_prf.seed );
4947 }
4948
4949 if( operation->ctx.tls12_prf.label != NULL )
4950 {
4951 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4952 operation->ctx.tls12_prf.label_length );
4953 mbedtls_free( operation->ctx.tls12_prf.label );
4954 }
4955
4956 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4957
4958 /* We leave the fields Ai and output_block to be erased safely by the
4959 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004960 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004961 else
John Durkop07cc04a2020-11-16 22:08:34 -08004962#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4963 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004964 {
4965 status = PSA_ERROR_BAD_STATE;
4966 }
Janos Follath083036a2019-06-11 10:22:26 +01004967 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004968 return( status );
4969}
4970
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004971psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004972 size_t *capacity)
4973{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004975 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004977 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004978 }
4979
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004980 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004981 return( PSA_SUCCESS );
4982}
4983
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004984psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 size_t capacity )
4986{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004987 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004988 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004989 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004990 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004991 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004992 return( PSA_SUCCESS );
4993}
4994
John Durkopd0321952020-10-29 21:37:36 -07004995#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004996/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004997 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004998static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004999 psa_algorithm_t hash_alg,
5000 uint8_t *output,
5001 size_t output_length )
5002{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005003 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005004 psa_status_t status;
5005
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005006 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5007 return( PSA_ERROR_BAD_STATE );
5008 hkdf->state = HKDF_STATE_OUTPUT;
5009
Gilles Peskinebef7f142018-07-12 17:22:21 +02005010 while( output_length != 0 )
5011 {
5012 /* Copy what remains of the current block */
5013 uint8_t n = hash_length - hkdf->offset_in_block;
5014 if( n > output_length )
5015 n = (uint8_t) output_length;
5016 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5017 output += n;
5018 output_length -= n;
5019 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005020 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005021 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005022 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005023 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005024 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005025 * object was corrupted or if this function is called directly
5026 * inside the library. */
5027 if( hkdf->block_number == 0xff )
5028 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005029
5030 /* We need a new block */
5031 ++hkdf->block_number;
5032 hkdf->offset_in_block = 0;
5033 status = psa_hmac_setup_internal( &hkdf->hmac,
5034 hkdf->prk, hash_length,
5035 hash_alg );
5036 if( status != PSA_SUCCESS )
5037 return( status );
5038 if( hkdf->block_number != 1 )
5039 {
5040 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5041 hkdf->output_block,
5042 hash_length );
5043 if( status != PSA_SUCCESS )
5044 return( status );
5045 }
5046 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5047 hkdf->info,
5048 hkdf->info_length );
5049 if( status != PSA_SUCCESS )
5050 return( status );
5051 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5052 &hkdf->block_number, 1 );
5053 if( status != PSA_SUCCESS )
5054 return( status );
5055 status = psa_hmac_finish_internal( &hkdf->hmac,
5056 hkdf->output_block,
5057 sizeof( hkdf->output_block ) );
5058 if( status != PSA_SUCCESS )
5059 return( status );
5060 }
5061
5062 return( PSA_SUCCESS );
5063}
John Durkop07cc04a2020-11-16 22:08:34 -08005064#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005065
John Durkop07cc04a2020-11-16 22:08:34 -08005066#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5067 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005068static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5069 psa_tls12_prf_key_derivation_t *tls12_prf,
5070 psa_algorithm_t alg )
5071{
5072 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005073 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005074 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5075 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005076
Janos Follath7742fee2019-06-17 12:58:10 +01005077 /* We can't be wanting more output after block 0xff, otherwise
5078 * the capacity check in psa_key_derivation_output_bytes() would have
5079 * prevented this call. It could happen only if the operation
5080 * object was corrupted or if this function is called directly
5081 * inside the library. */
5082 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005083 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005084
5085 /* We need a new block */
5086 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005087 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005088
5089 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5090 *
5091 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5092 *
5093 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5094 * HMAC_hash(secret, A(2) + seed) +
5095 * HMAC_hash(secret, A(3) + seed) + ...
5096 *
5097 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005098 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005099 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005100 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005101 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005102 * is currently extracted as `output_block` and where i is
5103 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005104 */
5105
Janos Follathea29bfb2019-06-19 12:21:20 +01005106 /* Save the hash context before using it, to preserve the hash state with
5107 * only the inner padding in it. We need this, because inner padding depends
5108 * on the key (secret in the RFC's terminology). */
5109 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5110 if( status != PSA_SUCCESS )
5111 goto cleanup;
5112
5113 /* Calculate A(i) where i = tls12_prf->block_number. */
5114 if( tls12_prf->block_number == 1 )
5115 {
5116 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5117 * the variable seed and in this instance means it in the context of the
5118 * P_hash function, where seed = label + seed.) */
5119 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5120 tls12_prf->label, tls12_prf->label_length );
5121 if( status != PSA_SUCCESS )
5122 goto cleanup;
5123 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5124 tls12_prf->seed, tls12_prf->seed_length );
5125 if( status != PSA_SUCCESS )
5126 goto cleanup;
5127 }
5128 else
5129 {
5130 /* A(i) = HMAC_hash(secret, A(i-1)) */
5131 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5132 tls12_prf->Ai, hash_length );
5133 if( status != PSA_SUCCESS )
5134 goto cleanup;
5135 }
5136
5137 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5138 tls12_prf->Ai, hash_length );
5139 if( status != PSA_SUCCESS )
5140 goto cleanup;
5141 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5142 if( status != PSA_SUCCESS )
5143 goto cleanup;
5144
5145 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5146 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5147 tls12_prf->Ai, hash_length );
5148 if( status != PSA_SUCCESS )
5149 goto cleanup;
5150 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5151 tls12_prf->label, tls12_prf->label_length );
5152 if( status != PSA_SUCCESS )
5153 goto cleanup;
5154 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5155 tls12_prf->seed, tls12_prf->seed_length );
5156 if( status != PSA_SUCCESS )
5157 goto cleanup;
5158 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5159 tls12_prf->output_block, hash_length );
5160 if( status != PSA_SUCCESS )
5161 goto cleanup;
5162 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5163 if( status != PSA_SUCCESS )
5164 goto cleanup;
5165
Janos Follath7742fee2019-06-17 12:58:10 +01005166
5167cleanup:
5168
Janos Follathea29bfb2019-06-19 12:21:20 +01005169 cleanup_status = psa_hash_abort( &backup );
5170 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5171 status = cleanup_status;
5172
5173 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005174}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005175
Janos Follath844eb0e2019-06-19 12:10:49 +01005176static psa_status_t psa_key_derivation_tls12_prf_read(
5177 psa_tls12_prf_key_derivation_t *tls12_prf,
5178 psa_algorithm_t alg,
5179 uint8_t *output,
5180 size_t output_length )
5181{
5182 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005183 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005184 psa_status_t status;
5185 uint8_t offset, length;
5186
5187 while( output_length != 0 )
5188 {
5189 /* Check if we have fully processed the current block. */
5190 if( tls12_prf->left_in_block == 0 )
5191 {
5192 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5193 alg );
5194 if( status != PSA_SUCCESS )
5195 return( status );
5196
5197 continue;
5198 }
5199
5200 if( tls12_prf->left_in_block > output_length )
5201 length = (uint8_t) output_length;
5202 else
5203 length = tls12_prf->left_in_block;
5204
5205 offset = hash_length - tls12_prf->left_in_block;
5206 memcpy( output, tls12_prf->output_block + offset, length );
5207 output += length;
5208 output_length -= length;
5209 tls12_prf->left_in_block -= length;
5210 }
5211
5212 return( PSA_SUCCESS );
5213}
John Durkop07cc04a2020-11-16 22:08:34 -08005214#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5215 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005216
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005217psa_status_t psa_key_derivation_output_bytes(
5218 psa_key_derivation_operation_t *operation,
5219 uint8_t *output,
5220 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005221{
5222 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005226 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005228 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005229 }
5230
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005234 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005236 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005237 goto exit;
5238 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005242 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005243 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5244 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005246 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005247 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005248 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005249 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005250
John Durkopd0321952020-10-29 21:37:36 -07005251#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005253 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005254 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 output, output_length );
5257 }
Janos Follathadbec812019-06-14 11:05:39 +01005258 else
John Durkop07cc04a2020-11-16 22:08:34 -08005259#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5260#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5261 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005262 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005263 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005264 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005266 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005267 output_length );
5268 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005269 else
John Durkop07cc04a2020-11-16 22:08:34 -08005270#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5271 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005273 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005274 return( PSA_ERROR_BAD_STATE );
5275 }
5276
5277exit:
5278 if( status != PSA_SUCCESS )
5279 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005280 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005281 * This allows us to differentiate between exhausted operations and
5282 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5283 * operations. */
5284 psa_algorithm_t alg = operation->alg;
5285 psa_key_derivation_abort( operation );
5286 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287 memset( output, '!', output_length );
5288 }
5289 return( status );
5290}
5291
David Brown7807bf72021-02-09 16:28:23 -07005292#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005293static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5294{
5295 if( data_size >= 8 )
5296 mbedtls_des_key_set_parity( data );
5297 if( data_size >= 16 )
5298 mbedtls_des_key_set_parity( data + 8 );
5299 if( data_size >= 24 )
5300 mbedtls_des_key_set_parity( data + 16 );
5301}
David Brown7807bf72021-02-09 16:28:23 -07005302#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005303
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005304static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005305 psa_key_slot_t *slot,
5306 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308{
5309 uint8_t *data = NULL;
5310 size_t bytes = PSA_BITS_TO_BYTES( bits );
5311 psa_status_t status;
5312
Gilles Peskine8e338702019-07-30 20:06:31 +02005313 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314 return( PSA_ERROR_INVALID_ARGUMENT );
5315 if( bits % 8 != 0 )
5316 return( PSA_ERROR_INVALID_ARGUMENT );
5317 data = mbedtls_calloc( 1, bytes );
5318 if( data == NULL )
5319 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5320
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005321 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005322 if( status != PSA_SUCCESS )
5323 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005324#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005325 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005326 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005327#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005328
5329 status = psa_allocate_buffer_to_slot( slot, bytes );
5330 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005331 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005332
Ronald Cronbf33c932020-11-28 18:06:53 +01005333 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005334 psa_key_attributes_t attributes = {
5335 .core = slot->attr
5336 };
5337
Ronald Cronbf33c932020-11-28 18:06:53 +01005338 status = psa_driver_wrapper_import_key( &attributes,
5339 data, bytes,
5340 slot->key.data,
5341 slot->key.bytes,
5342 &slot->key.bytes, &bits );
5343 if( bits != slot->attr.bits )
5344 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005345
5346exit:
5347 mbedtls_free( data );
5348 return( status );
5349}
5350
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005351psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005353 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005354{
5355 psa_status_t status;
5356 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005357 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005358
Ronald Cron81709fc2020-11-14 12:10:32 +01005359 *key = MBEDTLS_SVC_KEY_ID_INIT;
5360
Gilles Peskine0f84d622019-09-12 19:03:13 +02005361 /* Reject any attempt to create a zero-length key so that we don't
5362 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5363 if( psa_get_key_bits( attributes ) == 0 )
5364 return( PSA_ERROR_INVALID_ARGUMENT );
5365
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005366 if( ! operation->can_output_key )
5367 return( PSA_ERROR_NOT_PERMITTED );
5368
Ronald Cron81709fc2020-11-14 12:10:32 +01005369 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5370 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5372 if( driver != NULL )
5373 {
5374 /* Deriving a key in a secure element is not implemented yet. */
5375 status = PSA_ERROR_NOT_SUPPORTED;
5376 }
5377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005378 if( status == PSA_SUCCESS )
5379 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005380 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005381 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005382 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005383 }
5384 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005385 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005386 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005387 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005388
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005389 return( status );
5390}
5391
Gilles Peskineeab56e42018-07-12 17:12:33 +02005392
5393
5394/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005395/* Key derivation */
5396/****************************************************************/
5397
Steven Cooreman932ffb72021-02-15 12:14:32 +01005398#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005399static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005400 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005401 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005402{
John Durkop07cc04a2020-11-16 22:08:34 -08005403 int is_kdf_alg_supported;
5404
Janos Follath5fe19732019-06-20 15:09:30 +01005405 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5406 * initialisers for this union leave some bytes unspecified.) */
5407 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5408
Gilles Peskine969c5d62019-01-16 15:53:06 +01005409 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005410#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005411 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5412 is_kdf_alg_supported = 1;
5413 else
5414#endif
5415#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5416 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5417 is_kdf_alg_supported = 1;
5418 else
5419#endif
5420#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5421 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5422 is_kdf_alg_supported = 1;
5423 else
5424#endif
5425 is_kdf_alg_supported = 0;
5426
5427 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005429 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005430 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431 if( hash_size == 0 )
5432 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005433 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5434 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005435 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005436 {
5437 return( PSA_ERROR_NOT_SUPPORTED );
5438 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005441 }
John Durkop07cc04a2020-11-16 22:08:34 -08005442
5443 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444}
John Durkop07cc04a2020-11-16 22:08:34 -08005445#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005446
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005448 psa_algorithm_t alg )
5449{
5450 psa_status_t status;
5451
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005452 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005453 return( PSA_ERROR_BAD_STATE );
5454
Gilles Peskine6843c292019-01-18 16:44:49 +01005455 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5456 return( PSA_ERROR_INVALID_ARGUMENT );
5457 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005458 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005459#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005460 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005462#else
5463 return( PSA_ERROR_NOT_SUPPORTED );
5464#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005465 }
5466 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5467 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005468#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005469 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005470#else
5471 return( PSA_ERROR_NOT_SUPPORTED );
5472#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005473 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005474 else
5475 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005476
5477 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005478 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005479 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005480}
5481
John Durkopd0321952020-10-29 21:37:36 -07005482#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005483static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005484 psa_algorithm_t hash_alg,
5485 psa_key_derivation_step_t step,
5486 const uint8_t *data,
5487 size_t data_length )
5488{
5489 psa_status_t status;
5490 switch( step )
5491 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005492 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005493 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005494 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005495 status = psa_hmac_setup_internal( &hkdf->hmac,
5496 data, data_length,
5497 hash_alg );
5498 if( status != PSA_SUCCESS )
5499 return( status );
5500 hkdf->state = HKDF_STATE_STARTED;
5501 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005502 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005503 /* If no salt was provided, use an empty salt. */
5504 if( hkdf->state == HKDF_STATE_INIT )
5505 {
5506 status = psa_hmac_setup_internal( &hkdf->hmac,
5507 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005508 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005509 if( status != PSA_SUCCESS )
5510 return( status );
5511 hkdf->state = HKDF_STATE_STARTED;
5512 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005513 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005514 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005515 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5516 data, data_length );
5517 if( status != PSA_SUCCESS )
5518 return( status );
5519 status = psa_hmac_finish_internal( &hkdf->hmac,
5520 hkdf->prk,
5521 sizeof( hkdf->prk ) );
5522 if( status != PSA_SUCCESS )
5523 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005524 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005525 hkdf->block_number = 0;
5526 hkdf->state = HKDF_STATE_KEYED;
5527 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005528 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005529 if( hkdf->state == HKDF_STATE_OUTPUT )
5530 return( PSA_ERROR_BAD_STATE );
5531 if( hkdf->info_set )
5532 return( PSA_ERROR_BAD_STATE );
5533 hkdf->info_length = data_length;
5534 if( data_length != 0 )
5535 {
5536 hkdf->info = mbedtls_calloc( 1, data_length );
5537 if( hkdf->info == NULL )
5538 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5539 memcpy( hkdf->info, data, data_length );
5540 }
5541 hkdf->info_set = 1;
5542 return( PSA_SUCCESS );
5543 default:
5544 return( PSA_ERROR_INVALID_ARGUMENT );
5545 }
5546}
John Durkop07cc04a2020-11-16 22:08:34 -08005547#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005548
John Durkop07cc04a2020-11-16 22:08:34 -08005549#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5550 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005551static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5552 const uint8_t *data,
5553 size_t data_length )
5554{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005555 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005556 return( PSA_ERROR_BAD_STATE );
5557
Janos Follathd6dce9f2019-07-04 09:11:38 +01005558 if( data_length != 0 )
5559 {
5560 prf->seed = mbedtls_calloc( 1, data_length );
5561 if( prf->seed == NULL )
5562 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005563
Janos Follathd6dce9f2019-07-04 09:11:38 +01005564 memcpy( prf->seed, data, data_length );
5565 prf->seed_length = data_length;
5566 }
Janos Follathf08e2652019-06-13 09:05:41 +01005567
Gilles Peskine43f958b2020-12-13 14:55:14 +01005568 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005569
5570 return( PSA_SUCCESS );
5571}
5572
Janos Follath81550542019-06-13 14:26:34 +01005573static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5574 psa_algorithm_t hash_alg,
5575 const uint8_t *data,
5576 size_t data_length )
5577{
5578 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005579 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005580 return( PSA_ERROR_BAD_STATE );
5581
5582 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5583 if( status != PSA_SUCCESS )
5584 return( status );
5585
Gilles Peskine43f958b2020-12-13 14:55:14 +01005586 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005587
5588 return( PSA_SUCCESS );
5589}
5590
Janos Follath63028dd2019-06-13 09:15:47 +01005591static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5592 const uint8_t *data,
5593 size_t data_length )
5594{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005595 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005596 return( PSA_ERROR_BAD_STATE );
5597
Janos Follathd6dce9f2019-07-04 09:11:38 +01005598 if( data_length != 0 )
5599 {
5600 prf->label = mbedtls_calloc( 1, data_length );
5601 if( prf->label == NULL )
5602 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005603
Janos Follathd6dce9f2019-07-04 09:11:38 +01005604 memcpy( prf->label, data, data_length );
5605 prf->label_length = data_length;
5606 }
Janos Follath63028dd2019-06-13 09:15:47 +01005607
Gilles Peskine43f958b2020-12-13 14:55:14 +01005608 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005609
5610 return( PSA_SUCCESS );
5611}
5612
Janos Follathb03233e2019-06-11 15:30:30 +01005613static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5614 psa_algorithm_t hash_alg,
5615 psa_key_derivation_step_t step,
5616 const uint8_t *data,
5617 size_t data_length )
5618{
Janos Follathb03233e2019-06-11 15:30:30 +01005619 switch( step )
5620 {
Janos Follathf08e2652019-06-13 09:05:41 +01005621 case PSA_KEY_DERIVATION_INPUT_SEED:
5622 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005623 case PSA_KEY_DERIVATION_INPUT_SECRET:
5624 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005625 case PSA_KEY_DERIVATION_INPUT_LABEL:
5626 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005627 default:
5628 return( PSA_ERROR_INVALID_ARGUMENT );
5629 }
5630}
John Durkop07cc04a2020-11-16 22:08:34 -08005631#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5632 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5633
5634#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5635static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5636 psa_tls12_prf_key_derivation_t *prf,
5637 psa_algorithm_t hash_alg,
5638 const uint8_t *data,
5639 size_t data_length )
5640{
5641 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005642 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005643 uint8_t *cur = pms;
5644
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005645 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005646 return( PSA_ERROR_INVALID_ARGUMENT );
5647
5648 /* Quoting RFC 4279, Section 2:
5649 *
5650 * The premaster secret is formed as follows: if the PSK is N octets
5651 * long, concatenate a uint16 with the value N, N zero octets, a second
5652 * uint16 with the value N, and the PSK itself.
5653 */
5654
5655 *cur++ = ( data_length >> 8 ) & 0xff;
5656 *cur++ = ( data_length >> 0 ) & 0xff;
5657 memset( cur, 0, data_length );
5658 cur += data_length;
5659 *cur++ = pms[0];
5660 *cur++ = pms[1];
5661 memcpy( cur, data, data_length );
5662 cur += data_length;
5663
5664 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5665
5666 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5667 return( status );
5668}
Janos Follath6660f0e2019-06-17 08:44:03 +01005669
5670static psa_status_t psa_tls12_prf_psk_to_ms_input(
5671 psa_tls12_prf_key_derivation_t *prf,
5672 psa_algorithm_t hash_alg,
5673 psa_key_derivation_step_t step,
5674 const uint8_t *data,
5675 size_t data_length )
5676{
5677 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005678 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005679 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5680 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005681 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005682
5683 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5684}
John Durkop07cc04a2020-11-16 22:08:34 -08005685#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005686
Gilles Peskineb8965192019-09-24 16:21:10 +02005687/** Check whether the given key type is acceptable for the given
5688 * input step of a key derivation.
5689 *
5690 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5691 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5692 * Both secret and non-secret inputs can alternatively have the type
5693 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5694 * that the input was passed as a buffer rather than via a key object.
5695 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005696static int psa_key_derivation_check_input_type(
5697 psa_key_derivation_step_t step,
5698 psa_key_type_t key_type )
5699{
5700 switch( step )
5701 {
5702 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005703 if( key_type == PSA_KEY_TYPE_DERIVE )
5704 return( PSA_SUCCESS );
5705 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005706 return( PSA_SUCCESS );
5707 break;
5708 case PSA_KEY_DERIVATION_INPUT_LABEL:
5709 case PSA_KEY_DERIVATION_INPUT_SALT:
5710 case PSA_KEY_DERIVATION_INPUT_INFO:
5711 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005712 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5713 return( PSA_SUCCESS );
5714 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005715 return( PSA_SUCCESS );
5716 break;
5717 }
5718 return( PSA_ERROR_INVALID_ARGUMENT );
5719}
5720
Janos Follathb80a94e2019-06-12 15:54:46 +01005721static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005722 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005723 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005724 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005725 const uint8_t *data,
5726 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005727{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005728 psa_status_t status;
5729 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5730
5731 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005732 if( status != PSA_SUCCESS )
5733 goto exit;
5734
John Durkopd0321952020-10-29 21:37:36 -07005735#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005737 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005738 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005739 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005740 step, data, data_length );
5741 }
John Durkop07cc04a2020-11-16 22:08:34 -08005742 else
5743#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5744#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5745 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746 {
Janos Follathb03233e2019-06-11 15:30:30 +01005747 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5748 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5749 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005750 }
John Durkop07cc04a2020-11-16 22:08:34 -08005751 else
5752#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5753#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5754 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005755 {
5756 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5757 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5758 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 }
5760 else
John Durkop07cc04a2020-11-16 22:08:34 -08005761#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005763 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005764 (void) data;
5765 (void) data_length;
5766 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005767 return( PSA_ERROR_BAD_STATE );
5768 }
5769
Gilles Peskine224b0d62019-09-23 18:13:17 +02005770exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005772 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005773 return( status );
5774}
5775
Janos Follath51f4a0f2019-06-14 11:35:55 +01005776psa_status_t psa_key_derivation_input_bytes(
5777 psa_key_derivation_operation_t *operation,
5778 psa_key_derivation_step_t step,
5779 const uint8_t *data,
5780 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005781{
Gilles Peskineb8965192019-09-24 16:21:10 +02005782 return( psa_key_derivation_input_internal( operation, step,
5783 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005784 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005785}
5786
Janos Follath51f4a0f2019-06-14 11:35:55 +01005787psa_status_t psa_key_derivation_input_key(
5788 psa_key_derivation_operation_t *operation,
5789 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005790 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005791{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005792 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005793 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005794 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005795
Ronald Cron5c522922020-11-14 16:35:34 +01005796 status = psa_get_and_lock_transparent_key_slot_with_policy(
5797 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005798 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005799 {
5800 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005802 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005803
5804 /* Passing a key object as a SECRET input unlocks the permission
5805 * to output to a key object. */
5806 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5807 operation->can_output_key = 1;
5808
Ronald Cronf95a2b12020-10-22 15:24:49 +02005809 status = psa_key_derivation_input_internal( operation,
5810 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005811 slot->key.data,
5812 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005813
Ronald Cron5c522922020-11-14 16:35:34 +01005814 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005815
Ronald Cron5c522922020-11-14 16:35:34 +01005816 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005817}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005818
5819
5820
5821/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005822/* Key agreement */
5823/****************************************************************/
5824
John Durkop6ba40d12020-11-10 08:50:04 -08005825#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005826static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5827 size_t peer_key_length,
5828 const mbedtls_ecp_keypair *our_key,
5829 uint8_t *shared_secret,
5830 size_t shared_secret_size,
5831 size_t *shared_secret_length )
5832{
Steven Cooremand4867872020-08-05 16:31:39 +02005833 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005834 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005835 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005836 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005837 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005838 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005839
Ronald Cron90857082020-11-25 14:58:33 +01005840 status = mbedtls_psa_ecp_load_representation(
5841 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005842 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005843 peer_key,
5844 peer_key_length,
5845 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005846 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005847 goto exit;
5848
Jaeden Amero97271b32019-01-10 19:38:51 +00005849 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005850 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005851 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005852 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005853 status = mbedtls_to_psa_error(
5854 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5855 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005856 goto exit;
5857
Jaeden Amero97271b32019-01-10 19:38:51 +00005858 status = mbedtls_to_psa_error(
5859 mbedtls_ecdh_calc_secret( &ecdh,
5860 shared_secret_length,
5861 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005862 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005863 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005864 if( status != PSA_SUCCESS )
5865 goto exit;
5866 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5867 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005868
5869exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005870 if( status != PSA_SUCCESS )
5871 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005872 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005873 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005874 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005875
Jaeden Amero97271b32019-01-10 19:38:51 +00005876 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005877}
John Durkop6ba40d12020-11-10 08:50:04 -08005878#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005879
Gilles Peskine01d718c2018-09-18 12:01:02 +02005880#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5881
Gilles Peskine0216fe12019-04-11 21:23:21 +02005882static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5883 psa_key_slot_t *private_key,
5884 const uint8_t *peer_key,
5885 size_t peer_key_length,
5886 uint8_t *shared_secret,
5887 size_t shared_secret_size,
5888 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005889{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005890 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005891 {
John Durkop6ba40d12020-11-10 08:50:04 -08005892#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005893 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005894 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005895 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005896 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005897 psa_status_t status = mbedtls_psa_ecp_load_representation(
5898 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005899 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005900 private_key->key.data,
5901 private_key->key.bytes,
5902 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005903 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005904 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005905 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005906 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005907 shared_secret, shared_secret_size,
5908 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005909 mbedtls_ecp_keypair_free( ecp );
5910 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005911 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005912#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005913 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005914 (void) private_key;
5915 (void) peer_key;
5916 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005917 (void) shared_secret;
5918 (void) shared_secret_size;
5919 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005920 return( PSA_ERROR_NOT_SUPPORTED );
5921 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005922}
5923
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005924/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005925 * to potentially free embedded data structures and wipe confidential data.
5926 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005927static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005928 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005929 psa_key_slot_t *private_key,
5930 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005931 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005932{
5933 psa_status_t status;
5934 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5935 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005936 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005937
5938 /* Step 1: run the secret agreement algorithm to generate the shared
5939 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005940 status = psa_key_agreement_raw_internal( ka_alg,
5941 private_key,
5942 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005943 shared_secret,
5944 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005945 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005946 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005947 goto exit;
5948
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005949 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005950 * the shared secret. A shared secret is permitted wherever a key
5951 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005952 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005953 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005954 shared_secret,
5955 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005957 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005958 return( status );
5959}
5960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005961psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005962 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005963 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005964 const uint8_t *peer_key,
5965 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005966{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005967 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005968 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005969 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005970
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005971 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005972 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005973 status = psa_get_and_lock_transparent_key_slot_with_policy(
5974 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005975 if( status != PSA_SUCCESS )
5976 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005977 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005978 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005979 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005980 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005981 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005982 else
5983 {
5984 /* If a private key has been added as SECRET, we allow the derived
5985 * key material to be used as a key in PSA Crypto. */
5986 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5987 operation->can_output_key = 1;
5988 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005989
Ronald Cron5c522922020-11-14 16:35:34 +01005990 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005991
Ronald Cron5c522922020-11-14 16:35:34 +01005992 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005993}
5994
Gilles Peskinebe697d82019-05-16 18:00:41 +02005995psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005996 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005997 const uint8_t *peer_key,
5998 size_t peer_key_length,
5999 uint8_t *output,
6000 size_t output_size,
6001 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006002{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006004 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006005 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006006
6007 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6008 {
6009 status = PSA_ERROR_INVALID_ARGUMENT;
6010 goto exit;
6011 }
Ronald Cron5c522922020-11-14 16:35:34 +01006012 status = psa_get_and_lock_transparent_key_slot_with_policy(
6013 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006014 if( status != PSA_SUCCESS )
6015 goto exit;
6016
6017 status = psa_key_agreement_raw_internal( alg, slot,
6018 peer_key, peer_key_length,
6019 output, output_size,
6020 output_length );
6021
6022exit:
6023 if( status != PSA_SUCCESS )
6024 {
6025 /* If an error happens and is not handled properly, the output
6026 * may be used as a key to protect sensitive data. Arrange for such
6027 * a key to be random, which is likely to result in decryption or
6028 * verification errors. This is better than filling the buffer with
6029 * some constant data such as zeros, which would result in the data
6030 * being protected with a reproducible, easily knowable key.
6031 */
6032 psa_generate_random( output, output_size );
6033 *output_length = output_size;
6034 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006035
Ronald Cron5c522922020-11-14 16:35:34 +01006036 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006037
Ronald Cron5c522922020-11-14 16:35:34 +01006038 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006039}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006040
6041
Gilles Peskine30524eb2020-11-13 17:02:26 +01006042
Gilles Peskine86a440b2018-11-12 18:39:40 +01006043/****************************************************************/
6044/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006045/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006046
Gilles Peskine30524eb2020-11-13 17:02:26 +01006047/** Initialize the PSA random generator.
6048 */
6049static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6050{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006051#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6052 memset( rng, 0, sizeof( *rng ) );
6053#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6054
Gilles Peskine30524eb2020-11-13 17:02:26 +01006055 /* Set default configuration if
6056 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6057 if( rng->entropy_init == NULL )
6058 rng->entropy_init = mbedtls_entropy_init;
6059 if( rng->entropy_free == NULL )
6060 rng->entropy_free = mbedtls_entropy_free;
6061
6062 rng->entropy_init( &rng->entropy );
6063#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6064 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6065 /* The PSA entropy injection feature depends on using NV seed as an entropy
6066 * source. Add NV seed as an entropy source for PSA entropy injection. */
6067 mbedtls_entropy_add_source( &rng->entropy,
6068 mbedtls_nv_seed_poll, NULL,
6069 MBEDTLS_ENTROPY_BLOCK_SIZE,
6070 MBEDTLS_ENTROPY_SOURCE_STRONG );
6071#endif
6072
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006073 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006074#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006075}
6076
6077/** Deinitialize the PSA random generator.
6078 */
6079static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6080{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006081#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6082 memset( rng, 0, sizeof( *rng ) );
6083#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006084 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006085 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006086#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006087}
6088
6089/** Seed the PSA random generator.
6090 */
6091static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6092{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006093#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6094 /* Do nothing: the external RNG seeds itself. */
6095 (void) rng;
6096 return( PSA_SUCCESS );
6097#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006098 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006099 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6100 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006101 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006102#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006103}
6104
Gilles Peskinee59236f2018-01-27 23:32:46 +01006105psa_status_t psa_generate_random( uint8_t *output,
6106 size_t output_size )
6107{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006108 GUARD_MODULE_INITIALIZED;
6109
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006110#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6111
6112 size_t output_length = 0;
6113 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6114 output, output_size,
6115 &output_length );
6116 if( status != PSA_SUCCESS )
6117 return( status );
6118 /* Breaking up a request into smaller chunks is currently not supported
6119 * for the extrernal RNG interface. */
6120 if( output_length != output_size )
6121 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6122 return( PSA_SUCCESS );
6123
6124#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6125
Gilles Peskine71ddab92021-01-04 21:01:07 +01006126 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006127 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006128 size_t request_size =
6129 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6130 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6131 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006132 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6133 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006134 if( ret != 0 )
6135 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006136 output_size -= request_size;
6137 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006138 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006139 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006140#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006141}
6142
Gilles Peskine8814fc42020-12-14 15:33:44 +01006143/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006144 *
6145 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6146 * `psa_generate_random(...)`. The state parameter is ignored since the
6147 * PSA API doesn't support passing an explicit state.
6148 *
6149 * In the non-external case, psa_generate_random() calls an
6150 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6151 * and semantics as mbedtls_psa_get_random(). As an optimization,
6152 * instead of doing this back-and-forth between the PSA API and the
6153 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6154 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006155 */
6156#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6157int mbedtls_psa_get_random( void *p_rng,
6158 unsigned char *output,
6159 size_t output_size )
6160{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006161 /* This function takes a pointer to the RNG state because that's what
6162 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6163 * its own state internally and doesn't let the caller access that state.
6164 * So we just ignore the state parameter, and in practice we'll pass
6165 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006166 (void) p_rng;
6167 psa_status_t status = psa_generate_random( output, output_size );
6168 if( status == PSA_SUCCESS )
6169 return( 0 );
6170 else
6171 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6172}
6173#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6174
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006175#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6176#include "mbedtls/entropy_poll.h"
6177
Gilles Peskine7228da22019-07-15 11:06:15 +02006178psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006179 size_t seed_size )
6180{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006181 if( global_data.initialized )
6182 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006183
6184 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6185 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6186 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6187 return( PSA_ERROR_INVALID_ARGUMENT );
6188
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006189 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006190}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006191#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006192
Ronald Cron3772afe2021-02-08 16:10:05 +01006193/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006194 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006195 * \param type The key type
6196 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006197 *
6198 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006199 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006200 * \retval #PSA_ERROR_INVALID_ARGUMENT
6201 * The size in bits of the key is not valid.
6202 * \retval #PSA_ERROR_NOT_SUPPORTED
6203 * The type and/or the size in bits of the key or the combination of
6204 * the two is not supported.
6205 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006206static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6207 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006208{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006209 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006210
Gilles Peskinee59236f2018-01-27 23:32:46 +01006211 if( key_type_is_raw_bytes( type ) )
6212 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006213 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006214 if( status != PSA_SUCCESS )
6215 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006216 }
6217 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006218#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006219 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6220 {
6221 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6222 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006223
Ronald Cron01b2aba2020-10-05 09:42:02 +02006224 /* Accept only byte-aligned keys, for the same reasons as
6225 * in psa_import_rsa_key(). */
6226 if( bits % 8 != 0 )
6227 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006228 }
6229 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006230#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006231
Ronald Cron5c4d3862020-12-07 11:07:24 +01006232#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006233 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6234 {
Ronald Crond81ab562021-02-16 09:01:16 +01006235 /* To avoid empty block, return successfully here. */
6236 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006237 }
6238 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006239#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006240 {
6241 return( PSA_ERROR_NOT_SUPPORTED );
6242 }
6243
6244 return( PSA_SUCCESS );
6245}
6246
Ronald Cron55ed0592020-10-05 10:30:40 +02006247psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006248 const psa_key_attributes_t *attributes,
6249 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006250{
6251 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006252 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006253
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006254 if( ( attributes->domain_parameters == NULL ) &&
6255 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006256 return( PSA_ERROR_INVALID_ARGUMENT );
6257
Ronald Cron01b2aba2020-10-05 09:42:02 +02006258 if( key_type_is_raw_bytes( type ) )
6259 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006260 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006261 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006262 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006263
David Brown12ca5032021-02-11 11:02:00 -07006264#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006266 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006267#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268 }
6269 else
6270
John Durkop07cc04a2020-11-16 22:08:34 -08006271#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006272 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006274 return( mbedtls_psa_rsa_generate_key( attributes,
6275 key_buffer,
6276 key_buffer_size,
6277 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006278 }
6279 else
John Durkop07cc04a2020-11-16 22:08:34 -08006280#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006281
John Durkop9814fa22020-11-04 12:28:15 -08006282#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006283 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006284 {
Ronald Cron7023db52020-11-20 18:17:42 +01006285 return( mbedtls_psa_ecp_generate_key( attributes,
6286 key_buffer,
6287 key_buffer_size,
6288 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006289 }
6290 else
John Durkop9814fa22020-11-04 12:28:15 -08006291#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006292 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006293 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006294 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006295 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006296
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006297 return( PSA_SUCCESS );
6298}
Darryl Green0c6575a2018-11-07 16:05:30 +00006299
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006300psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006301 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006302{
6303 psa_status_t status;
6304 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006305 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006306 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006307
Ronald Cron81709fc2020-11-14 12:10:32 +01006308 *key = MBEDTLS_SVC_KEY_ID_INIT;
6309
Gilles Peskine0f84d622019-09-12 19:03:13 +02006310 /* Reject any attempt to create a zero-length key so that we don't
6311 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6312 if( psa_get_key_bits( attributes ) == 0 )
6313 return( PSA_ERROR_INVALID_ARGUMENT );
6314
Ronald Cron81709fc2020-11-14 12:10:32 +01006315 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6316 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006317 if( status != PSA_SUCCESS )
6318 goto exit;
6319
Ronald Cron977c2472020-10-13 08:32:21 +02006320 /* In the case of a transparent key or an opaque key stored in local
6321 * storage (thus not in the case of generating a key in a secure element
6322 * or cryptoprocessor with storage), we have to allocate a buffer to
6323 * hold the generated key material. */
6324 if( slot->key.data == NULL )
6325 {
6326 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6327 PSA_KEY_LOCATION_LOCAL_STORAGE )
6328 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006329 status = psa_validate_key_type_and_size_for_key_generation(
6330 attributes->core.type, attributes->core.bits );
6331 if( status != PSA_SUCCESS )
6332 goto exit;
6333
6334 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6335 attributes->core.type,
6336 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006337 }
6338 else
6339 {
6340 status = psa_driver_wrapper_get_key_buffer_size(
6341 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006342 if( status != PSA_SUCCESS )
6343 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006344 }
Ronald Cron977c2472020-10-13 08:32:21 +02006345
6346 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6347 if( status != PSA_SUCCESS )
6348 goto exit;
6349 }
6350
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006351 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006352 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006353
Ronald Cron2b56bc82020-10-05 10:02:26 +02006354 if( status != PSA_SUCCESS )
6355 psa_remove_key_data_from_memory( slot );
6356
Gilles Peskine11792082019-08-06 18:36:36 +02006357exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006358 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006359 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006360 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006361 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006362
Darryl Green0c6575a2018-11-07 16:05:30 +00006363 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006364}
6365
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366/****************************************************************/
6367/* Module setup */
6368/****************************************************************/
6369
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006370#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006371psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6372 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6373 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6374{
6375 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6376 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006377 global_data.rng.entropy_init = entropy_init;
6378 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006379 return( PSA_SUCCESS );
6380}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006381#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006382
Gilles Peskinee59236f2018-01-27 23:32:46 +01006383void mbedtls_psa_crypto_free( void )
6384{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006385 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006386 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6387 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006388 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006389 }
6390 /* Wipe all remaining data, including configuration.
6391 * In particular, this sets all state indicator to the value
6392 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006393 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006394#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006395 /* Unregister all secure element drivers, so that we restart from
6396 * a pristine state. */
6397 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006398#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006399}
6400
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006401#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6402/** Recover a transaction that was interrupted by a power failure.
6403 *
6404 * This function is called during initialization, before psa_crypto_init()
6405 * returns. If this function returns a failure status, the initialization
6406 * fails.
6407 */
6408static psa_status_t psa_crypto_recover_transaction(
6409 const psa_crypto_transaction_t *transaction )
6410{
6411 switch( transaction->unknown.type )
6412 {
6413 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6414 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006415 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006416 * is implemented.
6417 * https://github.com/ARMmbed/mbed-crypto/issues/218
6418 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006419 default:
6420 /* We found an unsupported transaction in the storage.
6421 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006422 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006423 }
6424}
6425#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6426
Gilles Peskinee59236f2018-01-27 23:32:46 +01006427psa_status_t psa_crypto_init( void )
6428{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006429 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430
Gilles Peskinec6b69072018-11-20 21:42:52 +01006431 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432 if( global_data.initialized != 0 )
6433 return( PSA_SUCCESS );
6434
Gilles Peskine30524eb2020-11-13 17:02:26 +01006435 /* Initialize and seed the random generator. */
6436 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006437 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006438 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006439 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006440 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006441 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006442
Gilles Peskine66fb1262018-12-10 16:29:04 +01006443 status = psa_initialize_key_slots( );
6444 if( status != PSA_SUCCESS )
6445 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006446
Gilles Peskined9348f22019-10-01 15:22:29 +02006447#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6448 status = psa_init_all_se_drivers( );
6449 if( status != PSA_SUCCESS )
6450 goto exit;
6451#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6452
Gilles Peskine4b734222019-07-24 15:56:31 +02006453#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006454 status = psa_crypto_load_transaction( );
6455 if( status == PSA_SUCCESS )
6456 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006457 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6458 if( status != PSA_SUCCESS )
6459 goto exit;
6460 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006461 }
6462 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6463 {
6464 /* There's no transaction to complete. It's all good. */
6465 status = PSA_SUCCESS;
6466 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006467#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006468
Gilles Peskinec6b69072018-11-20 21:42:52 +01006469 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006470 global_data.initialized = 1;
6471
6472exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006473 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006474 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006475 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476}
6477
6478#endif /* MBEDTLS_PSA_CRYPTO_C */