blob: 13a0835ff8bc690d97c47fa6bb3f952b05b05d95 [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 *
Steven Cooreman7d4b0d72021-03-02 21:29:45 +0100550 * \param[in] algorithm The specific (non-wildcard) MAC algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100551 * \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
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman7d4b0d72021-03-02 21:29:45 +0100560 * \p algorithm is not a valid, specific MAC algorithm recognized and
561 * supported by this core, or \p key_type describes a key which is
562 * inconsistent with the specified \p algorithm.
563 * \retval #PSA_ERROR_INVALID_ARGUMENT
564 * \p algorithm tries to truncate the MAC to a size which would be
565 * larger than the underlying algorithm's maximum output length.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100566 */
Steven Cooreman58c94d32021-03-02 21:31:17 +0100567MBEDTLS_STATIC_TESTABLE psa_status_t psa_get_mac_output_length(
568 psa_algorithm_t algorithm,
569 psa_key_type_t key_type,
570 size_t *length )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100571{
Steven Cooreman7d4b0d72021-03-02 21:29:45 +0100572 /* Get the default length for the algorithm and key combination. None of the
573 * currently supported algorithms have a default output length dependent on
574 * key size, so setting it to a bogus value is OK. */
575 size_t default_length = PSA_MAC_LENGTH( key_type, 0,
576 PSA_ALG_FULL_LENGTH_MAC( algorithm ) );
577
578 /* PSA_MAC_LENGTH, when called on a full-length algorithm identifier, can
579 * currently return either 0 (unknown algorithm) or 1 (cipher-MAC with
580 * stream cipher) in cases where the key type / algorithm combination would
581 * be invalid. */
582 if( default_length == 0 || default_length == 1 )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 return( PSA_ERROR_INVALID_ARGUMENT );
584
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100585 /* Output the expected (potentially truncated) length as long as it can
Steven Cooreman4ff9a292021-03-03 12:07:20 +0100586 * actually be output by the algorithm. Truncation length of '0' means
587 * default output length of the keytype-algorithm combination. */
588 if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) == 0 )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100589 {
590 *length = default_length;
591 return( PSA_SUCCESS );
592 }
593 else if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) <= default_length )
594 {
595 *length = PSA_MAC_TRUNCATED_LENGTH( algorithm );
596 return( PSA_SUCCESS );
597 }
598 else
599 return( PSA_ERROR_INVALID_ARGUMENT );
600}
601
Steven Cooreman75b74362020-07-28 14:30:13 +0200602/** Try to allocate a buffer to an empty key slot.
603 *
604 * \param[in,out] slot Key slot to attach buffer to.
605 * \param[in] buffer_length Requested size of the buffer.
606 *
607 * \retval #PSA_SUCCESS
608 * The buffer has been successfully allocated.
609 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
610 * Not enough memory was available for allocation.
611 * \retval #PSA_ERROR_ALREADY_EXISTS
612 * Trying to allocate a buffer to a non-empty key slot.
613 */
614static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
615 size_t buffer_length )
616{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100617 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200618 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200619
Ronald Cronea0f8a62020-11-25 17:52:23 +0100620 slot->key.data = mbedtls_calloc( 1, buffer_length );
621 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200622 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200623
Ronald Cronea0f8a62020-11-25 17:52:23 +0100624 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200625 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200626}
627
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200628psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
629 const uint8_t* data,
630 size_t data_length )
631{
632 psa_status_t status = psa_allocate_buffer_to_slot( slot,
633 data_length );
634 if( status != PSA_SUCCESS )
635 return( status );
636
Ronald Cronea0f8a62020-11-25 17:52:23 +0100637 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200638 return( PSA_SUCCESS );
639}
640
Ronald Crona0fe59f2020-11-29 11:14:53 +0100641psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 const psa_key_attributes_t *attributes,
643 const uint8_t *data, size_t data_length,
644 uint8_t *key_buffer, size_t key_buffer_size,
645 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100646{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100647 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
648 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100649
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200650 /* zero-length keys are never supported. */
651 if( data_length == 0 )
652 return( PSA_ERROR_NOT_SUPPORTED );
653
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100654 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100655 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100656 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200657
Steven Cooreman75b74362020-07-28 14:30:13 +0200658 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
659 if( data_length > SIZE_MAX / 8 )
660 return( PSA_ERROR_NOT_SUPPORTED );
661
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200662 /* Enforce a size limit, and in particular ensure that the bit
663 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200665 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200666
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100667 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200668 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200669 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200670
Ronald Crondd04d422020-11-28 15:14:42 +0100671 /* Copy the key material. */
672 memcpy( key_buffer, data, data_length );
673 *key_buffer_length = data_length;
674 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200675
Steven Cooreman40120f62020-10-29 11:42:22 +0100676 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100677 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100678 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200679 {
John Durkop9814fa22020-11-04 12:28:15 -0800680#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
681 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100682 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200683 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100684 return( mbedtls_psa_ecp_import_key( attributes,
685 data, data_length,
686 key_buffer, key_buffer_size,
687 key_buffer_length,
688 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100689 }
John Durkop9814fa22020-11-04 12:28:15 -0800690#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
691 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700692#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
693 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100694 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200695 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100696 return( mbedtls_psa_rsa_import_key( attributes,
697 data, data_length,
698 key_buffer, key_buffer_size,
699 key_buffer_length,
700 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200701 }
John Durkop9814fa22020-11-04 12:28:15 -0800702#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
703 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100704 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100705
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100706 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100707}
708
Gilles Peskinef603c712019-01-19 13:40:11 +0100709/** Calculate the intersection of two algorithm usage policies.
710 *
711 * Return 0 (which allows no operation) on incompatibility.
712 */
713static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100714 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100715 psa_algorithm_t alg1,
716 psa_algorithm_t alg2 )
717{
Gilles Peskine549ea862019-05-22 11:45:59 +0200718 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100719 if( alg1 == alg2 )
720 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100721 /* If the policies are from the same hash-and-sign family, check
722 * if one is a wildcard. If so the other has the specific algorithm. */
723 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
724 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
725 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100726 {
Steven Cooreman03488022021-02-18 12:07:20 +0100727 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
728 return( alg2 );
729 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
730 return( alg1 );
731 }
732 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100733 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100734 * restrictive tag length. */
735 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
736 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
737 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
738 {
739 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
740 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
741 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100742
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100743 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100744 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
745 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100746 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100747 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100748 }
749 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100750 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100751 ( alg1_len <= alg2_len ) )
752 {
753 return( alg2 );
754 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100755 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100756 ( alg2_len <= alg1_len ) )
757 {
758 return( alg1 );
759 }
760 }
761 /* If the policies are from the same MAC family, check whether one
762 * of them is a minimum-MAC-length policy. Calculate the most
763 * restrictive tag length. */
764 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
765 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
766 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
767 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100768 /* Calculate the actual requested output length for both sides. In case
769 * of at-least-this-length wildcard algorithms, the requested output
770 * length is the shortest allowed length. */
771 size_t alg1_len = 0;
772 size_t alg2_len = 0;
773 if( PSA_SUCCESS != psa_get_mac_output_length(
774 PSA_ALG_TRUNCATED_MAC( alg1,
775 PSA_MAC_TRUNCATED_LENGTH( alg1 ) ),
776 key_type,
777 &alg1_len ) )
778 {
779 return( 0 );
780 }
781 if( PSA_SUCCESS != psa_get_mac_output_length(
782 PSA_ALG_TRUNCATED_MAC( alg2,
783 PSA_MAC_TRUNCATED_LENGTH( alg2 ) ),
784 key_type,
785 &alg2_len ) )
786 {
787 return( 0 );
788 }
789
Steven Cooreman03488022021-02-18 12:07:20 +0100790 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100791
Steven Cooremana1d83222021-02-25 10:20:29 +0100792 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100793 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
794 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100795 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100796 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100797 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100798 /* If only one is a wildcard, return specific algorithm if compatible. */
799 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100800 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100801 if( alg1_len <= alg2_len )
802 return( alg2 );
803 else
804 return( 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100805 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100806 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100807 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100808 if( alg2_len <= alg1_len )
809 return( alg1 );
810 else
811 return( 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100812 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100813 /* If none of them are wildcards, check whether we can match
814 * default-length with exact-length, and return exact-length in that
815 * case. */
816 if( alg1_len == alg2_len )
817 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100818 }
819 /* If the policies are incompatible, allow nothing. */
820 return( 0 );
821}
822
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100823static int psa_key_algorithm_permits( psa_key_type_t key_type,
824 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200825 psa_algorithm_t requested_alg )
826{
Gilles Peskine549ea862019-05-22 11:45:59 +0200827 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200828 if( requested_alg == policy_alg )
829 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100830 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
831 * and requested_alg is the same hash-and-sign family with any hash,
832 * then requested_alg is compliant with policy_alg. */
833 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
834 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200835 {
Steven Cooreman03488022021-02-18 12:07:20 +0100836 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
837 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
838 }
839 /* If policy_alg is a wildcard AEAD algorithm of the same base as
840 * the requested algorithm, check the requested tag length to be
841 * equal-length or longer than the wildcard-specified length. */
842 if( PSA_ALG_IS_AEAD( policy_alg ) &&
843 PSA_ALG_IS_AEAD( requested_alg ) &&
844 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
845 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100846 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100847 {
848 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
849 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
850 }
Steven Cooreman03488022021-02-18 12:07:20 +0100851 if( PSA_ALG_IS_MAC( policy_alg ) &&
852 PSA_ALG_IS_MAC( requested_alg ) &&
853 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100854 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100855 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100856 size_t actual_output_length;
857 size_t default_output_length;
858 if( PSA_SUCCESS != psa_get_mac_output_length(
859 requested_alg,
860 key_type,
861 &actual_output_length ) )
862 {
863 return( 0 );
864 }
865 if( PSA_SUCCESS != psa_get_mac_output_length(
866 PSA_ALG_FULL_LENGTH_MAC( requested_alg ),
867 key_type,
868 &default_output_length ) )
869 {
870 return( 0 );
871 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100872
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100873 /* If the policy is default-length, only allow an algorithm with
874 * a declared exact-length matching the default. */
875 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
876 return( actual_output_length == default_output_length );
877
878 /* If the requested algorithm is default-length, allow it if the policy
879 * is exactly the default length. */
880 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
881 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
882 {
883 return( 1 );
884 }
885
886 /* If policy_alg is a wildcard MAC algorithm of the same base as
887 * the requested algorithm, check the requested tag length to be
888 * equal-length or longer than the wildcard-specified length. */
889 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
890 {
891 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
892 actual_output_length );
893 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200894 }
Steven Cooremance48e852020-10-05 16:02:45 +0200895 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200896 * a key derivation with that key agreement should also be allowed. This
897 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200898 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
899 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
900 {
901 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
902 policy_alg );
903 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100904 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200905 return( 0 );
906}
907
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100908/** Test whether a policy permits an algorithm.
909 *
910 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100911 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100912 * \note This function requires providing the key type for which the policy is
913 * being validated, since some algorithm policy definitions (e.g. MAC)
914 * have different properties depending on what kind of cipher it is
915 * combined with.
916 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100917 * \retval PSA_SUCCESS When \p alg is a specific algorithm
918 * allowed by the \p policy.
919 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
920 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
921 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100922 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100923static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100924 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100925 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100926{
Steven Cooremand788fab2021-02-25 11:29:17 +0100927 /* '0' is not a valid algorithm */
928 if( alg == 0 )
929 return( PSA_ERROR_INVALID_ARGUMENT );
930
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931 /* A requested algorithm cannot be a wildcard. */
932 if( PSA_ALG_IS_WILDCARD( alg ) )
933 return( PSA_ERROR_INVALID_ARGUMENT );
934
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100935 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
936 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100937 return( PSA_SUCCESS );
938 else
939 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100940}
941
Gilles Peskinef603c712019-01-19 13:40:11 +0100942/** Restrict a key policy based on a constraint.
943 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100944 * \note This function requires providing the key type for which the policy is
945 * being restricted, since some algorithm policy definitions (e.g. MAC)
946 * have different properties depending on what kind of cipher it is
947 * combined with.
948 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100949 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100950 * \param[in,out] policy The policy to restrict.
951 * \param[in] constraint The policy constraint to apply.
952 *
953 * \retval #PSA_SUCCESS
954 * \c *policy contains the intersection of the original value of
955 * \c *policy and \c *constraint.
956 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100957 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100958 * \c *policy is unchanged.
959 */
960static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100961 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100962 psa_key_policy_t *policy,
963 const psa_key_policy_t *constraint )
964{
965 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100966 psa_key_policy_algorithm_intersection( key_type, policy->alg,
967 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200968 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100969 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
970 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100971 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
972 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200973 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
974 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100975 policy->usage &= constraint->usage;
976 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200977 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100978 return( PSA_SUCCESS );
979}
980
Ronald Cron5c522922020-11-14 16:35:34 +0100981/** Get the description of a key given its identifier and policy constraints
982 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 *
Ronald Cron5c522922020-11-14 16:35:34 +0100984 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100985 * nonzero, the key must allow operations with this algorithm. If \p alg is
986 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100987 *
Ronald Cron5c522922020-11-14 16:35:34 +0100988 * In case of a persistent key, the function loads the description of the key
989 * into a key slot if not already done.
990 *
991 * On success, the returned key slot is locked. It is the responsibility of
992 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200993 */
Ronald Cron5c522922020-11-14 16:35:34 +0100994static psa_status_t psa_get_and_lock_key_slot_with_policy(
995 mbedtls_svc_key_id_t key,
996 psa_key_slot_t **p_slot,
997 psa_key_usage_t usage,
998 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100999{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001000 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1001 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001002
Ronald Cron5c522922020-11-14 16:35:34 +01001003 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001004 if( status != PSA_SUCCESS )
1005 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001006 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001007
1008 /* Enforce that usage policy for the key slot contains all the flags
1009 * required by the usage parameter. There is one exception: public
1010 * keys can always be exported, so we treat public key objects as
1011 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001012 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001013 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014
Gilles Peskine8e338702019-07-30 20:06:31 +02001015 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +01001016 {
1017 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001018 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001019 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001020
1021 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001022 if( alg != 0 )
1023 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001024 status = psa_key_policy_permits( &slot->attr.policy,
1025 slot->attr.type,
1026 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001027 if( status != PSA_SUCCESS )
1028 goto error;
1029 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001030
Darryl Green06fd18d2018-07-16 11:21:11 +01001031 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001032
1033error:
1034 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001035 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001036
1037 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001038}
Darryl Green940d72c2018-07-13 13:18:51 +01001039
Ronald Cron5c522922020-11-14 16:35:34 +01001040/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041 *
1042 * A transparent key is a key for which the key material is directly
1043 * available, as opposed to a key in a secure element.
1044 *
Ronald Cron5c522922020-11-14 16:35:34 +01001045 * This is a temporary function to use instead of
1046 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1047 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001048 *
Ronald Cron5c522922020-11-14 16:35:34 +01001049 * On success, the returned key slot is locked. It is the responsibility of the
1050 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001051 */
1052#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001053static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1054 mbedtls_svc_key_id_t key,
1055 psa_key_slot_t **p_slot,
1056 psa_key_usage_t usage,
1057 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001058{
Ronald Cron5c522922020-11-14 16:35:34 +01001059 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1060 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001061 if( status != PSA_SUCCESS )
1062 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001063
Gilles Peskineadad8132019-07-25 11:31:23 +02001064 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001065 {
Ronald Cron5c522922020-11-14 16:35:34 +01001066 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001067 *p_slot = NULL;
1068 return( PSA_ERROR_NOT_SUPPORTED );
1069 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001070
Gilles Peskine28f8f302019-07-24 13:30:31 +02001071 return( PSA_SUCCESS );
1072}
1073#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1074/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001075#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1076 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001077#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1078
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001079/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001080static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001081{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001082 /* Data pointer will always be either a valid pointer or NULL in an
1083 * initialized slot, so we can just free it. */
1084 if( slot->key.data != NULL )
1085 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1086
1087 mbedtls_free( slot->key.data );
1088 slot->key.data = NULL;
1089 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001090
1091 return( PSA_SUCCESS );
1092}
1093
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001094/** Completely wipe a slot in memory, including its policy.
1095 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001096psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001097{
1098 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001099
1100 /*
1101 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001102 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001103 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1104 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001105 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001106 */
Ronald Cron5c522922020-11-14 16:35:34 +01001107 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001108 {
1109#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001110 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001111#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001112 status = PSA_ERROR_CORRUPTION_DETECTED;
1113 }
1114
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001115 /* Multipart operations may still be using the key. This is safe
1116 * because all multipart operation objects are independent from
1117 * the key slot: if they need to access the key after the setup
1118 * phase, they have a copy of the key. Note that this means that
1119 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001120 /* At this point, key material and other type-specific content has
1121 * been wiped. Clear remaining metadata. We can call memset and not
1122 * zeroize because the metadata is not particularly sensitive. */
1123 memset( slot, 0, sizeof( *slot ) );
1124 return( status );
1125}
1126
Ronald Croncf56a0a2020-08-04 09:51:30 +02001127psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001128{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001129 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001130 psa_status_t status; /* status of the last operation */
1131 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1133 psa_se_drv_table_entry_t *driver;
1134#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001135
Ronald Croncf56a0a2020-08-04 09:51:30 +02001136 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001137 return( PSA_SUCCESS );
1138
Ronald Cronf2911112020-10-29 17:51:10 +01001139 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001140 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001141 * key, this will load the key description from persistent memory if not
1142 * done yet. We cannot avoid this loading as without it we don't know if
1143 * the key is operated by an SE or not and this information is needed by
1144 * the current implementation.
1145 */
Ronald Cron5c522922020-11-14 16:35:34 +01001146 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001147 if( status != PSA_SUCCESS )
1148 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001149
Ronald Cronf2911112020-10-29 17:51:10 +01001150 /*
1151 * If the key slot containing the key description is under access by the
1152 * library (apart from the present access), the key cannot be destroyed
1153 * yet. For the time being, just return in error. Eventually (to be
1154 * implemented), the key should be destroyed when all accesses have
1155 * stopped.
1156 */
Ronald Cron5c522922020-11-14 16:35:34 +01001157 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001158 {
Ronald Cron5c522922020-11-14 16:35:34 +01001159 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001160 return( PSA_ERROR_GENERIC_ERROR );
1161 }
1162
Gilles Peskine354f7672019-07-12 23:46:38 +02001163#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001164 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001165 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001166 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001167 /* For a key in a secure element, we need to do three things:
1168 * remove the key file in internal storage, destroy the
1169 * key inside the secure element, and update the driver's
1170 * persistent data. Start a transaction that will encompass these
1171 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001172 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001173 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001174 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001175 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001176 status = psa_crypto_save_transaction( );
1177 if( status != PSA_SUCCESS )
1178 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001179 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001180 /* We should still try to destroy the key in the secure
1181 * element and the key metadata in storage. This is especially
1182 * important if the error is that the storage is full.
1183 * But how to do it exactly without risking an inconsistent
1184 * state after a reset?
1185 * https://github.com/ARMmbed/mbed-crypto/issues/215
1186 */
1187 overall_status = status;
1188 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001189 }
1190
Ronald Cronea0f8a62020-11-25 17:52:23 +01001191 status = psa_destroy_se_key( driver,
1192 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001193 if( overall_status == PSA_SUCCESS )
1194 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001195 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001196#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1197
Darryl Greend49a4992018-06-18 17:27:26 +01001198#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001199 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001200 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 status = psa_destroy_persistent_key( slot->attr.id );
1202 if( overall_status == PSA_SUCCESS )
1203 overall_status = status;
1204
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001205 /* TODO: other slots may have a copy of the same key. We should
1206 * invalidate them.
1207 * https://github.com/ARMmbed/mbed-crypto/issues/214
1208 */
Darryl Greend49a4992018-06-18 17:27:26 +01001209 }
1210#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001211
Gilles Peskinefc762652019-07-22 19:30:34 +02001212#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1213 if( driver != NULL )
1214 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001215 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001216 if( overall_status == PSA_SUCCESS )
1217 overall_status = status;
1218 status = psa_crypto_stop_transaction( );
1219 if( overall_status == PSA_SUCCESS )
1220 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001221 }
1222#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1223
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1225exit:
1226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001227 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001228 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1229 if( overall_status == PSA_SUCCESS )
1230 overall_status = status;
1231 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001232}
1233
John Durkop07cc04a2020-11-16 22:08:34 -08001234#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001235 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001236static psa_status_t psa_get_rsa_public_exponent(
1237 const mbedtls_rsa_context *rsa,
1238 psa_key_attributes_t *attributes )
1239{
1240 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001241 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001242 uint8_t *buffer = NULL;
1243 size_t buflen;
1244 mbedtls_mpi_init( &mpi );
1245
1246 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1247 if( ret != 0 )
1248 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001249 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1250 {
1251 /* It's the default value, which is reported as an empty string,
1252 * so there's nothing to do. */
1253 goto exit;
1254 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001255
1256 buflen = mbedtls_mpi_size( &mpi );
1257 buffer = mbedtls_calloc( 1, buflen );
1258 if( buffer == NULL )
1259 {
1260 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1261 goto exit;
1262 }
1263 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1264 if( ret != 0 )
1265 goto exit;
1266 attributes->domain_parameters = buffer;
1267 attributes->domain_parameters_size = buflen;
1268
1269exit:
1270 mbedtls_mpi_free( &mpi );
1271 if( ret != 0 )
1272 mbedtls_free( buffer );
1273 return( mbedtls_to_psa_error( ret ) );
1274}
John Durkop07cc04a2020-11-16 22:08:34 -08001275#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001276 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001277
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001278/** Retrieve all the publicly-accessible attributes of a key.
1279 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001280psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001281 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001282{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001283 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001284 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001285 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001286
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001287 psa_reset_key_attributes( attributes );
1288
Ronald Cron5c522922020-11-14 16:35:34 +01001289 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001290 if( status != PSA_SUCCESS )
1291 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001292
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001293 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001294 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1295 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1296
1297#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1298 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001299 psa_set_key_slot_number( attributes,
1300 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001301#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001302
Gilles Peskine8e338702019-07-30 20:06:31 +02001303 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001304 {
John Durkop07cc04a2020-11-16 22:08:34 -08001305#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001306 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001307 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001308 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001309#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001310 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001311 * is not yet implemented.
1312 * https://github.com/ARMmbed/mbed-crypto/issues/216
1313 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001314 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001315 break;
1316#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001317 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001318 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001319
Ronald Cron90857082020-11-25 14:58:33 +01001320 status = mbedtls_psa_rsa_load_representation(
1321 slot->attr.type,
1322 slot->key.data,
1323 slot->key.bytes,
1324 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001325 if( status != PSA_SUCCESS )
1326 break;
1327
Steven Cooremana2371e52020-07-28 14:30:39 +02001328 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001329 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001330 mbedtls_rsa_free( rsa );
1331 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001332 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001333 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001334#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001335 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001336 default:
1337 /* Nothing else to do. */
1338 break;
1339 }
1340
1341 if( status != PSA_SUCCESS )
1342 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001343
Ronald Cron5c522922020-11-14 16:35:34 +01001344 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001345
Ronald Cron5c522922020-11-14 16:35:34 +01001346 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001347}
1348
Gilles Peskinec8000c02019-08-02 20:15:51 +02001349#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1350psa_status_t psa_get_key_slot_number(
1351 const psa_key_attributes_t *attributes,
1352 psa_key_slot_number_t *slot_number )
1353{
1354 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1355 {
1356 *slot_number = attributes->slot_number;
1357 return( PSA_SUCCESS );
1358 }
1359 else
1360 return( PSA_ERROR_INVALID_ARGUMENT );
1361}
1362#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1363
Ronald Crond18b5f82020-11-25 16:46:05 +01001364static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1365 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001366 uint8_t *data,
1367 size_t data_size,
1368 size_t *data_length )
1369{
Ronald Crond18b5f82020-11-25 16:46:05 +01001370 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001371 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001372 memcpy( data, key_buffer, key_buffer_size );
1373 memset( data + key_buffer_size, 0,
1374 data_size - key_buffer_size );
1375 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001376 return( PSA_SUCCESS );
1377}
1378
Ronald Cron7285cda2020-11-26 14:46:37 +01001379psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 const psa_key_attributes_t *attributes,
1381 const uint8_t *key_buffer, size_t key_buffer_size,
1382 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001383{
Ronald Crond18b5f82020-11-25 16:46:05 +01001384 psa_key_type_t type = attributes->core.type;
1385
Ronald Crond18b5f82020-11-25 16:46:05 +01001386 if( key_type_is_raw_bytes( type ) ||
1387 PSA_KEY_TYPE_IS_RSA( type ) ||
1388 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001389 {
1390 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001391 key_buffer, key_buffer_size,
1392 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001393 }
1394 else
1395 {
1396 /* This shouldn't happen in the reference implementation, but
1397 it is valid for a special-purpose implementation to omit
1398 support for exporting certain key types. */
1399 return( PSA_ERROR_NOT_SUPPORTED );
1400 }
1401}
1402
1403psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1404 uint8_t *data,
1405 size_t data_size,
1406 size_t *data_length )
1407{
1408 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1409 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1410 psa_key_slot_t *slot;
1411
Ronald Crone907e552021-01-18 13:22:38 +01001412 /* Reject a zero-length output buffer now, since this can never be a
1413 * valid key representation. This way we know that data must be a valid
1414 * pointer and we can do things like memset(data, ..., data_size). */
1415 if( data_size == 0 )
1416 return( PSA_ERROR_BUFFER_TOO_SMALL );
1417
Ronald Cron9486f9d2020-11-26 13:36:23 +01001418 /* Set the key to empty now, so that even when there are errors, we always
1419 * set data_length to a value between 0 and data_size. On error, setting
1420 * the key to empty is a good choice because an empty key representation is
1421 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001422 *data_length = 0;
1423
Ronald Cron9486f9d2020-11-26 13:36:23 +01001424 /* Export requires the EXPORT flag. There is an exception for public keys,
1425 * which don't require any flag, but
1426 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1427 */
1428 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1429 PSA_KEY_USAGE_EXPORT, 0 );
1430 if( status != PSA_SUCCESS )
1431 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001432
Ronald Crond18b5f82020-11-25 16:46:05 +01001433 psa_key_attributes_t attributes = {
1434 .core = slot->attr
1435 };
Ronald Cron67227982020-11-26 15:16:05 +01001436 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001437 slot->key.data, slot->key.bytes,
1438 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001439
Ronald Cron9486f9d2020-11-26 13:36:23 +01001440 unlock_status = psa_unlock_key_slot( slot );
1441
1442 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1443}
1444
Ronald Cron7285cda2020-11-26 14:46:37 +01001445psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001446 const psa_key_attributes_t *attributes,
1447 const uint8_t *key_buffer,
1448 size_t key_buffer_size,
1449 uint8_t *data,
1450 size_t data_size,
1451 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001452{
Ronald Crond18b5f82020-11-25 16:46:05 +01001453 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001454
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001456 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001457 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001458 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001459 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001460 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 key_buffer, key_buffer_size,
1462 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001463 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001464
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001466 {
John Durkop0e005192020-10-31 22:06:54 -07001467#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1468 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001469 return( mbedtls_psa_rsa_export_public_key( attributes,
1470 key_buffer,
1471 key_buffer_size,
1472 data,
1473 data_size,
1474 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001475#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001476 /* We don't know how to convert a private RSA key to public. */
1477 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001478#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1479 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001480 }
1481 else
Moran Pekera998bc62018-04-16 18:16:20 +03001482 {
John Durkop6ba40d12020-11-10 08:50:04 -08001483#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1484 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001485 return( mbedtls_psa_ecp_export_public_key( attributes,
1486 key_buffer,
1487 key_buffer_size,
1488 data,
1489 data_size,
1490 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001491#else
1492 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001493 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001494#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1495 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001496 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001497 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001498 else
1499 {
1500 /* This shouldn't happen in the reference implementation, but
1501 it is valid for a special-purpose implementation to omit
1502 support for exporting certain key types. */
1503 return( PSA_ERROR_NOT_SUPPORTED );
1504 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505}
Ronald Crond18b5f82020-11-25 16:46:05 +01001506
Ronald Croncf56a0a2020-08-04 09:51:30 +02001507psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001508 uint8_t *data,
1509 size_t data_size,
1510 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001511{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001512 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001513 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001514 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001515
Ronald Crone907e552021-01-18 13:22:38 +01001516 /* Reject a zero-length output buffer now, since this can never be a
1517 * valid key representation. This way we know that data must be a valid
1518 * pointer and we can do things like memset(data, ..., data_size). */
1519 if( data_size == 0 )
1520 return( PSA_ERROR_BUFFER_TOO_SMALL );
1521
Darryl Greendd8fb772018-11-07 16:00:44 +00001522 /* Set the key to empty now, so that even when there are errors, we always
1523 * set data_length to a value between 0 and data_size. On error, setting
1524 * the key to empty is a good choice because an empty key representation is
1525 * unlikely to be accepted anywhere. */
1526 *data_length = 0;
1527
1528 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001529 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001530 if( status != PSA_SUCCESS )
1531 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001532
Ronald Cron9486f9d2020-11-26 13:36:23 +01001533 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1534 {
1535 status = PSA_ERROR_INVALID_ARGUMENT;
1536 goto exit;
1537 }
1538
Ronald Crond18b5f82020-11-25 16:46:05 +01001539 psa_key_attributes_t attributes = {
1540 .core = slot->attr
1541 };
Ronald Cron67227982020-11-26 15:16:05 +01001542 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001543 &attributes, slot->key.data, slot->key.bytes,
1544 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001545
1546exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001547 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001548
Ronald Cron5c522922020-11-14 16:35:34 +01001549 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001550}
1551
Gilles Peskine91e8c332019-08-02 19:19:39 +02001552#if defined(static_assert)
1553static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1554 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001555static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001556 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001557static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001558 "One or more key attribute flag is listed as both internal-only and external-only" );
1559#endif
1560
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001561/** Validate that a key policy is internally well-formed.
1562 *
1563 * This function only rejects invalid policies. It does not validate the
1564 * consistency of the policy with respect to other attributes of the key
1565 * such as the key type.
1566 */
1567static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001568{
1569 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001570 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001571 PSA_KEY_USAGE_ENCRYPT |
1572 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001573 PSA_KEY_USAGE_SIGN_HASH |
1574 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001575 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1576 return( PSA_ERROR_INVALID_ARGUMENT );
1577
Gilles Peskine4747d192019-04-17 15:05:45 +02001578 return( PSA_SUCCESS );
1579}
1580
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001581/** Validate the internal consistency of key attributes.
1582 *
1583 * This function only rejects invalid attribute values. If does not
1584 * validate the consistency of the attributes with any key data that may
1585 * be involved in the creation of the key.
1586 *
1587 * Call this function early in the key creation process.
1588 *
1589 * \param[in] attributes Key attributes for the new key.
1590 * \param[out] p_drv On any return, the driver for the key, if any.
1591 * NULL for a transparent key.
1592 *
1593 */
1594static psa_status_t psa_validate_key_attributes(
1595 const psa_key_attributes_t *attributes,
1596 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001597{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001598 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001599 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001600 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001601
Ronald Cron54b90082020-10-29 15:26:43 +01001602 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001603 if( status != PSA_SUCCESS )
1604 return( status );
1605
Ronald Crond2ed4812020-07-17 16:11:30 +02001606 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001607 if( status != PSA_SUCCESS )
1608 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001609
Ronald Cron65f38a32020-10-23 17:11:13 +02001610 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1611 {
1612 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1613 return( PSA_ERROR_INVALID_ARGUMENT );
1614 }
1615 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001616 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001617 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001618 if( status != PSA_SUCCESS )
1619 return( status );
1620 }
1621
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001622 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001623 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001624 return( status );
1625
1626 /* Refuse to create overly large keys.
1627 * Note that this doesn't trigger on import if the attributes don't
1628 * explicitly specify a size (so psa_get_key_bits returns 0), so
1629 * psa_import_key() needs its own checks. */
1630 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1631 return( PSA_ERROR_NOT_SUPPORTED );
1632
Gilles Peskine91e8c332019-08-02 19:19:39 +02001633 /* Reject invalid flags. These should not be reachable through the API. */
1634 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1635 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1636 return( PSA_ERROR_INVALID_ARGUMENT );
1637
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001638 return( PSA_SUCCESS );
1639}
1640
Gilles Peskine4747d192019-04-17 15:05:45 +02001641/** Prepare a key slot to receive key material.
1642 *
1643 * This function allocates a key slot and sets its metadata.
1644 *
1645 * If this function fails, call psa_fail_key_creation().
1646 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 * This function is intended to be used as follows:
1648 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001649 * it with the specified attributes, and in case of a volatile key assign it
1650 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001651 * -# Populate the slot with the key material.
1652 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1653 * In case of failure at any step, stop the sequence and call
1654 * psa_fail_key_creation().
1655 *
Ronald Cron5c522922020-11-14 16:35:34 +01001656 * On success, the key slot is locked. It is the responsibility of the caller
1657 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001658 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001659 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001660 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 * \param[out] p_slot On success, a pointer to the prepared slot.
1662 * \param[out] p_drv On any return, the driver for the key, if any.
1663 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001664 *
1665 * \retval #PSA_SUCCESS
1666 * The key slot is ready to receive key material.
1667 * \return If this function fails, the key slot is an invalid state.
1668 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001669 */
1670static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001671 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001672 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001673 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001674 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001675{
1676 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001677 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001678 psa_key_slot_t *slot;
1679
Gilles Peskinedf179142019-07-15 22:02:14 +02001680 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 *p_drv = NULL;
1682
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001683 status = psa_validate_key_attributes( attributes, p_drv );
1684 if( status != PSA_SUCCESS )
1685 return( status );
1686
Ronald Cronc4d1b512020-07-31 11:26:37 +02001687 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001688 if( status != PSA_SUCCESS )
1689 return( status );
1690 slot = *p_slot;
1691
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001692 /* We're storing the declared bit-size of the key. It's up to each
1693 * creation mechanism to verify that this information is correct.
1694 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001695 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001696 * is optional (import, copy). In case of a volatile key, assign it the
1697 * volatile key identifier associated to the slot returned to contain its
1698 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001699
1700 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001701 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1702 {
1703#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1704 slot->attr.id = volatile_key_id;
1705#else
1706 slot->attr.id.key_id = volatile_key_id;
1707#endif
1708 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001709
Gilles Peskine91e8c332019-08-02 19:19:39 +02001710 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001711 * external-only flags, query `attributes`. Thanks to the check
1712 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001713 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001714 * may have set. */
1715 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001716
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001718 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001719 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001720 * create the key file in internal storage, create the
1721 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001722 * persistent data. This is done by starting a transaction that will
1723 * encompass these three actions.
1724 * For registering a volatile key, we just need to find an appropriate
1725 * slot number inside the SE. Since the key is designated volatile, creating
1726 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001727 /* The first thing to do is to find a slot number for the new key.
1728 * We save the slot number in persistent storage as part of the
1729 * transaction data. It will be needed to recover if the power
1730 * fails during the key creation process, to clean up on the secure
1731 * element side after restarting. Obtaining a slot number from the
1732 * secure element driver updates its persistent state, but we do not yet
1733 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001734 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001735 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001736 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001737 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001738 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001739 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001740 if( status != PSA_SUCCESS )
1741 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001742
1743 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001744 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001745 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1746 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001747 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001748 psa_crypto_transaction.key.id = slot->attr.id;
1749 status = psa_crypto_save_transaction( );
1750 if( status != PSA_SUCCESS )
1751 {
1752 (void) psa_crypto_stop_transaction( );
1753 return( status );
1754 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001755 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001756
1757 status = psa_copy_key_material_into_slot(
1758 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001759 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001760
1761 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1762 {
1763 /* Key registration only makes sense with a secure element. */
1764 return( PSA_ERROR_INVALID_ARGUMENT );
1765 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001766#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1767
Ronald Cronc4d1b512020-07-31 11:26:37 +02001768 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001769}
Gilles Peskine4747d192019-04-17 15:05:45 +02001770
1771/** Finalize the creation of a key once its key material has been set.
1772 *
1773 * This entails writing the key to persistent storage.
1774 *
1775 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001776 * See the documentation of psa_start_key_creation() for the intended use
1777 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001778 *
Ronald Cron5c522922020-11-14 16:35:34 +01001779 * If the finalization succeeds, the function unlocks the key slot (it was
1780 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1781 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001782 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001783 * \param[in,out] slot Pointer to the slot with key material.
1784 * \param[in] driver The secure element driver for the key,
1785 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 * \param[out] key On success, identifier of the key. Note that the
1787 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001788 *
1789 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001790 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001791 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1792 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1793 * \retval #PSA_ERROR_ALREADY_EXISTS
1794 * \retval #PSA_ERROR_DATA_INVALID
1795 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001796 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001797 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001798 * \return If this function fails, the key slot is an invalid state.
1799 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001800 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001801static psa_status_t psa_finish_key_creation(
1802 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001803 psa_se_drv_table_entry_t *driver,
1804 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001805{
1806 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001807 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001808 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001809
1810#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001811 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001812 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001813#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1814 if( driver != NULL )
1815 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001816 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001817 psa_key_slot_number_t slot_number =
1818 psa_key_slot_get_slot_number( slot ) ;
1819
Gilles Peskineb46bef22019-07-30 21:32:04 +02001820#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001821 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822 sizeof( data.slot_number ),
1823 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001824#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001825 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001826 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001827 (uint8_t*) &data,
1828 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001829 }
1830 else
1831#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1832 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001833 /* Key material is saved in export representation in the slot, so
1834 * just pass the slot buffer for storage. */
1835 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001836 slot->key.data,
1837 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001838 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001839 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001840#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1841
Gilles Peskinecbaff462019-07-12 23:46:04 +02001842#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001843 /* Finish the transaction for a key creation. This does not
1844 * happen when registering an existing key. Detect this case
1845 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001846 * creation of a persistent key in a secure element requires a transaction,
1847 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001848 if( driver != NULL &&
1849 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850 {
1851 status = psa_save_se_persistent_data( driver );
1852 if( status != PSA_SUCCESS )
1853 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001854 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001855 return( status );
1856 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001857 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001858 }
1859#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1860
Ronald Cron50972942020-11-14 11:28:25 +01001861 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001862 {
1863 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001864 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001865 if( status != PSA_SUCCESS )
1866 *key = MBEDTLS_SVC_KEY_ID_INIT;
1867 }
Ronald Cron50972942020-11-14 11:28:25 +01001868
Gilles Peskine4747d192019-04-17 15:05:45 +02001869 return( status );
1870}
1871
1872/** Abort the creation of a key.
1873 *
1874 * You may call this function after calling psa_start_key_creation(),
1875 * or after psa_finish_key_creation() fails. In other circumstances, this
1876 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001877 * See the documentation of psa_start_key_creation() for the intended use
1878 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001880 * \param[in,out] slot Pointer to the slot with key material.
1881 * \param[in] driver The secure element driver for the key,
1882 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001883 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001884static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001885 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001886{
Gilles Peskine011e4282019-06-26 18:34:38 +02001887 (void) driver;
1888
Gilles Peskine4747d192019-04-17 15:05:45 +02001889 if( slot == NULL )
1890 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001891
1892#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001893 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 * element, and the failure happened later (when saving metadata
1895 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001896 * element.
1897 * https://github.com/ARMmbed/mbed-crypto/issues/217
1898 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001899
Gilles Peskined7729582019-08-05 15:55:54 +02001900 /* Abort the ongoing transaction if any (there may not be one if
1901 * the creation process failed before starting one, or if the
1902 * key creation is a registration of a key in a secure element).
1903 * Earlier functions must already have done what it takes to undo any
1904 * partial creation. All that's left is to update the transaction data
1905 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001906 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001907#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1908
Gilles Peskine4747d192019-04-17 15:05:45 +02001909 psa_wipe_key_slot( slot );
1910}
1911
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001912/** Validate optional attributes during key creation.
1913 *
1914 * Some key attributes are optional during key creation. If they are
1915 * specified in the attributes structure, check that they are consistent
1916 * with the data in the slot.
1917 *
1918 * This function should be called near the end of key creation, after
1919 * the slot in memory is fully populated but before saving persistent data.
1920 */
1921static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001922 const psa_key_slot_t *slot,
1923 const psa_key_attributes_t *attributes )
1924{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001925 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001927 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001928 return( PSA_ERROR_INVALID_ARGUMENT );
1929 }
1930
1931 if( attributes->domain_parameters_size != 0 )
1932 {
John Durkop0e005192020-10-31 22:06:54 -07001933#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1934 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001935 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001937 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001938 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001939 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001940
Ronald Cron90857082020-11-25 14:58:33 +01001941 psa_status_t status = mbedtls_psa_rsa_load_representation(
1942 slot->attr.type,
1943 slot->key.data,
1944 slot->key.bytes,
1945 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001946 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001947 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001948
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 mbedtls_mpi_init( &actual );
1950 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001951 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001952 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001953 mbedtls_rsa_free( rsa );
1954 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955 if( ret != 0 )
1956 goto rsa_exit;
1957 ret = mbedtls_mpi_read_binary( &required,
1958 attributes->domain_parameters,
1959 attributes->domain_parameters_size );
1960 if( ret != 0 )
1961 goto rsa_exit;
1962 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1963 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1964 rsa_exit:
1965 mbedtls_mpi_free( &actual );
1966 mbedtls_mpi_free( &required );
1967 if( ret != 0)
1968 return( mbedtls_to_psa_error( ret ) );
1969 }
1970 else
John Durkop9814fa22020-11-04 12:28:15 -08001971#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1972 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001973 {
1974 return( PSA_ERROR_INVALID_ARGUMENT );
1975 }
1976 }
1977
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001978 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001980 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 return( PSA_ERROR_INVALID_ARGUMENT );
1982 }
1983
1984 return( PSA_SUCCESS );
1985}
1986
Gilles Peskine4747d192019-04-17 15:05:45 +02001987psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001988 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001989 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001990 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001991{
1992 psa_status_t status;
1993 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001994 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001995 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001996
Ronald Cron81709fc2020-11-14 12:10:32 +01001997 *key = MBEDTLS_SVC_KEY_ID_INIT;
1998
Gilles Peskine0f84d622019-09-12 19:03:13 +02001999 /* Reject zero-length symmetric keys (including raw data key objects).
2000 * This also rejects any key which might be encoded as an empty string,
2001 * which is never valid. */
2002 if( data_length == 0 )
2003 return( PSA_ERROR_INVALID_ARGUMENT );
2004
Gilles Peskinedf179142019-07-15 22:02:14 +02002005 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002006 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002007 if( status != PSA_SUCCESS )
2008 goto exit;
2009
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002010 /* In the case of a transparent key or an opaque key stored in local
2011 * storage (thus not in the case of generating a key in a secure element
2012 * or cryptoprocessor with storage), we have to allocate a buffer to
2013 * hold the generated key material. */
2014 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02002015 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002016 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002017 if( status != PSA_SUCCESS )
2018 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002019 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002020
2021 bits = slot->attr.bits;
2022 status = psa_driver_wrapper_import_key( attributes,
2023 data, data_length,
2024 slot->key.data,
2025 slot->key.bytes,
2026 &slot->key.bytes, &bits );
2027 if( status != PSA_SUCCESS )
2028 goto exit;
2029
2030 if( slot->attr.bits == 0 )
2031 slot->attr.bits = (psa_key_bits_t) bits;
2032 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002033 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002034 status = PSA_ERROR_INVALID_ARGUMENT;
2035 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002036 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002037
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002038 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002039 if( status != PSA_SUCCESS )
2040 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041
Ronald Cron81709fc2020-11-14 12:10:32 +01002042 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002043exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002044 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002045 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002046
Gilles Peskine4747d192019-04-17 15:05:45 +02002047 return( status );
2048}
2049
Gilles Peskined7729582019-08-05 15:55:54 +02002050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2051psa_status_t mbedtls_psa_register_se_key(
2052 const psa_key_attributes_t *attributes )
2053{
2054 psa_status_t status;
2055 psa_key_slot_t *slot = NULL;
2056 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002057 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002058
2059 /* Leaving attributes unspecified is not currently supported.
2060 * It could make sense to query the key type and size from the
2061 * secure element, but not all secure elements support this
2062 * and the driver HAL doesn't currently support it. */
2063 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2064 return( PSA_ERROR_NOT_SUPPORTED );
2065 if( psa_get_key_bits( attributes ) == 0 )
2066 return( PSA_ERROR_NOT_SUPPORTED );
2067
2068 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002069 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002070 if( status != PSA_SUCCESS )
2071 goto exit;
2072
Ronald Cron81709fc2020-11-14 12:10:32 +01002073 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002074
2075exit:
2076 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002077 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002078
Gilles Peskined7729582019-08-05 15:55:54 +02002079 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002080 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002081 return( status );
2082}
2083#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2084
Gilles Peskinef603c712019-01-19 13:40:11 +01002085static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002086 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002087{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002088 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002089 source->key.data,
2090 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002091 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002092 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002093
Steven Cooreman398aee52020-10-13 14:35:45 +02002094 target->attr.type = source->attr.type;
2095 target->attr.bits = source->attr.bits;
2096
2097 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002098}
2099
Ronald Croncf56a0a2020-08-04 09:51:30 +02002100psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002101 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002102 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002103{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002104 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002105 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002106 psa_key_slot_t *source_slot = NULL;
2107 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002108 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002109 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002110
Ronald Cron81709fc2020-11-14 12:10:32 +01002111 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2112
Ronald Cron5c522922020-11-14 16:35:34 +01002113 status = psa_get_and_lock_transparent_key_slot_with_policy(
2114 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002115 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002116 goto exit;
2117
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002118 status = psa_validate_optional_attributes( source_slot,
2119 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002120 if( status != PSA_SUCCESS )
2121 goto exit;
2122
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002123 status = psa_restrict_key_policy( source_slot->attr.type,
2124 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002125 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 if( status != PSA_SUCCESS )
2127 goto exit;
2128
Ronald Cron81709fc2020-11-14 12:10:32 +01002129 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2130 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131 if( status != PSA_SUCCESS )
2132 goto exit;
2133
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002134#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2135 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002136 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002137 /* Copying to a secure element is not implemented yet. */
2138 status = PSA_ERROR_NOT_SUPPORTED;
2139 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002140 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002141#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002142
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002143 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002145 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002146
Ronald Cron81709fc2020-11-14 12:10:32 +01002147 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002148exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002149 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002150 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002151
Ronald Cron5c522922020-11-14 16:35:34 +01002152 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002153
Ronald Cron5c522922020-11-14 16:35:34 +01002154 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002155}
2156
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002157
2158
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002159/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002160/* Message digests */
2161/****************************************************************/
2162
John Durkop07cc04a2020-11-16 22:08:34 -08002163#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002164 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2165 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002166 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002167static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002168{
2169 switch( alg )
2170 {
John Durkopee4e6602020-11-27 08:48:46 -08002171#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002172 case PSA_ALG_MD2:
2173 return( &mbedtls_md2_info );
2174#endif
John Durkopee4e6602020-11-27 08:48:46 -08002175#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002176 case PSA_ALG_MD4:
2177 return( &mbedtls_md4_info );
2178#endif
John Durkopee4e6602020-11-27 08:48:46 -08002179#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002180 case PSA_ALG_MD5:
2181 return( &mbedtls_md5_info );
2182#endif
John Durkopee4e6602020-11-27 08:48:46 -08002183#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002184 case PSA_ALG_RIPEMD160:
2185 return( &mbedtls_ripemd160_info );
2186#endif
John Durkopee4e6602020-11-27 08:48:46 -08002187#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002188 case PSA_ALG_SHA_1:
2189 return( &mbedtls_sha1_info );
2190#endif
John Durkopee4e6602020-11-27 08:48:46 -08002191#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002192 case PSA_ALG_SHA_224:
2193 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002194#endif
2195#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002196 case PSA_ALG_SHA_256:
2197 return( &mbedtls_sha256_info );
2198#endif
John Durkopee4e6602020-11-27 08:48:46 -08002199#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002200 case PSA_ALG_SHA_384:
2201 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002202#endif
John Durkopee4e6602020-11-27 08:48:46 -08002203#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002204 case PSA_ALG_SHA_512:
2205 return( &mbedtls_sha512_info );
2206#endif
2207 default:
2208 return( NULL );
2209 }
2210}
John Durkop07cc04a2020-11-16 22:08:34 -08002211#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002212 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2213 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2214 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002215
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002216psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2217{
2218 switch( operation->alg )
2219 {
Gilles Peskine81736312018-06-26 15:04:31 +02002220 case 0:
2221 /* The object has (apparently) been initialized but it is not
2222 * in use. It's ok to call abort on such an object, and there's
2223 * nothing to do. */
2224 break;
John Durkopee4e6602020-11-27 08:48:46 -08002225#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002226 case PSA_ALG_MD2:
2227 mbedtls_md2_free( &operation->ctx.md2 );
2228 break;
2229#endif
John Durkopee4e6602020-11-27 08:48:46 -08002230#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002231 case PSA_ALG_MD4:
2232 mbedtls_md4_free( &operation->ctx.md4 );
2233 break;
2234#endif
John Durkopee4e6602020-11-27 08:48:46 -08002235#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002236 case PSA_ALG_MD5:
2237 mbedtls_md5_free( &operation->ctx.md5 );
2238 break;
2239#endif
John Durkopee4e6602020-11-27 08:48:46 -08002240#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002241 case PSA_ALG_RIPEMD160:
2242 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2243 break;
2244#endif
John Durkopee4e6602020-11-27 08:48:46 -08002245#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246 case PSA_ALG_SHA_1:
2247 mbedtls_sha1_free( &operation->ctx.sha1 );
2248 break;
2249#endif
John Durkop6ca23272020-12-03 06:01:32 -08002250#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002252 mbedtls_sha256_free( &operation->ctx.sha256 );
2253 break;
2254#endif
2255#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002256 case PSA_ALG_SHA_256:
2257 mbedtls_sha256_free( &operation->ctx.sha256 );
2258 break;
2259#endif
John Durkop6ca23272020-12-03 06:01:32 -08002260#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002262 mbedtls_sha512_free( &operation->ctx.sha512 );
2263 break;
2264#endif
2265#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002266 case PSA_ALG_SHA_512:
2267 mbedtls_sha512_free( &operation->ctx.sha512 );
2268 break;
2269#endif
2270 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002271 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002272 }
2273 operation->alg = 0;
2274 return( PSA_SUCCESS );
2275}
2276
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002277psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002278 psa_algorithm_t alg )
2279{
Janos Follath24eed8d2019-11-22 13:21:35 +00002280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002281
2282 /* A context must be freshly initialized before it can be set up. */
2283 if( operation->alg != 0 )
2284 {
2285 return( PSA_ERROR_BAD_STATE );
2286 }
2287
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002288 switch( alg )
2289 {
John Durkopee4e6602020-11-27 08:48:46 -08002290#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291 case PSA_ALG_MD2:
2292 mbedtls_md2_init( &operation->ctx.md2 );
2293 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2294 break;
2295#endif
John Durkopee4e6602020-11-27 08:48:46 -08002296#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297 case PSA_ALG_MD4:
2298 mbedtls_md4_init( &operation->ctx.md4 );
2299 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2300 break;
2301#endif
John Durkopee4e6602020-11-27 08:48:46 -08002302#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303 case PSA_ALG_MD5:
2304 mbedtls_md5_init( &operation->ctx.md5 );
2305 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2306 break;
2307#endif
John Durkopee4e6602020-11-27 08:48:46 -08002308#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309 case PSA_ALG_RIPEMD160:
2310 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2311 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2312 break;
2313#endif
John Durkopee4e6602020-11-27 08:48:46 -08002314#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315 case PSA_ALG_SHA_1:
2316 mbedtls_sha1_init( &operation->ctx.sha1 );
2317 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2318 break;
2319#endif
John Durkopee4e6602020-11-27 08:48:46 -08002320#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002321 case PSA_ALG_SHA_224:
2322 mbedtls_sha256_init( &operation->ctx.sha256 );
2323 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2324 break;
John Durkopee4e6602020-11-27 08:48:46 -08002325#endif
2326#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 case PSA_ALG_SHA_256:
2328 mbedtls_sha256_init( &operation->ctx.sha256 );
2329 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2330 break;
2331#endif
John Durkopee4e6602020-11-27 08:48:46 -08002332#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 case PSA_ALG_SHA_384:
2334 mbedtls_sha512_init( &operation->ctx.sha512 );
2335 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2336 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002337#endif
John Durkopee4e6602020-11-27 08:48:46 -08002338#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 case PSA_ALG_SHA_512:
2340 mbedtls_sha512_init( &operation->ctx.sha512 );
2341 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2342 break;
2343#endif
2344 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002345 return( PSA_ALG_IS_HASH( alg ) ?
2346 PSA_ERROR_NOT_SUPPORTED :
2347 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 }
2349 if( ret == 0 )
2350 operation->alg = alg;
2351 else
2352 psa_hash_abort( operation );
2353 return( mbedtls_to_psa_error( ret ) );
2354}
2355
2356psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2357 const uint8_t *input,
2358 size_t input_length )
2359{
Janos Follath24eed8d2019-11-22 13:21:35 +00002360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002361
2362 /* Don't require hash implementations to behave correctly on a
2363 * zero-length input, which may have an invalid pointer. */
2364 if( input_length == 0 )
2365 return( PSA_SUCCESS );
2366
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002367 switch( operation->alg )
2368 {
John Durkopee4e6602020-11-27 08:48:46 -08002369#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 case PSA_ALG_MD2:
2371 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2372 input, input_length );
2373 break;
2374#endif
John Durkopee4e6602020-11-27 08:48:46 -08002375#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376 case PSA_ALG_MD4:
2377 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2378 input, input_length );
2379 break;
2380#endif
John Durkopee4e6602020-11-27 08:48:46 -08002381#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382 case PSA_ALG_MD5:
2383 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2384 input, input_length );
2385 break;
2386#endif
John Durkopee4e6602020-11-27 08:48:46 -08002387#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 case PSA_ALG_RIPEMD160:
2389 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2390 input, input_length );
2391 break;
2392#endif
John Durkopee4e6602020-11-27 08:48:46 -08002393#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 case PSA_ALG_SHA_1:
2395 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2396 input, input_length );
2397 break;
2398#endif
John Durkop6ca23272020-12-03 06:01:32 -08002399#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002400 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002401 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2402 input, input_length );
2403 break;
2404#endif
2405#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002406 case PSA_ALG_SHA_256:
2407 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2408 input, input_length );
2409 break;
2410#endif
John Durkop6ca23272020-12-03 06:01:32 -08002411#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002412 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002413 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2414 input, input_length );
2415 break;
2416#endif
2417#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002418 case PSA_ALG_SHA_512:
2419 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2420 input, input_length );
2421 break;
2422#endif
2423 default:
John Durkopee4e6602020-11-27 08:48:46 -08002424 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002425 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002427
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002428 if( ret != 0 )
2429 psa_hash_abort( operation );
2430 return( mbedtls_to_psa_error( ret ) );
2431}
2432
2433psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2434 uint8_t *hash,
2435 size_t hash_size,
2436 size_t *hash_length )
2437{
itayzafrir40835d42018-08-02 13:14:17 +03002438 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002439 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002440 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002441
2442 /* Fill the output buffer with something that isn't a valid hash
2443 * (barring an attack on the hash and deliberately-crafted input),
2444 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002445 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002446 /* If hash_size is 0 then hash may be NULL and then the
2447 * call to memset would have undefined behavior. */
2448 if( hash_size != 0 )
2449 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002450
2451 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002452 {
2453 status = PSA_ERROR_BUFFER_TOO_SMALL;
2454 goto exit;
2455 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456
2457 switch( operation->alg )
2458 {
John Durkopee4e6602020-11-27 08:48:46 -08002459#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002460 case PSA_ALG_MD2:
2461 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2462 break;
2463#endif
John Durkopee4e6602020-11-27 08:48:46 -08002464#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002465 case PSA_ALG_MD4:
2466 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2467 break;
2468#endif
John Durkopee4e6602020-11-27 08:48:46 -08002469#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002470 case PSA_ALG_MD5:
2471 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2472 break;
2473#endif
John Durkopee4e6602020-11-27 08:48:46 -08002474#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002475 case PSA_ALG_RIPEMD160:
2476 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2477 break;
2478#endif
John Durkopee4e6602020-11-27 08:48:46 -08002479#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002480 case PSA_ALG_SHA_1:
2481 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2482 break;
2483#endif
John Durkop6ca23272020-12-03 06:01:32 -08002484#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002485 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002486 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2487 break;
2488#endif
2489#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490 case PSA_ALG_SHA_256:
2491 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2492 break;
2493#endif
John Durkop6ca23272020-12-03 06:01:32 -08002494#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002495 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002496 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2497 break;
2498#endif
2499#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002500 case PSA_ALG_SHA_512:
2501 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2502 break;
2503#endif
2504 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002505 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002506 }
itayzafrir40835d42018-08-02 13:14:17 +03002507 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508
itayzafrir40835d42018-08-02 13:14:17 +03002509exit:
2510 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002512 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002513 return( psa_hash_abort( operation ) );
2514 }
2515 else
2516 {
2517 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002518 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002519 }
2520}
2521
Gilles Peskine2d277862018-06-18 15:41:12 +02002522psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2523 const uint8_t *hash,
2524 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002525{
2526 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2527 size_t actual_hash_length;
2528 psa_status_t status = psa_hash_finish( operation,
2529 actual_hash, sizeof( actual_hash ),
2530 &actual_hash_length );
2531 if( status != PSA_SUCCESS )
2532 return( status );
2533 if( actual_hash_length != hash_length )
2534 return( PSA_ERROR_INVALID_SIGNATURE );
2535 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2536 return( PSA_ERROR_INVALID_SIGNATURE );
2537 return( PSA_SUCCESS );
2538}
2539
Gilles Peskine0a749c82019-11-28 19:33:58 +01002540psa_status_t psa_hash_compute( psa_algorithm_t alg,
2541 const uint8_t *input, size_t input_length,
2542 uint8_t *hash, size_t hash_size,
2543 size_t *hash_length )
2544{
2545 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2546 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2547
2548 *hash_length = hash_size;
2549 status = psa_hash_setup( &operation, alg );
2550 if( status != PSA_SUCCESS )
2551 goto exit;
2552 status = psa_hash_update( &operation, input, input_length );
2553 if( status != PSA_SUCCESS )
2554 goto exit;
2555 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2556 if( status != PSA_SUCCESS )
2557 goto exit;
2558
2559exit:
2560 if( status == PSA_SUCCESS )
2561 status = psa_hash_abort( &operation );
2562 else
2563 psa_hash_abort( &operation );
2564 return( status );
2565}
2566
2567psa_status_t psa_hash_compare( psa_algorithm_t alg,
2568 const uint8_t *input, size_t input_length,
2569 const uint8_t *hash, size_t hash_length )
2570{
2571 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2572 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2573
2574 status = psa_hash_setup( &operation, alg );
2575 if( status != PSA_SUCCESS )
2576 goto exit;
2577 status = psa_hash_update( &operation, input, input_length );
2578 if( status != PSA_SUCCESS )
2579 goto exit;
2580 status = psa_hash_verify( &operation, hash, hash_length );
2581 if( status != PSA_SUCCESS )
2582 goto exit;
2583
2584exit:
2585 if( status == PSA_SUCCESS )
2586 status = psa_hash_abort( &operation );
2587 else
2588 psa_hash_abort( &operation );
2589 return( status );
2590}
2591
Gilles Peskineeb35d782019-01-22 17:56:16 +01002592psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2593 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002594{
2595 if( target_operation->alg != 0 )
2596 return( PSA_ERROR_BAD_STATE );
2597
2598 switch( source_operation->alg )
2599 {
2600 case 0:
2601 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002602#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002603 case PSA_ALG_MD2:
2604 mbedtls_md2_clone( &target_operation->ctx.md2,
2605 &source_operation->ctx.md2 );
2606 break;
2607#endif
John Durkopee4e6602020-11-27 08:48:46 -08002608#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002609 case PSA_ALG_MD4:
2610 mbedtls_md4_clone( &target_operation->ctx.md4,
2611 &source_operation->ctx.md4 );
2612 break;
2613#endif
John Durkopee4e6602020-11-27 08:48:46 -08002614#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002615 case PSA_ALG_MD5:
2616 mbedtls_md5_clone( &target_operation->ctx.md5,
2617 &source_operation->ctx.md5 );
2618 break;
2619#endif
John Durkopee4e6602020-11-27 08:48:46 -08002620#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002621 case PSA_ALG_RIPEMD160:
2622 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2623 &source_operation->ctx.ripemd160 );
2624 break;
2625#endif
John Durkopee4e6602020-11-27 08:48:46 -08002626#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002627 case PSA_ALG_SHA_1:
2628 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2629 &source_operation->ctx.sha1 );
2630 break;
2631#endif
John Durkop6ca23272020-12-03 06:01:32 -08002632#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002633 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002634 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2635 &source_operation->ctx.sha256 );
2636 break;
2637#endif
2638#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002639 case PSA_ALG_SHA_256:
2640 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2641 &source_operation->ctx.sha256 );
2642 break;
2643#endif
John Durkop6ca23272020-12-03 06:01:32 -08002644#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002645 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002646 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2647 &source_operation->ctx.sha512 );
2648 break;
2649#endif
2650#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002651 case PSA_ALG_SHA_512:
2652 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2653 &source_operation->ctx.sha512 );
2654 break;
2655#endif
2656 default:
2657 return( PSA_ERROR_NOT_SUPPORTED );
2658 }
2659
2660 target_operation->alg = source_operation->alg;
2661 return( PSA_SUCCESS );
2662}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663
2664
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002665/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002666/* MAC */
2667/****************************************************************/
2668
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002669static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002670 psa_algorithm_t alg,
2671 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002672 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002673 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002676 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002677
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002678 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002679 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002680
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2682 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002683 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002685 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 mode = MBEDTLS_MODE_STREAM;
2687 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688 case PSA_ALG_CTR:
2689 mode = MBEDTLS_MODE_CTR;
2690 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002691 case PSA_ALG_CFB:
2692 mode = MBEDTLS_MODE_CFB;
2693 break;
2694 case PSA_ALG_OFB:
2695 mode = MBEDTLS_MODE_OFB;
2696 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002697 case PSA_ALG_ECB_NO_PADDING:
2698 mode = MBEDTLS_MODE_ECB;
2699 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002700 case PSA_ALG_CBC_NO_PADDING:
2701 mode = MBEDTLS_MODE_CBC;
2702 break;
2703 case PSA_ALG_CBC_PKCS7:
2704 mode = MBEDTLS_MODE_CBC;
2705 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002706 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002707 mode = MBEDTLS_MODE_CCM;
2708 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002709 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 mode = MBEDTLS_MODE_GCM;
2711 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002712 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002713 mode = MBEDTLS_MODE_CHACHAPOLY;
2714 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 default:
2716 return( NULL );
2717 }
2718 }
2719 else if( alg == PSA_ALG_CMAC )
2720 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002721 else
2722 return( NULL );
2723
2724 switch( key_type )
2725 {
2726 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002727 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002728 break;
2729 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002730 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2731 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002733 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002735 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002736 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2737 * but two-key Triple-DES is functionally three-key Triple-DES
2738 * with K1=K3, so that's how we present it to mbedtls. */
2739 if( key_bits == 128 )
2740 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741 break;
2742 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002743 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002744 break;
2745 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002746 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002747 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002748 case PSA_KEY_TYPE_CHACHA20:
2749 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2750 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002751 default:
2752 return( NULL );
2753 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002754 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002755 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002756
Jaeden Amero23bbb752018-06-26 14:16:54 +01002757 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2758 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002759}
2760
John Durkop6ba40d12020-11-10 08:50:04 -08002761#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002762static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002763{
Gilles Peskine2d277862018-06-18 15:41:12 +02002764 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002765 {
2766 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002767 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002768 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002769 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002770 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002771 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002772 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002773 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002774 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002775 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002776 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002778 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002779 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002780 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002781 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002782 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002783 return( 128 );
2784 default:
2785 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002786 }
2787}
John Durkop07cc04a2020-11-16 22:08:34 -08002788#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002789
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002790/* Initialize the MAC operation structure. Once this function has been
2791 * called, psa_mac_abort can run and will do the right thing. */
2792static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2793 psa_algorithm_t alg )
2794{
2795 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2796
Steven Cooreman15472f82021-03-02 16:16:22 +01002797 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002798 operation->key_set = 0;
2799 operation->iv_set = 0;
2800 operation->iv_required = 0;
2801 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002802 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002803
2804#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002805 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002806 {
2807 operation->iv_required = 0;
2808 mbedtls_cipher_init( &operation->ctx.cmac );
2809 status = PSA_SUCCESS;
2810 }
2811 else
2812#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002813#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002814 if( PSA_ALG_IS_HMAC( operation->alg ) )
2815 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002816 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2817 operation->ctx.hmac.hash_ctx.alg = 0;
2818 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002819 }
2820 else
John Durkopd0321952020-10-29 21:37:36 -07002821#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002822 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002823 if( ! PSA_ALG_IS_MAC( alg ) )
2824 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002825 }
2826
2827 if( status != PSA_SUCCESS )
2828 memset( operation, 0, sizeof( *operation ) );
2829 return( status );
2830}
2831
John Durkop6ba40d12020-11-10 08:50:04 -08002832#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002833static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2834{
Gilles Peskine3f108122018-12-07 18:14:53 +01002835 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002836 return( psa_hash_abort( &hmac->hash_ctx ) );
2837}
John Durkop6ba40d12020-11-10 08:50:04 -08002838#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002839
Gilles Peskine8c9def32018-02-08 10:02:12 +01002840psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2841{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002843 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002844 /* The object has (apparently) been initialized but it is not
2845 * in use. It's ok to call abort on such an object, and there's
2846 * nothing to do. */
2847 return( PSA_SUCCESS );
2848 }
2849 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002850#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002851 if( operation->alg == PSA_ALG_CMAC )
2852 {
2853 mbedtls_cipher_free( &operation->ctx.cmac );
2854 }
2855 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002856#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002857#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002858 if( PSA_ALG_IS_HMAC( operation->alg ) )
2859 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002860 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861 }
2862 else
John Durkopd0321952020-10-29 21:37:36 -07002863#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002864 {
2865 /* Sanity check (shouldn't happen: operation->alg should
2866 * always have been initialized to a valid value). */
2867 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868 }
Moran Peker41deec42018-04-04 15:43:05 +03002869
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870 operation->alg = 0;
2871 operation->key_set = 0;
2872 operation->iv_set = 0;
2873 operation->iv_required = 0;
2874 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002875 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002876
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002878
2879bad_state:
2880 /* If abort is called on an uninitialized object, we can't trust
2881 * anything. Wipe the object in case it contains confidential data.
2882 * This may result in a memory leak if a pointer gets overwritten,
2883 * but it's too late to do anything about this. */
2884 memset( operation, 0, sizeof( *operation ) );
2885 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002886}
2887
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002888#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002889static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2890 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002891{
Janos Follath24eed8d2019-11-22 13:21:35 +00002892 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002893 const mbedtls_cipher_info_t *cipher_info =
2894 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2895 slot->attr.type, slot->attr.bits,
2896 NULL );
2897 if( cipher_info == NULL )
2898 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002899
2900 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2901 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002902 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903
2904 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002905 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002906 slot->attr.bits );
2907exit:
2908 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002910#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002911
John Durkop6ba40d12020-11-10 08:50:04 -08002912#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002913static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2914 const uint8_t *key,
2915 size_t key_length,
2916 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002918 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002920 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002921 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002922 psa_status_t status;
2923
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002924 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2925 * overflow below. This should never trigger if the hash algorithm
2926 * is implemented correctly. */
2927 /* The size checks against the ipad and opad buffers cannot be written
2928 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2929 * because that triggers -Wlogical-op on GCC 7.3. */
2930 if( block_size > sizeof( ipad ) )
2931 return( PSA_ERROR_NOT_SUPPORTED );
2932 if( block_size > sizeof( hmac->opad ) )
2933 return( PSA_ERROR_NOT_SUPPORTED );
2934 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002935 return( PSA_ERROR_NOT_SUPPORTED );
2936
Gilles Peskined223b522018-06-11 18:12:58 +02002937 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002938 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002939 status = psa_hash_compute( hash_alg, key, key_length,
2940 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002942 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002943 }
Gilles Peskine96889972018-07-12 17:07:03 +02002944 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2945 * but it is permitted. It is common when HMAC is used in HKDF, for
2946 * example. Don't call `memcpy` in the 0-length because `key` could be
2947 * an invalid pointer which would make the behavior undefined. */
2948 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002949 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002950
Gilles Peskined223b522018-06-11 18:12:58 +02002951 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2952 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002953 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002954 ipad[i] ^= 0x36;
2955 memset( ipad + key_length, 0x36, block_size - key_length );
2956
2957 /* Copy the key material from ipad to opad, flipping the requisite bits,
2958 * and filling the rest of opad with the requisite constant. */
2959 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002960 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2961 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962
Gilles Peskine01126fa2018-07-12 17:04:55 +02002963 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002964 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002965 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002966
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002968
2969cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002970 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002971
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002972 return( status );
2973}
John Durkop6ba40d12020-11-10 08:50:04 -08002974#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002975
Gilles Peskine89167cb2018-07-08 20:12:23 +02002976static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002977 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002978 psa_algorithm_t alg,
2979 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002980{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002981 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002982 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002983 psa_key_slot_t *slot;
Steven Cooreman15472f82021-03-02 16:16:22 +01002984 size_t output_length = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002985 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002986 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002988 /* A context must be freshly initialized before it can be set up. */
2989 if( operation->alg != 0 )
2990 {
2991 return( PSA_ERROR_BAD_STATE );
2992 }
2993
Steven Cooreman15472f82021-03-02 16:16:22 +01002994 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002995 if( status != PSA_SUCCESS )
2996 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002997 if( is_sign )
2998 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999
Ronald Cron5c522922020-11-14 16:35:34 +01003000 status = psa_get_and_lock_transparent_key_slot_with_policy(
3001 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003002 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003003 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01003004
3005 status = psa_get_mac_output_length( alg, slot->attr.type,
3006 &output_length );
3007 if( status != PSA_SUCCESS )
3008 goto exit;
3009
3010 operation->mac_size = (uint8_t) output_length;
3011
3012 if( operation->mac_size < 4 )
3013 {
3014 /* A very short MAC is too short for security since it can be
3015 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3016 * so we make this our minimum, even though 32 bits is still
3017 * too small for security. */
3018 status = PSA_ERROR_NOT_SUPPORTED;
3019 goto exit;
3020 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003021
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01003023 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003024 {
Steven Cooreman15472f82021-03-02 16:16:22 +01003025 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003026 }
3027 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003029#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003030 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003031 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003032 /* Sanity check. This shouldn't fail on a valid configuration. */
3033 if( operation->mac_size == 0 ||
3034 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3035 {
3036 status = PSA_ERROR_NOT_SUPPORTED;
3037 goto exit;
3038 }
3039
Gilles Peskine8e338702019-07-30 20:06:31 +02003040 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003041 {
3042 status = PSA_ERROR_INVALID_ARGUMENT;
3043 goto exit;
3044 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003045
Gilles Peskine01126fa2018-07-12 17:04:55 +02003046 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003047 slot->key.data,
3048 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003049 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003050 }
3051 else
John Durkopd0321952020-10-29 21:37:36 -07003052#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003053 {
3054 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003056
Gilles Peskinefbfac682018-07-08 20:51:54 +02003057exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003058 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003060 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003062 else
3063 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003064 operation->key_set = 1;
3065 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003066
Ronald Cron5c522922020-11-14 16:35:34 +01003067 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003068
Ronald Cron5c522922020-11-14 16:35:34 +01003069 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003070}
3071
Gilles Peskine89167cb2018-07-08 20:12:23 +02003072psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003073 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003074 psa_algorithm_t alg )
3075{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003076 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003077}
3078
3079psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003080 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003081 psa_algorithm_t alg )
3082{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003083 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003084}
3085
Gilles Peskine8c9def32018-02-08 10:02:12 +01003086psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3087 const uint8_t *input,
3088 size_t input_length )
3089{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003090 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003092 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003094 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095 operation->has_input = 1;
3096
Gilles Peskine8c9def32018-02-08 10:02:12 +01003097#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003098 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003099 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003100 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3101 input, input_length );
3102 status = mbedtls_to_psa_error( ret );
3103 }
3104 else
3105#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003106#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 if( PSA_ALG_IS_HMAC( operation->alg ) )
3108 {
3109 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3110 input_length );
3111 }
3112 else
John Durkopd0321952020-10-29 21:37:36 -07003113#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 {
3115 /* This shouldn't happen if `operation` was initialized by
3116 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003117 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003118 }
3119
Gilles Peskinefbfac682018-07-08 20:51:54 +02003120 if( status != PSA_SUCCESS )
3121 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003122 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123}
3124
John Durkop6ba40d12020-11-10 08:50:04 -08003125#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003126static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3127 uint8_t *mac,
3128 size_t mac_size )
3129{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003130 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003131 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3132 size_t hash_size = 0;
3133 size_t block_size = psa_get_hash_block_size( hash_alg );
3134 psa_status_t status;
3135
Gilles Peskine01126fa2018-07-12 17:04:55 +02003136 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3137 if( status != PSA_SUCCESS )
3138 return( status );
3139 /* From here on, tmp needs to be wiped. */
3140
3141 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3142 if( status != PSA_SUCCESS )
3143 goto exit;
3144
3145 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3146 if( status != PSA_SUCCESS )
3147 goto exit;
3148
3149 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3150 if( status != PSA_SUCCESS )
3151 goto exit;
3152
Gilles Peskined911eb72018-08-14 15:18:45 +02003153 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3154 if( status != PSA_SUCCESS )
3155 goto exit;
3156
3157 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003158
3159exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003160 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003161 return( status );
3162}
John Durkop6ba40d12020-11-10 08:50:04 -08003163#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003164
mohammad16036df908f2018-04-02 08:34:15 -07003165static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003166 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003167 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003169 if( ! operation->key_set )
3170 return( PSA_ERROR_BAD_STATE );
3171 if( operation->iv_required && ! operation->iv_set )
3172 return( PSA_ERROR_BAD_STATE );
3173
Gilles Peskine8c9def32018-02-08 10:02:12 +01003174 if( mac_size < operation->mac_size )
3175 return( PSA_ERROR_BUFFER_TOO_SMALL );
3176
Gilles Peskine8c9def32018-02-08 10:02:12 +01003177#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003178 if( operation->alg == PSA_ALG_CMAC )
3179 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003180 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003181 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3182 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003183 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003184 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003185 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003186 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003187 else
3188#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003190 if( PSA_ALG_IS_HMAC( operation->alg ) )
3191 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003192 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003193 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003194 }
3195 else
John Durkopd0321952020-10-29 21:37:36 -07003196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197 {
3198 /* This shouldn't happen if `operation` was initialized by
3199 * a setup function. */
3200 return( PSA_ERROR_BAD_STATE );
3201 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003202}
3203
Gilles Peskineacd4be32018-07-08 19:56:25 +02003204psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3205 uint8_t *mac,
3206 size_t mac_size,
3207 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003208{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003209 psa_status_t status;
3210
Jaeden Amero252ef282019-02-15 14:05:35 +00003211 if( operation->alg == 0 )
3212 {
3213 return( PSA_ERROR_BAD_STATE );
3214 }
3215
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003216 /* Fill the output buffer with something that isn't a valid mac
3217 * (barring an attack on the mac and deliberately-crafted input),
3218 * in case the caller doesn't check the return status properly. */
3219 *mac_length = mac_size;
3220 /* If mac_size is 0 then mac may be NULL and then the
3221 * call to memset would have undefined behavior. */
3222 if( mac_size != 0 )
3223 memset( mac, '!', mac_size );
3224
Gilles Peskine89167cb2018-07-08 20:12:23 +02003225 if( ! operation->is_sign )
3226 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003227 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003228 }
mohammad16036df908f2018-04-02 08:34:15 -07003229
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003230 status = psa_mac_finish_internal( operation, mac, mac_size );
3231
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003232 if( status == PSA_SUCCESS )
3233 {
3234 status = psa_mac_abort( operation );
3235 if( status == PSA_SUCCESS )
3236 *mac_length = operation->mac_size;
3237 else
3238 memset( mac, '!', mac_size );
3239 }
3240 else
3241 psa_mac_abort( operation );
3242 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003243}
3244
Gilles Peskineacd4be32018-07-08 19:56:25 +02003245psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3246 const uint8_t *mac,
3247 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003248{
Gilles Peskine828ed142018-06-18 23:25:51 +02003249 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003250 psa_status_t status;
3251
Jaeden Amero252ef282019-02-15 14:05:35 +00003252 if( operation->alg == 0 )
3253 {
3254 return( PSA_ERROR_BAD_STATE );
3255 }
3256
Gilles Peskine89167cb2018-07-08 20:12:23 +02003257 if( operation->is_sign )
3258 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003260 }
3261 if( operation->mac_size != mac_length )
3262 {
3263 status = PSA_ERROR_INVALID_SIGNATURE;
3264 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003265 }
mohammad16036df908f2018-04-02 08:34:15 -07003266
3267 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003268 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003269 if( status != PSA_SUCCESS )
3270 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003271
3272 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3273 status = PSA_ERROR_INVALID_SIGNATURE;
3274
3275cleanup:
3276 if( status == PSA_SUCCESS )
3277 status = psa_mac_abort( operation );
3278 else
3279 psa_mac_abort( operation );
3280
Gilles Peskine3f108122018-12-07 18:14:53 +01003281 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003282
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003283 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003284}
3285
3286
Gilles Peskine20035e32018-02-03 22:44:14 +01003287
Gilles Peskine20035e32018-02-03 22:44:14 +01003288/****************************************************************/
3289/* Asymmetric cryptography */
3290/****************************************************************/
3291
John Durkop6ba40d12020-11-10 08:50:04 -08003292#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3293 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003294/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003295 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003296static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3297 size_t hash_length,
3298 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003299{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003300 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003301 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003302 *md_alg = mbedtls_md_get_type( md_info );
3303
3304 /* The Mbed TLS RSA module uses an unsigned int for hash length
3305 * parameters. Validate that it fits so that we don't risk an
3306 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003307#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003308 if( hash_length > UINT_MAX )
3309 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003310#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003311
John Durkop0e005192020-10-31 22:06:54 -07003312#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003313 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3314 * must be correct. */
3315 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3316 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003317 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003318 if( md_info == NULL )
3319 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003320 if( mbedtls_md_get_size( md_info ) != hash_length )
3321 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003322 }
John Durkop0e005192020-10-31 22:06:54 -07003323#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003324
John Durkop0e005192020-10-31 22:06:54 -07003325#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003326 /* PSS requires a hash internally. */
3327 if( PSA_ALG_IS_RSA_PSS( alg ) )
3328 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003329 if( md_info == NULL )
3330 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003331 }
John Durkop0e005192020-10-31 22:06:54 -07003332#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003333
Gilles Peskine61b91d42018-06-08 16:09:36 +02003334 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003335}
3336
Gilles Peskine2b450e32018-06-27 15:42:46 +02003337static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3338 psa_algorithm_t alg,
3339 const uint8_t *hash,
3340 size_t hash_length,
3341 uint8_t *signature,
3342 size_t signature_size,
3343 size_t *signature_length )
3344{
3345 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003346 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003347 mbedtls_md_type_t md_alg;
3348
3349 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3350 if( status != PSA_SUCCESS )
3351 return( status );
3352
Gilles Peskine630a18a2018-06-29 17:49:35 +02003353 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003354 return( PSA_ERROR_BUFFER_TOO_SMALL );
3355
John Durkop0e005192020-10-31 22:06:54 -07003356#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003357 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3358 {
3359 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3360 MBEDTLS_MD_NONE );
3361 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003362 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003363 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003364 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003365 md_alg,
3366 (unsigned int) hash_length,
3367 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003368 signature );
3369 }
3370 else
John Durkop0e005192020-10-31 22:06:54 -07003371#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3372#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003373 if( PSA_ALG_IS_RSA_PSS( alg ) )
3374 {
3375 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3376 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003377 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003378 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003379 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003380 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003381 (unsigned int) hash_length,
3382 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003383 signature );
3384 }
3385 else
John Durkop0e005192020-10-31 22:06:54 -07003386#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387 {
3388 return( PSA_ERROR_INVALID_ARGUMENT );
3389 }
3390
3391 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003392 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003393 return( mbedtls_to_psa_error( ret ) );
3394}
3395
3396static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3397 psa_algorithm_t alg,
3398 const uint8_t *hash,
3399 size_t hash_length,
3400 const uint8_t *signature,
3401 size_t signature_length )
3402{
3403 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003404 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003405 mbedtls_md_type_t md_alg;
3406
3407 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3408 if( status != PSA_SUCCESS )
3409 return( status );
3410
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003411 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3412 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413
John Durkop0e005192020-10-31 22:06:54 -07003414#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003415 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3416 {
3417 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3418 MBEDTLS_MD_NONE );
3419 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003420 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003421 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003422 MBEDTLS_RSA_PUBLIC,
3423 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003424 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003425 hash,
3426 signature );
3427 }
3428 else
John Durkop0e005192020-10-31 22:06:54 -07003429#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3430#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003431 if( PSA_ALG_IS_RSA_PSS( alg ) )
3432 {
3433 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3434 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003435 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003436 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003437 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003438 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003439 (unsigned int) hash_length,
3440 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003441 signature );
3442 }
3443 else
John Durkop0e005192020-10-31 22:06:54 -07003444#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445 {
3446 return( PSA_ERROR_INVALID_ARGUMENT );
3447 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003448
3449 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3450 * the rest of the signature is invalid". This has little use in
3451 * practice and PSA doesn't report this distinction. */
3452 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3453 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003454 return( mbedtls_to_psa_error( ret ) );
3455}
John Durkop6ba40d12020-11-10 08:50:04 -08003456#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3457 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003458
John Durkop6ba40d12020-11-10 08:50:04 -08003459#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3460 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003461/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3462 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3463 * (even though these functions don't modify it). */
3464static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3465 psa_algorithm_t alg,
3466 const uint8_t *hash,
3467 size_t hash_length,
3468 uint8_t *signature,
3469 size_t signature_size,
3470 size_t *signature_length )
3471{
Janos Follath24eed8d2019-11-22 13:21:35 +00003472 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003473 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003474 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003475 mbedtls_mpi_init( &r );
3476 mbedtls_mpi_init( &s );
3477
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003478 if( signature_size < 2 * curve_bytes )
3479 {
3480 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3481 goto cleanup;
3482 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003483
John Durkop0ea39e02020-10-13 19:58:20 -07003484#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003485 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3486 {
3487 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3488 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3489 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003490 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3491 &ecp->d, hash,
3492 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003493 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003494 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003495 }
3496 else
John Durkop0ea39e02020-10-13 19:58:20 -07003497#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003498 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003499 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003500 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3501 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003502 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003503 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003504 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003505
3506 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3507 signature,
3508 curve_bytes ) );
3509 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3510 signature + curve_bytes,
3511 curve_bytes ) );
3512
3513cleanup:
3514 mbedtls_mpi_free( &r );
3515 mbedtls_mpi_free( &s );
3516 if( ret == 0 )
3517 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003518 return( mbedtls_to_psa_error( ret ) );
3519}
3520
3521static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3522 const uint8_t *hash,
3523 size_t hash_length,
3524 const uint8_t *signature,
3525 size_t signature_length )
3526{
Janos Follath24eed8d2019-11-22 13:21:35 +00003527 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003528 mbedtls_mpi r, s;
3529 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3530 mbedtls_mpi_init( &r );
3531 mbedtls_mpi_init( &s );
3532
3533 if( signature_length != 2 * curve_bytes )
3534 return( PSA_ERROR_INVALID_SIGNATURE );
3535
3536 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3537 signature,
3538 curve_bytes ) );
3539 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3540 signature + curve_bytes,
3541 curve_bytes ) );
3542
Steven Cooremanacda8342020-07-24 23:09:52 +02003543 /* Check whether the public part is loaded. If not, load it. */
3544 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3545 {
3546 MBEDTLS_MPI_CHK(
3547 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003548 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003549 }
3550
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003551 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3552 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003553
3554cleanup:
3555 mbedtls_mpi_free( &r );
3556 mbedtls_mpi_free( &s );
3557 return( mbedtls_to_psa_error( ret ) );
3558}
John Durkop6ba40d12020-11-10 08:50:04 -08003559#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3560 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003561
Ronald Croncf56a0a2020-08-04 09:51:30 +02003562psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003563 psa_algorithm_t alg,
3564 const uint8_t *hash,
3565 size_t hash_length,
3566 uint8_t *signature,
3567 size_t signature_size,
3568 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003569{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003570 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003571 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003572 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003573
3574 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003575 /* Immediately reject a zero-length signature buffer. This guarantees
3576 * that signature must be a valid pointer. (On the other hand, the hash
3577 * buffer can in principle be empty since it doesn't actually have
3578 * to be a hash.) */
3579 if( signature_size == 0 )
3580 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003581
Ronald Cron5c522922020-11-14 16:35:34 +01003582 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3583 PSA_KEY_USAGE_SIGN_HASH,
3584 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003585 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003586 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003587 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003588 {
3589 status = PSA_ERROR_INVALID_ARGUMENT;
3590 goto exit;
3591 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003592
Steven Cooremancd84cb42020-07-16 20:28:36 +02003593 /* Try any of the available accelerators first */
3594 status = psa_driver_wrapper_sign_hash( slot,
3595 alg,
3596 hash,
3597 hash_length,
3598 signature,
3599 signature_size,
3600 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003601 if( status != PSA_ERROR_NOT_SUPPORTED ||
3602 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003603 goto exit;
3604
Steven Cooreman7a250572020-07-17 16:43:05 +02003605 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003606#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3607 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003608 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003609 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003610 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003611
Ronald Cron90857082020-11-25 14:58:33 +01003612 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3613 slot->key.data,
3614 slot->key.bytes,
3615 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003616 if( status != PSA_SUCCESS )
3617 goto exit;
3618
Steven Cooremana2371e52020-07-28 14:30:39 +02003619 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003620 alg,
3621 hash, hash_length,
3622 signature, signature_size,
3623 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003624
Steven Cooremana2371e52020-07-28 14:30:39 +02003625 mbedtls_rsa_free( rsa );
3626 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003627 }
3628 else
John Durkop6ba40d12020-11-10 08:50:04 -08003629#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3630 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003631 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003632 {
John Durkop9814fa22020-11-04 12:28:15 -08003633#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3634 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003635 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003636#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003637 PSA_ALG_IS_ECDSA( alg )
3638#else
3639 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3640#endif
3641 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003642 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003643 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003644 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003645 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003646 slot->key.data,
3647 slot->key.bytes,
3648 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003649 if( status != PSA_SUCCESS )
3650 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003651 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003652 alg,
3653 hash, hash_length,
3654 signature, signature_size,
3655 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003656 mbedtls_ecp_keypair_free( ecp );
3657 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003658 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003659 else
John Durkop9814fa22020-11-04 12:28:15 -08003660#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3661 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003662 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003663 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003664 }
itayzafrir5c753392018-05-08 11:18:38 +03003665 }
3666 else
itayzafrir5c753392018-05-08 11:18:38 +03003667 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003669 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003670
3671exit:
3672 /* Fill the unused part of the output buffer (the whole buffer on error,
3673 * the trailing part on success) with something that isn't a valid mac
3674 * (barring an attack on the mac and deliberately-crafted input),
3675 * in case the caller doesn't check the return status properly. */
3676 if( status == PSA_SUCCESS )
3677 memset( signature + *signature_length, '!',
3678 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003679 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003680 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003681 /* If signature_size is 0 then we have nothing to do. We must not call
3682 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003683
Ronald Cron5c522922020-11-14 16:35:34 +01003684 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003685
Ronald Cron5c522922020-11-14 16:35:34 +01003686 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003687}
3688
Ronald Croncf56a0a2020-08-04 09:51:30 +02003689psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003690 psa_algorithm_t alg,
3691 const uint8_t *hash,
3692 size_t hash_length,
3693 const uint8_t *signature,
3694 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003695{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003696 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003697 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003698 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003699
Ronald Cron5c522922020-11-14 16:35:34 +01003700 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3701 PSA_KEY_USAGE_VERIFY_HASH,
3702 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003703 if( status != PSA_SUCCESS )
3704 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003705
Steven Cooreman55ae2172020-07-17 19:46:15 +02003706 /* Try any of the available accelerators first */
3707 status = psa_driver_wrapper_verify_hash( slot,
3708 alg,
3709 hash,
3710 hash_length,
3711 signature,
3712 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003713 if( status != PSA_ERROR_NOT_SUPPORTED ||
3714 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003715 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003716
John Durkop6ba40d12020-11-10 08:50:04 -08003717#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3718 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003719 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003721 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003722
Ronald Cron90857082020-11-25 14:58:33 +01003723 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3724 slot->key.data,
3725 slot->key.bytes,
3726 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003727 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003728 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003729
Steven Cooremana2371e52020-07-28 14:30:39 +02003730 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003731 alg,
3732 hash, hash_length,
3733 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003734 mbedtls_rsa_free( rsa );
3735 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003736 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003737 }
3738 else
John Durkop6ba40d12020-11-10 08:50:04 -08003739#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3740 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003741 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003742 {
John Durkop9814fa22020-11-04 12:28:15 -08003743#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3744 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003745 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003746 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003747 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003748 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003749 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003750 slot->key.data,
3751 slot->key.bytes,
3752 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003753 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003754 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003755 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003756 hash, hash_length,
3757 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003758 mbedtls_ecp_keypair_free( ecp );
3759 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003760 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003761 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003762 else
John Durkop9814fa22020-11-04 12:28:15 -08003763#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3764 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003765 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003766 status = PSA_ERROR_INVALID_ARGUMENT;
3767 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003768 }
itayzafrir5c753392018-05-08 11:18:38 +03003769 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003770 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003771 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003772 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003773 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003774
3775exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003776 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003777
Ronald Cron5c522922020-11-14 16:35:34 +01003778 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003779}
3780
John Durkop0e005192020-10-31 22:06:54 -07003781#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003782static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3783 mbedtls_rsa_context *rsa )
3784{
3785 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3786 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3787 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3788 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3789}
John Durkop0e005192020-10-31 22:06:54 -07003790#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003791
Ronald Croncf56a0a2020-08-04 09:51:30 +02003792psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003793 psa_algorithm_t alg,
3794 const uint8_t *input,
3795 size_t input_length,
3796 const uint8_t *salt,
3797 size_t salt_length,
3798 uint8_t *output,
3799 size_t output_size,
3800 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003801{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003802 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003803 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003804 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003805
Darryl Green5cc689a2018-07-24 15:34:10 +01003806 (void) input;
3807 (void) input_length;
3808 (void) salt;
3809 (void) output;
3810 (void) output_size;
3811
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003812 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003813
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003814 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3815 return( PSA_ERROR_INVALID_ARGUMENT );
3816
Ronald Cron5c522922020-11-14 16:35:34 +01003817 status = psa_get_and_lock_transparent_key_slot_with_policy(
3818 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003819 if( status != PSA_SUCCESS )
3820 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003821 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3822 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003823 {
3824 status = PSA_ERROR_INVALID_ARGUMENT;
3825 goto exit;
3826 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003827
John Durkop6ba40d12020-11-10 08:50:04 -08003828#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3829 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003830 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003831 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003832 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003833 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3834 slot->key.data,
3835 slot->key.bytes,
3836 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003837 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003838 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003839
3840 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003841 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003842 status = PSA_ERROR_BUFFER_TOO_SMALL;
3843 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003844 }
John Durkop0e005192020-10-31 22:06:54 -07003845#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003846 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003847 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003848 status = mbedtls_to_psa_error(
3849 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003850 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003851 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003852 MBEDTLS_RSA_PUBLIC,
3853 input_length,
3854 input,
3855 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003856 }
3857 else
John Durkop0e005192020-10-31 22:06:54 -07003858#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3859#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003860 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003861 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003862 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003863 status = mbedtls_to_psa_error(
3864 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003865 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003866 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003867 MBEDTLS_RSA_PUBLIC,
3868 salt, salt_length,
3869 input_length,
3870 input,
3871 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 }
3873 else
John Durkop0e005192020-10-31 22:06:54 -07003874#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003875 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003876 status = PSA_ERROR_INVALID_ARGUMENT;
3877 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003879rsa_exit:
3880 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003881 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003882
Steven Cooremana2371e52020-07-28 14:30:39 +02003883 mbedtls_rsa_free( rsa );
3884 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003886 else
John Durkop9814fa22020-11-04 12:28:15 -08003887#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003888 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003890 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003891 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003892
3893exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003894 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003895
Ronald Cron5c522922020-11-14 16:35:34 +01003896 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003897}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003898
Ronald Croncf56a0a2020-08-04 09:51:30 +02003899psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003900 psa_algorithm_t alg,
3901 const uint8_t *input,
3902 size_t input_length,
3903 const uint8_t *salt,
3904 size_t salt_length,
3905 uint8_t *output,
3906 size_t output_size,
3907 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003908{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003909 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003910 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003911 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003912
Darryl Green5cc689a2018-07-24 15:34:10 +01003913 (void) input;
3914 (void) input_length;
3915 (void) salt;
3916 (void) output;
3917 (void) output_size;
3918
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003919 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003921 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3922 return( PSA_ERROR_INVALID_ARGUMENT );
3923
Ronald Cron5c522922020-11-14 16:35:34 +01003924 status = psa_get_and_lock_transparent_key_slot_with_policy(
3925 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003926 if( status != PSA_SUCCESS )
3927 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003928 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003929 {
3930 status = PSA_ERROR_INVALID_ARGUMENT;
3931 goto exit;
3932 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003933
John Durkop6ba40d12020-11-10 08:50:04 -08003934#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3935 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003936 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003937 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003938 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003939 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3940 slot->key.data,
3941 slot->key.bytes,
3942 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003943 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003944 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945
Steven Cooremana2371e52020-07-28 14:30:39 +02003946 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003947 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003948 status = PSA_ERROR_INVALID_ARGUMENT;
3949 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003950 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951
John Durkop0e005192020-10-31 22:06:54 -07003952#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003953 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003954 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003955 status = mbedtls_to_psa_error(
3956 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003957 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003958 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003959 MBEDTLS_RSA_PRIVATE,
3960 output_length,
3961 input,
3962 output,
3963 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003964 }
3965 else
John Durkop0e005192020-10-31 22:06:54 -07003966#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3967#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003968 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003969 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003970 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003971 status = mbedtls_to_psa_error(
3972 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003973 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003974 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003975 MBEDTLS_RSA_PRIVATE,
3976 salt, salt_length,
3977 output_length,
3978 input,
3979 output,
3980 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003981 }
3982 else
John Durkop0e005192020-10-31 22:06:54 -07003983#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003985 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003987
Steven Cooreman4fed4552020-08-03 14:46:03 +02003988rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003989 mbedtls_rsa_free( rsa );
3990 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003992 else
John Durkop6ba40d12020-11-10 08:50:04 -08003993#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3994 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003996 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003997 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003998
3999exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004000 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004001
Ronald Cron5c522922020-11-14 16:35:34 +01004002 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004003}
Gilles Peskine20035e32018-02-03 22:44:14 +01004004
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004005
4006
mohammad1603503973b2018-03-12 15:59:30 +02004007/****************************************************************/
4008/* Symmetric cryptography */
4009/****************************************************************/
4010
Gilles Peskinee553c652018-06-04 16:22:46 +02004011static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004012 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004013 psa_algorithm_t alg,
4014 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004015{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004016 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004017 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004018 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004019 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004020 size_t key_bits;
4021 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004022 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4023 PSA_KEY_USAGE_ENCRYPT :
4024 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004025
Steven Cooremand3feccd2020-09-01 15:56:14 +02004026 /* A context must be freshly initialized before it can be set up. */
4027 if( operation->alg != 0 )
4028 return( PSA_ERROR_BAD_STATE );
4029
Steven Cooremana07b9972020-09-10 14:54:14 +02004030 /* The requested algorithm must be one that can be processed by cipher. */
4031 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004032 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004033
Steven Cooremanef8575e2020-09-11 11:44:50 +02004034 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004035 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004036 if( status != PSA_SUCCESS )
4037 goto exit;
4038
4039 /* Initialize the operation struct members, except for alg. The alg member
4040 * is used to indicate to psa_cipher_abort that there are resources to free,
4041 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004042 operation->key_set = 0;
4043 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004044 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004045 operation->iv_size = 0;
4046 operation->block_size = 0;
4047 if( alg == PSA_ALG_ECB_NO_PADDING )
4048 operation->iv_required = 0;
4049 else
4050 operation->iv_required = 1;
4051
Steven Cooremana07b9972020-09-10 14:54:14 +02004052 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004053 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004054 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004055 slot,
4056 alg );
4057 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004058 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004059 slot,
4060 alg );
4061
Steven Cooremanef8575e2020-09-11 11:44:50 +02004062 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004063 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004064 /* Once the driver context is initialised, it needs to be freed using
4065 * psa_cipher_abort. Indicate this through setting alg. */
4066 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004067 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004068
Steven Cooremand3feccd2020-09-01 15:56:14 +02004069 if( status != PSA_ERROR_NOT_SUPPORTED ||
4070 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4071 goto exit;
4072
Steven Cooremana07b9972020-09-10 14:54:14 +02004073 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004074 * available for the given algorithm & key. */
4075 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004076
Steven Cooremana07b9972020-09-10 14:54:14 +02004077 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004078 * psa_cipher_abort. Indicate there is something to be freed through setting
4079 * alg, and indicate the operation is being done using mbedtls crypto through
4080 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004081 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004082 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004083
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004084 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004085 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004086 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004087 {
4088 status = PSA_ERROR_NOT_SUPPORTED;
4089 goto exit;
4090 }
mohammad1603503973b2018-03-12 15:59:30 +02004091
mohammad1603503973b2018-03-12 15:59:30 +02004092 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004093 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004094 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004095
David Brown12ca5032021-02-11 11:02:00 -07004096#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004097 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004098 {
4099 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004100 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004101 memcpy( keys, slot->key.data, 16 );
4102 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004103 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4104 keys,
4105 192, cipher_operation );
4106 }
4107 else
4108#endif
4109 {
4110 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004111 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004112 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004113 }
Moran Peker41deec42018-04-04 15:43:05 +03004114 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004115 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004116
David Brown63ca2602021-01-26 11:51:12 -07004117#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4118 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004119 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004120 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004121 case PSA_ALG_CBC_NO_PADDING:
4122 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4123 MBEDTLS_PADDING_NONE );
4124 break;
4125 case PSA_ALG_CBC_PKCS7:
4126 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4127 MBEDTLS_PADDING_PKCS7 );
4128 break;
4129 default:
4130 /* The algorithm doesn't involve padding. */
4131 ret = 0;
4132 break;
4133 }
4134 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004135 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004136#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004137
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004138 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004139 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004140 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4141 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004142 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004143 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004144 }
David Brown1bfe4d72021-02-16 12:54:35 -07004145#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004146 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004147 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004148 operation->iv_size = 12;
4149#endif
mohammad1603503973b2018-03-12 15:59:30 +02004150
Steven Cooreman7df02922020-09-09 15:28:49 +02004151 status = PSA_SUCCESS;
4152
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004153exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004154 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004155 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004156 if( status == PSA_SUCCESS )
4157 {
4158 /* Update operation flags for both driver and software implementations */
4159 operation->key_set = 1;
4160 }
4161 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004162 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004163
Ronald Cron5c522922020-11-14 16:35:34 +01004164 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004165
Ronald Cron5c522922020-11-14 16:35:34 +01004166 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004167}
4168
Gilles Peskinefe119512018-07-08 21:39:34 +02004169psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004170 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004171 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004172{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004173 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004174}
4175
Gilles Peskinefe119512018-07-08 21:39:34 +02004176psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004177 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004178 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004179{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004180 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004181}
4182
Gilles Peskinefe119512018-07-08 21:39:34 +02004183psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004184 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004185 size_t iv_size,
4186 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004187{
itayzafrir534bd7c2018-08-02 13:56:32 +03004188 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004190 if( operation->iv_set || ! operation->iv_required )
4191 {
4192 return( PSA_ERROR_BAD_STATE );
4193 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004194
Steven Cooremanef8575e2020-09-11 11:44:50 +02004195 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004196 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004197 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004198 iv,
4199 iv_size,
4200 iv_length );
4201 goto exit;
4202 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004203
Moran Peker41deec42018-04-04 15:43:05 +03004204 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004205 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004206 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004207 goto exit;
4208 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004209 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004210 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004211 if( ret != 0 )
4212 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004213 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004214 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004215 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004216
mohammad16038481e742018-03-18 13:57:31 +02004217 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004218 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004219
Moran Peker395db872018-05-31 14:07:14 +03004220exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004221 if( status == PSA_SUCCESS )
4222 operation->iv_set = 1;
4223 else
Moran Peker395db872018-05-31 14:07:14 +03004224 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004225 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004226}
4227
Gilles Peskinefe119512018-07-08 21:39:34 +02004228psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004229 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004230 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004231{
itayzafrir534bd7c2018-08-02 13:56:32 +03004232 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004233 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004234 if( operation->iv_set || ! operation->iv_required )
4235 {
4236 return( PSA_ERROR_BAD_STATE );
4237 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004238
Steven Cooremanef8575e2020-09-11 11:44:50 +02004239 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004240 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004241 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004242 iv,
4243 iv_length );
4244 goto exit;
4245 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004246
Moran Pekera28258c2018-05-29 16:25:04 +03004247 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004248 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004249 status = PSA_ERROR_INVALID_ARGUMENT;
4250 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004251 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004252 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4253 status = mbedtls_to_psa_error( ret );
4254exit:
4255 if( status == PSA_SUCCESS )
4256 operation->iv_set = 1;
4257 else
mohammad1603503973b2018-03-12 15:59:30 +02004258 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004259 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004260}
4261
Steven Cooreman177deba2020-09-07 17:14:14 +02004262/* Process input for which the algorithm is set to ECB mode. This requires
4263 * manual processing, since the PSA API is defined as being able to process
4264 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4265 * underlying mbedtls_cipher_update only takes full blocks. */
4266static psa_status_t psa_cipher_update_ecb_internal(
4267 mbedtls_cipher_context_t *ctx,
4268 const uint8_t *input,
4269 size_t input_length,
4270 uint8_t *output,
4271 size_t output_size,
4272 size_t *output_length )
4273{
4274 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4275 size_t block_size = ctx->cipher_info->block_size;
4276 size_t internal_output_length = 0;
4277 *output_length = 0;
4278
4279 if( input_length == 0 )
4280 {
4281 status = PSA_SUCCESS;
4282 goto exit;
4283 }
4284
4285 if( ctx->unprocessed_len > 0 )
4286 {
4287 /* Fill up to block size, and run the block if there's a full one. */
4288 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4289
4290 if( input_length < bytes_to_copy )
4291 bytes_to_copy = input_length;
4292
4293 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4294 input, bytes_to_copy );
4295 input_length -= bytes_to_copy;
4296 input += bytes_to_copy;
4297 ctx->unprocessed_len += bytes_to_copy;
4298
4299 if( ctx->unprocessed_len == block_size )
4300 {
4301 status = mbedtls_to_psa_error(
4302 mbedtls_cipher_update( ctx,
4303 ctx->unprocessed_data,
4304 block_size,
4305 output, &internal_output_length ) );
4306
4307 if( status != PSA_SUCCESS )
4308 goto exit;
4309
4310 output += internal_output_length;
4311 output_size -= internal_output_length;
4312 *output_length += internal_output_length;
4313 ctx->unprocessed_len = 0;
4314 }
4315 }
4316
4317 while( input_length >= block_size )
4318 {
4319 /* Run all full blocks we have, one by one */
4320 status = mbedtls_to_psa_error(
4321 mbedtls_cipher_update( ctx, input,
4322 block_size,
4323 output, &internal_output_length ) );
4324
4325 if( status != PSA_SUCCESS )
4326 goto exit;
4327
4328 input_length -= block_size;
4329 input += block_size;
4330
4331 output += internal_output_length;
4332 output_size -= internal_output_length;
4333 *output_length += internal_output_length;
4334 }
4335
4336 if( input_length > 0 )
4337 {
4338 /* Save unprocessed bytes for later processing */
4339 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4340 input, input_length );
4341 ctx->unprocessed_len += input_length;
4342 }
4343
4344 status = PSA_SUCCESS;
4345
4346exit:
4347 return( status );
4348}
4349
Gilles Peskinee553c652018-06-04 16:22:46 +02004350psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4351 const uint8_t *input,
4352 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004353 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004354 size_t output_size,
4355 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004356{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004357 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004358 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004359 if( operation->alg == 0 )
4360 {
4361 return( PSA_ERROR_BAD_STATE );
4362 }
4363 if( operation->iv_required && ! operation->iv_set )
4364 {
4365 return( PSA_ERROR_BAD_STATE );
4366 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004367
Steven Cooremanef8575e2020-09-11 11:44:50 +02004368 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004369 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004370 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004371 input,
4372 input_length,
4373 output,
4374 output_size,
4375 output_length );
4376 goto exit;
4377 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004378
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004379 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004380 {
4381 /* Take the unprocessed partial block left over from previous
4382 * update calls, if any, plus the input to this call. Remove
4383 * the last partial block, if any. You get the data that will be
4384 * output in this call. */
4385 expected_output_size =
4386 ( operation->ctx.cipher.unprocessed_len + input_length )
4387 / operation->block_size * operation->block_size;
4388 }
4389 else
4390 {
4391 expected_output_size = input_length;
4392 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004393
Gilles Peskine89d789c2018-06-04 17:17:16 +02004394 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004395 {
4396 status = PSA_ERROR_BUFFER_TOO_SMALL;
4397 goto exit;
4398 }
mohammad160382759612018-03-12 18:16:40 +02004399
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004400 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4401 {
4402 /* mbedtls_cipher_update has an API inconsistency: it will only
4403 * process a single block at a time in ECB mode. Abstract away that
4404 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004405 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4406 input,
4407 input_length,
4408 output,
4409 output_size,
4410 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004411 }
4412 else
4413 {
4414 status = mbedtls_to_psa_error(
4415 mbedtls_cipher_update( &operation->ctx.cipher, input,
4416 input_length, output, output_length ) );
4417 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004418exit:
4419 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004420 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004421 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004422}
4423
Gilles Peskinee553c652018-06-04 16:22:46 +02004424psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4425 uint8_t *output,
4426 size_t output_size,
4427 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004428{
David Saadab4ecc272019-02-14 13:48:10 +02004429 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004430 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004431 if( operation->alg == 0 )
4432 {
4433 return( PSA_ERROR_BAD_STATE );
4434 }
4435 if( operation->iv_required && ! operation->iv_set )
4436 {
4437 return( PSA_ERROR_BAD_STATE );
4438 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004439
Steven Cooremanef8575e2020-09-11 11:44:50 +02004440 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004441 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004442 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004443 output,
4444 output_size,
4445 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004446 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004447 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004448
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004449 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004450 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004451 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004452 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004453 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004454 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004455 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004456 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004457 }
4458
Steven Cooremanef8575e2020-09-11 11:44:50 +02004459 status = mbedtls_to_psa_error(
4460 mbedtls_cipher_finish( &operation->ctx.cipher,
4461 temp_output_buffer,
4462 output_length ) );
4463 if( status != PSA_SUCCESS )
4464 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004465
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004466 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004467 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004468 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004469 memcpy( output, temp_output_buffer, *output_length );
4470 else
Janos Follath315b51c2018-07-09 16:04:51 +01004471 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004472
Steven Cooremanef8575e2020-09-11 11:44:50 +02004473exit:
4474 if( operation->mbedtls_in_use == 1 )
4475 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004476
Steven Cooremanef8575e2020-09-11 11:44:50 +02004477 if( status == PSA_SUCCESS )
4478 return( psa_cipher_abort( operation ) );
4479 else
4480 {
4481 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004482 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004483
Steven Cooremanef8575e2020-09-11 11:44:50 +02004484 return( status );
4485 }
mohammad1603503973b2018-03-12 15:59:30 +02004486}
4487
Gilles Peskinee553c652018-06-04 16:22:46 +02004488psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4489{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004490 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004491 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004492 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004493 * in use. It's ok to call abort on such an object, and there's
4494 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004495 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004496 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004497
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004498 /* Sanity check (shouldn't happen: operation->alg should
4499 * always have been initialized to a valid value). */
4500 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4501 return( PSA_ERROR_BAD_STATE );
4502
Steven Cooremanef8575e2020-09-11 11:44:50 +02004503 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004504 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004505 else
4506 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004507
Moran Peker41deec42018-04-04 15:43:05 +03004508 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004509 operation->key_set = 0;
4510 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004511 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004512 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004513 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004514 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004515
Moran Peker395db872018-05-31 14:07:14 +03004516 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004517}
4518
Gilles Peskinea0655c32018-04-30 17:06:50 +02004519
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004520
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004521
mohammad16035955c982018-04-26 00:53:03 +03004522/****************************************************************/
4523/* AEAD */
4524/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004525
Gilles Peskineedf9a652018-08-17 18:11:56 +02004526typedef struct
4527{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004528 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004529 const mbedtls_cipher_info_t *cipher_info;
4530 union
4531 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004532 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004533#if defined(MBEDTLS_CCM_C)
4534 mbedtls_ccm_context ccm;
4535#endif /* MBEDTLS_CCM_C */
4536#if defined(MBEDTLS_GCM_C)
4537 mbedtls_gcm_context gcm;
4538#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004539#if defined(MBEDTLS_CHACHAPOLY_C)
4540 mbedtls_chachapoly_context chachapoly;
4541#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004542 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004543 psa_algorithm_t core_alg;
4544 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004545 uint8_t tag_length;
4546} aead_operation_t;
4547
Ronald Cronf95a2b12020-10-22 15:24:49 +02004548#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4549
Gilles Peskine30a9e412019-01-14 18:36:12 +01004550static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004551{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004552 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004553 {
4554#if defined(MBEDTLS_CCM_C)
4555 case PSA_ALG_CCM:
4556 mbedtls_ccm_free( &operation->ctx.ccm );
4557 break;
4558#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004559#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004560 case PSA_ALG_GCM:
4561 mbedtls_gcm_free( &operation->ctx.gcm );
4562 break;
4563#endif /* MBEDTLS_GCM_C */
4564 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004565
Ronald Cron5c522922020-11-14 16:35:34 +01004566 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004567}
4568
4569static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004570 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004571 psa_key_usage_t usage,
4572 psa_algorithm_t alg )
4573{
4574 psa_status_t status;
4575 size_t key_bits;
4576 mbedtls_cipher_id_t cipher_id;
4577
Ronald Cron5c522922020-11-14 16:35:34 +01004578 status = psa_get_and_lock_transparent_key_slot_with_policy(
4579 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004580 if( status != PSA_SUCCESS )
4581 return( status );
4582
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004583 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004584
4585 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004586 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004587 &cipher_id );
4588 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004589 {
4590 status = PSA_ERROR_NOT_SUPPORTED;
4591 goto cleanup;
4592 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004593
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004594 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004595 {
4596#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004597 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004598 operation->core_alg = PSA_ALG_CCM;
4599 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004600 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4601 * The call to mbedtls_ccm_encrypt_and_tag or
4602 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004603 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004604 {
4605 status = PSA_ERROR_INVALID_ARGUMENT;
4606 goto cleanup;
4607 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004608 mbedtls_ccm_init( &operation->ctx.ccm );
4609 status = mbedtls_to_psa_error(
4610 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004611 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004612 (unsigned int) key_bits ) );
4613 if( status != 0 )
4614 goto cleanup;
4615 break;
4616#endif /* MBEDTLS_CCM_C */
4617
4618#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004619 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004620 operation->core_alg = PSA_ALG_GCM;
4621 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004622 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4623 * The call to mbedtls_gcm_crypt_and_tag or
4624 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004625 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004626 {
4627 status = PSA_ERROR_INVALID_ARGUMENT;
4628 goto cleanup;
4629 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004630 mbedtls_gcm_init( &operation->ctx.gcm );
4631 status = mbedtls_to_psa_error(
4632 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004633 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004634 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004635 if( status != 0 )
4636 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004637 break;
4638#endif /* MBEDTLS_GCM_C */
4639
Gilles Peskine26869f22019-05-06 15:25:00 +02004640#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004641 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004642 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4643 operation->full_tag_length = 16;
4644 /* We only support the default tag length. */
4645 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004646 {
4647 status = PSA_ERROR_NOT_SUPPORTED;
4648 goto cleanup;
4649 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004650 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4651 status = mbedtls_to_psa_error(
4652 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004653 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004654 if( status != 0 )
4655 goto cleanup;
4656 break;
4657#endif /* MBEDTLS_CHACHAPOLY_C */
4658
Gilles Peskineedf9a652018-08-17 18:11:56 +02004659 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004660 status = PSA_ERROR_NOT_SUPPORTED;
4661 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004662 }
4663
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004664 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4665 {
4666 status = PSA_ERROR_INVALID_ARGUMENT;
4667 goto cleanup;
4668 }
4669 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004670
Gilles Peskineedf9a652018-08-17 18:11:56 +02004671 return( PSA_SUCCESS );
4672
4673cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004674 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004675 return( status );
4676}
4677
Ronald Croncf56a0a2020-08-04 09:51:30 +02004678psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004679 psa_algorithm_t alg,
4680 const uint8_t *nonce,
4681 size_t nonce_length,
4682 const uint8_t *additional_data,
4683 size_t additional_data_length,
4684 const uint8_t *plaintext,
4685 size_t plaintext_length,
4686 uint8_t *ciphertext,
4687 size_t ciphertext_size,
4688 size_t *ciphertext_length )
4689{
mohammad16035955c982018-04-26 00:53:03 +03004690 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004691 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004692 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004693
mohammad1603f08a5502018-06-03 15:05:47 +03004694 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004695
Ronald Croncf56a0a2020-08-04 09:51:30 +02004696 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004697 if( status != PSA_SUCCESS )
4698 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004699
Gilles Peskineedf9a652018-08-17 18:11:56 +02004700 /* For all currently supported modes, the tag is at the end of the
4701 * ciphertext. */
4702 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4703 {
4704 status = PSA_ERROR_BUFFER_TOO_SMALL;
4705 goto exit;
4706 }
4707 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004708
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004709#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004710 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004711 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004712 status = mbedtls_to_psa_error(
4713 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4714 MBEDTLS_GCM_ENCRYPT,
4715 plaintext_length,
4716 nonce, nonce_length,
4717 additional_data, additional_data_length,
4718 plaintext, ciphertext,
4719 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004720 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004721 else
4722#endif /* MBEDTLS_GCM_C */
4723#if defined(MBEDTLS_CCM_C)
4724 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004725 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004726 status = mbedtls_to_psa_error(
4727 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4728 plaintext_length,
4729 nonce, nonce_length,
4730 additional_data,
4731 additional_data_length,
4732 plaintext, ciphertext,
4733 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004734 }
mohammad16035c8845f2018-05-09 05:40:09 -07004735 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004736#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004737#if defined(MBEDTLS_CHACHAPOLY_C)
4738 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4739 {
4740 if( nonce_length != 12 || operation.tag_length != 16 )
4741 {
4742 status = PSA_ERROR_NOT_SUPPORTED;
4743 goto exit;
4744 }
4745 status = mbedtls_to_psa_error(
4746 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4747 plaintext_length,
4748 nonce,
4749 additional_data,
4750 additional_data_length,
4751 plaintext,
4752 ciphertext,
4753 tag ) );
4754 }
4755 else
4756#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004757 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004758 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004759 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004760 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004761
Gilles Peskineedf9a652018-08-17 18:11:56 +02004762 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4763 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004764
Gilles Peskineedf9a652018-08-17 18:11:56 +02004765exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004766 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004767 if( status == PSA_SUCCESS )
4768 *ciphertext_length = plaintext_length + operation.tag_length;
4769 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004770}
4771
Gilles Peskineee652a32018-06-01 19:23:52 +02004772/* Locate the tag in a ciphertext buffer containing the encrypted data
4773 * followed by the tag. Return the length of the part preceding the tag in
4774 * *plaintext_length. This is the size of the plaintext in modes where
4775 * the encrypted data has the same size as the plaintext, such as
4776 * CCM and GCM. */
4777static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4778 const uint8_t *ciphertext,
4779 size_t ciphertext_length,
4780 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004781 const uint8_t **p_tag )
4782{
4783 size_t payload_length;
4784 if( tag_length > ciphertext_length )
4785 return( PSA_ERROR_INVALID_ARGUMENT );
4786 payload_length = ciphertext_length - tag_length;
4787 if( payload_length > plaintext_size )
4788 return( PSA_ERROR_BUFFER_TOO_SMALL );
4789 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004790 return( PSA_SUCCESS );
4791}
4792
Ronald Croncf56a0a2020-08-04 09:51:30 +02004793psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004794 psa_algorithm_t alg,
4795 const uint8_t *nonce,
4796 size_t nonce_length,
4797 const uint8_t *additional_data,
4798 size_t additional_data_length,
4799 const uint8_t *ciphertext,
4800 size_t ciphertext_length,
4801 uint8_t *plaintext,
4802 size_t plaintext_size,
4803 size_t *plaintext_length )
4804{
mohammad16035955c982018-04-26 00:53:03 +03004805 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004806 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004807 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004808
Gilles Peskineee652a32018-06-01 19:23:52 +02004809 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004810
Ronald Croncf56a0a2020-08-04 09:51:30 +02004811 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004812 if( status != PSA_SUCCESS )
4813 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004814
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004815 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4816 ciphertext, ciphertext_length,
4817 plaintext_size, &tag );
4818 if( status != PSA_SUCCESS )
4819 goto exit;
4820
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004821#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004822 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004823 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004824 status = mbedtls_to_psa_error(
4825 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4826 ciphertext_length - operation.tag_length,
4827 nonce, nonce_length,
4828 additional_data,
4829 additional_data_length,
4830 tag, operation.tag_length,
4831 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004832 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004833 else
4834#endif /* MBEDTLS_GCM_C */
4835#if defined(MBEDTLS_CCM_C)
4836 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004837 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004838 status = mbedtls_to_psa_error(
4839 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4840 ciphertext_length - operation.tag_length,
4841 nonce, nonce_length,
4842 additional_data,
4843 additional_data_length,
4844 ciphertext, plaintext,
4845 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004846 }
mohammad160339574652018-06-01 04:39:53 -07004847 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004848#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004849#if defined(MBEDTLS_CHACHAPOLY_C)
4850 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4851 {
4852 if( nonce_length != 12 || operation.tag_length != 16 )
4853 {
4854 status = PSA_ERROR_NOT_SUPPORTED;
4855 goto exit;
4856 }
4857 status = mbedtls_to_psa_error(
4858 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4859 ciphertext_length - operation.tag_length,
4860 nonce,
4861 additional_data,
4862 additional_data_length,
4863 tag,
4864 ciphertext,
4865 plaintext ) );
4866 }
4867 else
4868#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004869 {
mohammad1603554faad2018-06-03 15:07:38 +03004870 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004871 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004872
Gilles Peskineedf9a652018-08-17 18:11:56 +02004873 if( status != PSA_SUCCESS && plaintext_size != 0 )
4874 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004875
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004877 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004878 if( status == PSA_SUCCESS )
4879 *plaintext_length = ciphertext_length - operation.tag_length;
4880 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004881}
4882
Gilles Peskinea0655c32018-04-30 17:06:50 +02004883
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004884
Gilles Peskine20035e32018-02-03 22:44:14 +01004885/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004886/* Generators */
4887/****************************************************************/
4888
John Durkop07cc04a2020-11-16 22:08:34 -08004889#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4890 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4891 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4892#define AT_LEAST_ONE_BUILTIN_KDF
4893#endif
4894
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895#define HKDF_STATE_INIT 0 /* no input yet */
4896#define HKDF_STATE_STARTED 1 /* got salt */
4897#define HKDF_STATE_KEYED 2 /* got key */
4898#define HKDF_STATE_OUTPUT 3 /* output started */
4899
Gilles Peskinecbe66502019-05-16 16:59:18 +02004900static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004902{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4904 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907}
4908
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004909psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004910{
4911 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004914 {
4915 /* The object has (apparently) been initialized but it is not
4916 * in use. It's ok to call abort on such an object, and there's
4917 * nothing to do. */
4918 }
4919 else
John Durkopd0321952020-10-29 21:37:36 -07004920#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004922 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004923 mbedtls_free( operation->ctx.hkdf.info );
4924 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004925 }
John Durkop07cc04a2020-11-16 22:08:34 -08004926 else
4927#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4928#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4929 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4930 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004933 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004934 if( operation->ctx.tls12_prf.seed != NULL )
4935 {
4936 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4937 operation->ctx.tls12_prf.seed_length );
4938 mbedtls_free( operation->ctx.tls12_prf.seed );
4939 }
4940
4941 if( operation->ctx.tls12_prf.label != NULL )
4942 {
4943 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4944 operation->ctx.tls12_prf.label_length );
4945 mbedtls_free( operation->ctx.tls12_prf.label );
4946 }
4947
4948 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4949
4950 /* We leave the fields Ai and output_block to be erased safely by the
4951 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004952 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004953 else
John Durkop07cc04a2020-11-16 22:08:34 -08004954#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4955 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004956 {
4957 status = PSA_ERROR_BAD_STATE;
4958 }
Janos Follath083036a2019-06-11 10:22:26 +01004959 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004960 return( status );
4961}
4962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004964 size_t *capacity)
4965{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004967 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004969 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004970 }
4971
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004972 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004973 return( PSA_SUCCESS );
4974}
4975
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004976psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004977 size_t capacity )
4978{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004982 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004983 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004984 return( PSA_SUCCESS );
4985}
4986
John Durkopd0321952020-10-29 21:37:36 -07004987#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004988/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004989 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004990static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004991 psa_algorithm_t hash_alg,
4992 uint8_t *output,
4993 size_t output_length )
4994{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004995 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004996 psa_status_t status;
4997
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004998 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4999 return( PSA_ERROR_BAD_STATE );
5000 hkdf->state = HKDF_STATE_OUTPUT;
5001
Gilles Peskinebef7f142018-07-12 17:22:21 +02005002 while( output_length != 0 )
5003 {
5004 /* Copy what remains of the current block */
5005 uint8_t n = hash_length - hkdf->offset_in_block;
5006 if( n > output_length )
5007 n = (uint8_t) output_length;
5008 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5009 output += n;
5010 output_length -= n;
5011 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005012 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005013 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005014 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005015 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005016 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005017 * object was corrupted or if this function is called directly
5018 * inside the library. */
5019 if( hkdf->block_number == 0xff )
5020 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005021
5022 /* We need a new block */
5023 ++hkdf->block_number;
5024 hkdf->offset_in_block = 0;
5025 status = psa_hmac_setup_internal( &hkdf->hmac,
5026 hkdf->prk, hash_length,
5027 hash_alg );
5028 if( status != PSA_SUCCESS )
5029 return( status );
5030 if( hkdf->block_number != 1 )
5031 {
5032 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5033 hkdf->output_block,
5034 hash_length );
5035 if( status != PSA_SUCCESS )
5036 return( status );
5037 }
5038 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5039 hkdf->info,
5040 hkdf->info_length );
5041 if( status != PSA_SUCCESS )
5042 return( status );
5043 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5044 &hkdf->block_number, 1 );
5045 if( status != PSA_SUCCESS )
5046 return( status );
5047 status = psa_hmac_finish_internal( &hkdf->hmac,
5048 hkdf->output_block,
5049 sizeof( hkdf->output_block ) );
5050 if( status != PSA_SUCCESS )
5051 return( status );
5052 }
5053
5054 return( PSA_SUCCESS );
5055}
John Durkop07cc04a2020-11-16 22:08:34 -08005056#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005057
John Durkop07cc04a2020-11-16 22:08:34 -08005058#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5059 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005060static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5061 psa_tls12_prf_key_derivation_t *tls12_prf,
5062 psa_algorithm_t alg )
5063{
5064 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005065 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005066 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5067 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005068
Janos Follath7742fee2019-06-17 12:58:10 +01005069 /* We can't be wanting more output after block 0xff, otherwise
5070 * the capacity check in psa_key_derivation_output_bytes() would have
5071 * prevented this call. It could happen only if the operation
5072 * object was corrupted or if this function is called directly
5073 * inside the library. */
5074 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005075 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005076
5077 /* We need a new block */
5078 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005079 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005080
5081 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5082 *
5083 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5084 *
5085 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5086 * HMAC_hash(secret, A(2) + seed) +
5087 * HMAC_hash(secret, A(3) + seed) + ...
5088 *
5089 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005090 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005091 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005092 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005093 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005094 * is currently extracted as `output_block` and where i is
5095 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005096 */
5097
Janos Follathea29bfb2019-06-19 12:21:20 +01005098 /* Save the hash context before using it, to preserve the hash state with
5099 * only the inner padding in it. We need this, because inner padding depends
5100 * on the key (secret in the RFC's terminology). */
5101 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5102 if( status != PSA_SUCCESS )
5103 goto cleanup;
5104
5105 /* Calculate A(i) where i = tls12_prf->block_number. */
5106 if( tls12_prf->block_number == 1 )
5107 {
5108 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5109 * the variable seed and in this instance means it in the context of the
5110 * P_hash function, where seed = label + seed.) */
5111 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5112 tls12_prf->label, tls12_prf->label_length );
5113 if( status != PSA_SUCCESS )
5114 goto cleanup;
5115 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5116 tls12_prf->seed, tls12_prf->seed_length );
5117 if( status != PSA_SUCCESS )
5118 goto cleanup;
5119 }
5120 else
5121 {
5122 /* A(i) = HMAC_hash(secret, A(i-1)) */
5123 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5124 tls12_prf->Ai, hash_length );
5125 if( status != PSA_SUCCESS )
5126 goto cleanup;
5127 }
5128
5129 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5130 tls12_prf->Ai, hash_length );
5131 if( status != PSA_SUCCESS )
5132 goto cleanup;
5133 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5134 if( status != PSA_SUCCESS )
5135 goto cleanup;
5136
5137 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5138 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5139 tls12_prf->Ai, hash_length );
5140 if( status != PSA_SUCCESS )
5141 goto cleanup;
5142 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5143 tls12_prf->label, tls12_prf->label_length );
5144 if( status != PSA_SUCCESS )
5145 goto cleanup;
5146 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5147 tls12_prf->seed, tls12_prf->seed_length );
5148 if( status != PSA_SUCCESS )
5149 goto cleanup;
5150 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5151 tls12_prf->output_block, hash_length );
5152 if( status != PSA_SUCCESS )
5153 goto cleanup;
5154 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5155 if( status != PSA_SUCCESS )
5156 goto cleanup;
5157
Janos Follath7742fee2019-06-17 12:58:10 +01005158
5159cleanup:
5160
Janos Follathea29bfb2019-06-19 12:21:20 +01005161 cleanup_status = psa_hash_abort( &backup );
5162 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5163 status = cleanup_status;
5164
5165 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005166}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005167
Janos Follath844eb0e2019-06-19 12:10:49 +01005168static psa_status_t psa_key_derivation_tls12_prf_read(
5169 psa_tls12_prf_key_derivation_t *tls12_prf,
5170 psa_algorithm_t alg,
5171 uint8_t *output,
5172 size_t output_length )
5173{
5174 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005175 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005176 psa_status_t status;
5177 uint8_t offset, length;
5178
5179 while( output_length != 0 )
5180 {
5181 /* Check if we have fully processed the current block. */
5182 if( tls12_prf->left_in_block == 0 )
5183 {
5184 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5185 alg );
5186 if( status != PSA_SUCCESS )
5187 return( status );
5188
5189 continue;
5190 }
5191
5192 if( tls12_prf->left_in_block > output_length )
5193 length = (uint8_t) output_length;
5194 else
5195 length = tls12_prf->left_in_block;
5196
5197 offset = hash_length - tls12_prf->left_in_block;
5198 memcpy( output, tls12_prf->output_block + offset, length );
5199 output += length;
5200 output_length -= length;
5201 tls12_prf->left_in_block -= length;
5202 }
5203
5204 return( PSA_SUCCESS );
5205}
John Durkop07cc04a2020-11-16 22:08:34 -08005206#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5207 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005208
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005209psa_status_t psa_key_derivation_output_bytes(
5210 psa_key_derivation_operation_t *operation,
5211 uint8_t *output,
5212 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213{
5214 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005218 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005220 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005221 }
5222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005226 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005228 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005229 goto exit;
5230 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005233 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005234 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005235 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5236 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005237 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005238 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005239 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005242
John Durkopd0321952020-10-29 21:37:36 -07005243#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005244 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005245 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005248 output, output_length );
5249 }
Janos Follathadbec812019-06-14 11:05:39 +01005250 else
John Durkop07cc04a2020-11-16 22:08:34 -08005251#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5252#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5253 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005254 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005255 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005256 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005257 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005258 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005259 output_length );
5260 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005261 else
John Durkop07cc04a2020-11-16 22:08:34 -08005262#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5263 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005265 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 return( PSA_ERROR_BAD_STATE );
5267 }
5268
5269exit:
5270 if( status != PSA_SUCCESS )
5271 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005272 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005273 * This allows us to differentiate between exhausted operations and
5274 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5275 * operations. */
5276 psa_algorithm_t alg = operation->alg;
5277 psa_key_derivation_abort( operation );
5278 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005279 memset( output, '!', output_length );
5280 }
5281 return( status );
5282}
5283
David Brown7807bf72021-02-09 16:28:23 -07005284#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005285static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5286{
5287 if( data_size >= 8 )
5288 mbedtls_des_key_set_parity( data );
5289 if( data_size >= 16 )
5290 mbedtls_des_key_set_parity( data + 8 );
5291 if( data_size >= 24 )
5292 mbedtls_des_key_set_parity( data + 16 );
5293}
David Brown7807bf72021-02-09 16:28:23 -07005294#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005295
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005296static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005297 psa_key_slot_t *slot,
5298 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005299 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005300{
5301 uint8_t *data = NULL;
5302 size_t bytes = PSA_BITS_TO_BYTES( bits );
5303 psa_status_t status;
5304
Gilles Peskine8e338702019-07-30 20:06:31 +02005305 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005306 return( PSA_ERROR_INVALID_ARGUMENT );
5307 if( bits % 8 != 0 )
5308 return( PSA_ERROR_INVALID_ARGUMENT );
5309 data = mbedtls_calloc( 1, bytes );
5310 if( data == NULL )
5311 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314 if( status != PSA_SUCCESS )
5315 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005316#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005317 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005318 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005319#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005320
5321 status = psa_allocate_buffer_to_slot( slot, bytes );
5322 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005323 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005324
Ronald Cronbf33c932020-11-28 18:06:53 +01005325 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005326 psa_key_attributes_t attributes = {
5327 .core = slot->attr
5328 };
5329
Ronald Cronbf33c932020-11-28 18:06:53 +01005330 status = psa_driver_wrapper_import_key( &attributes,
5331 data, bytes,
5332 slot->key.data,
5333 slot->key.bytes,
5334 &slot->key.bytes, &bits );
5335 if( bits != slot->attr.bits )
5336 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005337
5338exit:
5339 mbedtls_free( data );
5340 return( status );
5341}
5342
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005343psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005345 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005346{
5347 psa_status_t status;
5348 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005349 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005350
Ronald Cron81709fc2020-11-14 12:10:32 +01005351 *key = MBEDTLS_SVC_KEY_ID_INIT;
5352
Gilles Peskine0f84d622019-09-12 19:03:13 +02005353 /* Reject any attempt to create a zero-length key so that we don't
5354 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5355 if( psa_get_key_bits( attributes ) == 0 )
5356 return( PSA_ERROR_INVALID_ARGUMENT );
5357
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005358 if( ! operation->can_output_key )
5359 return( PSA_ERROR_NOT_PERMITTED );
5360
Ronald Cron81709fc2020-11-14 12:10:32 +01005361 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5362 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5364 if( driver != NULL )
5365 {
5366 /* Deriving a key in a secure element is not implemented yet. */
5367 status = PSA_ERROR_NOT_SUPPORTED;
5368 }
5369#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005370 if( status == PSA_SUCCESS )
5371 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005372 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005373 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005374 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005375 }
5376 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005377 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005378 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005379 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005380
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005381 return( status );
5382}
5383
Gilles Peskineeab56e42018-07-12 17:12:33 +02005384
5385
5386/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005387/* Key derivation */
5388/****************************************************************/
5389
Steven Cooreman932ffb72021-02-15 12:14:32 +01005390#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005392 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005393 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005394{
John Durkop07cc04a2020-11-16 22:08:34 -08005395 int is_kdf_alg_supported;
5396
Janos Follath5fe19732019-06-20 15:09:30 +01005397 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5398 * initialisers for this union leave some bytes unspecified.) */
5399 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5400
Gilles Peskine969c5d62019-01-16 15:53:06 +01005401 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005402#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005403 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5404 is_kdf_alg_supported = 1;
5405 else
5406#endif
5407#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5408 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5409 is_kdf_alg_supported = 1;
5410 else
5411#endif
5412#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5413 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5414 is_kdf_alg_supported = 1;
5415 else
5416#endif
5417 is_kdf_alg_supported = 0;
5418
5419 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005420 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005421 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005422 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005423 if( hash_size == 0 )
5424 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5426 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005427 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005428 {
5429 return( PSA_ERROR_NOT_SUPPORTED );
5430 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005432 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005433 }
John Durkop07cc04a2020-11-16 22:08:34 -08005434
5435 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436}
John Durkop07cc04a2020-11-16 22:08:34 -08005437#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005438
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005439psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005440 psa_algorithm_t alg )
5441{
5442 psa_status_t status;
5443
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005444 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005445 return( PSA_ERROR_BAD_STATE );
5446
Gilles Peskine6843c292019-01-18 16:44:49 +01005447 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5448 return( PSA_ERROR_INVALID_ARGUMENT );
5449 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005450 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005451#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005452 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005453 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005454#else
5455 return( PSA_ERROR_NOT_SUPPORTED );
5456#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005457 }
5458 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5459 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005460#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005461 status = psa_key_derivation_setup_kdf( operation, 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 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466 else
5467 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005468
5469 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005470 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005471 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005472}
5473
John Durkopd0321952020-10-29 21:37:36 -07005474#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005475static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005476 psa_algorithm_t hash_alg,
5477 psa_key_derivation_step_t step,
5478 const uint8_t *data,
5479 size_t data_length )
5480{
5481 psa_status_t status;
5482 switch( step )
5483 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005484 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005485 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005486 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005487 status = psa_hmac_setup_internal( &hkdf->hmac,
5488 data, data_length,
5489 hash_alg );
5490 if( status != PSA_SUCCESS )
5491 return( status );
5492 hkdf->state = HKDF_STATE_STARTED;
5493 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005494 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005495 /* If no salt was provided, use an empty salt. */
5496 if( hkdf->state == HKDF_STATE_INIT )
5497 {
5498 status = psa_hmac_setup_internal( &hkdf->hmac,
5499 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005500 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005501 if( status != PSA_SUCCESS )
5502 return( status );
5503 hkdf->state = HKDF_STATE_STARTED;
5504 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005505 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005506 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005507 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5508 data, data_length );
5509 if( status != PSA_SUCCESS )
5510 return( status );
5511 status = psa_hmac_finish_internal( &hkdf->hmac,
5512 hkdf->prk,
5513 sizeof( hkdf->prk ) );
5514 if( status != PSA_SUCCESS )
5515 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005516 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005517 hkdf->block_number = 0;
5518 hkdf->state = HKDF_STATE_KEYED;
5519 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005520 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005521 if( hkdf->state == HKDF_STATE_OUTPUT )
5522 return( PSA_ERROR_BAD_STATE );
5523 if( hkdf->info_set )
5524 return( PSA_ERROR_BAD_STATE );
5525 hkdf->info_length = data_length;
5526 if( data_length != 0 )
5527 {
5528 hkdf->info = mbedtls_calloc( 1, data_length );
5529 if( hkdf->info == NULL )
5530 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5531 memcpy( hkdf->info, data, data_length );
5532 }
5533 hkdf->info_set = 1;
5534 return( PSA_SUCCESS );
5535 default:
5536 return( PSA_ERROR_INVALID_ARGUMENT );
5537 }
5538}
John Durkop07cc04a2020-11-16 22:08:34 -08005539#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005540
John Durkop07cc04a2020-11-16 22:08:34 -08005541#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5542 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005543static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5544 const uint8_t *data,
5545 size_t data_length )
5546{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005547 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005548 return( PSA_ERROR_BAD_STATE );
5549
Janos Follathd6dce9f2019-07-04 09:11:38 +01005550 if( data_length != 0 )
5551 {
5552 prf->seed = mbedtls_calloc( 1, data_length );
5553 if( prf->seed == NULL )
5554 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005555
Janos Follathd6dce9f2019-07-04 09:11:38 +01005556 memcpy( prf->seed, data, data_length );
5557 prf->seed_length = data_length;
5558 }
Janos Follathf08e2652019-06-13 09:05:41 +01005559
Gilles Peskine43f958b2020-12-13 14:55:14 +01005560 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005561
5562 return( PSA_SUCCESS );
5563}
5564
Janos Follath81550542019-06-13 14:26:34 +01005565static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5566 psa_algorithm_t hash_alg,
5567 const uint8_t *data,
5568 size_t data_length )
5569{
5570 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005571 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005572 return( PSA_ERROR_BAD_STATE );
5573
5574 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5575 if( status != PSA_SUCCESS )
5576 return( status );
5577
Gilles Peskine43f958b2020-12-13 14:55:14 +01005578 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005579
5580 return( PSA_SUCCESS );
5581}
5582
Janos Follath63028dd2019-06-13 09:15:47 +01005583static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5584 const uint8_t *data,
5585 size_t data_length )
5586{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005587 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005588 return( PSA_ERROR_BAD_STATE );
5589
Janos Follathd6dce9f2019-07-04 09:11:38 +01005590 if( data_length != 0 )
5591 {
5592 prf->label = mbedtls_calloc( 1, data_length );
5593 if( prf->label == NULL )
5594 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005595
Janos Follathd6dce9f2019-07-04 09:11:38 +01005596 memcpy( prf->label, data, data_length );
5597 prf->label_length = data_length;
5598 }
Janos Follath63028dd2019-06-13 09:15:47 +01005599
Gilles Peskine43f958b2020-12-13 14:55:14 +01005600 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005601
5602 return( PSA_SUCCESS );
5603}
5604
Janos Follathb03233e2019-06-11 15:30:30 +01005605static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5606 psa_algorithm_t hash_alg,
5607 psa_key_derivation_step_t step,
5608 const uint8_t *data,
5609 size_t data_length )
5610{
Janos Follathb03233e2019-06-11 15:30:30 +01005611 switch( step )
5612 {
Janos Follathf08e2652019-06-13 09:05:41 +01005613 case PSA_KEY_DERIVATION_INPUT_SEED:
5614 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005615 case PSA_KEY_DERIVATION_INPUT_SECRET:
5616 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005617 case PSA_KEY_DERIVATION_INPUT_LABEL:
5618 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005619 default:
5620 return( PSA_ERROR_INVALID_ARGUMENT );
5621 }
5622}
John Durkop07cc04a2020-11-16 22:08:34 -08005623#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5624 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5625
5626#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5627static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5628 psa_tls12_prf_key_derivation_t *prf,
5629 psa_algorithm_t hash_alg,
5630 const uint8_t *data,
5631 size_t data_length )
5632{
5633 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005634 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005635 uint8_t *cur = pms;
5636
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005637 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005638 return( PSA_ERROR_INVALID_ARGUMENT );
5639
5640 /* Quoting RFC 4279, Section 2:
5641 *
5642 * The premaster secret is formed as follows: if the PSK is N octets
5643 * long, concatenate a uint16 with the value N, N zero octets, a second
5644 * uint16 with the value N, and the PSK itself.
5645 */
5646
5647 *cur++ = ( data_length >> 8 ) & 0xff;
5648 *cur++ = ( data_length >> 0 ) & 0xff;
5649 memset( cur, 0, data_length );
5650 cur += data_length;
5651 *cur++ = pms[0];
5652 *cur++ = pms[1];
5653 memcpy( cur, data, data_length );
5654 cur += data_length;
5655
5656 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5657
5658 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5659 return( status );
5660}
Janos Follath6660f0e2019-06-17 08:44:03 +01005661
5662static psa_status_t psa_tls12_prf_psk_to_ms_input(
5663 psa_tls12_prf_key_derivation_t *prf,
5664 psa_algorithm_t hash_alg,
5665 psa_key_derivation_step_t step,
5666 const uint8_t *data,
5667 size_t data_length )
5668{
5669 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005670 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005671 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5672 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005673 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005674
5675 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5676}
John Durkop07cc04a2020-11-16 22:08:34 -08005677#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005678
Gilles Peskineb8965192019-09-24 16:21:10 +02005679/** Check whether the given key type is acceptable for the given
5680 * input step of a key derivation.
5681 *
5682 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5683 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5684 * Both secret and non-secret inputs can alternatively have the type
5685 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5686 * that the input was passed as a buffer rather than via a key object.
5687 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005688static int psa_key_derivation_check_input_type(
5689 psa_key_derivation_step_t step,
5690 psa_key_type_t key_type )
5691{
5692 switch( step )
5693 {
5694 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005695 if( key_type == PSA_KEY_TYPE_DERIVE )
5696 return( PSA_SUCCESS );
5697 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005698 return( PSA_SUCCESS );
5699 break;
5700 case PSA_KEY_DERIVATION_INPUT_LABEL:
5701 case PSA_KEY_DERIVATION_INPUT_SALT:
5702 case PSA_KEY_DERIVATION_INPUT_INFO:
5703 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005704 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5705 return( PSA_SUCCESS );
5706 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005707 return( PSA_SUCCESS );
5708 break;
5709 }
5710 return( PSA_ERROR_INVALID_ARGUMENT );
5711}
5712
Janos Follathb80a94e2019-06-12 15:54:46 +01005713static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005714 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005715 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005716 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005717 const uint8_t *data,
5718 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005720 psa_status_t status;
5721 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5722
5723 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005724 if( status != PSA_SUCCESS )
5725 goto exit;
5726
John Durkopd0321952020-10-29 21:37:36 -07005727#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005728 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005732 step, data, data_length );
5733 }
John Durkop07cc04a2020-11-16 22:08:34 -08005734 else
5735#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5736#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5737 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005738 {
Janos Follathb03233e2019-06-11 15:30:30 +01005739 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5740 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5741 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005742 }
John Durkop07cc04a2020-11-16 22:08:34 -08005743 else
5744#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5745#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5746 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005747 {
5748 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5749 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5750 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005751 }
5752 else
John Durkop07cc04a2020-11-16 22:08:34 -08005753#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005754 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005756 (void) data;
5757 (void) data_length;
5758 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 return( PSA_ERROR_BAD_STATE );
5760 }
5761
Gilles Peskine224b0d62019-09-23 18:13:17 +02005762exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005763 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005764 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005765 return( status );
5766}
5767
Janos Follath51f4a0f2019-06-14 11:35:55 +01005768psa_status_t psa_key_derivation_input_bytes(
5769 psa_key_derivation_operation_t *operation,
5770 psa_key_derivation_step_t step,
5771 const uint8_t *data,
5772 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005773{
Gilles Peskineb8965192019-09-24 16:21:10 +02005774 return( psa_key_derivation_input_internal( operation, step,
5775 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005776 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005777}
5778
Janos Follath51f4a0f2019-06-14 11:35:55 +01005779psa_status_t psa_key_derivation_input_key(
5780 psa_key_derivation_operation_t *operation,
5781 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005782 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005783{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005784 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005785 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005787
Ronald Cron5c522922020-11-14 16:35:34 +01005788 status = psa_get_and_lock_transparent_key_slot_with_policy(
5789 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005791 {
5792 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005793 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005794 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005795
5796 /* Passing a key object as a SECRET input unlocks the permission
5797 * to output to a key object. */
5798 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5799 operation->can_output_key = 1;
5800
Ronald Cronf95a2b12020-10-22 15:24:49 +02005801 status = psa_key_derivation_input_internal( operation,
5802 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005803 slot->key.data,
5804 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005805
Ronald Cron5c522922020-11-14 16:35:34 +01005806 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005807
Ronald Cron5c522922020-11-14 16:35:34 +01005808 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005809}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005810
5811
5812
5813/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005814/* Key agreement */
5815/****************************************************************/
5816
John Durkop6ba40d12020-11-10 08:50:04 -08005817#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005818static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5819 size_t peer_key_length,
5820 const mbedtls_ecp_keypair *our_key,
5821 uint8_t *shared_secret,
5822 size_t shared_secret_size,
5823 size_t *shared_secret_length )
5824{
Steven Cooremand4867872020-08-05 16:31:39 +02005825 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005826 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005827 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005828 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005829 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005830 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005831
Ronald Cron90857082020-11-25 14:58:33 +01005832 status = mbedtls_psa_ecp_load_representation(
5833 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005834 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005835 peer_key,
5836 peer_key_length,
5837 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005838 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005839 goto exit;
5840
Jaeden Amero97271b32019-01-10 19:38:51 +00005841 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005842 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005843 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005844 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005845 status = mbedtls_to_psa_error(
5846 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5847 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005848 goto exit;
5849
Jaeden Amero97271b32019-01-10 19:38:51 +00005850 status = mbedtls_to_psa_error(
5851 mbedtls_ecdh_calc_secret( &ecdh,
5852 shared_secret_length,
5853 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005854 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005855 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005856 if( status != PSA_SUCCESS )
5857 goto exit;
5858 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5859 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005860
5861exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005862 if( status != PSA_SUCCESS )
5863 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005864 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005865 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005866 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005867
Jaeden Amero97271b32019-01-10 19:38:51 +00005868 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005869}
John Durkop6ba40d12020-11-10 08:50:04 -08005870#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005871
Gilles Peskine01d718c2018-09-18 12:01:02 +02005872#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5873
Gilles Peskine0216fe12019-04-11 21:23:21 +02005874static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5875 psa_key_slot_t *private_key,
5876 const uint8_t *peer_key,
5877 size_t peer_key_length,
5878 uint8_t *shared_secret,
5879 size_t shared_secret_size,
5880 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005881{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005882 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005883 {
John Durkop6ba40d12020-11-10 08:50:04 -08005884#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005885 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005886 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005887 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005888 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005889 psa_status_t status = mbedtls_psa_ecp_load_representation(
5890 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005891 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005892 private_key->key.data,
5893 private_key->key.bytes,
5894 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005895 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005896 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005897 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005898 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005899 shared_secret, shared_secret_size,
5900 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005901 mbedtls_ecp_keypair_free( ecp );
5902 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005903 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005904#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005905 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005906 (void) private_key;
5907 (void) peer_key;
5908 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005909 (void) shared_secret;
5910 (void) shared_secret_size;
5911 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005912 return( PSA_ERROR_NOT_SUPPORTED );
5913 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005914}
5915
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005916/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005917 * to potentially free embedded data structures and wipe confidential data.
5918 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005919static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005920 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005921 psa_key_slot_t *private_key,
5922 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005923 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005924{
5925 psa_status_t status;
5926 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5927 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005928 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005929
5930 /* Step 1: run the secret agreement algorithm to generate the shared
5931 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005932 status = psa_key_agreement_raw_internal( ka_alg,
5933 private_key,
5934 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005935 shared_secret,
5936 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005937 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005938 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005939 goto exit;
5940
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005941 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005942 * the shared secret. A shared secret is permitted wherever a key
5943 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005944 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005945 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005946 shared_secret,
5947 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005949 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005950 return( status );
5951}
5952
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005953psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005954 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005955 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005956 const uint8_t *peer_key,
5957 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005958{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005959 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005960 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005961 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005963 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005964 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005965 status = psa_get_and_lock_transparent_key_slot_with_policy(
5966 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005967 if( status != PSA_SUCCESS )
5968 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005969 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005970 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005971 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005972 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005973 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005974 else
5975 {
5976 /* If a private key has been added as SECRET, we allow the derived
5977 * key material to be used as a key in PSA Crypto. */
5978 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5979 operation->can_output_key = 1;
5980 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005981
Ronald Cron5c522922020-11-14 16:35:34 +01005982 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005983
Ronald Cron5c522922020-11-14 16:35:34 +01005984 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005985}
5986
Gilles Peskinebe697d82019-05-16 18:00:41 +02005987psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005988 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005989 const uint8_t *peer_key,
5990 size_t peer_key_length,
5991 uint8_t *output,
5992 size_t output_size,
5993 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005994{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005995 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005996 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005997 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005998
5999 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6000 {
6001 status = PSA_ERROR_INVALID_ARGUMENT;
6002 goto exit;
6003 }
Ronald Cron5c522922020-11-14 16:35:34 +01006004 status = psa_get_and_lock_transparent_key_slot_with_policy(
6005 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006006 if( status != PSA_SUCCESS )
6007 goto exit;
6008
6009 status = psa_key_agreement_raw_internal( alg, slot,
6010 peer_key, peer_key_length,
6011 output, output_size,
6012 output_length );
6013
6014exit:
6015 if( status != PSA_SUCCESS )
6016 {
6017 /* If an error happens and is not handled properly, the output
6018 * may be used as a key to protect sensitive data. Arrange for such
6019 * a key to be random, which is likely to result in decryption or
6020 * verification errors. This is better than filling the buffer with
6021 * some constant data such as zeros, which would result in the data
6022 * being protected with a reproducible, easily knowable key.
6023 */
6024 psa_generate_random( output, output_size );
6025 *output_length = output_size;
6026 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006027
Ronald Cron5c522922020-11-14 16:35:34 +01006028 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006029
Ronald Cron5c522922020-11-14 16:35:34 +01006030 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006031}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006032
6033
Gilles Peskine30524eb2020-11-13 17:02:26 +01006034
Gilles Peskine86a440b2018-11-12 18:39:40 +01006035/****************************************************************/
6036/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006037/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006038
Gilles Peskine30524eb2020-11-13 17:02:26 +01006039/** Initialize the PSA random generator.
6040 */
6041static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6042{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006043#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6044 memset( rng, 0, sizeof( *rng ) );
6045#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6046
Gilles Peskine30524eb2020-11-13 17:02:26 +01006047 /* Set default configuration if
6048 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6049 if( rng->entropy_init == NULL )
6050 rng->entropy_init = mbedtls_entropy_init;
6051 if( rng->entropy_free == NULL )
6052 rng->entropy_free = mbedtls_entropy_free;
6053
6054 rng->entropy_init( &rng->entropy );
6055#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6056 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6057 /* The PSA entropy injection feature depends on using NV seed as an entropy
6058 * source. Add NV seed as an entropy source for PSA entropy injection. */
6059 mbedtls_entropy_add_source( &rng->entropy,
6060 mbedtls_nv_seed_poll, NULL,
6061 MBEDTLS_ENTROPY_BLOCK_SIZE,
6062 MBEDTLS_ENTROPY_SOURCE_STRONG );
6063#endif
6064
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006065 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006066#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006067}
6068
6069/** Deinitialize the PSA random generator.
6070 */
6071static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6072{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006073#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6074 memset( rng, 0, sizeof( *rng ) );
6075#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006076 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006077 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006078#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006079}
6080
6081/** Seed the PSA random generator.
6082 */
6083static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6084{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006085#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6086 /* Do nothing: the external RNG seeds itself. */
6087 (void) rng;
6088 return( PSA_SUCCESS );
6089#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006090 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006091 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6092 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006093 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006094#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006095}
6096
Gilles Peskinee59236f2018-01-27 23:32:46 +01006097psa_status_t psa_generate_random( uint8_t *output,
6098 size_t output_size )
6099{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006100 GUARD_MODULE_INITIALIZED;
6101
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006102#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6103
6104 size_t output_length = 0;
6105 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6106 output, output_size,
6107 &output_length );
6108 if( status != PSA_SUCCESS )
6109 return( status );
6110 /* Breaking up a request into smaller chunks is currently not supported
6111 * for the extrernal RNG interface. */
6112 if( output_length != output_size )
6113 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6114 return( PSA_SUCCESS );
6115
6116#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6117
Gilles Peskine71ddab92021-01-04 21:01:07 +01006118 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006119 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006120 size_t request_size =
6121 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6122 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6123 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006124 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6125 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006126 if( ret != 0 )
6127 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006128 output_size -= request_size;
6129 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006130 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006131 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006132#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006133}
6134
Gilles Peskine8814fc42020-12-14 15:33:44 +01006135/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006136 *
6137 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6138 * `psa_generate_random(...)`. The state parameter is ignored since the
6139 * PSA API doesn't support passing an explicit state.
6140 *
6141 * In the non-external case, psa_generate_random() calls an
6142 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6143 * and semantics as mbedtls_psa_get_random(). As an optimization,
6144 * instead of doing this back-and-forth between the PSA API and the
6145 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6146 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006147 */
6148#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6149int mbedtls_psa_get_random( void *p_rng,
6150 unsigned char *output,
6151 size_t output_size )
6152{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006153 /* This function takes a pointer to the RNG state because that's what
6154 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6155 * its own state internally and doesn't let the caller access that state.
6156 * So we just ignore the state parameter, and in practice we'll pass
6157 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006158 (void) p_rng;
6159 psa_status_t status = psa_generate_random( output, output_size );
6160 if( status == PSA_SUCCESS )
6161 return( 0 );
6162 else
6163 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6164}
6165#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6166
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006167#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6168#include "mbedtls/entropy_poll.h"
6169
Gilles Peskine7228da22019-07-15 11:06:15 +02006170psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006171 size_t seed_size )
6172{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006173 if( global_data.initialized )
6174 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006175
6176 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6177 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6178 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6179 return( PSA_ERROR_INVALID_ARGUMENT );
6180
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006181 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006182}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006183#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006184
Ronald Cron3772afe2021-02-08 16:10:05 +01006185/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006186 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006187 * \param type The key type
6188 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006189 *
6190 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006191 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006192 * \retval #PSA_ERROR_INVALID_ARGUMENT
6193 * The size in bits of the key is not valid.
6194 * \retval #PSA_ERROR_NOT_SUPPORTED
6195 * The type and/or the size in bits of the key or the combination of
6196 * the two is not supported.
6197 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006198static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6199 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006201 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006202
Gilles Peskinee59236f2018-01-27 23:32:46 +01006203 if( key_type_is_raw_bytes( type ) )
6204 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006205 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006206 if( status != PSA_SUCCESS )
6207 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006208 }
6209 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006210#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006211 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6212 {
6213 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6214 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006215
Ronald Cron01b2aba2020-10-05 09:42:02 +02006216 /* Accept only byte-aligned keys, for the same reasons as
6217 * in psa_import_rsa_key(). */
6218 if( bits % 8 != 0 )
6219 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006220 }
6221 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006222#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006223
Ronald Cron5c4d3862020-12-07 11:07:24 +01006224#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006225 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6226 {
Ronald Crond81ab562021-02-16 09:01:16 +01006227 /* To avoid empty block, return successfully here. */
6228 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006229 }
6230 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006231#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006232 {
6233 return( PSA_ERROR_NOT_SUPPORTED );
6234 }
6235
6236 return( PSA_SUCCESS );
6237}
6238
Ronald Cron55ed0592020-10-05 10:30:40 +02006239psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006240 const psa_key_attributes_t *attributes,
6241 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006242{
6243 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006244 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006245
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006246 if( ( attributes->domain_parameters == NULL ) &&
6247 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006248 return( PSA_ERROR_INVALID_ARGUMENT );
6249
Ronald Cron01b2aba2020-10-05 09:42:02 +02006250 if( key_type_is_raw_bytes( type ) )
6251 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006252 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006253 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006255
David Brown12ca5032021-02-11 11:02:00 -07006256#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006257 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006258 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006259#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006260 }
6261 else
6262
John Durkop07cc04a2020-11-16 22:08:34 -08006263#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006264 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006265 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006266 return( mbedtls_psa_rsa_generate_key( attributes,
6267 key_buffer,
6268 key_buffer_size,
6269 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270 }
6271 else
John Durkop07cc04a2020-11-16 22:08:34 -08006272#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273
John Durkop9814fa22020-11-04 12:28:15 -08006274#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006275 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006276 {
Ronald Cron7023db52020-11-20 18:17:42 +01006277 return( mbedtls_psa_ecp_generate_key( attributes,
6278 key_buffer,
6279 key_buffer_size,
6280 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006281 }
6282 else
John Durkop9814fa22020-11-04 12:28:15 -08006283#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006284 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006285 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006286 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006287 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006288
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006289 return( PSA_SUCCESS );
6290}
Darryl Green0c6575a2018-11-07 16:05:30 +00006291
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006292psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006293 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006294{
6295 psa_status_t status;
6296 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006297 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006298 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006299
Ronald Cron81709fc2020-11-14 12:10:32 +01006300 *key = MBEDTLS_SVC_KEY_ID_INIT;
6301
Gilles Peskine0f84d622019-09-12 19:03:13 +02006302 /* Reject any attempt to create a zero-length key so that we don't
6303 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6304 if( psa_get_key_bits( attributes ) == 0 )
6305 return( PSA_ERROR_INVALID_ARGUMENT );
6306
Ronald Cron81709fc2020-11-14 12:10:32 +01006307 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6308 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006309 if( status != PSA_SUCCESS )
6310 goto exit;
6311
Ronald Cron977c2472020-10-13 08:32:21 +02006312 /* In the case of a transparent key or an opaque key stored in local
6313 * storage (thus not in the case of generating a key in a secure element
6314 * or cryptoprocessor with storage), we have to allocate a buffer to
6315 * hold the generated key material. */
6316 if( slot->key.data == NULL )
6317 {
6318 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6319 PSA_KEY_LOCATION_LOCAL_STORAGE )
6320 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006321 status = psa_validate_key_type_and_size_for_key_generation(
6322 attributes->core.type, attributes->core.bits );
6323 if( status != PSA_SUCCESS )
6324 goto exit;
6325
6326 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6327 attributes->core.type,
6328 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006329 }
6330 else
6331 {
6332 status = psa_driver_wrapper_get_key_buffer_size(
6333 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006334 if( status != PSA_SUCCESS )
6335 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006336 }
Ronald Cron977c2472020-10-13 08:32:21 +02006337
6338 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6339 if( status != PSA_SUCCESS )
6340 goto exit;
6341 }
6342
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006343 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006344 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006345
Ronald Cron2b56bc82020-10-05 10:02:26 +02006346 if( status != PSA_SUCCESS )
6347 psa_remove_key_data_from_memory( slot );
6348
Gilles Peskine11792082019-08-06 18:36:36 +02006349exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006350 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006351 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006352 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006353 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006354
Darryl Green0c6575a2018-11-07 16:05:30 +00006355 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006356}
6357
Gilles Peskinee59236f2018-01-27 23:32:46 +01006358/****************************************************************/
6359/* Module setup */
6360/****************************************************************/
6361
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006362#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006363psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6364 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6365 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6366{
6367 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6368 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006369 global_data.rng.entropy_init = entropy_init;
6370 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006371 return( PSA_SUCCESS );
6372}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006373#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006374
Gilles Peskinee59236f2018-01-27 23:32:46 +01006375void mbedtls_psa_crypto_free( void )
6376{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006377 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006378 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6379 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006380 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006381 }
6382 /* Wipe all remaining data, including configuration.
6383 * In particular, this sets all state indicator to the value
6384 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006385 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006387 /* Unregister all secure element drivers, so that we restart from
6388 * a pristine state. */
6389 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006390#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006391}
6392
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006393#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6394/** Recover a transaction that was interrupted by a power failure.
6395 *
6396 * This function is called during initialization, before psa_crypto_init()
6397 * returns. If this function returns a failure status, the initialization
6398 * fails.
6399 */
6400static psa_status_t psa_crypto_recover_transaction(
6401 const psa_crypto_transaction_t *transaction )
6402{
6403 switch( transaction->unknown.type )
6404 {
6405 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6406 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006407 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006408 * is implemented.
6409 * https://github.com/ARMmbed/mbed-crypto/issues/218
6410 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006411 default:
6412 /* We found an unsupported transaction in the storage.
6413 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006414 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006415 }
6416}
6417#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6418
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419psa_status_t psa_crypto_init( void )
6420{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006421 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422
Gilles Peskinec6b69072018-11-20 21:42:52 +01006423 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006424 if( global_data.initialized != 0 )
6425 return( PSA_SUCCESS );
6426
Gilles Peskine30524eb2020-11-13 17:02:26 +01006427 /* Initialize and seed the random generator. */
6428 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006429 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006430 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006431 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006433 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006434
Gilles Peskine66fb1262018-12-10 16:29:04 +01006435 status = psa_initialize_key_slots( );
6436 if( status != PSA_SUCCESS )
6437 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006438
Gilles Peskined9348f22019-10-01 15:22:29 +02006439#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6440 status = psa_init_all_se_drivers( );
6441 if( status != PSA_SUCCESS )
6442 goto exit;
6443#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6444
Gilles Peskine4b734222019-07-24 15:56:31 +02006445#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006446 status = psa_crypto_load_transaction( );
6447 if( status == PSA_SUCCESS )
6448 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006449 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6450 if( status != PSA_SUCCESS )
6451 goto exit;
6452 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006453 }
6454 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6455 {
6456 /* There's no transaction to complete. It's all good. */
6457 status = PSA_SUCCESS;
6458 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006459#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006460
Gilles Peskinec6b69072018-11-20 21:42:52 +01006461 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462 global_data.initialized = 1;
6463
6464exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006465 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006467 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006468}
6469
6470#endif /* MBEDTLS_PSA_CRYPTO_C */