blob: db4b3876dafe850c940402c177c3877368bd37ac [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 Cooremanf9f7fdf2021-03-03 19:04:05 +0100548/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550 * Upon successful return of this function, the behavioud of #PSA_MAC_LENGTH
551 * will be defined when called with the validated \p algorithm and \p key_type
552 *
553 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \param[in] key_type The key type of the key to be used with the
555 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 *
557 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100563 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565{
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566 if( PSA_ALG_IS_HMAC( algorithm ) ) {
567 if( key_type == PSA_KEY_TYPE_HMAC )
568 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100570
571 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100572 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100573 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
574 * key. */
575 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
576 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
577 {
578 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
579 * the block length (larger than 1) for block ciphers. */
580 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
581 return( PSA_SUCCESS );
582 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100584
585 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100586}
587
Steven Cooreman75b74362020-07-28 14:30:13 +0200588/** Try to allocate a buffer to an empty key slot.
589 *
590 * \param[in,out] slot Key slot to attach buffer to.
591 * \param[in] buffer_length Requested size of the buffer.
592 *
593 * \retval #PSA_SUCCESS
594 * The buffer has been successfully allocated.
595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
596 * Not enough memory was available for allocation.
597 * \retval #PSA_ERROR_ALREADY_EXISTS
598 * Trying to allocate a buffer to a non-empty key slot.
599 */
600static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
601 size_t buffer_length )
602{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100603 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200604 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200605
Ronald Cronea0f8a62020-11-25 17:52:23 +0100606 slot->key.data = mbedtls_calloc( 1, buffer_length );
607 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200609
Ronald Cronea0f8a62020-11-25 17:52:23 +0100610 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200611 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200612}
613
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200614psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
615 const uint8_t* data,
616 size_t data_length )
617{
618 psa_status_t status = psa_allocate_buffer_to_slot( slot,
619 data_length );
620 if( status != PSA_SUCCESS )
621 return( status );
622
Ronald Cronea0f8a62020-11-25 17:52:23 +0100623 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200624 return( PSA_SUCCESS );
625}
626
Ronald Crona0fe59f2020-11-29 11:14:53 +0100627psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100628 const psa_key_attributes_t *attributes,
629 const uint8_t *data, size_t data_length,
630 uint8_t *key_buffer, size_t key_buffer_size,
631 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
634 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200636 /* zero-length keys are never supported. */
637 if( data_length == 0 )
638 return( PSA_ERROR_NOT_SUPPORTED );
639
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100640 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200643
Steven Cooreman75b74362020-07-28 14:30:13 +0200644 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
645 if( data_length > SIZE_MAX / 8 )
646 return( PSA_ERROR_NOT_SUPPORTED );
647
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200648 /* Enforce a size limit, and in particular ensure that the bit
649 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200652
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200654 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200655 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200656
Ronald Crondd04d422020-11-28 15:14:42 +0100657 /* Copy the key material. */
658 memcpy( key_buffer, data, data_length );
659 *key_buffer_length = data_length;
660 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200661
Steven Cooreman40120f62020-10-29 11:42:22 +0100662 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200665 {
John Durkop9814fa22020-11-04 12:28:15 -0800666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100668 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200669 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100670 return( mbedtls_psa_ecp_import_key( attributes,
671 data, data_length,
672 key_buffer, key_buffer_size,
673 key_buffer_length,
674 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100675 }
John Durkop9814fa22020-11-04 12:28:15 -0800676#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
677 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700678#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
679 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100680 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200681 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100682 return( mbedtls_psa_rsa_import_key( attributes,
683 data, data_length,
684 key_buffer, key_buffer_size,
685 key_buffer_length,
686 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200687 }
John Durkop9814fa22020-11-04 12:28:15 -0800688#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
689 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100690 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100691
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100692 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100693}
694
Gilles Peskinef603c712019-01-19 13:40:11 +0100695/** Calculate the intersection of two algorithm usage policies.
696 *
697 * Return 0 (which allows no operation) on incompatibility.
698 */
699static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100700 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100701 psa_algorithm_t alg1,
702 psa_algorithm_t alg2 )
703{
Gilles Peskine549ea862019-05-22 11:45:59 +0200704 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100705 if( alg1 == alg2 )
706 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100707 /* If the policies are from the same hash-and-sign family, check
708 * if one is a wildcard. If so the other has the specific algorithm. */
709 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
710 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
711 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100712 {
Steven Cooreman03488022021-02-18 12:07:20 +0100713 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
714 return( alg2 );
715 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
716 return( alg1 );
717 }
718 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100719 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100720 * restrictive tag length. */
721 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
722 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
723 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
724 {
725 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
726 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
727 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100728
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100729 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100730 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
731 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100732 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100733 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
735 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100736 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100737 ( alg1_len <= alg2_len ) )
738 {
739 return( alg2 );
740 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100741 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100742 ( alg2_len <= alg1_len ) )
743 {
744 return( alg1 );
745 }
746 }
747 /* If the policies are from the same MAC family, check whether one
748 * of them is a minimum-MAC-length policy. Calculate the most
749 * restrictive tag length. */
750 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
751 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
752 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
753 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754 /* Validate the combination of key type and algorithm. Since the base
755 * algorithm of alg1 and alg2 are the same, we only need this once. */
756 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100757 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100758
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100759 /* Get the output length for the algorithm and key combination. None of
760 * the currently supported algorithms have an output length dependent on
761 * actual key size, so setting it to a bogus value is currently OK.
762 * Note that for at-least-this-length wildcard algorithms, the output
763 * length is set to the shortest allowed length, which allows us to
764 * calculate the most restrictive tag length for the intersection. */
765 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
766 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooreman03488022021-02-18 12:07:20 +0100767 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100768
Steven Cooremana1d83222021-02-25 10:20:29 +0100769 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100770 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
771 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100772 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100773 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100774 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100775 /* If only one is a wildcard, return specific algorithm if compatible. */
776 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100777 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100778 if( alg1_len <= alg2_len )
779 return( alg2 );
780 else
781 return( 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100782 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100783 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100784 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100785 if( alg2_len <= alg1_len )
786 return( alg1 );
787 else
788 return( 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100789 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100790 /* If none of them are wildcards, check whether this is a case of one
791 * specifying the default length and the other a specific length. If the
792 * specific length equals the default length for this key type, the
793 * intersection would be the specific-length algorithm. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100794 if( alg1_len == alg2_len )
795 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100796 }
797 /* If the policies are incompatible, allow nothing. */
798 return( 0 );
799}
800
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100801static int psa_key_algorithm_permits( psa_key_type_t key_type,
802 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200803 psa_algorithm_t requested_alg )
804{
Gilles Peskine549ea862019-05-22 11:45:59 +0200805 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200806 if( requested_alg == policy_alg )
807 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100808 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
809 * and requested_alg is the same hash-and-sign family with any hash,
810 * then requested_alg is compliant with policy_alg. */
811 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
812 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200813 {
Steven Cooreman03488022021-02-18 12:07:20 +0100814 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
815 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
816 }
817 /* If policy_alg is a wildcard AEAD algorithm of the same base as
818 * the requested algorithm, check the requested tag length to be
819 * equal-length or longer than the wildcard-specified length. */
820 if( PSA_ALG_IS_AEAD( policy_alg ) &&
821 PSA_ALG_IS_AEAD( requested_alg ) &&
822 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
823 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100824 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100825 {
826 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
827 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
828 }
Steven Cooreman03488022021-02-18 12:07:20 +0100829 if( PSA_ALG_IS_MAC( policy_alg ) &&
830 PSA_ALG_IS_MAC( requested_alg ) &&
831 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100832 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100833 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100834 /* Validate the combination of key type and algorithm. Since the policy
835 * and requested algorithms are the same, we only need this once. */
836 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100837 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100838
839 /* Get both the requested and the default output length for this
840 * algorithm and key combination. None of the currently supported
841 * algorithms have an output length dependent on actual key size, so
842 * setting it to a bogus value is currently OK. */
843 size_t requested_output_length = PSA_MAC_LENGTH(
844 key_type, 0, requested_alg );
845 size_t default_output_length = PSA_MAC_LENGTH(
846 key_type, 0,
847 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100848
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100849 /* If the policy is default-length, only allow an algorithm with
850 * a declared exact-length matching the default. */
851 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100852 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100853
854 /* If the requested algorithm is default-length, allow it if the policy
855 * is exactly the default length. */
856 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
857 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
858 {
859 return( 1 );
860 }
861
862 /* If policy_alg is a wildcard MAC algorithm of the same base as
863 * the requested algorithm, check the requested tag length to be
864 * equal-length or longer than the wildcard-specified length. */
865 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
866 {
867 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100868 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100869 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200870 }
Steven Cooremance48e852020-10-05 16:02:45 +0200871 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200872 * a key derivation with that key agreement should also be allowed. This
873 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200874 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
875 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
876 {
877 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
878 policy_alg );
879 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100880 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200881 return( 0 );
882}
883
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100884/** Test whether a policy permits an algorithm.
885 *
886 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100887 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100888 * \note This function requires providing the key type for which the policy is
889 * being validated, since some algorithm policy definitions (e.g. MAC)
890 * have different properties depending on what kind of cipher it is
891 * combined with.
892 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100893 * \retval PSA_SUCCESS When \p alg is a specific algorithm
894 * allowed by the \p policy.
895 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
896 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
897 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100898 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100899static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100900 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100901 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100902{
Steven Cooremand788fab2021-02-25 11:29:17 +0100903 /* '0' is not a valid algorithm */
904 if( alg == 0 )
905 return( PSA_ERROR_INVALID_ARGUMENT );
906
Steven Cooreman7e39f052021-02-23 14:18:32 +0100907 /* A requested algorithm cannot be a wildcard. */
908 if( PSA_ALG_IS_WILDCARD( alg ) )
909 return( PSA_ERROR_INVALID_ARGUMENT );
910
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100911 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
912 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100913 return( PSA_SUCCESS );
914 else
915 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100916}
917
Gilles Peskinef603c712019-01-19 13:40:11 +0100918/** Restrict a key policy based on a constraint.
919 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100920 * \note This function requires providing the key type for which the policy is
921 * being restricted, since some algorithm policy definitions (e.g. MAC)
922 * have different properties depending on what kind of cipher it is
923 * combined with.
924 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100925 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100926 * \param[in,out] policy The policy to restrict.
927 * \param[in] constraint The policy constraint to apply.
928 *
929 * \retval #PSA_SUCCESS
930 * \c *policy contains the intersection of the original value of
931 * \c *policy and \c *constraint.
932 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100933 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100934 * \c *policy is unchanged.
935 */
936static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100937 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100938 psa_key_policy_t *policy,
939 const psa_key_policy_t *constraint )
940{
941 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100942 psa_key_policy_algorithm_intersection( key_type, policy->alg,
943 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200944 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100945 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
946 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100947 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
948 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200949 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
950 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100951 policy->usage &= constraint->usage;
952 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200953 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100954 return( PSA_SUCCESS );
955}
956
Ronald Cron5c522922020-11-14 16:35:34 +0100957/** Get the description of a key given its identifier and policy constraints
958 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200959 *
Ronald Cron5c522922020-11-14 16:35:34 +0100960 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100961 * nonzero, the key must allow operations with this algorithm. If \p alg is
962 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100963 *
Ronald Cron5c522922020-11-14 16:35:34 +0100964 * In case of a persistent key, the function loads the description of the key
965 * into a key slot if not already done.
966 *
967 * On success, the returned key slot is locked. It is the responsibility of
968 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200969 */
Ronald Cron5c522922020-11-14 16:35:34 +0100970static psa_status_t psa_get_and_lock_key_slot_with_policy(
971 mbedtls_svc_key_id_t key,
972 psa_key_slot_t **p_slot,
973 psa_key_usage_t usage,
974 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100975{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200976 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
977 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100978
Ronald Cron5c522922020-11-14 16:35:34 +0100979 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100980 if( status != PSA_SUCCESS )
981 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200982 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100983
984 /* Enforce that usage policy for the key slot contains all the flags
985 * required by the usage parameter. There is one exception: public
986 * keys can always be exported, so we treat public key objects as
987 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200988 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100989 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200990
Gilles Peskine8e338702019-07-30 20:06:31 +0200991 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100992 {
993 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200994 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +0100995 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100996
997 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100998 if( alg != 0 )
999 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001000 status = psa_key_policy_permits( &slot->attr.policy,
1001 slot->attr.type,
1002 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001003 if( status != PSA_SUCCESS )
1004 goto error;
1005 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001006
Darryl Green06fd18d2018-07-16 11:21:11 +01001007 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001008
1009error:
1010 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001011 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001012
1013 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001014}
Darryl Green940d72c2018-07-13 13:18:51 +01001015
Ronald Cron5c522922020-11-14 16:35:34 +01001016/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001017 *
1018 * A transparent key is a key for which the key material is directly
1019 * available, as opposed to a key in a secure element.
1020 *
Ronald Cron5c522922020-11-14 16:35:34 +01001021 * This is a temporary function to use instead of
1022 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1023 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001024 *
Ronald Cron5c522922020-11-14 16:35:34 +01001025 * On success, the returned key slot is locked. It is the responsibility of the
1026 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001027 */
1028#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001029static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1030 mbedtls_svc_key_id_t key,
1031 psa_key_slot_t **p_slot,
1032 psa_key_usage_t usage,
1033 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034{
Ronald Cron5c522922020-11-14 16:35:34 +01001035 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1036 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001037 if( status != PSA_SUCCESS )
1038 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001039
Gilles Peskineadad8132019-07-25 11:31:23 +02001040 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041 {
Ronald Cron5c522922020-11-14 16:35:34 +01001042 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001043 *p_slot = NULL;
1044 return( PSA_ERROR_NOT_SUPPORTED );
1045 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046
Gilles Peskine28f8f302019-07-24 13:30:31 +02001047 return( PSA_SUCCESS );
1048}
1049#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1050/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001051#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1052 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001053#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1054
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001055/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001056static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001057{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001058 /* Data pointer will always be either a valid pointer or NULL in an
1059 * initialized slot, so we can just free it. */
1060 if( slot->key.data != NULL )
1061 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1062
1063 mbedtls_free( slot->key.data );
1064 slot->key.data = NULL;
1065 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001066
1067 return( PSA_SUCCESS );
1068}
1069
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001070/** Completely wipe a slot in memory, including its policy.
1071 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001072psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001073{
1074 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001075
1076 /*
1077 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001078 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001079 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1080 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001081 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001082 */
Ronald Cron5c522922020-11-14 16:35:34 +01001083 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001084 {
1085#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001086 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001087#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001088 status = PSA_ERROR_CORRUPTION_DETECTED;
1089 }
1090
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001091 /* Multipart operations may still be using the key. This is safe
1092 * because all multipart operation objects are independent from
1093 * the key slot: if they need to access the key after the setup
1094 * phase, they have a copy of the key. Note that this means that
1095 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001096 /* At this point, key material and other type-specific content has
1097 * been wiped. Clear remaining metadata. We can call memset and not
1098 * zeroize because the metadata is not particularly sensitive. */
1099 memset( slot, 0, sizeof( *slot ) );
1100 return( status );
1101}
1102
Ronald Croncf56a0a2020-08-04 09:51:30 +02001103psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001104{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001105 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001106 psa_status_t status; /* status of the last operation */
1107 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001108#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1109 psa_se_drv_table_entry_t *driver;
1110#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111
Ronald Croncf56a0a2020-08-04 09:51:30 +02001112 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001113 return( PSA_SUCCESS );
1114
Ronald Cronf2911112020-10-29 17:51:10 +01001115 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001116 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001117 * key, this will load the key description from persistent memory if not
1118 * done yet. We cannot avoid this loading as without it we don't know if
1119 * the key is operated by an SE or not and this information is needed by
1120 * the current implementation.
1121 */
Ronald Cron5c522922020-11-14 16:35:34 +01001122 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001123 if( status != PSA_SUCCESS )
1124 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001125
Ronald Cronf2911112020-10-29 17:51:10 +01001126 /*
1127 * If the key slot containing the key description is under access by the
1128 * library (apart from the present access), the key cannot be destroyed
1129 * yet. For the time being, just return in error. Eventually (to be
1130 * implemented), the key should be destroyed when all accesses have
1131 * stopped.
1132 */
Ronald Cron5c522922020-11-14 16:35:34 +01001133 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001134 {
Ronald Cron5c522922020-11-14 16:35:34 +01001135 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001136 return( PSA_ERROR_GENERIC_ERROR );
1137 }
1138
Gilles Peskine354f7672019-07-12 23:46:38 +02001139#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001140 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001141 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001142 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001143 /* For a key in a secure element, we need to do three things:
1144 * remove the key file in internal storage, destroy the
1145 * key inside the secure element, and update the driver's
1146 * persistent data. Start a transaction that will encompass these
1147 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001148 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001149 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001150 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001151 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001152 status = psa_crypto_save_transaction( );
1153 if( status != PSA_SUCCESS )
1154 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001155 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001156 /* We should still try to destroy the key in the secure
1157 * element and the key metadata in storage. This is especially
1158 * important if the error is that the storage is full.
1159 * But how to do it exactly without risking an inconsistent
1160 * state after a reset?
1161 * https://github.com/ARMmbed/mbed-crypto/issues/215
1162 */
1163 overall_status = status;
1164 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001165 }
1166
Ronald Cronea0f8a62020-11-25 17:52:23 +01001167 status = psa_destroy_se_key( driver,
1168 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001169 if( overall_status == PSA_SUCCESS )
1170 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001171 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1173
Darryl Greend49a4992018-06-18 17:27:26 +01001174#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001175 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001176 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001177 status = psa_destroy_persistent_key( slot->attr.id );
1178 if( overall_status == PSA_SUCCESS )
1179 overall_status = status;
1180
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001181 /* TODO: other slots may have a copy of the same key. We should
1182 * invalidate them.
1183 * https://github.com/ARMmbed/mbed-crypto/issues/214
1184 */
Darryl Greend49a4992018-06-18 17:27:26 +01001185 }
1186#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001187
Gilles Peskinefc762652019-07-22 19:30:34 +02001188#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1189 if( driver != NULL )
1190 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001191 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001192 if( overall_status == PSA_SUCCESS )
1193 overall_status = status;
1194 status = psa_crypto_stop_transaction( );
1195 if( overall_status == PSA_SUCCESS )
1196 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001197 }
1198#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1199
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001200#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1201exit:
1202#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001203 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001204 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1205 if( overall_status == PSA_SUCCESS )
1206 overall_status = status;
1207 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001208}
1209
John Durkop07cc04a2020-11-16 22:08:34 -08001210#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001211 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001212static psa_status_t psa_get_rsa_public_exponent(
1213 const mbedtls_rsa_context *rsa,
1214 psa_key_attributes_t *attributes )
1215{
1216 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001217 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001218 uint8_t *buffer = NULL;
1219 size_t buflen;
1220 mbedtls_mpi_init( &mpi );
1221
1222 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1223 if( ret != 0 )
1224 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001225 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1226 {
1227 /* It's the default value, which is reported as an empty string,
1228 * so there's nothing to do. */
1229 goto exit;
1230 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001231
1232 buflen = mbedtls_mpi_size( &mpi );
1233 buffer = mbedtls_calloc( 1, buflen );
1234 if( buffer == NULL )
1235 {
1236 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1237 goto exit;
1238 }
1239 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1240 if( ret != 0 )
1241 goto exit;
1242 attributes->domain_parameters = buffer;
1243 attributes->domain_parameters_size = buflen;
1244
1245exit:
1246 mbedtls_mpi_free( &mpi );
1247 if( ret != 0 )
1248 mbedtls_free( buffer );
1249 return( mbedtls_to_psa_error( ret ) );
1250}
John Durkop07cc04a2020-11-16 22:08:34 -08001251#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001252 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001253
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001254/** Retrieve all the publicly-accessible attributes of a key.
1255 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001256psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001257 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001258{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001259 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001260 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001261 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001262
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001263 psa_reset_key_attributes( attributes );
1264
Ronald Cron5c522922020-11-14 16:35:34 +01001265 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001266 if( status != PSA_SUCCESS )
1267 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001268
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001269 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001270 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1271 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1272
1273#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1274 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001275 psa_set_key_slot_number( attributes,
1276 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001278
Gilles Peskine8e338702019-07-30 20:06:31 +02001279 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001280 {
John Durkop07cc04a2020-11-16 22:08:34 -08001281#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001282 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001283 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001284 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001286 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001287 * is not yet implemented.
1288 * https://github.com/ARMmbed/mbed-crypto/issues/216
1289 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001290 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001291 break;
1292#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001293 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001294 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001295
Ronald Cron90857082020-11-25 14:58:33 +01001296 status = mbedtls_psa_rsa_load_representation(
1297 slot->attr.type,
1298 slot->key.data,
1299 slot->key.bytes,
1300 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001301 if( status != PSA_SUCCESS )
1302 break;
1303
Steven Cooremana2371e52020-07-28 14:30:39 +02001304 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001305 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001306 mbedtls_rsa_free( rsa );
1307 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001308 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001309 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001310#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001311 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 default:
1313 /* Nothing else to do. */
1314 break;
1315 }
1316
1317 if( status != PSA_SUCCESS )
1318 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001319
Ronald Cron5c522922020-11-14 16:35:34 +01001320 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001321
Ronald Cron5c522922020-11-14 16:35:34 +01001322 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001323}
1324
Gilles Peskinec8000c02019-08-02 20:15:51 +02001325#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1326psa_status_t psa_get_key_slot_number(
1327 const psa_key_attributes_t *attributes,
1328 psa_key_slot_number_t *slot_number )
1329{
1330 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1331 {
1332 *slot_number = attributes->slot_number;
1333 return( PSA_SUCCESS );
1334 }
1335 else
1336 return( PSA_ERROR_INVALID_ARGUMENT );
1337}
1338#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1339
Ronald Crond18b5f82020-11-25 16:46:05 +01001340static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1341 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001342 uint8_t *data,
1343 size_t data_size,
1344 size_t *data_length )
1345{
Ronald Crond18b5f82020-11-25 16:46:05 +01001346 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001347 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001348 memcpy( data, key_buffer, key_buffer_size );
1349 memset( data + key_buffer_size, 0,
1350 data_size - key_buffer_size );
1351 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001352 return( PSA_SUCCESS );
1353}
1354
Ronald Cron7285cda2020-11-26 14:46:37 +01001355psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001356 const psa_key_attributes_t *attributes,
1357 const uint8_t *key_buffer, size_t key_buffer_size,
1358 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001359{
Ronald Crond18b5f82020-11-25 16:46:05 +01001360 psa_key_type_t type = attributes->core.type;
1361
Ronald Crond18b5f82020-11-25 16:46:05 +01001362 if( key_type_is_raw_bytes( type ) ||
1363 PSA_KEY_TYPE_IS_RSA( type ) ||
1364 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001365 {
1366 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 key_buffer, key_buffer_size,
1368 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001369 }
1370 else
1371 {
1372 /* This shouldn't happen in the reference implementation, but
1373 it is valid for a special-purpose implementation to omit
1374 support for exporting certain key types. */
1375 return( PSA_ERROR_NOT_SUPPORTED );
1376 }
1377}
1378
1379psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1380 uint8_t *data,
1381 size_t data_size,
1382 size_t *data_length )
1383{
1384 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1385 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1386 psa_key_slot_t *slot;
1387
Ronald Crone907e552021-01-18 13:22:38 +01001388 /* Reject a zero-length output buffer now, since this can never be a
1389 * valid key representation. This way we know that data must be a valid
1390 * pointer and we can do things like memset(data, ..., data_size). */
1391 if( data_size == 0 )
1392 return( PSA_ERROR_BUFFER_TOO_SMALL );
1393
Ronald Cron9486f9d2020-11-26 13:36:23 +01001394 /* Set the key to empty now, so that even when there are errors, we always
1395 * set data_length to a value between 0 and data_size. On error, setting
1396 * the key to empty is a good choice because an empty key representation is
1397 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001398 *data_length = 0;
1399
Ronald Cron9486f9d2020-11-26 13:36:23 +01001400 /* Export requires the EXPORT flag. There is an exception for public keys,
1401 * which don't require any flag, but
1402 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1403 */
1404 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1405 PSA_KEY_USAGE_EXPORT, 0 );
1406 if( status != PSA_SUCCESS )
1407 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001408
Ronald Crond18b5f82020-11-25 16:46:05 +01001409 psa_key_attributes_t attributes = {
1410 .core = slot->attr
1411 };
Ronald Cron67227982020-11-26 15:16:05 +01001412 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001413 slot->key.data, slot->key.bytes,
1414 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001415
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416 unlock_status = psa_unlock_key_slot( slot );
1417
1418 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1419}
1420
Ronald Cron7285cda2020-11-26 14:46:37 +01001421psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001422 const psa_key_attributes_t *attributes,
1423 const uint8_t *key_buffer,
1424 size_t key_buffer_size,
1425 uint8_t *data,
1426 size_t data_size,
1427 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001428{
Ronald Crond18b5f82020-11-25 16:46:05 +01001429 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001430
Ronald Crond18b5f82020-11-25 16:46:05 +01001431 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001432 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001433 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001434 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001435 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001436 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001437 key_buffer, key_buffer_size,
1438 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001439 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001440
Ronald Crond18b5f82020-11-25 16:46:05 +01001441 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442 {
John Durkop0e005192020-10-31 22:06:54 -07001443#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1444 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001445 return( mbedtls_psa_rsa_export_public_key( attributes,
1446 key_buffer,
1447 key_buffer_size,
1448 data,
1449 data_size,
1450 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001451#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001452 /* We don't know how to convert a private RSA key to public. */
1453 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001454#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1455 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001456 }
1457 else
Moran Pekera998bc62018-04-16 18:16:20 +03001458 {
John Durkop6ba40d12020-11-10 08:50:04 -08001459#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1460 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001461 return( mbedtls_psa_ecp_export_public_key( attributes,
1462 key_buffer,
1463 key_buffer_size,
1464 data,
1465 data_size,
1466 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467#else
1468 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001469 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001470#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1471 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001472 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001473 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 else
1475 {
1476 /* This shouldn't happen in the reference implementation, but
1477 it is valid for a special-purpose implementation to omit
1478 support for exporting certain key types. */
1479 return( PSA_ERROR_NOT_SUPPORTED );
1480 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001481}
Ronald Crond18b5f82020-11-25 16:46:05 +01001482
Ronald Croncf56a0a2020-08-04 09:51:30 +02001483psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001484 uint8_t *data,
1485 size_t data_size,
1486 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001487{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001488 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001489 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001490 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001491
Ronald Crone907e552021-01-18 13:22:38 +01001492 /* Reject a zero-length output buffer now, since this can never be a
1493 * valid key representation. This way we know that data must be a valid
1494 * pointer and we can do things like memset(data, ..., data_size). */
1495 if( data_size == 0 )
1496 return( PSA_ERROR_BUFFER_TOO_SMALL );
1497
Darryl Greendd8fb772018-11-07 16:00:44 +00001498 /* Set the key to empty now, so that even when there are errors, we always
1499 * set data_length to a value between 0 and data_size. On error, setting
1500 * the key to empty is a good choice because an empty key representation is
1501 * unlikely to be accepted anywhere. */
1502 *data_length = 0;
1503
1504 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001505 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001506 if( status != PSA_SUCCESS )
1507 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001508
Ronald Cron9486f9d2020-11-26 13:36:23 +01001509 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1510 {
1511 status = PSA_ERROR_INVALID_ARGUMENT;
1512 goto exit;
1513 }
1514
Ronald Crond18b5f82020-11-25 16:46:05 +01001515 psa_key_attributes_t attributes = {
1516 .core = slot->attr
1517 };
Ronald Cron67227982020-11-26 15:16:05 +01001518 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001519 &attributes, slot->key.data, slot->key.bytes,
1520 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001521
1522exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001523 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001524
Ronald Cron5c522922020-11-14 16:35:34 +01001525 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001526}
1527
Gilles Peskine91e8c332019-08-02 19:19:39 +02001528#if defined(static_assert)
1529static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1530 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001531static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001532 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001533static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001534 "One or more key attribute flag is listed as both internal-only and external-only" );
1535#endif
1536
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001537/** Validate that a key policy is internally well-formed.
1538 *
1539 * This function only rejects invalid policies. It does not validate the
1540 * consistency of the policy with respect to other attributes of the key
1541 * such as the key type.
1542 */
1543static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001544{
1545 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001546 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001547 PSA_KEY_USAGE_ENCRYPT |
1548 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001549 PSA_KEY_USAGE_SIGN_HASH |
1550 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1552 return( PSA_ERROR_INVALID_ARGUMENT );
1553
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 return( PSA_SUCCESS );
1555}
1556
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001557/** Validate the internal consistency of key attributes.
1558 *
1559 * This function only rejects invalid attribute values. If does not
1560 * validate the consistency of the attributes with any key data that may
1561 * be involved in the creation of the key.
1562 *
1563 * Call this function early in the key creation process.
1564 *
1565 * \param[in] attributes Key attributes for the new key.
1566 * \param[out] p_drv On any return, the driver for the key, if any.
1567 * NULL for a transparent key.
1568 *
1569 */
1570static psa_status_t psa_validate_key_attributes(
1571 const psa_key_attributes_t *attributes,
1572 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001573{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001574 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001575 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001576 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001577
Ronald Cron54b90082020-10-29 15:26:43 +01001578 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001579 if( status != PSA_SUCCESS )
1580 return( status );
1581
Ronald Crond2ed4812020-07-17 16:11:30 +02001582 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001583 if( status != PSA_SUCCESS )
1584 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001585
Ronald Cron65f38a32020-10-23 17:11:13 +02001586 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1587 {
1588 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1589 return( PSA_ERROR_INVALID_ARGUMENT );
1590 }
1591 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001592 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001593 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001594 if( status != PSA_SUCCESS )
1595 return( status );
1596 }
1597
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001598 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001599 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001600 return( status );
1601
1602 /* Refuse to create overly large keys.
1603 * Note that this doesn't trigger on import if the attributes don't
1604 * explicitly specify a size (so psa_get_key_bits returns 0), so
1605 * psa_import_key() needs its own checks. */
1606 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1607 return( PSA_ERROR_NOT_SUPPORTED );
1608
Gilles Peskine91e8c332019-08-02 19:19:39 +02001609 /* Reject invalid flags. These should not be reachable through the API. */
1610 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1611 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1612 return( PSA_ERROR_INVALID_ARGUMENT );
1613
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001614 return( PSA_SUCCESS );
1615}
1616
Gilles Peskine4747d192019-04-17 15:05:45 +02001617/** Prepare a key slot to receive key material.
1618 *
1619 * This function allocates a key slot and sets its metadata.
1620 *
1621 * If this function fails, call psa_fail_key_creation().
1622 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001623 * This function is intended to be used as follows:
1624 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001625 * it with the specified attributes, and in case of a volatile key assign it
1626 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001627 * -# Populate the slot with the key material.
1628 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1629 * In case of failure at any step, stop the sequence and call
1630 * psa_fail_key_creation().
1631 *
Ronald Cron5c522922020-11-14 16:35:34 +01001632 * On success, the key slot is locked. It is the responsibility of the caller
1633 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001634 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001635 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001636 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001637 * \param[out] p_slot On success, a pointer to the prepared slot.
1638 * \param[out] p_drv On any return, the driver for the key, if any.
1639 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001640 *
1641 * \retval #PSA_SUCCESS
1642 * The key slot is ready to receive key material.
1643 * \return If this function fails, the key slot is an invalid state.
1644 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 */
1646static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001647 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001648 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001649 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001650 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001651{
1652 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001653 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 psa_key_slot_t *slot;
1655
Gilles Peskinedf179142019-07-15 22:02:14 +02001656 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001657 *p_drv = NULL;
1658
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001659 status = psa_validate_key_attributes( attributes, p_drv );
1660 if( status != PSA_SUCCESS )
1661 return( status );
1662
Ronald Cronc4d1b512020-07-31 11:26:37 +02001663 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001664 if( status != PSA_SUCCESS )
1665 return( status );
1666 slot = *p_slot;
1667
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668 /* We're storing the declared bit-size of the key. It's up to each
1669 * creation mechanism to verify that this information is correct.
1670 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001671 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001672 * is optional (import, copy). In case of a volatile key, assign it the
1673 * volatile key identifier associated to the slot returned to contain its
1674 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675
1676 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001677 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1678 {
1679#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1680 slot->attr.id = volatile_key_id;
1681#else
1682 slot->attr.id.key_id = volatile_key_id;
1683#endif
1684 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001685
Gilles Peskine91e8c332019-08-02 19:19:39 +02001686 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001687 * external-only flags, query `attributes`. Thanks to the check
1688 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001689 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001690 * may have set. */
1691 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001692
Gilles Peskinecbaff462019-07-12 23:46:04 +02001693#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001694 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001695 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001696 * create the key file in internal storage, create the
1697 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001698 * persistent data. This is done by starting a transaction that will
1699 * encompass these three actions.
1700 * For registering a volatile key, we just need to find an appropriate
1701 * slot number inside the SE. Since the key is designated volatile, creating
1702 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001703 /* The first thing to do is to find a slot number for the new key.
1704 * We save the slot number in persistent storage as part of the
1705 * transaction data. It will be needed to recover if the power
1706 * fails during the key creation process, to clean up on the secure
1707 * element side after restarting. Obtaining a slot number from the
1708 * secure element driver updates its persistent state, but we do not yet
1709 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001710 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001711 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001712 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001713 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001714 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001715 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716 if( status != PSA_SUCCESS )
1717 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001718
1719 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001720 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001721 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1722 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001723 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001724 psa_crypto_transaction.key.id = slot->attr.id;
1725 status = psa_crypto_save_transaction( );
1726 if( status != PSA_SUCCESS )
1727 {
1728 (void) psa_crypto_stop_transaction( );
1729 return( status );
1730 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001731 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001732
1733 status = psa_copy_key_material_into_slot(
1734 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001735 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001736
1737 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1738 {
1739 /* Key registration only makes sense with a secure element. */
1740 return( PSA_ERROR_INVALID_ARGUMENT );
1741 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001742#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1743
Ronald Cronc4d1b512020-07-31 11:26:37 +02001744 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001745}
Gilles Peskine4747d192019-04-17 15:05:45 +02001746
1747/** Finalize the creation of a key once its key material has been set.
1748 *
1749 * This entails writing the key to persistent storage.
1750 *
1751 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001752 * See the documentation of psa_start_key_creation() for the intended use
1753 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001754 *
Ronald Cron5c522922020-11-14 16:35:34 +01001755 * If the finalization succeeds, the function unlocks the key slot (it was
1756 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1757 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001758 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001759 * \param[in,out] slot Pointer to the slot with key material.
1760 * \param[in] driver The secure element driver for the key,
1761 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001762 * \param[out] key On success, identifier of the key. Note that the
1763 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001764 *
1765 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001766 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001767 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1768 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1769 * \retval #PSA_ERROR_ALREADY_EXISTS
1770 * \retval #PSA_ERROR_DATA_INVALID
1771 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001772 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001773 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001774 * \return If this function fails, the key slot is an invalid state.
1775 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001777static psa_status_t psa_finish_key_creation(
1778 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001779 psa_se_drv_table_entry_t *driver,
1780 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001781{
1782 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001783 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001784 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001785
1786#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001787 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001788 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001789#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1790 if( driver != NULL )
1791 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001792 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001793 psa_key_slot_number_t slot_number =
1794 psa_key_slot_get_slot_number( slot ) ;
1795
Gilles Peskineb46bef22019-07-30 21:32:04 +02001796#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001797 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001798 sizeof( data.slot_number ),
1799 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001800#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001801 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001802 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001803 (uint8_t*) &data,
1804 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001805 }
1806 else
1807#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1808 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001809 /* Key material is saved in export representation in the slot, so
1810 * just pass the slot buffer for storage. */
1811 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001812 slot->key.data,
1813 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001814 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001815 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001816#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1817
Gilles Peskinecbaff462019-07-12 23:46:04 +02001818#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001819 /* Finish the transaction for a key creation. This does not
1820 * happen when registering an existing key. Detect this case
1821 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001822 * creation of a persistent key in a secure element requires a transaction,
1823 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001824 if( driver != NULL &&
1825 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001826 {
1827 status = psa_save_se_persistent_data( driver );
1828 if( status != PSA_SUCCESS )
1829 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001830 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001831 return( status );
1832 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001833 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001834 }
1835#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1836
Ronald Cron50972942020-11-14 11:28:25 +01001837 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001838 {
1839 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001840 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001841 if( status != PSA_SUCCESS )
1842 *key = MBEDTLS_SVC_KEY_ID_INIT;
1843 }
Ronald Cron50972942020-11-14 11:28:25 +01001844
Gilles Peskine4747d192019-04-17 15:05:45 +02001845 return( status );
1846}
1847
1848/** Abort the creation of a key.
1849 *
1850 * You may call this function after calling psa_start_key_creation(),
1851 * or after psa_finish_key_creation() fails. In other circumstances, this
1852 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001853 * See the documentation of psa_start_key_creation() for the intended use
1854 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001855 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001856 * \param[in,out] slot Pointer to the slot with key material.
1857 * \param[in] driver The secure element driver for the key,
1858 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001859 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001860static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001861 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001862{
Gilles Peskine011e4282019-06-26 18:34:38 +02001863 (void) driver;
1864
Gilles Peskine4747d192019-04-17 15:05:45 +02001865 if( slot == NULL )
1866 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001867
1868#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001869 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 * element, and the failure happened later (when saving metadata
1871 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001872 * element.
1873 * https://github.com/ARMmbed/mbed-crypto/issues/217
1874 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001875
Gilles Peskined7729582019-08-05 15:55:54 +02001876 /* Abort the ongoing transaction if any (there may not be one if
1877 * the creation process failed before starting one, or if the
1878 * key creation is a registration of a key in a secure element).
1879 * Earlier functions must already have done what it takes to undo any
1880 * partial creation. All that's left is to update the transaction data
1881 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001882 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001883#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1884
Gilles Peskine4747d192019-04-17 15:05:45 +02001885 psa_wipe_key_slot( slot );
1886}
1887
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001888/** Validate optional attributes during key creation.
1889 *
1890 * Some key attributes are optional during key creation. If they are
1891 * specified in the attributes structure, check that they are consistent
1892 * with the data in the slot.
1893 *
1894 * This function should be called near the end of key creation, after
1895 * the slot in memory is fully populated but before saving persistent data.
1896 */
1897static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001898 const psa_key_slot_t *slot,
1899 const psa_key_attributes_t *attributes )
1900{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001901 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001902 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001903 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001904 return( PSA_ERROR_INVALID_ARGUMENT );
1905 }
1906
1907 if( attributes->domain_parameters_size != 0 )
1908 {
John Durkop0e005192020-10-31 22:06:54 -07001909#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1910 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001911 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001912 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001913 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001914 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001915 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001916
Ronald Cron90857082020-11-25 14:58:33 +01001917 psa_status_t status = mbedtls_psa_rsa_load_representation(
1918 slot->attr.type,
1919 slot->key.data,
1920 slot->key.bytes,
1921 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001922 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001923 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001924
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001925 mbedtls_mpi_init( &actual );
1926 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001927 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001928 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001929 mbedtls_rsa_free( rsa );
1930 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001931 if( ret != 0 )
1932 goto rsa_exit;
1933 ret = mbedtls_mpi_read_binary( &required,
1934 attributes->domain_parameters,
1935 attributes->domain_parameters_size );
1936 if( ret != 0 )
1937 goto rsa_exit;
1938 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1939 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1940 rsa_exit:
1941 mbedtls_mpi_free( &actual );
1942 mbedtls_mpi_free( &required );
1943 if( ret != 0)
1944 return( mbedtls_to_psa_error( ret ) );
1945 }
1946 else
John Durkop9814fa22020-11-04 12:28:15 -08001947#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1948 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001949 {
1950 return( PSA_ERROR_INVALID_ARGUMENT );
1951 }
1952 }
1953
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001954 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001955 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001956 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 return( PSA_ERROR_INVALID_ARGUMENT );
1958 }
1959
1960 return( PSA_SUCCESS );
1961}
1962
Gilles Peskine4747d192019-04-17 15:05:45 +02001963psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001964 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001965 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001966 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001967{
1968 psa_status_t status;
1969 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001970 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001971 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001972
Ronald Cron81709fc2020-11-14 12:10:32 +01001973 *key = MBEDTLS_SVC_KEY_ID_INIT;
1974
Gilles Peskine0f84d622019-09-12 19:03:13 +02001975 /* Reject zero-length symmetric keys (including raw data key objects).
1976 * This also rejects any key which might be encoded as an empty string,
1977 * which is never valid. */
1978 if( data_length == 0 )
1979 return( PSA_ERROR_INVALID_ARGUMENT );
1980
Gilles Peskinedf179142019-07-15 22:02:14 +02001981 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001982 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001986 /* In the case of a transparent key or an opaque key stored in local
1987 * storage (thus not in the case of generating a key in a secure element
1988 * or cryptoprocessor with storage), we have to allocate a buffer to
1989 * hold the generated key material. */
1990 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001991 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001992 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001993 if( status != PSA_SUCCESS )
1994 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001995 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001996
1997 bits = slot->attr.bits;
1998 status = psa_driver_wrapper_import_key( attributes,
1999 data, data_length,
2000 slot->key.data,
2001 slot->key.bytes,
2002 &slot->key.bytes, &bits );
2003 if( status != PSA_SUCCESS )
2004 goto exit;
2005
2006 if( slot->attr.bits == 0 )
2007 slot->attr.bits = (psa_key_bits_t) bits;
2008 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002009 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002010 status = PSA_ERROR_INVALID_ARGUMENT;
2011 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002012 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002013
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002014 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002015 if( status != PSA_SUCCESS )
2016 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002017
Ronald Cron81709fc2020-11-14 12:10:32 +01002018 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002019exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002020 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002021 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002022
Gilles Peskine4747d192019-04-17 15:05:45 +02002023 return( status );
2024}
2025
Gilles Peskined7729582019-08-05 15:55:54 +02002026#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2027psa_status_t mbedtls_psa_register_se_key(
2028 const psa_key_attributes_t *attributes )
2029{
2030 psa_status_t status;
2031 psa_key_slot_t *slot = NULL;
2032 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002033 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002034
2035 /* Leaving attributes unspecified is not currently supported.
2036 * It could make sense to query the key type and size from the
2037 * secure element, but not all secure elements support this
2038 * and the driver HAL doesn't currently support it. */
2039 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2040 return( PSA_ERROR_NOT_SUPPORTED );
2041 if( psa_get_key_bits( attributes ) == 0 )
2042 return( PSA_ERROR_NOT_SUPPORTED );
2043
2044 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002045 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002046 if( status != PSA_SUCCESS )
2047 goto exit;
2048
Ronald Cron81709fc2020-11-14 12:10:32 +01002049 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002050
2051exit:
2052 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002053 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002054
Gilles Peskined7729582019-08-05 15:55:54 +02002055 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002056 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002057 return( status );
2058}
2059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2060
Gilles Peskinef603c712019-01-19 13:40:11 +01002061static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002062 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002063{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002064 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002065 source->key.data,
2066 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002067 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002068 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002069
Steven Cooreman398aee52020-10-13 14:35:45 +02002070 target->attr.type = source->attr.type;
2071 target->attr.bits = source->attr.bits;
2072
2073 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002074}
2075
Ronald Croncf56a0a2020-08-04 09:51:30 +02002076psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002077 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002078 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002079{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002080 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002081 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002082 psa_key_slot_t *source_slot = NULL;
2083 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002084 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002085 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002086
Ronald Cron81709fc2020-11-14 12:10:32 +01002087 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2088
Ronald Cron5c522922020-11-14 16:35:34 +01002089 status = psa_get_and_lock_transparent_key_slot_with_policy(
2090 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002091 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002092 goto exit;
2093
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002094 status = psa_validate_optional_attributes( source_slot,
2095 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002096 if( status != PSA_SUCCESS )
2097 goto exit;
2098
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002099 status = psa_restrict_key_policy( source_slot->attr.type,
2100 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002101 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002102 if( status != PSA_SUCCESS )
2103 goto exit;
2104
Ronald Cron81709fc2020-11-14 12:10:32 +01002105 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2106 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002107 if( status != PSA_SUCCESS )
2108 goto exit;
2109
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002110#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2111 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002112 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002113 /* Copying to a secure element is not implemented yet. */
2114 status = PSA_ERROR_NOT_SUPPORTED;
2115 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002116 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002117#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002118
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002119 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002120 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002121 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002122
Ronald Cron81709fc2020-11-14 12:10:32 +01002123 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002125 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002126 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002127
Ronald Cron5c522922020-11-14 16:35:34 +01002128 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002129
Ronald Cron5c522922020-11-14 16:35:34 +01002130 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002131}
2132
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002133
2134
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002135/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002136/* Message digests */
2137/****************************************************************/
2138
John Durkop07cc04a2020-11-16 22:08:34 -08002139#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002140 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2141 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002142 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002143static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002144{
2145 switch( alg )
2146 {
John Durkopee4e6602020-11-27 08:48:46 -08002147#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002148 case PSA_ALG_MD2:
2149 return( &mbedtls_md2_info );
2150#endif
John Durkopee4e6602020-11-27 08:48:46 -08002151#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002152 case PSA_ALG_MD4:
2153 return( &mbedtls_md4_info );
2154#endif
John Durkopee4e6602020-11-27 08:48:46 -08002155#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002156 case PSA_ALG_MD5:
2157 return( &mbedtls_md5_info );
2158#endif
John Durkopee4e6602020-11-27 08:48:46 -08002159#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002160 case PSA_ALG_RIPEMD160:
2161 return( &mbedtls_ripemd160_info );
2162#endif
John Durkopee4e6602020-11-27 08:48:46 -08002163#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002164 case PSA_ALG_SHA_1:
2165 return( &mbedtls_sha1_info );
2166#endif
John Durkopee4e6602020-11-27 08:48:46 -08002167#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002168 case PSA_ALG_SHA_224:
2169 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002170#endif
2171#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002172 case PSA_ALG_SHA_256:
2173 return( &mbedtls_sha256_info );
2174#endif
John Durkopee4e6602020-11-27 08:48:46 -08002175#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002176 case PSA_ALG_SHA_384:
2177 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002178#endif
John Durkopee4e6602020-11-27 08:48:46 -08002179#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002180 case PSA_ALG_SHA_512:
2181 return( &mbedtls_sha512_info );
2182#endif
2183 default:
2184 return( NULL );
2185 }
2186}
John Durkop07cc04a2020-11-16 22:08:34 -08002187#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002188 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2189 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2190 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002191
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002192psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2193{
2194 switch( operation->alg )
2195 {
Gilles Peskine81736312018-06-26 15:04:31 +02002196 case 0:
2197 /* The object has (apparently) been initialized but it is not
2198 * in use. It's ok to call abort on such an object, and there's
2199 * nothing to do. */
2200 break;
John Durkopee4e6602020-11-27 08:48:46 -08002201#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002202 case PSA_ALG_MD2:
2203 mbedtls_md2_free( &operation->ctx.md2 );
2204 break;
2205#endif
John Durkopee4e6602020-11-27 08:48:46 -08002206#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002207 case PSA_ALG_MD4:
2208 mbedtls_md4_free( &operation->ctx.md4 );
2209 break;
2210#endif
John Durkopee4e6602020-11-27 08:48:46 -08002211#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002212 case PSA_ALG_MD5:
2213 mbedtls_md5_free( &operation->ctx.md5 );
2214 break;
2215#endif
John Durkopee4e6602020-11-27 08:48:46 -08002216#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217 case PSA_ALG_RIPEMD160:
2218 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2219 break;
2220#endif
John Durkopee4e6602020-11-27 08:48:46 -08002221#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002222 case PSA_ALG_SHA_1:
2223 mbedtls_sha1_free( &operation->ctx.sha1 );
2224 break;
2225#endif
John Durkop6ca23272020-12-03 06:01:32 -08002226#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002227 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002228 mbedtls_sha256_free( &operation->ctx.sha256 );
2229 break;
2230#endif
2231#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002232 case PSA_ALG_SHA_256:
2233 mbedtls_sha256_free( &operation->ctx.sha256 );
2234 break;
2235#endif
John Durkop6ca23272020-12-03 06:01:32 -08002236#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002238 mbedtls_sha512_free( &operation->ctx.sha512 );
2239 break;
2240#endif
2241#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002242 case PSA_ALG_SHA_512:
2243 mbedtls_sha512_free( &operation->ctx.sha512 );
2244 break;
2245#endif
2246 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002247 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 }
2249 operation->alg = 0;
2250 return( PSA_SUCCESS );
2251}
2252
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002253psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 psa_algorithm_t alg )
2255{
Janos Follath24eed8d2019-11-22 13:21:35 +00002256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002257
2258 /* A context must be freshly initialized before it can be set up. */
2259 if( operation->alg != 0 )
2260 {
2261 return( PSA_ERROR_BAD_STATE );
2262 }
2263
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 switch( alg )
2265 {
John Durkopee4e6602020-11-27 08:48:46 -08002266#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267 case PSA_ALG_MD2:
2268 mbedtls_md2_init( &operation->ctx.md2 );
2269 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2270 break;
2271#endif
John Durkopee4e6602020-11-27 08:48:46 -08002272#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002273 case PSA_ALG_MD4:
2274 mbedtls_md4_init( &operation->ctx.md4 );
2275 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2276 break;
2277#endif
John Durkopee4e6602020-11-27 08:48:46 -08002278#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002279 case PSA_ALG_MD5:
2280 mbedtls_md5_init( &operation->ctx.md5 );
2281 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2282 break;
2283#endif
John Durkopee4e6602020-11-27 08:48:46 -08002284#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002285 case PSA_ALG_RIPEMD160:
2286 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2287 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2288 break;
2289#endif
John Durkopee4e6602020-11-27 08:48:46 -08002290#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291 case PSA_ALG_SHA_1:
2292 mbedtls_sha1_init( &operation->ctx.sha1 );
2293 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2294 break;
2295#endif
John Durkopee4e6602020-11-27 08:48:46 -08002296#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297 case PSA_ALG_SHA_224:
2298 mbedtls_sha256_init( &operation->ctx.sha256 );
2299 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2300 break;
John Durkopee4e6602020-11-27 08:48:46 -08002301#endif
2302#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002303 case PSA_ALG_SHA_256:
2304 mbedtls_sha256_init( &operation->ctx.sha256 );
2305 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2306 break;
2307#endif
John Durkopee4e6602020-11-27 08:48:46 -08002308#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002309 case PSA_ALG_SHA_384:
2310 mbedtls_sha512_init( &operation->ctx.sha512 );
2311 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2312 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002313#endif
John Durkopee4e6602020-11-27 08:48:46 -08002314#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002315 case PSA_ALG_SHA_512:
2316 mbedtls_sha512_init( &operation->ctx.sha512 );
2317 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2318 break;
2319#endif
2320 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002321 return( PSA_ALG_IS_HASH( alg ) ?
2322 PSA_ERROR_NOT_SUPPORTED :
2323 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002324 }
2325 if( ret == 0 )
2326 operation->alg = alg;
2327 else
2328 psa_hash_abort( operation );
2329 return( mbedtls_to_psa_error( ret ) );
2330}
2331
2332psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2333 const uint8_t *input,
2334 size_t input_length )
2335{
Janos Follath24eed8d2019-11-22 13:21:35 +00002336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002337
2338 /* Don't require hash implementations to behave correctly on a
2339 * zero-length input, which may have an invalid pointer. */
2340 if( input_length == 0 )
2341 return( PSA_SUCCESS );
2342
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343 switch( operation->alg )
2344 {
John Durkopee4e6602020-11-27 08:48:46 -08002345#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 case PSA_ALG_MD2:
2347 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2348 input, input_length );
2349 break;
2350#endif
John Durkopee4e6602020-11-27 08:48:46 -08002351#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352 case PSA_ALG_MD4:
2353 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2354 input, input_length );
2355 break;
2356#endif
John Durkopee4e6602020-11-27 08:48:46 -08002357#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002358 case PSA_ALG_MD5:
2359 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2360 input, input_length );
2361 break;
2362#endif
John Durkopee4e6602020-11-27 08:48:46 -08002363#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002364 case PSA_ALG_RIPEMD160:
2365 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2366 input, input_length );
2367 break;
2368#endif
John Durkopee4e6602020-11-27 08:48:46 -08002369#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002370 case PSA_ALG_SHA_1:
2371 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2372 input, input_length );
2373 break;
2374#endif
John Durkop6ca23272020-12-03 06:01:32 -08002375#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002376 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002377 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2378 input, input_length );
2379 break;
2380#endif
2381#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002382 case PSA_ALG_SHA_256:
2383 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2384 input, input_length );
2385 break;
2386#endif
John Durkop6ca23272020-12-03 06:01:32 -08002387#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002388 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002389 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2390 input, input_length );
2391 break;
2392#endif
2393#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002394 case PSA_ALG_SHA_512:
2395 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2396 input, input_length );
2397 break;
2398#endif
2399 default:
John Durkopee4e6602020-11-27 08:48:46 -08002400 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002401 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002402 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002403
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 if( ret != 0 )
2405 psa_hash_abort( operation );
2406 return( mbedtls_to_psa_error( ret ) );
2407}
2408
2409psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2410 uint8_t *hash,
2411 size_t hash_size,
2412 size_t *hash_length )
2413{
itayzafrir40835d42018-08-02 13:14:17 +03002414 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002415 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002416 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002417
2418 /* Fill the output buffer with something that isn't a valid hash
2419 * (barring an attack on the hash and deliberately-crafted input),
2420 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002421 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002422 /* If hash_size is 0 then hash may be NULL and then the
2423 * call to memset would have undefined behavior. */
2424 if( hash_size != 0 )
2425 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426
2427 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002428 {
2429 status = PSA_ERROR_BUFFER_TOO_SMALL;
2430 goto exit;
2431 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002432
2433 switch( operation->alg )
2434 {
John Durkopee4e6602020-11-27 08:48:46 -08002435#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002436 case PSA_ALG_MD2:
2437 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2438 break;
2439#endif
John Durkopee4e6602020-11-27 08:48:46 -08002440#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002441 case PSA_ALG_MD4:
2442 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2443 break;
2444#endif
John Durkopee4e6602020-11-27 08:48:46 -08002445#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002446 case PSA_ALG_MD5:
2447 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2448 break;
2449#endif
John Durkopee4e6602020-11-27 08:48:46 -08002450#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002451 case PSA_ALG_RIPEMD160:
2452 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2453 break;
2454#endif
John Durkopee4e6602020-11-27 08:48:46 -08002455#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002456 case PSA_ALG_SHA_1:
2457 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2458 break;
2459#endif
John Durkop6ca23272020-12-03 06:01:32 -08002460#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002461 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002462 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2463 break;
2464#endif
2465#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002466 case PSA_ALG_SHA_256:
2467 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2468 break;
2469#endif
John Durkop6ca23272020-12-03 06:01:32 -08002470#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002472 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2473 break;
2474#endif
2475#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002476 case PSA_ALG_SHA_512:
2477 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2478 break;
2479#endif
2480 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002481 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482 }
itayzafrir40835d42018-08-02 13:14:17 +03002483 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484
itayzafrir40835d42018-08-02 13:14:17 +03002485exit:
2486 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002487 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002488 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 return( psa_hash_abort( operation ) );
2490 }
2491 else
2492 {
2493 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002494 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002495 }
2496}
2497
Gilles Peskine2d277862018-06-18 15:41:12 +02002498psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2499 const uint8_t *hash,
2500 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002501{
2502 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2503 size_t actual_hash_length;
2504 psa_status_t status = psa_hash_finish( operation,
2505 actual_hash, sizeof( actual_hash ),
2506 &actual_hash_length );
2507 if( status != PSA_SUCCESS )
2508 return( status );
2509 if( actual_hash_length != hash_length )
2510 return( PSA_ERROR_INVALID_SIGNATURE );
2511 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2512 return( PSA_ERROR_INVALID_SIGNATURE );
2513 return( PSA_SUCCESS );
2514}
2515
Gilles Peskine0a749c82019-11-28 19:33:58 +01002516psa_status_t psa_hash_compute( psa_algorithm_t alg,
2517 const uint8_t *input, size_t input_length,
2518 uint8_t *hash, size_t hash_size,
2519 size_t *hash_length )
2520{
2521 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2522 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2523
2524 *hash_length = hash_size;
2525 status = psa_hash_setup( &operation, alg );
2526 if( status != PSA_SUCCESS )
2527 goto exit;
2528 status = psa_hash_update( &operation, input, input_length );
2529 if( status != PSA_SUCCESS )
2530 goto exit;
2531 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2532 if( status != PSA_SUCCESS )
2533 goto exit;
2534
2535exit:
2536 if( status == PSA_SUCCESS )
2537 status = psa_hash_abort( &operation );
2538 else
2539 psa_hash_abort( &operation );
2540 return( status );
2541}
2542
2543psa_status_t psa_hash_compare( psa_algorithm_t alg,
2544 const uint8_t *input, size_t input_length,
2545 const uint8_t *hash, size_t hash_length )
2546{
2547 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2548 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2549
2550 status = psa_hash_setup( &operation, alg );
2551 if( status != PSA_SUCCESS )
2552 goto exit;
2553 status = psa_hash_update( &operation, input, input_length );
2554 if( status != PSA_SUCCESS )
2555 goto exit;
2556 status = psa_hash_verify( &operation, hash, hash_length );
2557 if( status != PSA_SUCCESS )
2558 goto exit;
2559
2560exit:
2561 if( status == PSA_SUCCESS )
2562 status = psa_hash_abort( &operation );
2563 else
2564 psa_hash_abort( &operation );
2565 return( status );
2566}
2567
Gilles Peskineeb35d782019-01-22 17:56:16 +01002568psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2569 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002570{
2571 if( target_operation->alg != 0 )
2572 return( PSA_ERROR_BAD_STATE );
2573
2574 switch( source_operation->alg )
2575 {
2576 case 0:
2577 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002578#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002579 case PSA_ALG_MD2:
2580 mbedtls_md2_clone( &target_operation->ctx.md2,
2581 &source_operation->ctx.md2 );
2582 break;
2583#endif
John Durkopee4e6602020-11-27 08:48:46 -08002584#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002585 case PSA_ALG_MD4:
2586 mbedtls_md4_clone( &target_operation->ctx.md4,
2587 &source_operation->ctx.md4 );
2588 break;
2589#endif
John Durkopee4e6602020-11-27 08:48:46 -08002590#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002591 case PSA_ALG_MD5:
2592 mbedtls_md5_clone( &target_operation->ctx.md5,
2593 &source_operation->ctx.md5 );
2594 break;
2595#endif
John Durkopee4e6602020-11-27 08:48:46 -08002596#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002597 case PSA_ALG_RIPEMD160:
2598 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2599 &source_operation->ctx.ripemd160 );
2600 break;
2601#endif
John Durkopee4e6602020-11-27 08:48:46 -08002602#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002603 case PSA_ALG_SHA_1:
2604 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2605 &source_operation->ctx.sha1 );
2606 break;
2607#endif
John Durkop6ca23272020-12-03 06:01:32 -08002608#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002609 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002610 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2611 &source_operation->ctx.sha256 );
2612 break;
2613#endif
2614#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002615 case PSA_ALG_SHA_256:
2616 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2617 &source_operation->ctx.sha256 );
2618 break;
2619#endif
John Durkop6ca23272020-12-03 06:01:32 -08002620#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002621 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002622 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2623 &source_operation->ctx.sha512 );
2624 break;
2625#endif
2626#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002627 case PSA_ALG_SHA_512:
2628 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2629 &source_operation->ctx.sha512 );
2630 break;
2631#endif
2632 default:
2633 return( PSA_ERROR_NOT_SUPPORTED );
2634 }
2635
2636 target_operation->alg = source_operation->alg;
2637 return( PSA_SUCCESS );
2638}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002639
2640
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002641/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002642/* MAC */
2643/****************************************************************/
2644
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002645static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002646 psa_algorithm_t alg,
2647 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002648 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002649 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002650{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002651 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002652 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002654 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002655 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002656
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2658 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002659 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002661 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002662 mode = MBEDTLS_MODE_STREAM;
2663 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 case PSA_ALG_CTR:
2665 mode = MBEDTLS_MODE_CTR;
2666 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002667 case PSA_ALG_CFB:
2668 mode = MBEDTLS_MODE_CFB;
2669 break;
2670 case PSA_ALG_OFB:
2671 mode = MBEDTLS_MODE_OFB;
2672 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002673 case PSA_ALG_ECB_NO_PADDING:
2674 mode = MBEDTLS_MODE_ECB;
2675 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002676 case PSA_ALG_CBC_NO_PADDING:
2677 mode = MBEDTLS_MODE_CBC;
2678 break;
2679 case PSA_ALG_CBC_PKCS7:
2680 mode = MBEDTLS_MODE_CBC;
2681 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002682 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002683 mode = MBEDTLS_MODE_CCM;
2684 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002685 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 mode = MBEDTLS_MODE_GCM;
2687 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002688 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002689 mode = MBEDTLS_MODE_CHACHAPOLY;
2690 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002691 default:
2692 return( NULL );
2693 }
2694 }
2695 else if( alg == PSA_ALG_CMAC )
2696 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002697 else
2698 return( NULL );
2699
2700 switch( key_type )
2701 {
2702 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002703 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 break;
2705 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002706 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2707 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002709 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002711 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002712 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2713 * but two-key Triple-DES is functionally three-key Triple-DES
2714 * with K1=K3, so that's how we present it to mbedtls. */
2715 if( key_bits == 128 )
2716 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717 break;
2718 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002719 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002720 break;
2721 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002722 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002723 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002724 case PSA_KEY_TYPE_CHACHA20:
2725 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2726 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 default:
2728 return( NULL );
2729 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002730 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002731 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732
Jaeden Amero23bbb752018-06-26 14:16:54 +01002733 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2734 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002735}
2736
John Durkop6ba40d12020-11-10 08:50:04 -08002737#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002738static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002739{
Gilles Peskine2d277862018-06-18 15:41:12 +02002740 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002741 {
2742 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002743 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002744 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002745 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002746 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002747 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002748 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002749 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002750 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002751 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002752 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002753 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002754 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002755 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002756 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002757 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002758 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002759 return( 128 );
2760 default:
2761 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002762 }
2763}
John Durkop07cc04a2020-11-16 22:08:34 -08002764#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002765
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002766/* Initialize the MAC operation structure. Once this function has been
2767 * called, psa_mac_abort can run and will do the right thing. */
2768static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2769 psa_algorithm_t alg )
2770{
2771 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2772
Steven Cooreman15472f82021-03-02 16:16:22 +01002773 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002774 operation->key_set = 0;
2775 operation->iv_set = 0;
2776 operation->iv_required = 0;
2777 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002778 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002779
2780#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002781 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002782 {
2783 operation->iv_required = 0;
2784 mbedtls_cipher_init( &operation->ctx.cmac );
2785 status = PSA_SUCCESS;
2786 }
2787 else
2788#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002789#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002790 if( PSA_ALG_IS_HMAC( operation->alg ) )
2791 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002792 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2793 operation->ctx.hmac.hash_ctx.alg = 0;
2794 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002795 }
2796 else
John Durkopd0321952020-10-29 21:37:36 -07002797#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002798 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002799 if( ! PSA_ALG_IS_MAC( alg ) )
2800 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801 }
2802
2803 if( status != PSA_SUCCESS )
2804 memset( operation, 0, sizeof( *operation ) );
2805 return( status );
2806}
2807
John Durkop6ba40d12020-11-10 08:50:04 -08002808#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002809static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2810{
Gilles Peskine3f108122018-12-07 18:14:53 +01002811 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002812 return( psa_hash_abort( &hmac->hash_ctx ) );
2813}
John Durkop6ba40d12020-11-10 08:50:04 -08002814#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002815
Gilles Peskine8c9def32018-02-08 10:02:12 +01002816psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2817{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002819 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002820 /* The object has (apparently) been initialized but it is not
2821 * in use. It's ok to call abort on such an object, and there's
2822 * nothing to do. */
2823 return( PSA_SUCCESS );
2824 }
2825 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 if( operation->alg == PSA_ALG_CMAC )
2828 {
2829 mbedtls_cipher_free( &operation->ctx.cmac );
2830 }
2831 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002833#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 if( PSA_ALG_IS_HMAC( operation->alg ) )
2835 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002836 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002837 }
2838 else
John Durkopd0321952020-10-29 21:37:36 -07002839#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 {
2841 /* Sanity check (shouldn't happen: operation->alg should
2842 * always have been initialized to a valid value). */
2843 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002844 }
Moran Peker41deec42018-04-04 15:43:05 +03002845
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846 operation->alg = 0;
2847 operation->key_set = 0;
2848 operation->iv_set = 0;
2849 operation->iv_required = 0;
2850 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002851 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002852
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002854
2855bad_state:
2856 /* If abort is called on an uninitialized object, we can't trust
2857 * anything. Wipe the object in case it contains confidential data.
2858 * This may result in a memory leak if a pointer gets overwritten,
2859 * but it's too late to do anything about this. */
2860 memset( operation, 0, sizeof( *operation ) );
2861 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862}
2863
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002864#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002865static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2866 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002867{
Janos Follath24eed8d2019-11-22 13:21:35 +00002868 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002869 const mbedtls_cipher_info_t *cipher_info =
2870 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2871 slot->attr.type, slot->attr.bits,
2872 NULL );
2873 if( cipher_info == NULL )
2874 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002875
2876 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2877 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002878 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002879
2880 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002881 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002882 slot->attr.bits );
2883exit:
2884 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002885}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002886#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002887
John Durkop6ba40d12020-11-10 08:50:04 -08002888#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002889static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2890 const uint8_t *key,
2891 size_t key_length,
2892 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002893{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002894 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002896 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002897 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002898 psa_status_t status;
2899
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002900 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2901 * overflow below. This should never trigger if the hash algorithm
2902 * is implemented correctly. */
2903 /* The size checks against the ipad and opad buffers cannot be written
2904 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2905 * because that triggers -Wlogical-op on GCC 7.3. */
2906 if( block_size > sizeof( ipad ) )
2907 return( PSA_ERROR_NOT_SUPPORTED );
2908 if( block_size > sizeof( hmac->opad ) )
2909 return( PSA_ERROR_NOT_SUPPORTED );
2910 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002911 return( PSA_ERROR_NOT_SUPPORTED );
2912
Gilles Peskined223b522018-06-11 18:12:58 +02002913 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002914 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002915 status = psa_hash_compute( hash_alg, key, key_length,
2916 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002918 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002919 }
Gilles Peskine96889972018-07-12 17:07:03 +02002920 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2921 * but it is permitted. It is common when HMAC is used in HKDF, for
2922 * example. Don't call `memcpy` in the 0-length because `key` could be
2923 * an invalid pointer which would make the behavior undefined. */
2924 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002925 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002926
Gilles Peskined223b522018-06-11 18:12:58 +02002927 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2928 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002929 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002930 ipad[i] ^= 0x36;
2931 memset( ipad + key_length, 0x36, block_size - key_length );
2932
2933 /* Copy the key material from ipad to opad, flipping the requisite bits,
2934 * and filling the rest of opad with the requisite constant. */
2935 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002936 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2937 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002938
Gilles Peskine01126fa2018-07-12 17:04:55 +02002939 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002940 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002941 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002942
Gilles Peskine01126fa2018-07-12 17:04:55 +02002943 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002944
2945cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002946 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002947
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002948 return( status );
2949}
John Durkop6ba40d12020-11-10 08:50:04 -08002950#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951
Gilles Peskine89167cb2018-07-08 20:12:23 +02002952static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002953 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002954 psa_algorithm_t alg,
2955 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002956{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002959 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002960 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002961 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002962
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002963 /* A context must be freshly initialized before it can be set up. */
2964 if( operation->alg != 0 )
2965 {
2966 return( PSA_ERROR_BAD_STATE );
2967 }
2968
Steven Cooreman15472f82021-03-02 16:16:22 +01002969 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002970 if( status != PSA_SUCCESS )
2971 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002972 if( is_sign )
2973 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002974
Ronald Cron5c522922020-11-14 16:35:34 +01002975 status = psa_get_and_lock_transparent_key_slot_with_policy(
2976 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002977 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002978 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002979
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002980 /* Validate the combination of key type and algorithm */
2981 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002985 /* Get the output length for the algorithm and key combination. None of the
2986 * currently supported algorithms have an output length dependent on actual
2987 * key size, so setting it to a bogus value is currently OK. */
2988 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002989
2990 if( operation->mac_size < 4 )
2991 {
2992 /* A very short MAC is too short for security since it can be
2993 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2994 * so we make this our minimum, even though 32 bits is still
2995 * too small for security. */
2996 status = PSA_ERROR_NOT_SUPPORTED;
2997 goto exit;
2998 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002999
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01003000 if( operation->mac_size >
3001 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
3002 {
3003 /* It's impossible to "truncate" to a larger length than the full length
3004 * of the algorithm. */
3005 status = PSA_ERROR_INVALID_ARGUMENT;
3006 goto exit;
3007 }
3008
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01003010 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003011 {
Steven Cooreman15472f82021-03-02 16:16:22 +01003012 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003013 }
3014 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003015#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003016#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003017 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003019 /* Sanity check. This shouldn't fail on a valid configuration. */
3020 if( operation->mac_size == 0 ||
3021 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3022 {
3023 status = PSA_ERROR_NOT_SUPPORTED;
3024 goto exit;
3025 }
3026
Gilles Peskine8e338702019-07-30 20:06:31 +02003027 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003028 {
3029 status = PSA_ERROR_INVALID_ARGUMENT;
3030 goto exit;
3031 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003032
Gilles Peskine01126fa2018-07-12 17:04:55 +02003033 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003034 slot->key.data,
3035 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003036 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 }
3038 else
John Durkopd0321952020-10-29 21:37:36 -07003039#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003040 {
3041 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003042 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003043
Gilles Peskinefbfac682018-07-08 20:51:54 +02003044exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003045 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003046 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003047 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003049 else
3050 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003051 operation->key_set = 1;
3052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003053
Ronald Cron5c522922020-11-14 16:35:34 +01003054 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003055
Ronald Cron5c522922020-11-14 16:35:34 +01003056 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057}
3058
Gilles Peskine89167cb2018-07-08 20:12:23 +02003059psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003060 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003061 psa_algorithm_t alg )
3062{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003063 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003064}
3065
3066psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003067 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003068 psa_algorithm_t alg )
3069{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003070 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003071}
3072
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3074 const uint8_t *input,
3075 size_t input_length )
3076{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003077 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003079 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003081 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003082 operation->has_input = 1;
3083
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003085 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003086 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003087 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3088 input, input_length );
3089 status = mbedtls_to_psa_error( ret );
3090 }
3091 else
3092#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003093#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003094 if( PSA_ALG_IS_HMAC( operation->alg ) )
3095 {
3096 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3097 input_length );
3098 }
3099 else
John Durkopd0321952020-10-29 21:37:36 -07003100#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003101 {
3102 /* This shouldn't happen if `operation` was initialized by
3103 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003104 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003105 }
3106
Gilles Peskinefbfac682018-07-08 20:51:54 +02003107 if( status != PSA_SUCCESS )
3108 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003109 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003110}
3111
John Durkop6ba40d12020-11-10 08:50:04 -08003112#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3114 uint8_t *mac,
3115 size_t mac_size )
3116{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003117 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003118 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3119 size_t hash_size = 0;
3120 size_t block_size = psa_get_hash_block_size( hash_alg );
3121 psa_status_t status;
3122
Gilles Peskine01126fa2018-07-12 17:04:55 +02003123 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3124 if( status != PSA_SUCCESS )
3125 return( status );
3126 /* From here on, tmp needs to be wiped. */
3127
3128 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3129 if( status != PSA_SUCCESS )
3130 goto exit;
3131
3132 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3133 if( status != PSA_SUCCESS )
3134 goto exit;
3135
3136 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3137 if( status != PSA_SUCCESS )
3138 goto exit;
3139
Gilles Peskined911eb72018-08-14 15:18:45 +02003140 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3141 if( status != PSA_SUCCESS )
3142 goto exit;
3143
3144 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003145
3146exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003147 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003148 return( status );
3149}
John Durkop6ba40d12020-11-10 08:50:04 -08003150#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003151
mohammad16036df908f2018-04-02 08:34:15 -07003152static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003153 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003154 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003155{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003156 if( ! operation->key_set )
3157 return( PSA_ERROR_BAD_STATE );
3158 if( operation->iv_required && ! operation->iv_set )
3159 return( PSA_ERROR_BAD_STATE );
3160
Gilles Peskine8c9def32018-02-08 10:02:12 +01003161 if( mac_size < operation->mac_size )
3162 return( PSA_ERROR_BUFFER_TOO_SMALL );
3163
Gilles Peskine8c9def32018-02-08 10:02:12 +01003164#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003165 if( operation->alg == PSA_ALG_CMAC )
3166 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003167 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003168 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3169 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003170 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003171 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003172 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003174 else
3175#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003176#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177 if( PSA_ALG_IS_HMAC( operation->alg ) )
3178 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003179 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003180 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181 }
3182 else
John Durkopd0321952020-10-29 21:37:36 -07003183#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003184 {
3185 /* This shouldn't happen if `operation` was initialized by
3186 * a setup function. */
3187 return( PSA_ERROR_BAD_STATE );
3188 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003189}
3190
Gilles Peskineacd4be32018-07-08 19:56:25 +02003191psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3192 uint8_t *mac,
3193 size_t mac_size,
3194 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003195{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003196 psa_status_t status;
3197
Jaeden Amero252ef282019-02-15 14:05:35 +00003198 if( operation->alg == 0 )
3199 {
3200 return( PSA_ERROR_BAD_STATE );
3201 }
3202
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003203 /* Fill the output buffer with something that isn't a valid mac
3204 * (barring an attack on the mac and deliberately-crafted input),
3205 * in case the caller doesn't check the return status properly. */
3206 *mac_length = mac_size;
3207 /* If mac_size is 0 then mac may be NULL and then the
3208 * call to memset would have undefined behavior. */
3209 if( mac_size != 0 )
3210 memset( mac, '!', mac_size );
3211
Gilles Peskine89167cb2018-07-08 20:12:23 +02003212 if( ! operation->is_sign )
3213 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003214 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003215 }
mohammad16036df908f2018-04-02 08:34:15 -07003216
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003217 status = psa_mac_finish_internal( operation, mac, mac_size );
3218
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003219 if( status == PSA_SUCCESS )
3220 {
3221 status = psa_mac_abort( operation );
3222 if( status == PSA_SUCCESS )
3223 *mac_length = operation->mac_size;
3224 else
3225 memset( mac, '!', mac_size );
3226 }
3227 else
3228 psa_mac_abort( operation );
3229 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003230}
3231
Gilles Peskineacd4be32018-07-08 19:56:25 +02003232psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3233 const uint8_t *mac,
3234 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003235{
Gilles Peskine828ed142018-06-18 23:25:51 +02003236 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003237 psa_status_t status;
3238
Jaeden Amero252ef282019-02-15 14:05:35 +00003239 if( operation->alg == 0 )
3240 {
3241 return( PSA_ERROR_BAD_STATE );
3242 }
3243
Gilles Peskine89167cb2018-07-08 20:12:23 +02003244 if( operation->is_sign )
3245 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003246 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003247 }
3248 if( operation->mac_size != mac_length )
3249 {
3250 status = PSA_ERROR_INVALID_SIGNATURE;
3251 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003252 }
mohammad16036df908f2018-04-02 08:34:15 -07003253
3254 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003255 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003256 if( status != PSA_SUCCESS )
3257 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003258
3259 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3260 status = PSA_ERROR_INVALID_SIGNATURE;
3261
3262cleanup:
3263 if( status == PSA_SUCCESS )
3264 status = psa_mac_abort( operation );
3265 else
3266 psa_mac_abort( operation );
3267
Gilles Peskine3f108122018-12-07 18:14:53 +01003268 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003269
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003270 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003271}
3272
3273
Gilles Peskine20035e32018-02-03 22:44:14 +01003274
Gilles Peskine20035e32018-02-03 22:44:14 +01003275/****************************************************************/
3276/* Asymmetric cryptography */
3277/****************************************************************/
3278
John Durkop6ba40d12020-11-10 08:50:04 -08003279#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3280 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003281/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003282 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003283static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3284 size_t hash_length,
3285 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003286{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003287 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003288 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003289 *md_alg = mbedtls_md_get_type( md_info );
3290
3291 /* The Mbed TLS RSA module uses an unsigned int for hash length
3292 * parameters. Validate that it fits so that we don't risk an
3293 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003294#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003295 if( hash_length > UINT_MAX )
3296 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003297#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003298
John Durkop0e005192020-10-31 22:06:54 -07003299#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003300 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3301 * must be correct. */
3302 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3303 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003304 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003305 if( md_info == NULL )
3306 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003307 if( mbedtls_md_get_size( md_info ) != hash_length )
3308 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003309 }
John Durkop0e005192020-10-31 22:06:54 -07003310#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003311
John Durkop0e005192020-10-31 22:06:54 -07003312#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003313 /* PSS requires a hash internally. */
3314 if( PSA_ALG_IS_RSA_PSS( alg ) )
3315 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003316 if( md_info == NULL )
3317 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003318 }
John Durkop0e005192020-10-31 22:06:54 -07003319#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003320
Gilles Peskine61b91d42018-06-08 16:09:36 +02003321 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003322}
3323
Gilles Peskine2b450e32018-06-27 15:42:46 +02003324static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3325 psa_algorithm_t alg,
3326 const uint8_t *hash,
3327 size_t hash_length,
3328 uint8_t *signature,
3329 size_t signature_size,
3330 size_t *signature_length )
3331{
3332 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003334 mbedtls_md_type_t md_alg;
3335
3336 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3337 if( status != PSA_SUCCESS )
3338 return( status );
3339
Gilles Peskine630a18a2018-06-29 17:49:35 +02003340 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003341 return( PSA_ERROR_BUFFER_TOO_SMALL );
3342
John Durkop0e005192020-10-31 22:06:54 -07003343#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003344 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3345 {
3346 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3347 MBEDTLS_MD_NONE );
3348 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003349 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003350 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003351 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003352 md_alg,
3353 (unsigned int) hash_length,
3354 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003355 signature );
3356 }
3357 else
John Durkop0e005192020-10-31 22:06:54 -07003358#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3359#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003360 if( PSA_ALG_IS_RSA_PSS( alg ) )
3361 {
3362 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3363 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003364 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003365 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003366 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003367 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003368 (unsigned int) hash_length,
3369 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003370 signature );
3371 }
3372 else
John Durkop0e005192020-10-31 22:06:54 -07003373#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003374 {
3375 return( PSA_ERROR_INVALID_ARGUMENT );
3376 }
3377
3378 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003379 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003380 return( mbedtls_to_psa_error( ret ) );
3381}
3382
3383static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3384 psa_algorithm_t alg,
3385 const uint8_t *hash,
3386 size_t hash_length,
3387 const uint8_t *signature,
3388 size_t signature_length )
3389{
3390 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003392 mbedtls_md_type_t md_alg;
3393
3394 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3395 if( status != PSA_SUCCESS )
3396 return( status );
3397
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003398 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3399 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003400
John Durkop0e005192020-10-31 22:06:54 -07003401#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003402 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3403 {
3404 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3405 MBEDTLS_MD_NONE );
3406 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003407 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003408 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003409 MBEDTLS_RSA_PUBLIC,
3410 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003411 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003412 hash,
3413 signature );
3414 }
3415 else
John Durkop0e005192020-10-31 22:06:54 -07003416#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3417#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003418 if( PSA_ALG_IS_RSA_PSS( alg ) )
3419 {
3420 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3421 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003422 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003423 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003424 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003425 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003426 (unsigned int) hash_length,
3427 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003428 signature );
3429 }
3430 else
John Durkop0e005192020-10-31 22:06:54 -07003431#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003432 {
3433 return( PSA_ERROR_INVALID_ARGUMENT );
3434 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003435
3436 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3437 * the rest of the signature is invalid". This has little use in
3438 * practice and PSA doesn't report this distinction. */
3439 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3440 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003441 return( mbedtls_to_psa_error( ret ) );
3442}
John Durkop6ba40d12020-11-10 08:50:04 -08003443#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3444 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003445
John Durkop6ba40d12020-11-10 08:50:04 -08003446#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3447 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003448/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3449 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3450 * (even though these functions don't modify it). */
3451static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3452 psa_algorithm_t alg,
3453 const uint8_t *hash,
3454 size_t hash_length,
3455 uint8_t *signature,
3456 size_t signature_size,
3457 size_t *signature_length )
3458{
Janos Follath24eed8d2019-11-22 13:21:35 +00003459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003460 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003461 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462 mbedtls_mpi_init( &r );
3463 mbedtls_mpi_init( &s );
3464
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003465 if( signature_size < 2 * curve_bytes )
3466 {
3467 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3468 goto cleanup;
3469 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470
John Durkop0ea39e02020-10-13 19:58:20 -07003471#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003472 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3473 {
3474 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3475 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3476 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003477 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3478 &ecp->d, hash,
3479 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003480 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003481 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003482 }
3483 else
John Durkop0ea39e02020-10-13 19:58:20 -07003484#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003485 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003486 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003487 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3488 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003489 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003490 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003491 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003492
3493 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3494 signature,
3495 curve_bytes ) );
3496 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3497 signature + curve_bytes,
3498 curve_bytes ) );
3499
3500cleanup:
3501 mbedtls_mpi_free( &r );
3502 mbedtls_mpi_free( &s );
3503 if( ret == 0 )
3504 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003505 return( mbedtls_to_psa_error( ret ) );
3506}
3507
3508static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3509 const uint8_t *hash,
3510 size_t hash_length,
3511 const uint8_t *signature,
3512 size_t signature_length )
3513{
Janos Follath24eed8d2019-11-22 13:21:35 +00003514 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003515 mbedtls_mpi r, s;
3516 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3517 mbedtls_mpi_init( &r );
3518 mbedtls_mpi_init( &s );
3519
3520 if( signature_length != 2 * curve_bytes )
3521 return( PSA_ERROR_INVALID_SIGNATURE );
3522
3523 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3524 signature,
3525 curve_bytes ) );
3526 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3527 signature + curve_bytes,
3528 curve_bytes ) );
3529
Steven Cooremanacda8342020-07-24 23:09:52 +02003530 /* Check whether the public part is loaded. If not, load it. */
3531 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3532 {
3533 MBEDTLS_MPI_CHK(
3534 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003535 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003536 }
3537
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003538 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3539 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540
3541cleanup:
3542 mbedtls_mpi_free( &r );
3543 mbedtls_mpi_free( &s );
3544 return( mbedtls_to_psa_error( ret ) );
3545}
John Durkop6ba40d12020-11-10 08:50:04 -08003546#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3547 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003548
Ronald Croncf56a0a2020-08-04 09:51:30 +02003549psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003550 psa_algorithm_t alg,
3551 const uint8_t *hash,
3552 size_t hash_length,
3553 uint8_t *signature,
3554 size_t signature_size,
3555 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003556{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003557 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003558 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003559 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003560
3561 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003562 /* Immediately reject a zero-length signature buffer. This guarantees
3563 * that signature must be a valid pointer. (On the other hand, the hash
3564 * buffer can in principle be empty since it doesn't actually have
3565 * to be a hash.) */
3566 if( signature_size == 0 )
3567 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003568
Ronald Cron5c522922020-11-14 16:35:34 +01003569 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3570 PSA_KEY_USAGE_SIGN_HASH,
3571 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003572 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003573 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003574 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003575 {
3576 status = PSA_ERROR_INVALID_ARGUMENT;
3577 goto exit;
3578 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003579
Steven Cooremancd84cb42020-07-16 20:28:36 +02003580 /* Try any of the available accelerators first */
3581 status = psa_driver_wrapper_sign_hash( slot,
3582 alg,
3583 hash,
3584 hash_length,
3585 signature,
3586 signature_size,
3587 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003588 if( status != PSA_ERROR_NOT_SUPPORTED ||
3589 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003590 goto exit;
3591
Steven Cooreman7a250572020-07-17 16:43:05 +02003592 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003593#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3594 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003596 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003597 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003598
Ronald Cron90857082020-11-25 14:58:33 +01003599 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3600 slot->key.data,
3601 slot->key.bytes,
3602 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003603 if( status != PSA_SUCCESS )
3604 goto exit;
3605
Steven Cooremana2371e52020-07-28 14:30:39 +02003606 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003607 alg,
3608 hash, hash_length,
3609 signature, signature_size,
3610 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003611
Steven Cooremana2371e52020-07-28 14:30:39 +02003612 mbedtls_rsa_free( rsa );
3613 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003614 }
3615 else
John Durkop6ba40d12020-11-10 08:50:04 -08003616#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3617 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003618 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003619 {
John Durkop9814fa22020-11-04 12:28:15 -08003620#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3621 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003622 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003623#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003624 PSA_ALG_IS_ECDSA( alg )
3625#else
3626 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3627#endif
3628 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003629 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003630 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003631 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003632 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003633 slot->key.data,
3634 slot->key.bytes,
3635 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003636 if( status != PSA_SUCCESS )
3637 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003638 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003639 alg,
3640 hash, hash_length,
3641 signature, signature_size,
3642 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003643 mbedtls_ecp_keypair_free( ecp );
3644 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003645 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003646 else
John Durkop9814fa22020-11-04 12:28:15 -08003647#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3648 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003649 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003651 }
itayzafrir5c753392018-05-08 11:18:38 +03003652 }
3653 else
itayzafrir5c753392018-05-08 11:18:38 +03003654 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003655 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003656 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003657
3658exit:
3659 /* Fill the unused part of the output buffer (the whole buffer on error,
3660 * the trailing part on success) with something that isn't a valid mac
3661 * (barring an attack on the mac and deliberately-crafted input),
3662 * in case the caller doesn't check the return status properly. */
3663 if( status == PSA_SUCCESS )
3664 memset( signature + *signature_length, '!',
3665 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003666 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003667 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003668 /* If signature_size is 0 then we have nothing to do. We must not call
3669 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003670
Ronald Cron5c522922020-11-14 16:35:34 +01003671 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003672
Ronald Cron5c522922020-11-14 16:35:34 +01003673 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003674}
3675
Ronald Croncf56a0a2020-08-04 09:51:30 +02003676psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003677 psa_algorithm_t alg,
3678 const uint8_t *hash,
3679 size_t hash_length,
3680 const uint8_t *signature,
3681 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003682{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003683 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003684 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003685 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003686
Ronald Cron5c522922020-11-14 16:35:34 +01003687 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3688 PSA_KEY_USAGE_VERIFY_HASH,
3689 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003690 if( status != PSA_SUCCESS )
3691 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003692
Steven Cooreman55ae2172020-07-17 19:46:15 +02003693 /* Try any of the available accelerators first */
3694 status = psa_driver_wrapper_verify_hash( slot,
3695 alg,
3696 hash,
3697 hash_length,
3698 signature,
3699 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003700 if( status != PSA_ERROR_NOT_SUPPORTED ||
3701 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003702 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003703
John Durkop6ba40d12020-11-10 08:50:04 -08003704#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3705 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003706 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003707 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003708 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003709
Ronald Cron90857082020-11-25 14:58:33 +01003710 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3711 slot->key.data,
3712 slot->key.bytes,
3713 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003714 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003715 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003716
Steven Cooremana2371e52020-07-28 14:30:39 +02003717 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003718 alg,
3719 hash, hash_length,
3720 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003721 mbedtls_rsa_free( rsa );
3722 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003723 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 }
3725 else
John Durkop6ba40d12020-11-10 08:50:04 -08003726#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3727 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003728 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003729 {
John Durkop9814fa22020-11-04 12:28:15 -08003730#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3731 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003732 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003733 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003734 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003735 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003736 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003737 slot->key.data,
3738 slot->key.bytes,
3739 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003740 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003741 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003742 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003743 hash, hash_length,
3744 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003745 mbedtls_ecp_keypair_free( ecp );
3746 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003747 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003748 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003749 else
John Durkop9814fa22020-11-04 12:28:15 -08003750#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3751 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003752 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003753 status = PSA_ERROR_INVALID_ARGUMENT;
3754 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003755 }
itayzafrir5c753392018-05-08 11:18:38 +03003756 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003757 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003758 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003759 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003760 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003761
3762exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003763 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003764
Ronald Cron5c522922020-11-14 16:35:34 +01003765 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003766}
3767
John Durkop0e005192020-10-31 22:06:54 -07003768#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003769static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3770 mbedtls_rsa_context *rsa )
3771{
3772 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3773 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3774 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3775 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3776}
John Durkop0e005192020-10-31 22:06:54 -07003777#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003778
Ronald Croncf56a0a2020-08-04 09:51:30 +02003779psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003780 psa_algorithm_t alg,
3781 const uint8_t *input,
3782 size_t input_length,
3783 const uint8_t *salt,
3784 size_t salt_length,
3785 uint8_t *output,
3786 size_t output_size,
3787 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003788{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003789 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003790 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003791 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003792
Darryl Green5cc689a2018-07-24 15:34:10 +01003793 (void) input;
3794 (void) input_length;
3795 (void) salt;
3796 (void) output;
3797 (void) output_size;
3798
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003799 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003800
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003801 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3802 return( PSA_ERROR_INVALID_ARGUMENT );
3803
Ronald Cron5c522922020-11-14 16:35:34 +01003804 status = psa_get_and_lock_transparent_key_slot_with_policy(
3805 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003806 if( status != PSA_SUCCESS )
3807 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003808 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3809 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003810 {
3811 status = PSA_ERROR_INVALID_ARGUMENT;
3812 goto exit;
3813 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003814
John Durkop6ba40d12020-11-10 08:50:04 -08003815#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3816 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003817 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003818 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003819 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003820 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3821 slot->key.data,
3822 slot->key.bytes,
3823 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003824 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003825 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003826
3827 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003828 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003829 status = PSA_ERROR_BUFFER_TOO_SMALL;
3830 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003831 }
John Durkop0e005192020-10-31 22:06:54 -07003832#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003833 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003834 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003835 status = mbedtls_to_psa_error(
3836 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003837 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003838 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003839 MBEDTLS_RSA_PUBLIC,
3840 input_length,
3841 input,
3842 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003843 }
3844 else
John Durkop0e005192020-10-31 22:06:54 -07003845#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3846#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003847 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003848 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003849 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003850 status = mbedtls_to_psa_error(
3851 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003852 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003853 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003854 MBEDTLS_RSA_PUBLIC,
3855 salt, salt_length,
3856 input_length,
3857 input,
3858 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859 }
3860 else
John Durkop0e005192020-10-31 22:06:54 -07003861#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003862 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003863 status = PSA_ERROR_INVALID_ARGUMENT;
3864 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003865 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003866rsa_exit:
3867 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003868 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003869
Steven Cooremana2371e52020-07-28 14:30:39 +02003870 mbedtls_rsa_free( rsa );
3871 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 else
John Durkop9814fa22020-11-04 12:28:15 -08003874#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003875 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003877 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003878 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003879
3880exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003881 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003882
Ronald Cron5c522922020-11-14 16:35:34 +01003883 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003884}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885
Ronald Croncf56a0a2020-08-04 09:51:30 +02003886psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003887 psa_algorithm_t alg,
3888 const uint8_t *input,
3889 size_t input_length,
3890 const uint8_t *salt,
3891 size_t salt_length,
3892 uint8_t *output,
3893 size_t output_size,
3894 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003895{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003896 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003897 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003898 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003899
Darryl Green5cc689a2018-07-24 15:34:10 +01003900 (void) input;
3901 (void) input_length;
3902 (void) salt;
3903 (void) output;
3904 (void) output_size;
3905
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003906 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003907
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003908 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3909 return( PSA_ERROR_INVALID_ARGUMENT );
3910
Ronald Cron5c522922020-11-14 16:35:34 +01003911 status = psa_get_and_lock_transparent_key_slot_with_policy(
3912 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003913 if( status != PSA_SUCCESS )
3914 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003915 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003916 {
3917 status = PSA_ERROR_INVALID_ARGUMENT;
3918 goto exit;
3919 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003920
John Durkop6ba40d12020-11-10 08:50:04 -08003921#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3922 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003923 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003924 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003925 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003926 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3927 slot->key.data,
3928 slot->key.bytes,
3929 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003930 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003931 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003932
Steven Cooremana2371e52020-07-28 14:30:39 +02003933 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003934 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003935 status = PSA_ERROR_INVALID_ARGUMENT;
3936 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003937 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003938
John Durkop0e005192020-10-31 22:06:54 -07003939#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003940 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003941 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003942 status = mbedtls_to_psa_error(
3943 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003944 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003945 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003946 MBEDTLS_RSA_PRIVATE,
3947 output_length,
3948 input,
3949 output,
3950 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003951 }
3952 else
John Durkop0e005192020-10-31 22:06:54 -07003953#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3954#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003955 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003956 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003957 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003958 status = mbedtls_to_psa_error(
3959 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003960 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003961 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003962 MBEDTLS_RSA_PRIVATE,
3963 salt, salt_length,
3964 output_length,
3965 input,
3966 output,
3967 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003968 }
3969 else
John Durkop0e005192020-10-31 22:06:54 -07003970#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003971 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003972 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003973 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003974
Steven Cooreman4fed4552020-08-03 14:46:03 +02003975rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003976 mbedtls_rsa_free( rsa );
3977 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 else
John Durkop6ba40d12020-11-10 08:50:04 -08003980#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3981 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003983 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003985
3986exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003987 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003988
Ronald Cron5c522922020-11-14 16:35:34 +01003989 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003990}
Gilles Peskine20035e32018-02-03 22:44:14 +01003991
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003992
3993
mohammad1603503973b2018-03-12 15:59:30 +02003994/****************************************************************/
3995/* Symmetric cryptography */
3996/****************************************************************/
3997
Gilles Peskinee553c652018-06-04 16:22:46 +02003998static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003999 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004000 psa_algorithm_t alg,
4001 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004002{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004003 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004004 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004005 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004006 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004007 size_t key_bits;
4008 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004009 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4010 PSA_KEY_USAGE_ENCRYPT :
4011 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004012
Steven Cooremand3feccd2020-09-01 15:56:14 +02004013 /* A context must be freshly initialized before it can be set up. */
4014 if( operation->alg != 0 )
4015 return( PSA_ERROR_BAD_STATE );
4016
Steven Cooremana07b9972020-09-10 14:54:14 +02004017 /* The requested algorithm must be one that can be processed by cipher. */
4018 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004019 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004020
Steven Cooremanef8575e2020-09-11 11:44:50 +02004021 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004022 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004023 if( status != PSA_SUCCESS )
4024 goto exit;
4025
4026 /* Initialize the operation struct members, except for alg. The alg member
4027 * is used to indicate to psa_cipher_abort that there are resources to free,
4028 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004029 operation->key_set = 0;
4030 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004031 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004032 operation->iv_size = 0;
4033 operation->block_size = 0;
4034 if( alg == PSA_ALG_ECB_NO_PADDING )
4035 operation->iv_required = 0;
4036 else
4037 operation->iv_required = 1;
4038
Steven Cooremana07b9972020-09-10 14:54:14 +02004039 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004040 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004041 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004042 slot,
4043 alg );
4044 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004045 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004046 slot,
4047 alg );
4048
Steven Cooremanef8575e2020-09-11 11:44:50 +02004049 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004050 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004051 /* Once the driver context is initialised, it needs to be freed using
4052 * psa_cipher_abort. Indicate this through setting alg. */
4053 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004054 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004055
Steven Cooremand3feccd2020-09-01 15:56:14 +02004056 if( status != PSA_ERROR_NOT_SUPPORTED ||
4057 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4058 goto exit;
4059
Steven Cooremana07b9972020-09-10 14:54:14 +02004060 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004061 * available for the given algorithm & key. */
4062 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004063
Steven Cooremana07b9972020-09-10 14:54:14 +02004064 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004065 * psa_cipher_abort. Indicate there is something to be freed through setting
4066 * alg, and indicate the operation is being done using mbedtls crypto through
4067 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004068 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004069 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004070
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004071 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004072 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004073 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004074 {
4075 status = PSA_ERROR_NOT_SUPPORTED;
4076 goto exit;
4077 }
mohammad1603503973b2018-03-12 15:59:30 +02004078
mohammad1603503973b2018-03-12 15:59:30 +02004079 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004080 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004081 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004082
David Brown12ca5032021-02-11 11:02:00 -07004083#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004084 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004085 {
4086 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004087 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004088 memcpy( keys, slot->key.data, 16 );
4089 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004090 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4091 keys,
4092 192, cipher_operation );
4093 }
4094 else
4095#endif
4096 {
4097 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004098 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004099 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004100 }
Moran Peker41deec42018-04-04 15:43:05 +03004101 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004102 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004103
David Brown63ca2602021-01-26 11:51:12 -07004104#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4105 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004106 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004107 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004108 case PSA_ALG_CBC_NO_PADDING:
4109 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4110 MBEDTLS_PADDING_NONE );
4111 break;
4112 case PSA_ALG_CBC_PKCS7:
4113 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4114 MBEDTLS_PADDING_PKCS7 );
4115 break;
4116 default:
4117 /* The algorithm doesn't involve padding. */
4118 ret = 0;
4119 break;
4120 }
4121 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004122 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004123#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004124
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004125 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004126 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004127 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4128 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004129 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004130 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004131 }
David Brown1bfe4d72021-02-16 12:54:35 -07004132#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004133 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004134 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004135 operation->iv_size = 12;
4136#endif
mohammad1603503973b2018-03-12 15:59:30 +02004137
Steven Cooreman7df02922020-09-09 15:28:49 +02004138 status = PSA_SUCCESS;
4139
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004140exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004141 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004142 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004143 if( status == PSA_SUCCESS )
4144 {
4145 /* Update operation flags for both driver and software implementations */
4146 operation->key_set = 1;
4147 }
4148 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004149 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004150
Ronald Cron5c522922020-11-14 16:35:34 +01004151 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004152
Ronald Cron5c522922020-11-14 16:35:34 +01004153 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004154}
4155
Gilles Peskinefe119512018-07-08 21:39:34 +02004156psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004157 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004158 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004159{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004160 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004161}
4162
Gilles Peskinefe119512018-07-08 21:39:34 +02004163psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004164 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004165 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004166{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004167 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004168}
4169
Gilles Peskinefe119512018-07-08 21:39:34 +02004170psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004171 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004172 size_t iv_size,
4173 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004174{
itayzafrir534bd7c2018-08-02 13:56:32 +03004175 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004176 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004177 if( operation->iv_set || ! operation->iv_required )
4178 {
4179 return( PSA_ERROR_BAD_STATE );
4180 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004181
Steven Cooremanef8575e2020-09-11 11:44:50 +02004182 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004183 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004184 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004185 iv,
4186 iv_size,
4187 iv_length );
4188 goto exit;
4189 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004190
Moran Peker41deec42018-04-04 15:43:05 +03004191 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004192 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004193 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004194 goto exit;
4195 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004196 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004197 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004198 if( ret != 0 )
4199 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004200 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004201 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004202 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004203
mohammad16038481e742018-03-18 13:57:31 +02004204 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004205 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004206
Moran Peker395db872018-05-31 14:07:14 +03004207exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004208 if( status == PSA_SUCCESS )
4209 operation->iv_set = 1;
4210 else
Moran Peker395db872018-05-31 14:07:14 +03004211 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004212 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004213}
4214
Gilles Peskinefe119512018-07-08 21:39:34 +02004215psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004216 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004217 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004218{
itayzafrir534bd7c2018-08-02 13:56:32 +03004219 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004220 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004221 if( operation->iv_set || ! operation->iv_required )
4222 {
4223 return( PSA_ERROR_BAD_STATE );
4224 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004225
Steven Cooremanef8575e2020-09-11 11:44:50 +02004226 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004227 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004228 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004229 iv,
4230 iv_length );
4231 goto exit;
4232 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004233
Moran Pekera28258c2018-05-29 16:25:04 +03004234 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004235 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004236 status = PSA_ERROR_INVALID_ARGUMENT;
4237 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004238 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004239 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4240 status = mbedtls_to_psa_error( ret );
4241exit:
4242 if( status == PSA_SUCCESS )
4243 operation->iv_set = 1;
4244 else
mohammad1603503973b2018-03-12 15:59:30 +02004245 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004246 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004247}
4248
Steven Cooreman177deba2020-09-07 17:14:14 +02004249/* Process input for which the algorithm is set to ECB mode. This requires
4250 * manual processing, since the PSA API is defined as being able to process
4251 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4252 * underlying mbedtls_cipher_update only takes full blocks. */
4253static psa_status_t psa_cipher_update_ecb_internal(
4254 mbedtls_cipher_context_t *ctx,
4255 const uint8_t *input,
4256 size_t input_length,
4257 uint8_t *output,
4258 size_t output_size,
4259 size_t *output_length )
4260{
4261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4262 size_t block_size = ctx->cipher_info->block_size;
4263 size_t internal_output_length = 0;
4264 *output_length = 0;
4265
4266 if( input_length == 0 )
4267 {
4268 status = PSA_SUCCESS;
4269 goto exit;
4270 }
4271
4272 if( ctx->unprocessed_len > 0 )
4273 {
4274 /* Fill up to block size, and run the block if there's a full one. */
4275 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4276
4277 if( input_length < bytes_to_copy )
4278 bytes_to_copy = input_length;
4279
4280 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4281 input, bytes_to_copy );
4282 input_length -= bytes_to_copy;
4283 input += bytes_to_copy;
4284 ctx->unprocessed_len += bytes_to_copy;
4285
4286 if( ctx->unprocessed_len == block_size )
4287 {
4288 status = mbedtls_to_psa_error(
4289 mbedtls_cipher_update( ctx,
4290 ctx->unprocessed_data,
4291 block_size,
4292 output, &internal_output_length ) );
4293
4294 if( status != PSA_SUCCESS )
4295 goto exit;
4296
4297 output += internal_output_length;
4298 output_size -= internal_output_length;
4299 *output_length += internal_output_length;
4300 ctx->unprocessed_len = 0;
4301 }
4302 }
4303
4304 while( input_length >= block_size )
4305 {
4306 /* Run all full blocks we have, one by one */
4307 status = mbedtls_to_psa_error(
4308 mbedtls_cipher_update( ctx, input,
4309 block_size,
4310 output, &internal_output_length ) );
4311
4312 if( status != PSA_SUCCESS )
4313 goto exit;
4314
4315 input_length -= block_size;
4316 input += block_size;
4317
4318 output += internal_output_length;
4319 output_size -= internal_output_length;
4320 *output_length += internal_output_length;
4321 }
4322
4323 if( input_length > 0 )
4324 {
4325 /* Save unprocessed bytes for later processing */
4326 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4327 input, input_length );
4328 ctx->unprocessed_len += input_length;
4329 }
4330
4331 status = PSA_SUCCESS;
4332
4333exit:
4334 return( status );
4335}
4336
Gilles Peskinee553c652018-06-04 16:22:46 +02004337psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4338 const uint8_t *input,
4339 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004340 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004341 size_t output_size,
4342 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004343{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004344 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004345 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004346 if( operation->alg == 0 )
4347 {
4348 return( PSA_ERROR_BAD_STATE );
4349 }
4350 if( operation->iv_required && ! operation->iv_set )
4351 {
4352 return( PSA_ERROR_BAD_STATE );
4353 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004354
Steven Cooremanef8575e2020-09-11 11:44:50 +02004355 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004356 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004357 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004358 input,
4359 input_length,
4360 output,
4361 output_size,
4362 output_length );
4363 goto exit;
4364 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004365
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004366 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004367 {
4368 /* Take the unprocessed partial block left over from previous
4369 * update calls, if any, plus the input to this call. Remove
4370 * the last partial block, if any. You get the data that will be
4371 * output in this call. */
4372 expected_output_size =
4373 ( operation->ctx.cipher.unprocessed_len + input_length )
4374 / operation->block_size * operation->block_size;
4375 }
4376 else
4377 {
4378 expected_output_size = input_length;
4379 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004380
Gilles Peskine89d789c2018-06-04 17:17:16 +02004381 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004382 {
4383 status = PSA_ERROR_BUFFER_TOO_SMALL;
4384 goto exit;
4385 }
mohammad160382759612018-03-12 18:16:40 +02004386
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004387 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4388 {
4389 /* mbedtls_cipher_update has an API inconsistency: it will only
4390 * process a single block at a time in ECB mode. Abstract away that
4391 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004392 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4393 input,
4394 input_length,
4395 output,
4396 output_size,
4397 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004398 }
4399 else
4400 {
4401 status = mbedtls_to_psa_error(
4402 mbedtls_cipher_update( &operation->ctx.cipher, input,
4403 input_length, output, output_length ) );
4404 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004405exit:
4406 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004407 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004408 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004409}
4410
Gilles Peskinee553c652018-06-04 16:22:46 +02004411psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4412 uint8_t *output,
4413 size_t output_size,
4414 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004415{
David Saadab4ecc272019-02-14 13:48:10 +02004416 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004417 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004418 if( operation->alg == 0 )
4419 {
4420 return( PSA_ERROR_BAD_STATE );
4421 }
4422 if( operation->iv_required && ! operation->iv_set )
4423 {
4424 return( PSA_ERROR_BAD_STATE );
4425 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004426
Steven Cooremanef8575e2020-09-11 11:44:50 +02004427 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004428 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004429 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004430 output,
4431 output_size,
4432 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004433 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004434 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004435
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004436 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004437 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004438 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004439 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004440 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004441 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004442 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004443 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004444 }
4445
Steven Cooremanef8575e2020-09-11 11:44:50 +02004446 status = mbedtls_to_psa_error(
4447 mbedtls_cipher_finish( &operation->ctx.cipher,
4448 temp_output_buffer,
4449 output_length ) );
4450 if( status != PSA_SUCCESS )
4451 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004452
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004453 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004454 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004455 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004456 memcpy( output, temp_output_buffer, *output_length );
4457 else
Janos Follath315b51c2018-07-09 16:04:51 +01004458 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004459
Steven Cooremanef8575e2020-09-11 11:44:50 +02004460exit:
4461 if( operation->mbedtls_in_use == 1 )
4462 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004463
Steven Cooremanef8575e2020-09-11 11:44:50 +02004464 if( status == PSA_SUCCESS )
4465 return( psa_cipher_abort( operation ) );
4466 else
4467 {
4468 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004469 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004470
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471 return( status );
4472 }
mohammad1603503973b2018-03-12 15:59:30 +02004473}
4474
Gilles Peskinee553c652018-06-04 16:22:46 +02004475psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4476{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004477 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004478 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004479 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004480 * in use. It's ok to call abort on such an object, and there's
4481 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004482 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004483 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004484
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004485 /* Sanity check (shouldn't happen: operation->alg should
4486 * always have been initialized to a valid value). */
4487 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4488 return( PSA_ERROR_BAD_STATE );
4489
Steven Cooremanef8575e2020-09-11 11:44:50 +02004490 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004491 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004492 else
4493 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004494
Moran Peker41deec42018-04-04 15:43:05 +03004495 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004496 operation->key_set = 0;
4497 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004498 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004499 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004500 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004501 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004502
Moran Peker395db872018-05-31 14:07:14 +03004503 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004504}
4505
Gilles Peskinea0655c32018-04-30 17:06:50 +02004506
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004507
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004508
mohammad16035955c982018-04-26 00:53:03 +03004509/****************************************************************/
4510/* AEAD */
4511/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004512
Gilles Peskineedf9a652018-08-17 18:11:56 +02004513typedef struct
4514{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004515 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004516 const mbedtls_cipher_info_t *cipher_info;
4517 union
4518 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004519 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520#if defined(MBEDTLS_CCM_C)
4521 mbedtls_ccm_context ccm;
4522#endif /* MBEDTLS_CCM_C */
4523#if defined(MBEDTLS_GCM_C)
4524 mbedtls_gcm_context gcm;
4525#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004526#if defined(MBEDTLS_CHACHAPOLY_C)
4527 mbedtls_chachapoly_context chachapoly;
4528#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004529 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004530 psa_algorithm_t core_alg;
4531 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004532 uint8_t tag_length;
4533} aead_operation_t;
4534
Ronald Cronf95a2b12020-10-22 15:24:49 +02004535#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4536
Gilles Peskine30a9e412019-01-14 18:36:12 +01004537static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004538{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004539 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004540 {
4541#if defined(MBEDTLS_CCM_C)
4542 case PSA_ALG_CCM:
4543 mbedtls_ccm_free( &operation->ctx.ccm );
4544 break;
4545#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004546#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004547 case PSA_ALG_GCM:
4548 mbedtls_gcm_free( &operation->ctx.gcm );
4549 break;
4550#endif /* MBEDTLS_GCM_C */
4551 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004552
Ronald Cron5c522922020-11-14 16:35:34 +01004553 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554}
4555
4556static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004557 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004558 psa_key_usage_t usage,
4559 psa_algorithm_t alg )
4560{
4561 psa_status_t status;
4562 size_t key_bits;
4563 mbedtls_cipher_id_t cipher_id;
4564
Ronald Cron5c522922020-11-14 16:35:34 +01004565 status = psa_get_and_lock_transparent_key_slot_with_policy(
4566 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004567 if( status != PSA_SUCCESS )
4568 return( status );
4569
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004570 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004571
4572 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004573 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004574 &cipher_id );
4575 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004576 {
4577 status = PSA_ERROR_NOT_SUPPORTED;
4578 goto cleanup;
4579 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004580
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004581 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582 {
4583#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004584 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004585 operation->core_alg = PSA_ALG_CCM;
4586 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004587 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4588 * The call to mbedtls_ccm_encrypt_and_tag or
4589 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004590 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004591 {
4592 status = PSA_ERROR_INVALID_ARGUMENT;
4593 goto cleanup;
4594 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004595 mbedtls_ccm_init( &operation->ctx.ccm );
4596 status = mbedtls_to_psa_error(
4597 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004598 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004599 (unsigned int) key_bits ) );
4600 if( status != 0 )
4601 goto cleanup;
4602 break;
4603#endif /* MBEDTLS_CCM_C */
4604
4605#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004606 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004607 operation->core_alg = PSA_ALG_GCM;
4608 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004609 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4610 * The call to mbedtls_gcm_crypt_and_tag or
4611 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004612 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004613 {
4614 status = PSA_ERROR_INVALID_ARGUMENT;
4615 goto cleanup;
4616 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004617 mbedtls_gcm_init( &operation->ctx.gcm );
4618 status = mbedtls_to_psa_error(
4619 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004620 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004621 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004622 if( status != 0 )
4623 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624 break;
4625#endif /* MBEDTLS_GCM_C */
4626
Gilles Peskine26869f22019-05-06 15:25:00 +02004627#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004628 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004629 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4630 operation->full_tag_length = 16;
4631 /* We only support the default tag length. */
4632 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004633 {
4634 status = PSA_ERROR_NOT_SUPPORTED;
4635 goto cleanup;
4636 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004637 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4638 status = mbedtls_to_psa_error(
4639 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004640 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004641 if( status != 0 )
4642 goto cleanup;
4643 break;
4644#endif /* MBEDTLS_CHACHAPOLY_C */
4645
Gilles Peskineedf9a652018-08-17 18:11:56 +02004646 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004647 status = PSA_ERROR_NOT_SUPPORTED;
4648 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004649 }
4650
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004651 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4652 {
4653 status = PSA_ERROR_INVALID_ARGUMENT;
4654 goto cleanup;
4655 }
4656 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004657
Gilles Peskineedf9a652018-08-17 18:11:56 +02004658 return( PSA_SUCCESS );
4659
4660cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004661 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004662 return( status );
4663}
4664
Ronald Croncf56a0a2020-08-04 09:51:30 +02004665psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004666 psa_algorithm_t alg,
4667 const uint8_t *nonce,
4668 size_t nonce_length,
4669 const uint8_t *additional_data,
4670 size_t additional_data_length,
4671 const uint8_t *plaintext,
4672 size_t plaintext_length,
4673 uint8_t *ciphertext,
4674 size_t ciphertext_size,
4675 size_t *ciphertext_length )
4676{
mohammad16035955c982018-04-26 00:53:03 +03004677 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004678 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004679 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004680
mohammad1603f08a5502018-06-03 15:05:47 +03004681 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004682
Ronald Croncf56a0a2020-08-04 09:51:30 +02004683 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004684 if( status != PSA_SUCCESS )
4685 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004686
Gilles Peskineedf9a652018-08-17 18:11:56 +02004687 /* For all currently supported modes, the tag is at the end of the
4688 * ciphertext. */
4689 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4690 {
4691 status = PSA_ERROR_BUFFER_TOO_SMALL;
4692 goto exit;
4693 }
4694 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004695
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004696#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004697 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004698 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004699 status = mbedtls_to_psa_error(
4700 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4701 MBEDTLS_GCM_ENCRYPT,
4702 plaintext_length,
4703 nonce, nonce_length,
4704 additional_data, additional_data_length,
4705 plaintext, ciphertext,
4706 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004707 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004708 else
4709#endif /* MBEDTLS_GCM_C */
4710#if defined(MBEDTLS_CCM_C)
4711 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004712 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004713 status = mbedtls_to_psa_error(
4714 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4715 plaintext_length,
4716 nonce, nonce_length,
4717 additional_data,
4718 additional_data_length,
4719 plaintext, ciphertext,
4720 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004721 }
mohammad16035c8845f2018-05-09 05:40:09 -07004722 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004723#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004724#if defined(MBEDTLS_CHACHAPOLY_C)
4725 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4726 {
4727 if( nonce_length != 12 || operation.tag_length != 16 )
4728 {
4729 status = PSA_ERROR_NOT_SUPPORTED;
4730 goto exit;
4731 }
4732 status = mbedtls_to_psa_error(
4733 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4734 plaintext_length,
4735 nonce,
4736 additional_data,
4737 additional_data_length,
4738 plaintext,
4739 ciphertext,
4740 tag ) );
4741 }
4742 else
4743#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004744 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004745 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004746 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004747 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004748
Gilles Peskineedf9a652018-08-17 18:11:56 +02004749 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4750 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004751
Gilles Peskineedf9a652018-08-17 18:11:56 +02004752exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004753 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004754 if( status == PSA_SUCCESS )
4755 *ciphertext_length = plaintext_length + operation.tag_length;
4756 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004757}
4758
Gilles Peskineee652a32018-06-01 19:23:52 +02004759/* Locate the tag in a ciphertext buffer containing the encrypted data
4760 * followed by the tag. Return the length of the part preceding the tag in
4761 * *plaintext_length. This is the size of the plaintext in modes where
4762 * the encrypted data has the same size as the plaintext, such as
4763 * CCM and GCM. */
4764static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4765 const uint8_t *ciphertext,
4766 size_t ciphertext_length,
4767 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004768 const uint8_t **p_tag )
4769{
4770 size_t payload_length;
4771 if( tag_length > ciphertext_length )
4772 return( PSA_ERROR_INVALID_ARGUMENT );
4773 payload_length = ciphertext_length - tag_length;
4774 if( payload_length > plaintext_size )
4775 return( PSA_ERROR_BUFFER_TOO_SMALL );
4776 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004777 return( PSA_SUCCESS );
4778}
4779
Ronald Croncf56a0a2020-08-04 09:51:30 +02004780psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004781 psa_algorithm_t alg,
4782 const uint8_t *nonce,
4783 size_t nonce_length,
4784 const uint8_t *additional_data,
4785 size_t additional_data_length,
4786 const uint8_t *ciphertext,
4787 size_t ciphertext_length,
4788 uint8_t *plaintext,
4789 size_t plaintext_size,
4790 size_t *plaintext_length )
4791{
mohammad16035955c982018-04-26 00:53:03 +03004792 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004793 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004794 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004795
Gilles Peskineee652a32018-06-01 19:23:52 +02004796 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004797
Ronald Croncf56a0a2020-08-04 09:51:30 +02004798 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004799 if( status != PSA_SUCCESS )
4800 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004801
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004802 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4803 ciphertext, ciphertext_length,
4804 plaintext_size, &tag );
4805 if( status != PSA_SUCCESS )
4806 goto exit;
4807
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004808#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004809 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004810 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004811 status = mbedtls_to_psa_error(
4812 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4813 ciphertext_length - operation.tag_length,
4814 nonce, nonce_length,
4815 additional_data,
4816 additional_data_length,
4817 tag, operation.tag_length,
4818 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004819 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004820 else
4821#endif /* MBEDTLS_GCM_C */
4822#if defined(MBEDTLS_CCM_C)
4823 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004824 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004825 status = mbedtls_to_psa_error(
4826 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4827 ciphertext_length - operation.tag_length,
4828 nonce, nonce_length,
4829 additional_data,
4830 additional_data_length,
4831 ciphertext, plaintext,
4832 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004833 }
mohammad160339574652018-06-01 04:39:53 -07004834 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004835#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004836#if defined(MBEDTLS_CHACHAPOLY_C)
4837 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4838 {
4839 if( nonce_length != 12 || operation.tag_length != 16 )
4840 {
4841 status = PSA_ERROR_NOT_SUPPORTED;
4842 goto exit;
4843 }
4844 status = mbedtls_to_psa_error(
4845 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4846 ciphertext_length - operation.tag_length,
4847 nonce,
4848 additional_data,
4849 additional_data_length,
4850 tag,
4851 ciphertext,
4852 plaintext ) );
4853 }
4854 else
4855#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004856 {
mohammad1603554faad2018-06-03 15:07:38 +03004857 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004858 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004859
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860 if( status != PSA_SUCCESS && plaintext_size != 0 )
4861 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004862
Gilles Peskineedf9a652018-08-17 18:11:56 +02004863exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004864 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004865 if( status == PSA_SUCCESS )
4866 *plaintext_length = ciphertext_length - operation.tag_length;
4867 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004868}
4869
Gilles Peskinea0655c32018-04-30 17:06:50 +02004870
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004871
Gilles Peskine20035e32018-02-03 22:44:14 +01004872/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004873/* Generators */
4874/****************************************************************/
4875
John Durkop07cc04a2020-11-16 22:08:34 -08004876#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4877 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4878 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4879#define AT_LEAST_ONE_BUILTIN_KDF
4880#endif
4881
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882#define HKDF_STATE_INIT 0 /* no input yet */
4883#define HKDF_STATE_STARTED 1 /* got salt */
4884#define HKDF_STATE_KEYED 2 /* got key */
4885#define HKDF_STATE_OUTPUT 3 /* output started */
4886
Gilles Peskinecbe66502019-05-16 16:59:18 +02004887static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004888 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4891 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004892 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004893 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894}
4895
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004896psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004897{
4898 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004901 {
4902 /* The object has (apparently) been initialized but it is not
4903 * in use. It's ok to call abort on such an object, and there's
4904 * nothing to do. */
4905 }
4906 else
John Durkopd0321952020-10-29 21:37:36 -07004907#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004909 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 mbedtls_free( operation->ctx.hkdf.info );
4911 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004912 }
John Durkop07cc04a2020-11-16 22:08:34 -08004913 else
4914#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4915#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4916 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4917 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004918 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004920 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004921 if( operation->ctx.tls12_prf.seed != NULL )
4922 {
4923 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4924 operation->ctx.tls12_prf.seed_length );
4925 mbedtls_free( operation->ctx.tls12_prf.seed );
4926 }
4927
4928 if( operation->ctx.tls12_prf.label != NULL )
4929 {
4930 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4931 operation->ctx.tls12_prf.label_length );
4932 mbedtls_free( operation->ctx.tls12_prf.label );
4933 }
4934
4935 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4936
4937 /* We leave the fields Ai and output_block to be erased safely by the
4938 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004939 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004940 else
John Durkop07cc04a2020-11-16 22:08:34 -08004941#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4942 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004943 {
4944 status = PSA_ERROR_BAD_STATE;
4945 }
Janos Follath083036a2019-06-11 10:22:26 +01004946 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004947 return( status );
4948}
4949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004950psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004951 size_t *capacity)
4952{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004953 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004954 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004955 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004956 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004957 }
4958
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004959 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004960 return( PSA_SUCCESS );
4961}
4962
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004963psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004964 size_t capacity )
4965{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004968 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004969 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971 return( PSA_SUCCESS );
4972}
4973
John Durkopd0321952020-10-29 21:37:36 -07004974#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004976 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004977static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004978 psa_algorithm_t hash_alg,
4979 uint8_t *output,
4980 size_t output_length )
4981{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004982 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983 psa_status_t status;
4984
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004985 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4986 return( PSA_ERROR_BAD_STATE );
4987 hkdf->state = HKDF_STATE_OUTPUT;
4988
Gilles Peskinebef7f142018-07-12 17:22:21 +02004989 while( output_length != 0 )
4990 {
4991 /* Copy what remains of the current block */
4992 uint8_t n = hash_length - hkdf->offset_in_block;
4993 if( n > output_length )
4994 n = (uint8_t) output_length;
4995 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4996 output += n;
4997 output_length -= n;
4998 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004999 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005000 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005001 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005002 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005003 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005004 * object was corrupted or if this function is called directly
5005 * inside the library. */
5006 if( hkdf->block_number == 0xff )
5007 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005008
5009 /* We need a new block */
5010 ++hkdf->block_number;
5011 hkdf->offset_in_block = 0;
5012 status = psa_hmac_setup_internal( &hkdf->hmac,
5013 hkdf->prk, hash_length,
5014 hash_alg );
5015 if( status != PSA_SUCCESS )
5016 return( status );
5017 if( hkdf->block_number != 1 )
5018 {
5019 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5020 hkdf->output_block,
5021 hash_length );
5022 if( status != PSA_SUCCESS )
5023 return( status );
5024 }
5025 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5026 hkdf->info,
5027 hkdf->info_length );
5028 if( status != PSA_SUCCESS )
5029 return( status );
5030 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5031 &hkdf->block_number, 1 );
5032 if( status != PSA_SUCCESS )
5033 return( status );
5034 status = psa_hmac_finish_internal( &hkdf->hmac,
5035 hkdf->output_block,
5036 sizeof( hkdf->output_block ) );
5037 if( status != PSA_SUCCESS )
5038 return( status );
5039 }
5040
5041 return( PSA_SUCCESS );
5042}
John Durkop07cc04a2020-11-16 22:08:34 -08005043#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005044
John Durkop07cc04a2020-11-16 22:08:34 -08005045#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5046 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005047static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5048 psa_tls12_prf_key_derivation_t *tls12_prf,
5049 psa_algorithm_t alg )
5050{
5051 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005052 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005053 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5054 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005055
Janos Follath7742fee2019-06-17 12:58:10 +01005056 /* We can't be wanting more output after block 0xff, otherwise
5057 * the capacity check in psa_key_derivation_output_bytes() would have
5058 * prevented this call. It could happen only if the operation
5059 * object was corrupted or if this function is called directly
5060 * inside the library. */
5061 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005062 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005063
5064 /* We need a new block */
5065 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005066 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005067
5068 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5069 *
5070 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5071 *
5072 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5073 * HMAC_hash(secret, A(2) + seed) +
5074 * HMAC_hash(secret, A(3) + seed) + ...
5075 *
5076 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005077 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005078 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005079 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005080 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005081 * is currently extracted as `output_block` and where i is
5082 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005083 */
5084
Janos Follathea29bfb2019-06-19 12:21:20 +01005085 /* Save the hash context before using it, to preserve the hash state with
5086 * only the inner padding in it. We need this, because inner padding depends
5087 * on the key (secret in the RFC's terminology). */
5088 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5089 if( status != PSA_SUCCESS )
5090 goto cleanup;
5091
5092 /* Calculate A(i) where i = tls12_prf->block_number. */
5093 if( tls12_prf->block_number == 1 )
5094 {
5095 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5096 * the variable seed and in this instance means it in the context of the
5097 * P_hash function, where seed = label + seed.) */
5098 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5099 tls12_prf->label, tls12_prf->label_length );
5100 if( status != PSA_SUCCESS )
5101 goto cleanup;
5102 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5103 tls12_prf->seed, tls12_prf->seed_length );
5104 if( status != PSA_SUCCESS )
5105 goto cleanup;
5106 }
5107 else
5108 {
5109 /* A(i) = HMAC_hash(secret, A(i-1)) */
5110 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5111 tls12_prf->Ai, hash_length );
5112 if( status != PSA_SUCCESS )
5113 goto cleanup;
5114 }
5115
5116 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5117 tls12_prf->Ai, hash_length );
5118 if( status != PSA_SUCCESS )
5119 goto cleanup;
5120 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5121 if( status != PSA_SUCCESS )
5122 goto cleanup;
5123
5124 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5125 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5126 tls12_prf->Ai, hash_length );
5127 if( status != PSA_SUCCESS )
5128 goto cleanup;
5129 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5130 tls12_prf->label, tls12_prf->label_length );
5131 if( status != PSA_SUCCESS )
5132 goto cleanup;
5133 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5134 tls12_prf->seed, tls12_prf->seed_length );
5135 if( status != PSA_SUCCESS )
5136 goto cleanup;
5137 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5138 tls12_prf->output_block, hash_length );
5139 if( status != PSA_SUCCESS )
5140 goto cleanup;
5141 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5142 if( status != PSA_SUCCESS )
5143 goto cleanup;
5144
Janos Follath7742fee2019-06-17 12:58:10 +01005145
5146cleanup:
5147
Janos Follathea29bfb2019-06-19 12:21:20 +01005148 cleanup_status = psa_hash_abort( &backup );
5149 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5150 status = cleanup_status;
5151
5152 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005153}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005154
Janos Follath844eb0e2019-06-19 12:10:49 +01005155static psa_status_t psa_key_derivation_tls12_prf_read(
5156 psa_tls12_prf_key_derivation_t *tls12_prf,
5157 psa_algorithm_t alg,
5158 uint8_t *output,
5159 size_t output_length )
5160{
5161 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005162 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005163 psa_status_t status;
5164 uint8_t offset, length;
5165
5166 while( output_length != 0 )
5167 {
5168 /* Check if we have fully processed the current block. */
5169 if( tls12_prf->left_in_block == 0 )
5170 {
5171 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5172 alg );
5173 if( status != PSA_SUCCESS )
5174 return( status );
5175
5176 continue;
5177 }
5178
5179 if( tls12_prf->left_in_block > output_length )
5180 length = (uint8_t) output_length;
5181 else
5182 length = tls12_prf->left_in_block;
5183
5184 offset = hash_length - tls12_prf->left_in_block;
5185 memcpy( output, tls12_prf->output_block + offset, length );
5186 output += length;
5187 output_length -= length;
5188 tls12_prf->left_in_block -= length;
5189 }
5190
5191 return( PSA_SUCCESS );
5192}
John Durkop07cc04a2020-11-16 22:08:34 -08005193#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5194 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005195
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005196psa_status_t psa_key_derivation_output_bytes(
5197 psa_key_derivation_operation_t *operation,
5198 uint8_t *output,
5199 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005200{
5201 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005203
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005205 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005207 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005208 }
5209
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005214 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005215 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005216 goto exit;
5217 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005218 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005219 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005220 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005221 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005222 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5223 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005224 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005225 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005226 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005228 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005229
John Durkopd0321952020-10-29 21:37:36 -07005230#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005232 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005233 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005235 output, output_length );
5236 }
Janos Follathadbec812019-06-14 11:05:39 +01005237 else
John Durkop07cc04a2020-11-16 22:08:34 -08005238#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5239#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5240 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005241 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005242 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005243 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005245 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005246 output_length );
5247 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005248 else
John Durkop07cc04a2020-11-16 22:08:34 -08005249#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5250 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005251 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005252 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005253 return( PSA_ERROR_BAD_STATE );
5254 }
5255
5256exit:
5257 if( status != PSA_SUCCESS )
5258 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005259 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005260 * This allows us to differentiate between exhausted operations and
5261 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5262 * operations. */
5263 psa_algorithm_t alg = operation->alg;
5264 psa_key_derivation_abort( operation );
5265 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 memset( output, '!', output_length );
5267 }
5268 return( status );
5269}
5270
David Brown7807bf72021-02-09 16:28:23 -07005271#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005272static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5273{
5274 if( data_size >= 8 )
5275 mbedtls_des_key_set_parity( data );
5276 if( data_size >= 16 )
5277 mbedtls_des_key_set_parity( data + 8 );
5278 if( data_size >= 24 )
5279 mbedtls_des_key_set_parity( data + 16 );
5280}
David Brown7807bf72021-02-09 16:28:23 -07005281#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005282
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005283static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005284 psa_key_slot_t *slot,
5285 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287{
5288 uint8_t *data = NULL;
5289 size_t bytes = PSA_BITS_TO_BYTES( bits );
5290 psa_status_t status;
5291
Gilles Peskine8e338702019-07-30 20:06:31 +02005292 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005293 return( PSA_ERROR_INVALID_ARGUMENT );
5294 if( bits % 8 != 0 )
5295 return( PSA_ERROR_INVALID_ARGUMENT );
5296 data = mbedtls_calloc( 1, bytes );
5297 if( data == NULL )
5298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5299
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005300 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301 if( status != PSA_SUCCESS )
5302 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005303#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005304 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005305 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005306#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005307
5308 status = psa_allocate_buffer_to_slot( slot, bytes );
5309 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005310 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005311
Ronald Cronbf33c932020-11-28 18:06:53 +01005312 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005313 psa_key_attributes_t attributes = {
5314 .core = slot->attr
5315 };
5316
Ronald Cronbf33c932020-11-28 18:06:53 +01005317 status = psa_driver_wrapper_import_key( &attributes,
5318 data, bytes,
5319 slot->key.data,
5320 slot->key.bytes,
5321 &slot->key.bytes, &bits );
5322 if( bits != slot->attr.bits )
5323 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005324
5325exit:
5326 mbedtls_free( data );
5327 return( status );
5328}
5329
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005330psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005332 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005333{
5334 psa_status_t status;
5335 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005336 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005337
Ronald Cron81709fc2020-11-14 12:10:32 +01005338 *key = MBEDTLS_SVC_KEY_ID_INIT;
5339
Gilles Peskine0f84d622019-09-12 19:03:13 +02005340 /* Reject any attempt to create a zero-length key so that we don't
5341 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5342 if( psa_get_key_bits( attributes ) == 0 )
5343 return( PSA_ERROR_INVALID_ARGUMENT );
5344
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005345 if( ! operation->can_output_key )
5346 return( PSA_ERROR_NOT_PERMITTED );
5347
Ronald Cron81709fc2020-11-14 12:10:32 +01005348 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5349 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005350#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5351 if( driver != NULL )
5352 {
5353 /* Deriving a key in a secure element is not implemented yet. */
5354 status = PSA_ERROR_NOT_SUPPORTED;
5355 }
5356#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005357 if( status == PSA_SUCCESS )
5358 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005359 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005360 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005362 }
5363 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005364 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005365 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005366 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005367
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005368 return( status );
5369}
5370
Gilles Peskineeab56e42018-07-12 17:12:33 +02005371
5372
5373/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005374/* Key derivation */
5375/****************************************************************/
5376
Steven Cooreman932ffb72021-02-15 12:14:32 +01005377#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005378static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005379 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005380 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005381{
John Durkop07cc04a2020-11-16 22:08:34 -08005382 int is_kdf_alg_supported;
5383
Janos Follath5fe19732019-06-20 15:09:30 +01005384 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5385 * initialisers for this union leave some bytes unspecified.) */
5386 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5387
Gilles Peskine969c5d62019-01-16 15:53:06 +01005388 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005389#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005390 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5391 is_kdf_alg_supported = 1;
5392 else
5393#endif
5394#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5395 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5396 is_kdf_alg_supported = 1;
5397 else
5398#endif
5399#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5400 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5401 is_kdf_alg_supported = 1;
5402 else
5403#endif
5404 is_kdf_alg_supported = 0;
5405
5406 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005407 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005408 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005409 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005410 if( hash_size == 0 )
5411 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005412 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5413 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005414 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005415 {
5416 return( PSA_ERROR_NOT_SUPPORTED );
5417 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005418 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005419 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005420 }
John Durkop07cc04a2020-11-16 22:08:34 -08005421
5422 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423}
John Durkop07cc04a2020-11-16 22:08:34 -08005424#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005425
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005426psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005427 psa_algorithm_t alg )
5428{
5429 psa_status_t status;
5430
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005431 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005432 return( PSA_ERROR_BAD_STATE );
5433
Gilles Peskine6843c292019-01-18 16:44:49 +01005434 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5435 return( PSA_ERROR_INVALID_ARGUMENT );
5436 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005437 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005438#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005439 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005440 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005441#else
5442 return( PSA_ERROR_NOT_SUPPORTED );
5443#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 }
5445 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5446 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005447#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005448 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005449#else
5450 return( PSA_ERROR_NOT_SUPPORTED );
5451#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005452 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005453 else
5454 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005455
5456 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005457 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005458 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005459}
5460
John Durkopd0321952020-10-29 21:37:36 -07005461#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005462static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005463 psa_algorithm_t hash_alg,
5464 psa_key_derivation_step_t step,
5465 const uint8_t *data,
5466 size_t data_length )
5467{
5468 psa_status_t status;
5469 switch( step )
5470 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005471 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005472 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005473 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005474 status = psa_hmac_setup_internal( &hkdf->hmac,
5475 data, data_length,
5476 hash_alg );
5477 if( status != PSA_SUCCESS )
5478 return( status );
5479 hkdf->state = HKDF_STATE_STARTED;
5480 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005481 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005482 /* If no salt was provided, use an empty salt. */
5483 if( hkdf->state == HKDF_STATE_INIT )
5484 {
5485 status = psa_hmac_setup_internal( &hkdf->hmac,
5486 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005487 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005488 if( status != PSA_SUCCESS )
5489 return( status );
5490 hkdf->state = HKDF_STATE_STARTED;
5491 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005492 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005494 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5495 data, data_length );
5496 if( status != PSA_SUCCESS )
5497 return( status );
5498 status = psa_hmac_finish_internal( &hkdf->hmac,
5499 hkdf->prk,
5500 sizeof( hkdf->prk ) );
5501 if( status != PSA_SUCCESS )
5502 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005503 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005504 hkdf->block_number = 0;
5505 hkdf->state = HKDF_STATE_KEYED;
5506 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005507 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005508 if( hkdf->state == HKDF_STATE_OUTPUT )
5509 return( PSA_ERROR_BAD_STATE );
5510 if( hkdf->info_set )
5511 return( PSA_ERROR_BAD_STATE );
5512 hkdf->info_length = data_length;
5513 if( data_length != 0 )
5514 {
5515 hkdf->info = mbedtls_calloc( 1, data_length );
5516 if( hkdf->info == NULL )
5517 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5518 memcpy( hkdf->info, data, data_length );
5519 }
5520 hkdf->info_set = 1;
5521 return( PSA_SUCCESS );
5522 default:
5523 return( PSA_ERROR_INVALID_ARGUMENT );
5524 }
5525}
John Durkop07cc04a2020-11-16 22:08:34 -08005526#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005527
John Durkop07cc04a2020-11-16 22:08:34 -08005528#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5529 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005530static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5531 const uint8_t *data,
5532 size_t data_length )
5533{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005534 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005535 return( PSA_ERROR_BAD_STATE );
5536
Janos Follathd6dce9f2019-07-04 09:11:38 +01005537 if( data_length != 0 )
5538 {
5539 prf->seed = mbedtls_calloc( 1, data_length );
5540 if( prf->seed == NULL )
5541 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005542
Janos Follathd6dce9f2019-07-04 09:11:38 +01005543 memcpy( prf->seed, data, data_length );
5544 prf->seed_length = data_length;
5545 }
Janos Follathf08e2652019-06-13 09:05:41 +01005546
Gilles Peskine43f958b2020-12-13 14:55:14 +01005547 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005548
5549 return( PSA_SUCCESS );
5550}
5551
Janos Follath81550542019-06-13 14:26:34 +01005552static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5553 psa_algorithm_t hash_alg,
5554 const uint8_t *data,
5555 size_t data_length )
5556{
5557 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005558 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005559 return( PSA_ERROR_BAD_STATE );
5560
5561 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5562 if( status != PSA_SUCCESS )
5563 return( status );
5564
Gilles Peskine43f958b2020-12-13 14:55:14 +01005565 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005566
5567 return( PSA_SUCCESS );
5568}
5569
Janos Follath63028dd2019-06-13 09:15:47 +01005570static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5571 const uint8_t *data,
5572 size_t data_length )
5573{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005574 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005575 return( PSA_ERROR_BAD_STATE );
5576
Janos Follathd6dce9f2019-07-04 09:11:38 +01005577 if( data_length != 0 )
5578 {
5579 prf->label = mbedtls_calloc( 1, data_length );
5580 if( prf->label == NULL )
5581 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005582
Janos Follathd6dce9f2019-07-04 09:11:38 +01005583 memcpy( prf->label, data, data_length );
5584 prf->label_length = data_length;
5585 }
Janos Follath63028dd2019-06-13 09:15:47 +01005586
Gilles Peskine43f958b2020-12-13 14:55:14 +01005587 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005588
5589 return( PSA_SUCCESS );
5590}
5591
Janos Follathb03233e2019-06-11 15:30:30 +01005592static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5593 psa_algorithm_t hash_alg,
5594 psa_key_derivation_step_t step,
5595 const uint8_t *data,
5596 size_t data_length )
5597{
Janos Follathb03233e2019-06-11 15:30:30 +01005598 switch( step )
5599 {
Janos Follathf08e2652019-06-13 09:05:41 +01005600 case PSA_KEY_DERIVATION_INPUT_SEED:
5601 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005602 case PSA_KEY_DERIVATION_INPUT_SECRET:
5603 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005604 case PSA_KEY_DERIVATION_INPUT_LABEL:
5605 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005606 default:
5607 return( PSA_ERROR_INVALID_ARGUMENT );
5608 }
5609}
John Durkop07cc04a2020-11-16 22:08:34 -08005610#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5611 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5612
5613#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5614static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5615 psa_tls12_prf_key_derivation_t *prf,
5616 psa_algorithm_t hash_alg,
5617 const uint8_t *data,
5618 size_t data_length )
5619{
5620 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005621 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005622 uint8_t *cur = pms;
5623
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005624 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005625 return( PSA_ERROR_INVALID_ARGUMENT );
5626
5627 /* Quoting RFC 4279, Section 2:
5628 *
5629 * The premaster secret is formed as follows: if the PSK is N octets
5630 * long, concatenate a uint16 with the value N, N zero octets, a second
5631 * uint16 with the value N, and the PSK itself.
5632 */
5633
5634 *cur++ = ( data_length >> 8 ) & 0xff;
5635 *cur++ = ( data_length >> 0 ) & 0xff;
5636 memset( cur, 0, data_length );
5637 cur += data_length;
5638 *cur++ = pms[0];
5639 *cur++ = pms[1];
5640 memcpy( cur, data, data_length );
5641 cur += data_length;
5642
5643 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5644
5645 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5646 return( status );
5647}
Janos Follath6660f0e2019-06-17 08:44:03 +01005648
5649static psa_status_t psa_tls12_prf_psk_to_ms_input(
5650 psa_tls12_prf_key_derivation_t *prf,
5651 psa_algorithm_t hash_alg,
5652 psa_key_derivation_step_t step,
5653 const uint8_t *data,
5654 size_t data_length )
5655{
5656 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005657 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005658 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5659 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005660 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005661
5662 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5663}
John Durkop07cc04a2020-11-16 22:08:34 -08005664#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005665
Gilles Peskineb8965192019-09-24 16:21:10 +02005666/** Check whether the given key type is acceptable for the given
5667 * input step of a key derivation.
5668 *
5669 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5670 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5671 * Both secret and non-secret inputs can alternatively have the type
5672 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5673 * that the input was passed as a buffer rather than via a key object.
5674 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005675static int psa_key_derivation_check_input_type(
5676 psa_key_derivation_step_t step,
5677 psa_key_type_t key_type )
5678{
5679 switch( step )
5680 {
5681 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005682 if( key_type == PSA_KEY_TYPE_DERIVE )
5683 return( PSA_SUCCESS );
5684 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005685 return( PSA_SUCCESS );
5686 break;
5687 case PSA_KEY_DERIVATION_INPUT_LABEL:
5688 case PSA_KEY_DERIVATION_INPUT_SALT:
5689 case PSA_KEY_DERIVATION_INPUT_INFO:
5690 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005691 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5692 return( PSA_SUCCESS );
5693 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005694 return( PSA_SUCCESS );
5695 break;
5696 }
5697 return( PSA_ERROR_INVALID_ARGUMENT );
5698}
5699
Janos Follathb80a94e2019-06-12 15:54:46 +01005700static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005701 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005702 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005703 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005704 const uint8_t *data,
5705 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005706{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005707 psa_status_t status;
5708 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5709
5710 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005711 if( status != PSA_SUCCESS )
5712 goto exit;
5713
John Durkopd0321952020-10-29 21:37:36 -07005714#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005715 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005716 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005718 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719 step, data, data_length );
5720 }
John Durkop07cc04a2020-11-16 22:08:34 -08005721 else
5722#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5723#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5724 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005725 {
Janos Follathb03233e2019-06-11 15:30:30 +01005726 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5727 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5728 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005729 }
John Durkop07cc04a2020-11-16 22:08:34 -08005730 else
5731#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5732#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5733 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005734 {
5735 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5736 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5737 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005738 }
5739 else
John Durkop07cc04a2020-11-16 22:08:34 -08005740#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005741 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005742 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005743 (void) data;
5744 (void) data_length;
5745 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746 return( PSA_ERROR_BAD_STATE );
5747 }
5748
Gilles Peskine224b0d62019-09-23 18:13:17 +02005749exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005750 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005751 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752 return( status );
5753}
5754
Janos Follath51f4a0f2019-06-14 11:35:55 +01005755psa_status_t psa_key_derivation_input_bytes(
5756 psa_key_derivation_operation_t *operation,
5757 psa_key_derivation_step_t step,
5758 const uint8_t *data,
5759 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005760{
Gilles Peskineb8965192019-09-24 16:21:10 +02005761 return( psa_key_derivation_input_internal( operation, step,
5762 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005763 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005764}
5765
Janos Follath51f4a0f2019-06-14 11:35:55 +01005766psa_status_t psa_key_derivation_input_key(
5767 psa_key_derivation_operation_t *operation,
5768 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005769 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005770{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005771 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005772 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005773 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005774
Ronald Cron5c522922020-11-14 16:35:34 +01005775 status = psa_get_and_lock_transparent_key_slot_with_policy(
5776 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005777 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005778 {
5779 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005780 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005781 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005782
5783 /* Passing a key object as a SECRET input unlocks the permission
5784 * to output to a key object. */
5785 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5786 operation->can_output_key = 1;
5787
Ronald Cronf95a2b12020-10-22 15:24:49 +02005788 status = psa_key_derivation_input_internal( operation,
5789 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005790 slot->key.data,
5791 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005792
Ronald Cron5c522922020-11-14 16:35:34 +01005793 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005794
Ronald Cron5c522922020-11-14 16:35:34 +01005795 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005796}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005797
5798
5799
5800/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005801/* Key agreement */
5802/****************************************************************/
5803
John Durkop6ba40d12020-11-10 08:50:04 -08005804#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005805static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5806 size_t peer_key_length,
5807 const mbedtls_ecp_keypair *our_key,
5808 uint8_t *shared_secret,
5809 size_t shared_secret_size,
5810 size_t *shared_secret_length )
5811{
Steven Cooremand4867872020-08-05 16:31:39 +02005812 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005813 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005814 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005815 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005816 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005817 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005818
Ronald Cron90857082020-11-25 14:58:33 +01005819 status = mbedtls_psa_ecp_load_representation(
5820 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005821 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005822 peer_key,
5823 peer_key_length,
5824 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005825 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005826 goto exit;
5827
Jaeden Amero97271b32019-01-10 19:38:51 +00005828 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005829 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005830 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005831 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005832 status = mbedtls_to_psa_error(
5833 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5834 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005835 goto exit;
5836
Jaeden Amero97271b32019-01-10 19:38:51 +00005837 status = mbedtls_to_psa_error(
5838 mbedtls_ecdh_calc_secret( &ecdh,
5839 shared_secret_length,
5840 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005841 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005842 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005843 if( status != PSA_SUCCESS )
5844 goto exit;
5845 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5846 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005847
5848exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005849 if( status != PSA_SUCCESS )
5850 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005851 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005852 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005853 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005854
Jaeden Amero97271b32019-01-10 19:38:51 +00005855 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005856}
John Durkop6ba40d12020-11-10 08:50:04 -08005857#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005858
Gilles Peskine01d718c2018-09-18 12:01:02 +02005859#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5860
Gilles Peskine0216fe12019-04-11 21:23:21 +02005861static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5862 psa_key_slot_t *private_key,
5863 const uint8_t *peer_key,
5864 size_t peer_key_length,
5865 uint8_t *shared_secret,
5866 size_t shared_secret_size,
5867 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005868{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005869 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005870 {
John Durkop6ba40d12020-11-10 08:50:04 -08005871#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005872 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005873 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005874 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005875 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005876 psa_status_t status = mbedtls_psa_ecp_load_representation(
5877 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005878 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005879 private_key->key.data,
5880 private_key->key.bytes,
5881 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005882 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005883 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005884 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005885 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005886 shared_secret, shared_secret_size,
5887 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005888 mbedtls_ecp_keypair_free( ecp );
5889 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005890 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005891#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005892 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005893 (void) private_key;
5894 (void) peer_key;
5895 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005896 (void) shared_secret;
5897 (void) shared_secret_size;
5898 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005899 return( PSA_ERROR_NOT_SUPPORTED );
5900 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005901}
5902
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005903/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005904 * to potentially free embedded data structures and wipe confidential data.
5905 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005906static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005907 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005908 psa_key_slot_t *private_key,
5909 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005910 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005911{
5912 psa_status_t status;
5913 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5914 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005915 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005916
5917 /* Step 1: run the secret agreement algorithm to generate the shared
5918 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005919 status = psa_key_agreement_raw_internal( ka_alg,
5920 private_key,
5921 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005922 shared_secret,
5923 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005924 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005925 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005926 goto exit;
5927
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005928 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005929 * the shared secret. A shared secret is permitted wherever a key
5930 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005931 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005932 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005933 shared_secret,
5934 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005935exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005936 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005937 return( status );
5938}
5939
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005940psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005941 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005942 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005943 const uint8_t *peer_key,
5944 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005945{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005946 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005947 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005948 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005949
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005950 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005951 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005952 status = psa_get_and_lock_transparent_key_slot_with_policy(
5953 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005954 if( status != PSA_SUCCESS )
5955 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005956 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005957 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005958 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005959 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005960 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005961 else
5962 {
5963 /* If a private key has been added as SECRET, we allow the derived
5964 * key material to be used as a key in PSA Crypto. */
5965 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5966 operation->can_output_key = 1;
5967 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005968
Ronald Cron5c522922020-11-14 16:35:34 +01005969 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005970
Ronald Cron5c522922020-11-14 16:35:34 +01005971 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005972}
5973
Gilles Peskinebe697d82019-05-16 18:00:41 +02005974psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005975 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005976 const uint8_t *peer_key,
5977 size_t peer_key_length,
5978 uint8_t *output,
5979 size_t output_size,
5980 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005981{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005982 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005983 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005984 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005985
5986 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5987 {
5988 status = PSA_ERROR_INVALID_ARGUMENT;
5989 goto exit;
5990 }
Ronald Cron5c522922020-11-14 16:35:34 +01005991 status = psa_get_and_lock_transparent_key_slot_with_policy(
5992 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005993 if( status != PSA_SUCCESS )
5994 goto exit;
5995
5996 status = psa_key_agreement_raw_internal( alg, slot,
5997 peer_key, peer_key_length,
5998 output, output_size,
5999 output_length );
6000
6001exit:
6002 if( status != PSA_SUCCESS )
6003 {
6004 /* If an error happens and is not handled properly, the output
6005 * may be used as a key to protect sensitive data. Arrange for such
6006 * a key to be random, which is likely to result in decryption or
6007 * verification errors. This is better than filling the buffer with
6008 * some constant data such as zeros, which would result in the data
6009 * being protected with a reproducible, easily knowable key.
6010 */
6011 psa_generate_random( output, output_size );
6012 *output_length = output_size;
6013 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006014
Ronald Cron5c522922020-11-14 16:35:34 +01006015 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006016
Ronald Cron5c522922020-11-14 16:35:34 +01006017 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006018}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006019
6020
Gilles Peskine30524eb2020-11-13 17:02:26 +01006021
Gilles Peskine86a440b2018-11-12 18:39:40 +01006022/****************************************************************/
6023/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006024/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006025
Gilles Peskine30524eb2020-11-13 17:02:26 +01006026/** Initialize the PSA random generator.
6027 */
6028static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6029{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006030#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6031 memset( rng, 0, sizeof( *rng ) );
6032#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6033
Gilles Peskine30524eb2020-11-13 17:02:26 +01006034 /* Set default configuration if
6035 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6036 if( rng->entropy_init == NULL )
6037 rng->entropy_init = mbedtls_entropy_init;
6038 if( rng->entropy_free == NULL )
6039 rng->entropy_free = mbedtls_entropy_free;
6040
6041 rng->entropy_init( &rng->entropy );
6042#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6043 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6044 /* The PSA entropy injection feature depends on using NV seed as an entropy
6045 * source. Add NV seed as an entropy source for PSA entropy injection. */
6046 mbedtls_entropy_add_source( &rng->entropy,
6047 mbedtls_nv_seed_poll, NULL,
6048 MBEDTLS_ENTROPY_BLOCK_SIZE,
6049 MBEDTLS_ENTROPY_SOURCE_STRONG );
6050#endif
6051
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006052 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006053#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006054}
6055
6056/** Deinitialize the PSA random generator.
6057 */
6058static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6059{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006060#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6061 memset( rng, 0, sizeof( *rng ) );
6062#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006063 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006064 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006065#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006066}
6067
6068/** Seed the PSA random generator.
6069 */
6070static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6071{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006072#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6073 /* Do nothing: the external RNG seeds itself. */
6074 (void) rng;
6075 return( PSA_SUCCESS );
6076#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006077 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006078 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6079 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006080 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006081#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006082}
6083
Gilles Peskinee59236f2018-01-27 23:32:46 +01006084psa_status_t psa_generate_random( uint8_t *output,
6085 size_t output_size )
6086{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006087 GUARD_MODULE_INITIALIZED;
6088
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006089#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6090
6091 size_t output_length = 0;
6092 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6093 output, output_size,
6094 &output_length );
6095 if( status != PSA_SUCCESS )
6096 return( status );
6097 /* Breaking up a request into smaller chunks is currently not supported
6098 * for the extrernal RNG interface. */
6099 if( output_length != output_size )
6100 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6101 return( PSA_SUCCESS );
6102
6103#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6104
Gilles Peskine71ddab92021-01-04 21:01:07 +01006105 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006106 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006107 size_t request_size =
6108 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6109 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6110 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006111 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6112 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006113 if( ret != 0 )
6114 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006115 output_size -= request_size;
6116 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006117 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006118 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006119#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006120}
6121
Gilles Peskine8814fc42020-12-14 15:33:44 +01006122/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006123 *
6124 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6125 * `psa_generate_random(...)`. The state parameter is ignored since the
6126 * PSA API doesn't support passing an explicit state.
6127 *
6128 * In the non-external case, psa_generate_random() calls an
6129 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6130 * and semantics as mbedtls_psa_get_random(). As an optimization,
6131 * instead of doing this back-and-forth between the PSA API and the
6132 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6133 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006134 */
6135#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6136int mbedtls_psa_get_random( void *p_rng,
6137 unsigned char *output,
6138 size_t output_size )
6139{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006140 /* This function takes a pointer to the RNG state because that's what
6141 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6142 * its own state internally and doesn't let the caller access that state.
6143 * So we just ignore the state parameter, and in practice we'll pass
6144 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006145 (void) p_rng;
6146 psa_status_t status = psa_generate_random( output, output_size );
6147 if( status == PSA_SUCCESS )
6148 return( 0 );
6149 else
6150 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6151}
6152#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6153
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006154#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6155#include "mbedtls/entropy_poll.h"
6156
Gilles Peskine7228da22019-07-15 11:06:15 +02006157psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006158 size_t seed_size )
6159{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006160 if( global_data.initialized )
6161 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006162
6163 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6164 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6165 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6166 return( PSA_ERROR_INVALID_ARGUMENT );
6167
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006168 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006169}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006170#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006171
Ronald Cron3772afe2021-02-08 16:10:05 +01006172/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006173 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006174 * \param type The key type
6175 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006176 *
6177 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006178 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006179 * \retval #PSA_ERROR_INVALID_ARGUMENT
6180 * The size in bits of the key is not valid.
6181 * \retval #PSA_ERROR_NOT_SUPPORTED
6182 * The type and/or the size in bits of the key or the combination of
6183 * the two is not supported.
6184 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006185static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6186 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006187{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006188 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006189
Gilles Peskinee59236f2018-01-27 23:32:46 +01006190 if( key_type_is_raw_bytes( type ) )
6191 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006192 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006193 if( status != PSA_SUCCESS )
6194 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006195 }
6196 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006197#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006198 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6199 {
6200 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6201 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006202
Ronald Cron01b2aba2020-10-05 09:42:02 +02006203 /* Accept only byte-aligned keys, for the same reasons as
6204 * in psa_import_rsa_key(). */
6205 if( bits % 8 != 0 )
6206 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006207 }
6208 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006209#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006210
Ronald Cron5c4d3862020-12-07 11:07:24 +01006211#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006212 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6213 {
Ronald Crond81ab562021-02-16 09:01:16 +01006214 /* To avoid empty block, return successfully here. */
6215 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006216 }
6217 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006218#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006219 {
6220 return( PSA_ERROR_NOT_SUPPORTED );
6221 }
6222
6223 return( PSA_SUCCESS );
6224}
6225
Ronald Cron55ed0592020-10-05 10:30:40 +02006226psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006227 const psa_key_attributes_t *attributes,
6228 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006229{
6230 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006231 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006232
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006233 if( ( attributes->domain_parameters == NULL ) &&
6234 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006235 return( PSA_ERROR_INVALID_ARGUMENT );
6236
Ronald Cron01b2aba2020-10-05 09:42:02 +02006237 if( key_type_is_raw_bytes( type ) )
6238 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006239 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006240 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006241 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006242
David Brown12ca5032021-02-11 11:02:00 -07006243#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006244 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006245 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006246#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006247 }
6248 else
6249
John Durkop07cc04a2020-11-16 22:08:34 -08006250#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006251 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006253 return( mbedtls_psa_rsa_generate_key( attributes,
6254 key_buffer,
6255 key_buffer_size,
6256 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006257 }
6258 else
John Durkop07cc04a2020-11-16 22:08:34 -08006259#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006260
John Durkop9814fa22020-11-04 12:28:15 -08006261#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006262 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006263 {
Ronald Cron7023db52020-11-20 18:17:42 +01006264 return( mbedtls_psa_ecp_generate_key( attributes,
6265 key_buffer,
6266 key_buffer_size,
6267 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006268 }
6269 else
John Durkop9814fa22020-11-04 12:28:15 -08006270#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006271 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006272 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006273 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006274 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006275
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006276 return( PSA_SUCCESS );
6277}
Darryl Green0c6575a2018-11-07 16:05:30 +00006278
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006279psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006280 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006281{
6282 psa_status_t status;
6283 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006284 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006285 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006286
Ronald Cron81709fc2020-11-14 12:10:32 +01006287 *key = MBEDTLS_SVC_KEY_ID_INIT;
6288
Gilles Peskine0f84d622019-09-12 19:03:13 +02006289 /* Reject any attempt to create a zero-length key so that we don't
6290 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6291 if( psa_get_key_bits( attributes ) == 0 )
6292 return( PSA_ERROR_INVALID_ARGUMENT );
6293
Ronald Cron81709fc2020-11-14 12:10:32 +01006294 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6295 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006296 if( status != PSA_SUCCESS )
6297 goto exit;
6298
Ronald Cron977c2472020-10-13 08:32:21 +02006299 /* In the case of a transparent key or an opaque key stored in local
6300 * storage (thus not in the case of generating a key in a secure element
6301 * or cryptoprocessor with storage), we have to allocate a buffer to
6302 * hold the generated key material. */
6303 if( slot->key.data == NULL )
6304 {
6305 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6306 PSA_KEY_LOCATION_LOCAL_STORAGE )
6307 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006308 status = psa_validate_key_type_and_size_for_key_generation(
6309 attributes->core.type, attributes->core.bits );
6310 if( status != PSA_SUCCESS )
6311 goto exit;
6312
6313 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6314 attributes->core.type,
6315 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006316 }
6317 else
6318 {
6319 status = psa_driver_wrapper_get_key_buffer_size(
6320 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006321 if( status != PSA_SUCCESS )
6322 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006323 }
Ronald Cron977c2472020-10-13 08:32:21 +02006324
6325 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6326 if( status != PSA_SUCCESS )
6327 goto exit;
6328 }
6329
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006330 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006331 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006332
Ronald Cron2b56bc82020-10-05 10:02:26 +02006333 if( status != PSA_SUCCESS )
6334 psa_remove_key_data_from_memory( slot );
6335
Gilles Peskine11792082019-08-06 18:36:36 +02006336exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006337 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006338 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006339 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006340 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006341
Darryl Green0c6575a2018-11-07 16:05:30 +00006342 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006343}
6344
Gilles Peskinee59236f2018-01-27 23:32:46 +01006345/****************************************************************/
6346/* Module setup */
6347/****************************************************************/
6348
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006349#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006350psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6351 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6352 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6353{
6354 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6355 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006356 global_data.rng.entropy_init = entropy_init;
6357 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006358 return( PSA_SUCCESS );
6359}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006360#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006361
Gilles Peskinee59236f2018-01-27 23:32:46 +01006362void mbedtls_psa_crypto_free( void )
6363{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006364 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006365 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6366 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006367 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006368 }
6369 /* Wipe all remaining data, including configuration.
6370 * In particular, this sets all state indicator to the value
6371 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006372 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006373#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006374 /* Unregister all secure element drivers, so that we restart from
6375 * a pristine state. */
6376 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006377#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006378}
6379
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006380#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6381/** Recover a transaction that was interrupted by a power failure.
6382 *
6383 * This function is called during initialization, before psa_crypto_init()
6384 * returns. If this function returns a failure status, the initialization
6385 * fails.
6386 */
6387static psa_status_t psa_crypto_recover_transaction(
6388 const psa_crypto_transaction_t *transaction )
6389{
6390 switch( transaction->unknown.type )
6391 {
6392 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6393 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006394 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006395 * is implemented.
6396 * https://github.com/ARMmbed/mbed-crypto/issues/218
6397 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006398 default:
6399 /* We found an unsupported transaction in the storage.
6400 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006401 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006402 }
6403}
6404#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6405
Gilles Peskinee59236f2018-01-27 23:32:46 +01006406psa_status_t psa_crypto_init( void )
6407{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006408 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409
Gilles Peskinec6b69072018-11-20 21:42:52 +01006410 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006411 if( global_data.initialized != 0 )
6412 return( PSA_SUCCESS );
6413
Gilles Peskine30524eb2020-11-13 17:02:26 +01006414 /* Initialize and seed the random generator. */
6415 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006416 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006417 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006418 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006419 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006420 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006421
Gilles Peskine66fb1262018-12-10 16:29:04 +01006422 status = psa_initialize_key_slots( );
6423 if( status != PSA_SUCCESS )
6424 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006425
Gilles Peskined9348f22019-10-01 15:22:29 +02006426#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6427 status = psa_init_all_se_drivers( );
6428 if( status != PSA_SUCCESS )
6429 goto exit;
6430#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6431
Gilles Peskine4b734222019-07-24 15:56:31 +02006432#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006433 status = psa_crypto_load_transaction( );
6434 if( status == PSA_SUCCESS )
6435 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006436 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6437 if( status != PSA_SUCCESS )
6438 goto exit;
6439 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006440 }
6441 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6442 {
6443 /* There's no transaction to complete. It's all good. */
6444 status = PSA_SUCCESS;
6445 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006446#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006447
Gilles Peskinec6b69072018-11-20 21:42:52 +01006448 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006449 global_data.initialized = 1;
6450
6451exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006452 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006453 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006454 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455}
6456
6457#endif /* MBEDTLS_PSA_CRYPTO_C */