blob: 5cbf99d92beaf3eb6513bbcc47dfc70f3f94d5fc [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
152 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
153 return( PSA_ERROR_NOT_SUPPORTED );
154 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
157 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
Gilles Peskine9a944802018-06-21 09:35:35 +0200160 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
161 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
162 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
163 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
164 case MBEDTLS_ERR_ASN1_INVALID_DATA:
165 return( PSA_ERROR_INVALID_ARGUMENT );
166 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
168 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
169 return( PSA_ERROR_BUFFER_TOO_SMALL );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
Jaeden Amero93e21112019-02-20 13:57:28 +0000181#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000182 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000183#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
184 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CCM_BAD_INPUT:
192 return( PSA_ERROR_INVALID_ARGUMENT );
193 case MBEDTLS_ERR_CCM_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200
201 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
209 return( PSA_ERROR_INVALID_ARGUMENT );
210 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
211 return( PSA_ERROR_INSUFFICIENT_MEMORY );
212 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
213 return( PSA_ERROR_INVALID_PADDING );
214 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200215 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200219 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
223 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
227 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100228 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
229 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
232 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
233 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
239 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
240 return( PSA_ERROR_NOT_SUPPORTED );
241 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinee59236f2018-01-27 23:32:46 +0100244 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
245 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
246 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248
249 case MBEDTLS_ERR_GCM_AUTH_FAILED:
250 return( PSA_ERROR_INVALID_SIGNATURE );
251 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200252 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
257 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100258 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
259 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100260 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
263 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
264 return( PSA_ERROR_NOT_SUPPORTED );
265 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
266 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
267#endif
268
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MD_ALLOC_FAILED:
279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
280 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
283 return( PSA_ERROR_HARDWARE_FAILURE );
284
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
286 return( PSA_ERROR_STORAGE_FAILURE );
287 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_ALLOC_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
305 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
316 return( PSA_ERROR_NOT_PERMITTED );
317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
324 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
331 return( PSA_ERROR_NOT_SUPPORTED );
332
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_RSA_INVALID_PADDING:
339 return( PSA_ERROR_INVALID_PADDING );
340 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
343 return( PSA_ERROR_INVALID_ARGUMENT );
344 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
345 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200346 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
350 return( PSA_ERROR_BUFFER_TOO_SMALL );
351 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100352 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
360 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
363 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
364 return( PSA_ERROR_INVALID_ARGUMENT );
365 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
366 return( PSA_ERROR_HARDWARE_FAILURE );
367
itayzafrir5c753392018-05-08 11:18:38 +0300368 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300369 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300370 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300371 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
372 return( PSA_ERROR_BUFFER_TOO_SMALL );
373 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
374 return( PSA_ERROR_NOT_SUPPORTED );
375 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
376 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
377 return( PSA_ERROR_INVALID_SIGNATURE );
378 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
379 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100380 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
381 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300382 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
383 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100384
Janos Follatha13b9052019-11-22 12:48:59 +0000385 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
386 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300387
Gilles Peskinee59236f2018-01-27 23:32:46 +0100388 default:
David Saadab4ecc272019-02-14 13:48:10 +0200389 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100390 }
391}
392
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200393
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200394
395
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396/****************************************************************/
397/* Key management */
398/****************************************************************/
399
Gilles Peskine73167e12019-07-12 23:44:37 +0200400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
401static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
402{
Gilles Peskine8e338702019-07-30 20:06:31 +0200403 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200404}
405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
406
John Durkop07cc04a2020-11-16 22:08:34 -0800407/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
408 * current test driver in key_management.c is using this function
409 * when accelerators are used for ECC key pair and public key.
410 * Once that dependency is resolved these guards can be removed.
411 */
John Durkop9814fa22020-11-04 12:28:15 -0800412#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800413 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
415 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 size_t bits,
418 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419{
420 switch( curve )
421 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 528:
436 if( bits_is_sloppy )
437 return( MBEDTLS_ECP_DP_SECP521R1 );
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 }
440 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100441
Paul Elliott8ff510a2020-06-02 17:19:28 +0100442 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
460 if( bits_is_sloppy )
461 return( MBEDTLS_ECP_DP_CURVE25519 );
462 break;
463 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100464 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 }
466 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100467
Paul Elliott8ff510a2020-06-02 17:19:28 +0100468 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100477 }
478 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480
481 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
John Durkop9814fa22020-11-04 12:28:15 -0800483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
486 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200487
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200488static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
489 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490{
491 /* Check that the bit size is acceptable for the key type */
492 switch( type )
493 {
494 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200496 case PSA_KEY_TYPE_DERIVE:
497 break;
David Browne04acc22021-01-26 11:39:16 -0700498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
500 if( bits != 128 && bits != 192 && bits != 256 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
David Browne04acc22021-01-26 11:39:16 -0700504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_CAMELLIA:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
David Browne04acc22021-01-26 11:39:16 -0700510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_DES:
512 if( bits != 64 && bits != 128 && bits != 192 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
David Browne04acc22021-01-26 11:39:16 -0700516#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 case PSA_KEY_TYPE_ARC4:
518 if( bits < 8 || bits > 2048 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700522#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200523 case PSA_KEY_TYPE_CHACHA20:
524 if( bits != 256 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 return( PSA_SUCCESS );
535}
536
Gilles Peskineb46bef22019-07-30 21:32:04 +0200537/** Return the size of the key in the given slot, in bits.
538 *
539 * \param[in] slot A key slot.
540 *
541 * \return The key size in bits, read from the metadata in the slot.
542 */
543static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
544{
545 return( slot->attr.bits );
546}
547
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100548/** Return the output MAC length of a MAC algorithm, in bytes
549 *
550 * \param[in] algorithm The specific MAC algorithm
551 * \param[in] key_type The key type of the key to be used with the
552 * \p algorithm.
553 * \param[out] length The calculated output length of the given MAC
554 * \p algorithm when used with a key corresponding to
555 * the given \p key_type
556 *
557 * \retval #PSA_SUCCESS
558 * The \p length has been successfully calculated
559 * \retval #PSA_ERROR_NOT_SUPPORTED
560 * \p algorithm is a MAC algorithm, but ubsupported by this PSA core.
561 * \retval #PSA_ERROR_INVALID_ARGUMENT
562 * \p algorithm is not a valid, specific MAC algorithm or \p key_type
563 * describes a key incompatible with the specified \p algorithm.
564 */
565static psa_status_t psa_get_mac_output_length( psa_algorithm_t algorithm,
566 psa_key_type_t key_type,
567 size_t *length )
568{
569 if( !PSA_ALG_IS_MAC( algorithm ) || PSA_ALG_IS_WILDCARD( algorithm ) )
570 return( PSA_ERROR_INVALID_ARGUMENT );
571
572 size_t default_length = 0;
573
574 if( PSA_ALG_FULL_LENGTH_MAC( algorithm ) == PSA_ALG_CMAC ||
575 PSA_ALG_FULL_LENGTH_MAC( algorithm ) == PSA_ALG_CBC_MAC )
576 {
577 default_length = PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type );
578 /* CMAC and CBC-MAC are only defined on block ciphers */
579 if( default_length == 0 )
580 return( PSA_ERROR_INVALID_ARGUMENT );
581 }
582 else if( PSA_ALG_IS_HMAC( algorithm ) )
583 {
584 /* HMAC output length is dictated by the underlying hash operation */
585 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( algorithm );
586 default_length = PSA_HASH_LENGTH( hash_alg );
587
588 if( hash_alg == 0 || default_length == 0 )
589 return( PSA_ERROR_INVALID_ARGUMENT );
590 }
591 else
592 return( PSA_ERROR_NOT_SUPPORTED );
593
594 /* Output the expected (potentially truncated) length as long as it can
595 * actually be output by the algorithm */
596 if( PSA_ALG_FULL_LENGTH_MAC( algorithm ) == algorithm )
597 {
598 *length = default_length;
599 return( PSA_SUCCESS );
600 }
601 else if( PSA_MAC_TRUNCATED_LENGTH( algorithm ) <= default_length )
602 {
603 *length = PSA_MAC_TRUNCATED_LENGTH( algorithm );
604 return( PSA_SUCCESS );
605 }
606 else
607 return( PSA_ERROR_INVALID_ARGUMENT );
608}
609
Steven Cooreman75b74362020-07-28 14:30:13 +0200610/** Try to allocate a buffer to an empty key slot.
611 *
612 * \param[in,out] slot Key slot to attach buffer to.
613 * \param[in] buffer_length Requested size of the buffer.
614 *
615 * \retval #PSA_SUCCESS
616 * The buffer has been successfully allocated.
617 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
618 * Not enough memory was available for allocation.
619 * \retval #PSA_ERROR_ALREADY_EXISTS
620 * Trying to allocate a buffer to a non-empty key slot.
621 */
622static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
623 size_t buffer_length )
624{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100625 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200626 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200627
Ronald Cronea0f8a62020-11-25 17:52:23 +0100628 slot->key.data = mbedtls_calloc( 1, buffer_length );
629 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200630 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200631
Ronald Cronea0f8a62020-11-25 17:52:23 +0100632 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200633 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200634}
635
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200636psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
637 const uint8_t* data,
638 size_t data_length )
639{
640 psa_status_t status = psa_allocate_buffer_to_slot( slot,
641 data_length );
642 if( status != PSA_SUCCESS )
643 return( status );
644
Ronald Cronea0f8a62020-11-25 17:52:23 +0100645 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200646 return( PSA_SUCCESS );
647}
648
Ronald Crona0fe59f2020-11-29 11:14:53 +0100649psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 const psa_key_attributes_t *attributes,
651 const uint8_t *data, size_t data_length,
652 uint8_t *key_buffer, size_t key_buffer_size,
653 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100654{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100655 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
656 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100657
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200658 /* zero-length keys are never supported. */
659 if( data_length == 0 )
660 return( PSA_ERROR_NOT_SUPPORTED );
661
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100662 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200665
Steven Cooreman75b74362020-07-28 14:30:13 +0200666 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
667 if( data_length > SIZE_MAX / 8 )
668 return( PSA_ERROR_NOT_SUPPORTED );
669
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200670 /* Enforce a size limit, and in particular ensure that the bit
671 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100672 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200673 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200674
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100675 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200676 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200677 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200678
Ronald Crondd04d422020-11-28 15:14:42 +0100679 /* Copy the key material. */
680 memcpy( key_buffer, data, data_length );
681 *key_buffer_length = data_length;
682 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200683
Steven Cooreman40120f62020-10-29 11:42:22 +0100684 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100685 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100686 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200687 {
John Durkop9814fa22020-11-04 12:28:15 -0800688#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
689 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100690 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200691 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100692 return( mbedtls_psa_ecp_import_key( attributes,
693 data, data_length,
694 key_buffer, key_buffer_size,
695 key_buffer_length,
696 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100697 }
John Durkop9814fa22020-11-04 12:28:15 -0800698#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
699 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700700#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
701 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100702 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200703 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100704 return( mbedtls_psa_rsa_import_key( attributes,
705 data, data_length,
706 key_buffer, key_buffer_size,
707 key_buffer_length,
708 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200709 }
John Durkop9814fa22020-11-04 12:28:15 -0800710#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
711 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100712 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100713
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100714 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100715}
716
Gilles Peskinef603c712019-01-19 13:40:11 +0100717/** Calculate the intersection of two algorithm usage policies.
718 *
719 * Return 0 (which allows no operation) on incompatibility.
720 */
721static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100722 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100723 psa_algorithm_t alg1,
724 psa_algorithm_t alg2 )
725{
Gilles Peskine549ea862019-05-22 11:45:59 +0200726 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100727 if( alg1 == alg2 )
728 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100729 /* If the policies are from the same hash-and-sign family, check
730 * if one is a wildcard. If so the other has the specific algorithm. */
731 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
732 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
733 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100734 {
Steven Cooreman03488022021-02-18 12:07:20 +0100735 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
736 return( alg2 );
737 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
738 return( alg1 );
739 }
740 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100741 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100742 * restrictive tag length. */
743 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
744 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
745 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
746 {
747 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
748 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
749 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100750
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100751 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100752 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
753 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100754 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100755 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100756 }
757 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100758 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100759 ( alg1_len <= alg2_len ) )
760 {
761 return( alg2 );
762 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100763 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100764 ( alg2_len <= alg1_len ) )
765 {
766 return( alg1 );
767 }
768 }
769 /* If the policies are from the same MAC family, check whether one
770 * of them is a minimum-MAC-length policy. Calculate the most
771 * restrictive tag length. */
772 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
773 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
774 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
775 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100776 /* Calculate the actual requested output length for both sides. In case
777 * of at-least-this-length wildcard algorithms, the requested output
778 * length is the shortest allowed length. */
779 size_t alg1_len = 0;
780 size_t alg2_len = 0;
781 if( PSA_SUCCESS != psa_get_mac_output_length(
782 PSA_ALG_TRUNCATED_MAC( alg1,
783 PSA_MAC_TRUNCATED_LENGTH( alg1 ) ),
784 key_type,
785 &alg1_len ) )
786 {
787 return( 0 );
788 }
789 if( PSA_SUCCESS != psa_get_mac_output_length(
790 PSA_ALG_TRUNCATED_MAC( alg2,
791 PSA_MAC_TRUNCATED_LENGTH( alg2 ) ),
792 key_type,
793 &alg2_len ) )
794 {
795 return( 0 );
796 }
797
Steven Cooreman03488022021-02-18 12:07:20 +0100798 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799
Steven Cooremana1d83222021-02-25 10:20:29 +0100800 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100801 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
802 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100803 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100804 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100805 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100806 /* If only one is a wildcard, return specific algorithm if compatible. */
807 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100808 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100809 if( alg1_len <= alg2_len )
810 return( alg2 );
811 else
812 return( 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100814 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100815 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100816 if( alg2_len <= alg1_len )
817 return( alg1 );
818 else
819 return( 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100820 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100821 /* If none of them are wildcards, check whether we can match
822 * default-length with exact-length, and return exact-length in that
823 * case. */
824 if( alg1_len == alg2_len )
825 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100826 }
827 /* If the policies are incompatible, allow nothing. */
828 return( 0 );
829}
830
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100831static int psa_key_algorithm_permits( psa_key_type_t key_type,
832 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200833 psa_algorithm_t requested_alg )
834{
Gilles Peskine549ea862019-05-22 11:45:59 +0200835 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200836 if( requested_alg == policy_alg )
837 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100838 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
839 * and requested_alg is the same hash-and-sign family with any hash,
840 * then requested_alg is compliant with policy_alg. */
841 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
842 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843 {
Steven Cooreman03488022021-02-18 12:07:20 +0100844 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
845 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
846 }
847 /* If policy_alg is a wildcard AEAD algorithm of the same base as
848 * the requested algorithm, check the requested tag length to be
849 * equal-length or longer than the wildcard-specified length. */
850 if( PSA_ALG_IS_AEAD( policy_alg ) &&
851 PSA_ALG_IS_AEAD( requested_alg ) &&
852 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
853 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100854 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100855 {
856 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
857 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
858 }
Steven Cooreman03488022021-02-18 12:07:20 +0100859 if( PSA_ALG_IS_MAC( policy_alg ) &&
860 PSA_ALG_IS_MAC( requested_alg ) &&
861 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100862 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100863 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100864 size_t actual_output_length;
865 size_t default_output_length;
866 if( PSA_SUCCESS != psa_get_mac_output_length(
867 requested_alg,
868 key_type,
869 &actual_output_length ) )
870 {
871 return( 0 );
872 }
873 if( PSA_SUCCESS != psa_get_mac_output_length(
874 PSA_ALG_FULL_LENGTH_MAC( requested_alg ),
875 key_type,
876 &default_output_length ) )
877 {
878 return( 0 );
879 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100880
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100881 /* If the policy is default-length, only allow an algorithm with
882 * a declared exact-length matching the default. */
883 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
884 return( actual_output_length == default_output_length );
885
886 /* If the requested algorithm is default-length, allow it if the policy
887 * is exactly the default length. */
888 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
889 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
890 {
891 return( 1 );
892 }
893
894 /* If policy_alg is a wildcard MAC algorithm of the same base as
895 * the requested algorithm, check the requested tag length to be
896 * equal-length or longer than the wildcard-specified length. */
897 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
898 {
899 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
900 actual_output_length );
901 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902 }
Steven Cooremance48e852020-10-05 16:02:45 +0200903 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200904 * a key derivation with that key agreement should also be allowed. This
905 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200906 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
907 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
908 {
909 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
910 policy_alg );
911 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100912 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200913 return( 0 );
914}
915
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100916/** Test whether a policy permits an algorithm.
917 *
918 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100919 *
920 * \retval PSA_SUCCESS When \p alg is a specific algorithm
921 * allowed by the \p policy.
922 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
923 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
924 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100925 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100926static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100927 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100928 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100929{
Steven Cooremand788fab2021-02-25 11:29:17 +0100930 /* '0' is not a valid algorithm */
931 if( alg == 0 )
932 return( PSA_ERROR_INVALID_ARGUMENT );
933
Steven Cooreman7e39f052021-02-23 14:18:32 +0100934 /* A requested algorithm cannot be a wildcard. */
935 if( PSA_ALG_IS_WILDCARD( alg ) )
936 return( PSA_ERROR_INVALID_ARGUMENT );
937
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100938 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
939 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100940 return( PSA_SUCCESS );
941 else
942 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100943}
944
Gilles Peskinef603c712019-01-19 13:40:11 +0100945/** Restrict a key policy based on a constraint.
946 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100947 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100948 * \param[in,out] policy The policy to restrict.
949 * \param[in] constraint The policy constraint to apply.
950 *
951 * \retval #PSA_SUCCESS
952 * \c *policy contains the intersection of the original value of
953 * \c *policy and \c *constraint.
954 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100955 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100956 * \c *policy is unchanged.
957 */
958static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100959 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100960 psa_key_policy_t *policy,
961 const psa_key_policy_t *constraint )
962{
963 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100964 psa_key_policy_algorithm_intersection( key_type, policy->alg,
965 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200966 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100967 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
968 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100969 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
970 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200971 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
972 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100973 policy->usage &= constraint->usage;
974 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200975 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100976 return( PSA_SUCCESS );
977}
978
Ronald Cron5c522922020-11-14 16:35:34 +0100979/** Get the description of a key given its identifier and policy constraints
980 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200981 *
Ronald Cron5c522922020-11-14 16:35:34 +0100982 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100983 * nonzero, the key must allow operations with this algorithm. If \p alg is
984 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100985 *
Ronald Cron5c522922020-11-14 16:35:34 +0100986 * In case of a persistent key, the function loads the description of the key
987 * into a key slot if not already done.
988 *
989 * On success, the returned key slot is locked. It is the responsibility of
990 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 */
Ronald Cron5c522922020-11-14 16:35:34 +0100992static psa_status_t psa_get_and_lock_key_slot_with_policy(
993 mbedtls_svc_key_id_t key,
994 psa_key_slot_t **p_slot,
995 psa_key_usage_t usage,
996 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100997{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
999 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001000
Ronald Cron5c522922020-11-14 16:35:34 +01001001 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001002 if( status != PSA_SUCCESS )
1003 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001004 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001005
1006 /* Enforce that usage policy for the key slot contains all the flags
1007 * required by the usage parameter. There is one exception: public
1008 * keys can always be exported, so we treat public key objects as
1009 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001010 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001011 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001012
Gilles Peskine8e338702019-07-30 20:06:31 +02001013 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +01001014 {
1015 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001016 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001017 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001018
1019 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001020 if( alg != 0 )
1021 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001022 status = psa_key_policy_permits( &slot->attr.policy,
1023 slot->attr.type,
1024 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001025 if( status != PSA_SUCCESS )
1026 goto error;
1027 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001028
Darryl Green06fd18d2018-07-16 11:21:11 +01001029 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001030
1031error:
1032 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001033 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001034
1035 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001036}
Darryl Green940d72c2018-07-13 13:18:51 +01001037
Ronald Cron5c522922020-11-14 16:35:34 +01001038/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001039 *
1040 * A transparent key is a key for which the key material is directly
1041 * available, as opposed to a key in a secure element.
1042 *
Ronald Cron5c522922020-11-14 16:35:34 +01001043 * This is a temporary function to use instead of
1044 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1045 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046 *
Ronald Cron5c522922020-11-14 16:35:34 +01001047 * On success, the returned key slot is locked. It is the responsibility of the
1048 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001049 */
1050#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001051static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1052 mbedtls_svc_key_id_t key,
1053 psa_key_slot_t **p_slot,
1054 psa_key_usage_t usage,
1055 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001056{
Ronald Cron5c522922020-11-14 16:35:34 +01001057 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1058 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001059 if( status != PSA_SUCCESS )
1060 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001061
Gilles Peskineadad8132019-07-25 11:31:23 +02001062 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001063 {
Ronald Cron5c522922020-11-14 16:35:34 +01001064 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001065 *p_slot = NULL;
1066 return( PSA_ERROR_NOT_SUPPORTED );
1067 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001068
Gilles Peskine28f8f302019-07-24 13:30:31 +02001069 return( PSA_SUCCESS );
1070}
1071#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1072/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001073#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1074 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1076
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001077/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001078static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001079{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001080 /* Data pointer will always be either a valid pointer or NULL in an
1081 * initialized slot, so we can just free it. */
1082 if( slot->key.data != NULL )
1083 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1084
1085 mbedtls_free( slot->key.data );
1086 slot->key.data = NULL;
1087 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001088
1089 return( PSA_SUCCESS );
1090}
1091
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001092/** Completely wipe a slot in memory, including its policy.
1093 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001094psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001095{
1096 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001097
1098 /*
1099 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001100 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001101 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1102 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001103 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001104 */
Ronald Cron5c522922020-11-14 16:35:34 +01001105 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001106 {
1107#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001108 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001109#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001110 status = PSA_ERROR_CORRUPTION_DETECTED;
1111 }
1112
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001113 /* Multipart operations may still be using the key. This is safe
1114 * because all multipart operation objects are independent from
1115 * the key slot: if they need to access the key after the setup
1116 * phase, they have a copy of the key. Note that this means that
1117 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001118 /* At this point, key material and other type-specific content has
1119 * been wiped. Clear remaining metadata. We can call memset and not
1120 * zeroize because the metadata is not particularly sensitive. */
1121 memset( slot, 0, sizeof( *slot ) );
1122 return( status );
1123}
1124
Ronald Croncf56a0a2020-08-04 09:51:30 +02001125psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001126{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001127 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001128 psa_status_t status; /* status of the last operation */
1129 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001130#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1131 psa_se_drv_table_entry_t *driver;
1132#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001133
Ronald Croncf56a0a2020-08-04 09:51:30 +02001134 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001135 return( PSA_SUCCESS );
1136
Ronald Cronf2911112020-10-29 17:51:10 +01001137 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001138 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001139 * key, this will load the key description from persistent memory if not
1140 * done yet. We cannot avoid this loading as without it we don't know if
1141 * the key is operated by an SE or not and this information is needed by
1142 * the current implementation.
1143 */
Ronald Cron5c522922020-11-14 16:35:34 +01001144 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001145 if( status != PSA_SUCCESS )
1146 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001147
Ronald Cronf2911112020-10-29 17:51:10 +01001148 /*
1149 * If the key slot containing the key description is under access by the
1150 * library (apart from the present access), the key cannot be destroyed
1151 * yet. For the time being, just return in error. Eventually (to be
1152 * implemented), the key should be destroyed when all accesses have
1153 * stopped.
1154 */
Ronald Cron5c522922020-11-14 16:35:34 +01001155 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001156 {
Ronald Cron5c522922020-11-14 16:35:34 +01001157 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001158 return( PSA_ERROR_GENERIC_ERROR );
1159 }
1160
Gilles Peskine354f7672019-07-12 23:46:38 +02001161#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001162 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001163 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001164 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001165 /* For a key in a secure element, we need to do three things:
1166 * remove the key file in internal storage, destroy the
1167 * key inside the secure element, and update the driver's
1168 * persistent data. Start a transaction that will encompass these
1169 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001170 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001171 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001172 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001173 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001174 status = psa_crypto_save_transaction( );
1175 if( status != PSA_SUCCESS )
1176 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001177 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001178 /* We should still try to destroy the key in the secure
1179 * element and the key metadata in storage. This is especially
1180 * important if the error is that the storage is full.
1181 * But how to do it exactly without risking an inconsistent
1182 * state after a reset?
1183 * https://github.com/ARMmbed/mbed-crypto/issues/215
1184 */
1185 overall_status = status;
1186 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001187 }
1188
Ronald Cronea0f8a62020-11-25 17:52:23 +01001189 status = psa_destroy_se_key( driver,
1190 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001191 if( overall_status == PSA_SUCCESS )
1192 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001193 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001194#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1195
Darryl Greend49a4992018-06-18 17:27:26 +01001196#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001197 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001198 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 status = psa_destroy_persistent_key( slot->attr.id );
1200 if( overall_status == PSA_SUCCESS )
1201 overall_status = status;
1202
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001203 /* TODO: other slots may have a copy of the same key. We should
1204 * invalidate them.
1205 * https://github.com/ARMmbed/mbed-crypto/issues/214
1206 */
Darryl Greend49a4992018-06-18 17:27:26 +01001207 }
1208#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001209
Gilles Peskinefc762652019-07-22 19:30:34 +02001210#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1211 if( driver != NULL )
1212 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001213 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001214 if( overall_status == PSA_SUCCESS )
1215 overall_status = status;
1216 status = psa_crypto_stop_transaction( );
1217 if( overall_status == PSA_SUCCESS )
1218 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001219 }
1220#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1221
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001222#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1223exit:
1224#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001225 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001226 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1227 if( overall_status == PSA_SUCCESS )
1228 overall_status = status;
1229 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230}
1231
John Durkop07cc04a2020-11-16 22:08:34 -08001232#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001233 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001234static psa_status_t psa_get_rsa_public_exponent(
1235 const mbedtls_rsa_context *rsa,
1236 psa_key_attributes_t *attributes )
1237{
1238 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001240 uint8_t *buffer = NULL;
1241 size_t buflen;
1242 mbedtls_mpi_init( &mpi );
1243
1244 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1245 if( ret != 0 )
1246 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001247 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1248 {
1249 /* It's the default value, which is reported as an empty string,
1250 * so there's nothing to do. */
1251 goto exit;
1252 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001253
1254 buflen = mbedtls_mpi_size( &mpi );
1255 buffer = mbedtls_calloc( 1, buflen );
1256 if( buffer == NULL )
1257 {
1258 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1259 goto exit;
1260 }
1261 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1262 if( ret != 0 )
1263 goto exit;
1264 attributes->domain_parameters = buffer;
1265 attributes->domain_parameters_size = buflen;
1266
1267exit:
1268 mbedtls_mpi_free( &mpi );
1269 if( ret != 0 )
1270 mbedtls_free( buffer );
1271 return( mbedtls_to_psa_error( ret ) );
1272}
John Durkop07cc04a2020-11-16 22:08:34 -08001273#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001274 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001275
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001276/** Retrieve all the publicly-accessible attributes of a key.
1277 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001278psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001279 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001280{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001281 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001282 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001283 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001284
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001285 psa_reset_key_attributes( attributes );
1286
Ronald Cron5c522922020-11-14 16:35:34 +01001287 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001288 if( status != PSA_SUCCESS )
1289 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001290
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001291 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001292 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1293 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1294
1295#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1296 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001297 psa_set_key_slot_number( attributes,
1298 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001300
Gilles Peskine8e338702019-07-30 20:06:31 +02001301 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001302 {
John Durkop07cc04a2020-11-16 22:08:34 -08001303#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001304 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001305 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001306 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001307#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001308 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001309 * is not yet implemented.
1310 * https://github.com/ARMmbed/mbed-crypto/issues/216
1311 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001312 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001313 break;
1314#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001315 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001316 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001317
Ronald Cron90857082020-11-25 14:58:33 +01001318 status = mbedtls_psa_rsa_load_representation(
1319 slot->attr.type,
1320 slot->key.data,
1321 slot->key.bytes,
1322 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001323 if( status != PSA_SUCCESS )
1324 break;
1325
Steven Cooremana2371e52020-07-28 14:30:39 +02001326 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001327 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001328 mbedtls_rsa_free( rsa );
1329 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001330 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001331 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001332#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001333 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001334 default:
1335 /* Nothing else to do. */
1336 break;
1337 }
1338
1339 if( status != PSA_SUCCESS )
1340 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001341
Ronald Cron5c522922020-11-14 16:35:34 +01001342 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001343
Ronald Cron5c522922020-11-14 16:35:34 +01001344 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001345}
1346
Gilles Peskinec8000c02019-08-02 20:15:51 +02001347#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1348psa_status_t psa_get_key_slot_number(
1349 const psa_key_attributes_t *attributes,
1350 psa_key_slot_number_t *slot_number )
1351{
1352 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1353 {
1354 *slot_number = attributes->slot_number;
1355 return( PSA_SUCCESS );
1356 }
1357 else
1358 return( PSA_ERROR_INVALID_ARGUMENT );
1359}
1360#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1361
Ronald Crond18b5f82020-11-25 16:46:05 +01001362static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1363 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001364 uint8_t *data,
1365 size_t data_size,
1366 size_t *data_length )
1367{
Ronald Crond18b5f82020-11-25 16:46:05 +01001368 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001369 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001370 memcpy( data, key_buffer, key_buffer_size );
1371 memset( data + key_buffer_size, 0,
1372 data_size - key_buffer_size );
1373 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001374 return( PSA_SUCCESS );
1375}
1376
Ronald Cron7285cda2020-11-26 14:46:37 +01001377psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001378 const psa_key_attributes_t *attributes,
1379 const uint8_t *key_buffer, size_t key_buffer_size,
1380 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001381{
Ronald Crond18b5f82020-11-25 16:46:05 +01001382 psa_key_type_t type = attributes->core.type;
1383
Ronald Crond18b5f82020-11-25 16:46:05 +01001384 if( key_type_is_raw_bytes( type ) ||
1385 PSA_KEY_TYPE_IS_RSA( type ) ||
1386 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001387 {
1388 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001389 key_buffer, key_buffer_size,
1390 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001391 }
1392 else
1393 {
1394 /* This shouldn't happen in the reference implementation, but
1395 it is valid for a special-purpose implementation to omit
1396 support for exporting certain key types. */
1397 return( PSA_ERROR_NOT_SUPPORTED );
1398 }
1399}
1400
1401psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1402 uint8_t *data,
1403 size_t data_size,
1404 size_t *data_length )
1405{
1406 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1407 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1408 psa_key_slot_t *slot;
1409
Ronald Crone907e552021-01-18 13:22:38 +01001410 /* Reject a zero-length output buffer now, since this can never be a
1411 * valid key representation. This way we know that data must be a valid
1412 * pointer and we can do things like memset(data, ..., data_size). */
1413 if( data_size == 0 )
1414 return( PSA_ERROR_BUFFER_TOO_SMALL );
1415
Ronald Cron9486f9d2020-11-26 13:36:23 +01001416 /* Set the key to empty now, so that even when there are errors, we always
1417 * set data_length to a value between 0 and data_size. On error, setting
1418 * the key to empty is a good choice because an empty key representation is
1419 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001420 *data_length = 0;
1421
Ronald Cron9486f9d2020-11-26 13:36:23 +01001422 /* Export requires the EXPORT flag. There is an exception for public keys,
1423 * which don't require any flag, but
1424 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1425 */
1426 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1427 PSA_KEY_USAGE_EXPORT, 0 );
1428 if( status != PSA_SUCCESS )
1429 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001430
Ronald Crond18b5f82020-11-25 16:46:05 +01001431 psa_key_attributes_t attributes = {
1432 .core = slot->attr
1433 };
Ronald Cron67227982020-11-26 15:16:05 +01001434 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001435 slot->key.data, slot->key.bytes,
1436 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001437
Ronald Cron9486f9d2020-11-26 13:36:23 +01001438 unlock_status = psa_unlock_key_slot( slot );
1439
1440 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1441}
1442
Ronald Cron7285cda2020-11-26 14:46:37 +01001443psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001444 const psa_key_attributes_t *attributes,
1445 const uint8_t *key_buffer,
1446 size_t key_buffer_size,
1447 uint8_t *data,
1448 size_t data_size,
1449 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001450{
Ronald Crond18b5f82020-11-25 16:46:05 +01001451 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001452
Ronald Crond18b5f82020-11-25 16:46:05 +01001453 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001454 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001455 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001456 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001457 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001458 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001459 key_buffer, key_buffer_size,
1460 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001461 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001462
Ronald Crond18b5f82020-11-25 16:46:05 +01001463 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001464 {
John Durkop0e005192020-10-31 22:06:54 -07001465#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1466 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001467 return( mbedtls_psa_rsa_export_public_key( attributes,
1468 key_buffer,
1469 key_buffer_size,
1470 data,
1471 data_size,
1472 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001473#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 /* We don't know how to convert a private RSA key to public. */
1475 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001476#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1477 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001478 }
1479 else
Moran Pekera998bc62018-04-16 18:16:20 +03001480 {
John Durkop6ba40d12020-11-10 08:50:04 -08001481#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1482 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001483 return( mbedtls_psa_ecp_export_public_key( attributes,
1484 key_buffer,
1485 key_buffer_size,
1486 data,
1487 data_size,
1488 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001489#else
1490 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001491 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001492#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1493 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001494 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001495 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001496 else
1497 {
1498 /* This shouldn't happen in the reference implementation, but
1499 it is valid for a special-purpose implementation to omit
1500 support for exporting certain key types. */
1501 return( PSA_ERROR_NOT_SUPPORTED );
1502 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001503}
Ronald Crond18b5f82020-11-25 16:46:05 +01001504
Ronald Croncf56a0a2020-08-04 09:51:30 +02001505psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001506 uint8_t *data,
1507 size_t data_size,
1508 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001509{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001510 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001511 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001512 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001513
Ronald Crone907e552021-01-18 13:22:38 +01001514 /* Reject a zero-length output buffer now, since this can never be a
1515 * valid key representation. This way we know that data must be a valid
1516 * pointer and we can do things like memset(data, ..., data_size). */
1517 if( data_size == 0 )
1518 return( PSA_ERROR_BUFFER_TOO_SMALL );
1519
Darryl Greendd8fb772018-11-07 16:00:44 +00001520 /* Set the key to empty now, so that even when there are errors, we always
1521 * set data_length to a value between 0 and data_size. On error, setting
1522 * the key to empty is a good choice because an empty key representation is
1523 * unlikely to be accepted anywhere. */
1524 *data_length = 0;
1525
1526 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001527 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001528 if( status != PSA_SUCCESS )
1529 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001530
Ronald Cron9486f9d2020-11-26 13:36:23 +01001531 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1532 {
1533 status = PSA_ERROR_INVALID_ARGUMENT;
1534 goto exit;
1535 }
1536
Ronald Crond18b5f82020-11-25 16:46:05 +01001537 psa_key_attributes_t attributes = {
1538 .core = slot->attr
1539 };
Ronald Cron67227982020-11-26 15:16:05 +01001540 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001541 &attributes, slot->key.data, slot->key.bytes,
1542 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001543
1544exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001545 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001546
Ronald Cron5c522922020-11-14 16:35:34 +01001547 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001548}
1549
Gilles Peskine91e8c332019-08-02 19:19:39 +02001550#if defined(static_assert)
1551static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1552 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001553static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001554 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001555static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001556 "One or more key attribute flag is listed as both internal-only and external-only" );
1557#endif
1558
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001559/** Validate that a key policy is internally well-formed.
1560 *
1561 * This function only rejects invalid policies. It does not validate the
1562 * consistency of the policy with respect to other attributes of the key
1563 * such as the key type.
1564 */
1565static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001566{
1567 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001568 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001569 PSA_KEY_USAGE_ENCRYPT |
1570 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001571 PSA_KEY_USAGE_SIGN_HASH |
1572 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001573 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1574 return( PSA_ERROR_INVALID_ARGUMENT );
1575
Gilles Peskine4747d192019-04-17 15:05:45 +02001576 return( PSA_SUCCESS );
1577}
1578
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001579/** Validate the internal consistency of key attributes.
1580 *
1581 * This function only rejects invalid attribute values. If does not
1582 * validate the consistency of the attributes with any key data that may
1583 * be involved in the creation of the key.
1584 *
1585 * Call this function early in the key creation process.
1586 *
1587 * \param[in] attributes Key attributes for the new key.
1588 * \param[out] p_drv On any return, the driver for the key, if any.
1589 * NULL for a transparent key.
1590 *
1591 */
1592static psa_status_t psa_validate_key_attributes(
1593 const psa_key_attributes_t *attributes,
1594 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001595{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001596 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001597 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001598 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001599
Ronald Cron54b90082020-10-29 15:26:43 +01001600 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001601 if( status != PSA_SUCCESS )
1602 return( status );
1603
Ronald Crond2ed4812020-07-17 16:11:30 +02001604 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001605 if( status != PSA_SUCCESS )
1606 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001607
Ronald Cron65f38a32020-10-23 17:11:13 +02001608 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1609 {
1610 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1611 return( PSA_ERROR_INVALID_ARGUMENT );
1612 }
1613 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001614 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001615 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001616 if( status != PSA_SUCCESS )
1617 return( status );
1618 }
1619
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001620 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001621 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001622 return( status );
1623
1624 /* Refuse to create overly large keys.
1625 * Note that this doesn't trigger on import if the attributes don't
1626 * explicitly specify a size (so psa_get_key_bits returns 0), so
1627 * psa_import_key() needs its own checks. */
1628 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1629 return( PSA_ERROR_NOT_SUPPORTED );
1630
Gilles Peskine91e8c332019-08-02 19:19:39 +02001631 /* Reject invalid flags. These should not be reachable through the API. */
1632 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1633 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1634 return( PSA_ERROR_INVALID_ARGUMENT );
1635
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001636 return( PSA_SUCCESS );
1637}
1638
Gilles Peskine4747d192019-04-17 15:05:45 +02001639/** Prepare a key slot to receive key material.
1640 *
1641 * This function allocates a key slot and sets its metadata.
1642 *
1643 * If this function fails, call psa_fail_key_creation().
1644 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001645 * This function is intended to be used as follows:
1646 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001647 * it with the specified attributes, and in case of a volatile key assign it
1648 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 * -# Populate the slot with the key material.
1650 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1651 * In case of failure at any step, stop the sequence and call
1652 * psa_fail_key_creation().
1653 *
Ronald Cron5c522922020-11-14 16:35:34 +01001654 * On success, the key slot is locked. It is the responsibility of the caller
1655 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001656 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001657 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001658 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001659 * \param[out] p_slot On success, a pointer to the prepared slot.
1660 * \param[out] p_drv On any return, the driver for the key, if any.
1661 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001662 *
1663 * \retval #PSA_SUCCESS
1664 * The key slot is ready to receive key material.
1665 * \return If this function fails, the key slot is an invalid state.
1666 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001667 */
1668static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001669 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001670 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001671 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001672 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001673{
1674 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001675 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001676 psa_key_slot_t *slot;
1677
Gilles Peskinedf179142019-07-15 22:02:14 +02001678 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001679 *p_drv = NULL;
1680
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001681 status = psa_validate_key_attributes( attributes, p_drv );
1682 if( status != PSA_SUCCESS )
1683 return( status );
1684
Ronald Cronc4d1b512020-07-31 11:26:37 +02001685 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 if( status != PSA_SUCCESS )
1687 return( status );
1688 slot = *p_slot;
1689
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001690 /* We're storing the declared bit-size of the key. It's up to each
1691 * creation mechanism to verify that this information is correct.
1692 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001693 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001694 * is optional (import, copy). In case of a volatile key, assign it the
1695 * volatile key identifier associated to the slot returned to contain its
1696 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001697
1698 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001699 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1700 {
1701#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1702 slot->attr.id = volatile_key_id;
1703#else
1704 slot->attr.id.key_id = volatile_key_id;
1705#endif
1706 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001707
Gilles Peskine91e8c332019-08-02 19:19:39 +02001708 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001709 * external-only flags, query `attributes`. Thanks to the check
1710 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001711 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001712 * may have set. */
1713 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001714
Gilles Peskinecbaff462019-07-12 23:46:04 +02001715#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001716 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001717 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001718 * create the key file in internal storage, create the
1719 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 * persistent data. This is done by starting a transaction that will
1721 * encompass these three actions.
1722 * For registering a volatile key, we just need to find an appropriate
1723 * slot number inside the SE. Since the key is designated volatile, creating
1724 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001725 /* The first thing to do is to find a slot number for the new key.
1726 * We save the slot number in persistent storage as part of the
1727 * transaction data. It will be needed to recover if the power
1728 * fails during the key creation process, to clean up on the secure
1729 * element side after restarting. Obtaining a slot number from the
1730 * secure element driver updates its persistent state, but we do not yet
1731 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001732 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001733 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001734 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001735 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001736 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001737 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001738 if( status != PSA_SUCCESS )
1739 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001740
1741 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001742 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001743 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1744 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001745 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001746 psa_crypto_transaction.key.id = slot->attr.id;
1747 status = psa_crypto_save_transaction( );
1748 if( status != PSA_SUCCESS )
1749 {
1750 (void) psa_crypto_stop_transaction( );
1751 return( status );
1752 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001753 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001754
1755 status = psa_copy_key_material_into_slot(
1756 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001757 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001758
1759 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1760 {
1761 /* Key registration only makes sense with a secure element. */
1762 return( PSA_ERROR_INVALID_ARGUMENT );
1763 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001764#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1765
Ronald Cronc4d1b512020-07-31 11:26:37 +02001766 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001767}
Gilles Peskine4747d192019-04-17 15:05:45 +02001768
1769/** Finalize the creation of a key once its key material has been set.
1770 *
1771 * This entails writing the key to persistent storage.
1772 *
1773 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001774 * See the documentation of psa_start_key_creation() for the intended use
1775 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001776 *
Ronald Cron5c522922020-11-14 16:35:34 +01001777 * If the finalization succeeds, the function unlocks the key slot (it was
1778 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1779 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001780 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001781 * \param[in,out] slot Pointer to the slot with key material.
1782 * \param[in] driver The secure element driver for the key,
1783 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001784 * \param[out] key On success, identifier of the key. Note that the
1785 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001786 *
1787 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001788 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001789 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1790 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1791 * \retval #PSA_ERROR_ALREADY_EXISTS
1792 * \retval #PSA_ERROR_DATA_INVALID
1793 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001794 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001795 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001796 * \return If this function fails, the key slot is an invalid state.
1797 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001798 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001799static psa_status_t psa_finish_key_creation(
1800 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001801 psa_se_drv_table_entry_t *driver,
1802 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001803{
1804 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001805 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001806 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001807
1808#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001809 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001810 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001811#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1812 if( driver != NULL )
1813 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001814 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001815 psa_key_slot_number_t slot_number =
1816 psa_key_slot_get_slot_number( slot ) ;
1817
Gilles Peskineb46bef22019-07-30 21:32:04 +02001818#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001819 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001820 sizeof( data.slot_number ),
1821 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001822#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001823 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001824 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001825 (uint8_t*) &data,
1826 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001827 }
1828 else
1829#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1830 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001831 /* Key material is saved in export representation in the slot, so
1832 * just pass the slot buffer for storage. */
1833 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001834 slot->key.data,
1835 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001836 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001837 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001838#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1839
Gilles Peskinecbaff462019-07-12 23:46:04 +02001840#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001841 /* Finish the transaction for a key creation. This does not
1842 * happen when registering an existing key. Detect this case
1843 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001844 * creation of a persistent key in a secure element requires a transaction,
1845 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001846 if( driver != NULL &&
1847 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001848 {
1849 status = psa_save_se_persistent_data( driver );
1850 if( status != PSA_SUCCESS )
1851 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001852 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001853 return( status );
1854 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001855 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001856 }
1857#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1858
Ronald Cron50972942020-11-14 11:28:25 +01001859 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001860 {
1861 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001862 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001863 if( status != PSA_SUCCESS )
1864 *key = MBEDTLS_SVC_KEY_ID_INIT;
1865 }
Ronald Cron50972942020-11-14 11:28:25 +01001866
Gilles Peskine4747d192019-04-17 15:05:45 +02001867 return( status );
1868}
1869
1870/** Abort the creation of a key.
1871 *
1872 * You may call this function after calling psa_start_key_creation(),
1873 * or after psa_finish_key_creation() fails. In other circumstances, this
1874 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001875 * See the documentation of psa_start_key_creation() for the intended use
1876 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001878 * \param[in,out] slot Pointer to the slot with key material.
1879 * \param[in] driver The secure element driver for the key,
1880 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001881 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001882static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001883 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001884{
Gilles Peskine011e4282019-06-26 18:34:38 +02001885 (void) driver;
1886
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 if( slot == NULL )
1888 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001889
1890#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001891 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001892 * element, and the failure happened later (when saving metadata
1893 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001894 * element.
1895 * https://github.com/ARMmbed/mbed-crypto/issues/217
1896 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001897
Gilles Peskined7729582019-08-05 15:55:54 +02001898 /* Abort the ongoing transaction if any (there may not be one if
1899 * the creation process failed before starting one, or if the
1900 * key creation is a registration of a key in a secure element).
1901 * Earlier functions must already have done what it takes to undo any
1902 * partial creation. All that's left is to update the transaction data
1903 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001904 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001905#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1906
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 psa_wipe_key_slot( slot );
1908}
1909
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001910/** Validate optional attributes during key creation.
1911 *
1912 * Some key attributes are optional during key creation. If they are
1913 * specified in the attributes structure, check that they are consistent
1914 * with the data in the slot.
1915 *
1916 * This function should be called near the end of key creation, after
1917 * the slot in memory is fully populated but before saving persistent data.
1918 */
1919static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001920 const psa_key_slot_t *slot,
1921 const psa_key_attributes_t *attributes )
1922{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001923 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001924 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001925 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001926 return( PSA_ERROR_INVALID_ARGUMENT );
1927 }
1928
1929 if( attributes->domain_parameters_size != 0 )
1930 {
John Durkop0e005192020-10-31 22:06:54 -07001931#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1932 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001933 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001935 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001936 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001937 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001938
Ronald Cron90857082020-11-25 14:58:33 +01001939 psa_status_t status = mbedtls_psa_rsa_load_representation(
1940 slot->attr.type,
1941 slot->key.data,
1942 slot->key.bytes,
1943 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001944 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001945 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001946
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001947 mbedtls_mpi_init( &actual );
1948 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001949 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001950 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001951 mbedtls_rsa_free( rsa );
1952 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001953 if( ret != 0 )
1954 goto rsa_exit;
1955 ret = mbedtls_mpi_read_binary( &required,
1956 attributes->domain_parameters,
1957 attributes->domain_parameters_size );
1958 if( ret != 0 )
1959 goto rsa_exit;
1960 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1961 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1962 rsa_exit:
1963 mbedtls_mpi_free( &actual );
1964 mbedtls_mpi_free( &required );
1965 if( ret != 0)
1966 return( mbedtls_to_psa_error( ret ) );
1967 }
1968 else
John Durkop9814fa22020-11-04 12:28:15 -08001969#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1970 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001971 {
1972 return( PSA_ERROR_INVALID_ARGUMENT );
1973 }
1974 }
1975
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001976 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001977 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001978 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979 return( PSA_ERROR_INVALID_ARGUMENT );
1980 }
1981
1982 return( PSA_SUCCESS );
1983}
1984
Gilles Peskine4747d192019-04-17 15:05:45 +02001985psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001986 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001987 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001988 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001989{
1990 psa_status_t status;
1991 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001992 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001993 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001994
Ronald Cron81709fc2020-11-14 12:10:32 +01001995 *key = MBEDTLS_SVC_KEY_ID_INIT;
1996
Gilles Peskine0f84d622019-09-12 19:03:13 +02001997 /* Reject zero-length symmetric keys (including raw data key objects).
1998 * This also rejects any key which might be encoded as an empty string,
1999 * which is never valid. */
2000 if( data_length == 0 )
2001 return( PSA_ERROR_INVALID_ARGUMENT );
2002
Gilles Peskinedf179142019-07-15 22:02:14 +02002003 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002004 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002005 if( status != PSA_SUCCESS )
2006 goto exit;
2007
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002008 /* In the case of a transparent key or an opaque key stored in local
2009 * storage (thus not in the case of generating a key in a secure element
2010 * or cryptoprocessor with storage), we have to allocate a buffer to
2011 * hold the generated key material. */
2012 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02002013 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002014 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002015 if( status != PSA_SUCCESS )
2016 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002017 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002018
2019 bits = slot->attr.bits;
2020 status = psa_driver_wrapper_import_key( attributes,
2021 data, data_length,
2022 slot->key.data,
2023 slot->key.bytes,
2024 &slot->key.bytes, &bits );
2025 if( status != PSA_SUCCESS )
2026 goto exit;
2027
2028 if( slot->attr.bits == 0 )
2029 slot->attr.bits = (psa_key_bits_t) bits;
2030 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002031 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002032 status = PSA_ERROR_INVALID_ARGUMENT;
2033 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002034 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002035
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002036 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002037 if( status != PSA_SUCCESS )
2038 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002039
Ronald Cron81709fc2020-11-14 12:10:32 +01002040 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002041exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002042 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002043 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002044
Gilles Peskine4747d192019-04-17 15:05:45 +02002045 return( status );
2046}
2047
Gilles Peskined7729582019-08-05 15:55:54 +02002048#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2049psa_status_t mbedtls_psa_register_se_key(
2050 const psa_key_attributes_t *attributes )
2051{
2052 psa_status_t status;
2053 psa_key_slot_t *slot = NULL;
2054 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002055 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002056
2057 /* Leaving attributes unspecified is not currently supported.
2058 * It could make sense to query the key type and size from the
2059 * secure element, but not all secure elements support this
2060 * and the driver HAL doesn't currently support it. */
2061 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2062 return( PSA_ERROR_NOT_SUPPORTED );
2063 if( psa_get_key_bits( attributes ) == 0 )
2064 return( PSA_ERROR_NOT_SUPPORTED );
2065
2066 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002067 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002068 if( status != PSA_SUCCESS )
2069 goto exit;
2070
Ronald Cron81709fc2020-11-14 12:10:32 +01002071 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002072
2073exit:
2074 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002075 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002076
Gilles Peskined7729582019-08-05 15:55:54 +02002077 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002078 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002079 return( status );
2080}
2081#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2082
Gilles Peskinef603c712019-01-19 13:40:11 +01002083static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002084 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002085{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002086 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002087 source->key.data,
2088 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002089 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002090 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002091
Steven Cooreman398aee52020-10-13 14:35:45 +02002092 target->attr.type = source->attr.type;
2093 target->attr.bits = source->attr.bits;
2094
2095 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002096}
2097
Ronald Croncf56a0a2020-08-04 09:51:30 +02002098psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002100 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002101{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002102 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002103 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002104 psa_key_slot_t *source_slot = NULL;
2105 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002106 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002107 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002108
Ronald Cron81709fc2020-11-14 12:10:32 +01002109 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2110
Ronald Cron5c522922020-11-14 16:35:34 +01002111 status = psa_get_and_lock_transparent_key_slot_with_policy(
2112 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002113 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002114 goto exit;
2115
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002116 status = psa_validate_optional_attributes( source_slot,
2117 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002118 if( status != PSA_SUCCESS )
2119 goto exit;
2120
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002121 status = psa_restrict_key_policy( source_slot->attr.type,
2122 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002123 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 if( status != PSA_SUCCESS )
2125 goto exit;
2126
Ronald Cron81709fc2020-11-14 12:10:32 +01002127 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2128 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002129 if( status != PSA_SUCCESS )
2130 goto exit;
2131
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002132#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2133 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002134 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002135 /* Copying to a secure element is not implemented yet. */
2136 status = PSA_ERROR_NOT_SUPPORTED;
2137 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002138 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002139#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002140
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002141 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002142 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002143 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002144
Ronald Cron81709fc2020-11-14 12:10:32 +01002145 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002146exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002147 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002148 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002149
Ronald Cron5c522922020-11-14 16:35:34 +01002150 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002151
Ronald Cron5c522922020-11-14 16:35:34 +01002152 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002153}
2154
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002155
2156
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002157/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002158/* Message digests */
2159/****************************************************************/
2160
John Durkop07cc04a2020-11-16 22:08:34 -08002161#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002162 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2163 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002164 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002165static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002166{
2167 switch( alg )
2168 {
John Durkopee4e6602020-11-27 08:48:46 -08002169#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002170 case PSA_ALG_MD2:
2171 return( &mbedtls_md2_info );
2172#endif
John Durkopee4e6602020-11-27 08:48:46 -08002173#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002174 case PSA_ALG_MD4:
2175 return( &mbedtls_md4_info );
2176#endif
John Durkopee4e6602020-11-27 08:48:46 -08002177#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002178 case PSA_ALG_MD5:
2179 return( &mbedtls_md5_info );
2180#endif
John Durkopee4e6602020-11-27 08:48:46 -08002181#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002182 case PSA_ALG_RIPEMD160:
2183 return( &mbedtls_ripemd160_info );
2184#endif
John Durkopee4e6602020-11-27 08:48:46 -08002185#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002186 case PSA_ALG_SHA_1:
2187 return( &mbedtls_sha1_info );
2188#endif
John Durkopee4e6602020-11-27 08:48:46 -08002189#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002190 case PSA_ALG_SHA_224:
2191 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002192#endif
2193#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002194 case PSA_ALG_SHA_256:
2195 return( &mbedtls_sha256_info );
2196#endif
John Durkopee4e6602020-11-27 08:48:46 -08002197#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002198 case PSA_ALG_SHA_384:
2199 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002200#endif
John Durkopee4e6602020-11-27 08:48:46 -08002201#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002202 case PSA_ALG_SHA_512:
2203 return( &mbedtls_sha512_info );
2204#endif
2205 default:
2206 return( NULL );
2207 }
2208}
John Durkop07cc04a2020-11-16 22:08:34 -08002209#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002210 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2211 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2212 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002213
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2215{
2216 switch( operation->alg )
2217 {
Gilles Peskine81736312018-06-26 15:04:31 +02002218 case 0:
2219 /* The object has (apparently) been initialized but it is not
2220 * in use. It's ok to call abort on such an object, and there's
2221 * nothing to do. */
2222 break;
John Durkopee4e6602020-11-27 08:48:46 -08002223#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_MD2:
2225 mbedtls_md2_free( &operation->ctx.md2 );
2226 break;
2227#endif
John Durkopee4e6602020-11-27 08:48:46 -08002228#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 case PSA_ALG_MD4:
2230 mbedtls_md4_free( &operation->ctx.md4 );
2231 break;
2232#endif
John Durkopee4e6602020-11-27 08:48:46 -08002233#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 case PSA_ALG_MD5:
2235 mbedtls_md5_free( &operation->ctx.md5 );
2236 break;
2237#endif
John Durkopee4e6602020-11-27 08:48:46 -08002238#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_RIPEMD160:
2240 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2241 break;
2242#endif
John Durkopee4e6602020-11-27 08:48:46 -08002243#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 case PSA_ALG_SHA_1:
2245 mbedtls_sha1_free( &operation->ctx.sha1 );
2246 break;
2247#endif
John Durkop6ca23272020-12-03 06:01:32 -08002248#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002250 mbedtls_sha256_free( &operation->ctx.sha256 );
2251 break;
2252#endif
2253#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002254 case PSA_ALG_SHA_256:
2255 mbedtls_sha256_free( &operation->ctx.sha256 );
2256 break;
2257#endif
John Durkop6ca23272020-12-03 06:01:32 -08002258#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002260 mbedtls_sha512_free( &operation->ctx.sha512 );
2261 break;
2262#endif
2263#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002264 case PSA_ALG_SHA_512:
2265 mbedtls_sha512_free( &operation->ctx.sha512 );
2266 break;
2267#endif
2268 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002269 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270 }
2271 operation->alg = 0;
2272 return( PSA_SUCCESS );
2273}
2274
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002275psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276 psa_algorithm_t alg )
2277{
Janos Follath24eed8d2019-11-22 13:21:35 +00002278 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002279
2280 /* A context must be freshly initialized before it can be set up. */
2281 if( operation->alg != 0 )
2282 {
2283 return( PSA_ERROR_BAD_STATE );
2284 }
2285
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286 switch( alg )
2287 {
John Durkopee4e6602020-11-27 08:48:46 -08002288#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289 case PSA_ALG_MD2:
2290 mbedtls_md2_init( &operation->ctx.md2 );
2291 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2292 break;
2293#endif
John Durkopee4e6602020-11-27 08:48:46 -08002294#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002295 case PSA_ALG_MD4:
2296 mbedtls_md4_init( &operation->ctx.md4 );
2297 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2298 break;
2299#endif
John Durkopee4e6602020-11-27 08:48:46 -08002300#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002301 case PSA_ALG_MD5:
2302 mbedtls_md5_init( &operation->ctx.md5 );
2303 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2304 break;
2305#endif
John Durkopee4e6602020-11-27 08:48:46 -08002306#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002307 case PSA_ALG_RIPEMD160:
2308 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2309 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2310 break;
2311#endif
John Durkopee4e6602020-11-27 08:48:46 -08002312#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002313 case PSA_ALG_SHA_1:
2314 mbedtls_sha1_init( &operation->ctx.sha1 );
2315 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2316 break;
2317#endif
John Durkopee4e6602020-11-27 08:48:46 -08002318#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002319 case PSA_ALG_SHA_224:
2320 mbedtls_sha256_init( &operation->ctx.sha256 );
2321 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2322 break;
John Durkopee4e6602020-11-27 08:48:46 -08002323#endif
2324#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325 case PSA_ALG_SHA_256:
2326 mbedtls_sha256_init( &operation->ctx.sha256 );
2327 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2328 break;
2329#endif
John Durkopee4e6602020-11-27 08:48:46 -08002330#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 case PSA_ALG_SHA_384:
2332 mbedtls_sha512_init( &operation->ctx.sha512 );
2333 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2334 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002335#endif
John Durkopee4e6602020-11-27 08:48:46 -08002336#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002337 case PSA_ALG_SHA_512:
2338 mbedtls_sha512_init( &operation->ctx.sha512 );
2339 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2340 break;
2341#endif
2342 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002343 return( PSA_ALG_IS_HASH( alg ) ?
2344 PSA_ERROR_NOT_SUPPORTED :
2345 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 }
2347 if( ret == 0 )
2348 operation->alg = alg;
2349 else
2350 psa_hash_abort( operation );
2351 return( mbedtls_to_psa_error( ret ) );
2352}
2353
2354psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2355 const uint8_t *input,
2356 size_t input_length )
2357{
Janos Follath24eed8d2019-11-22 13:21:35 +00002358 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002359
2360 /* Don't require hash implementations to behave correctly on a
2361 * zero-length input, which may have an invalid pointer. */
2362 if( input_length == 0 )
2363 return( PSA_SUCCESS );
2364
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 switch( operation->alg )
2366 {
John Durkopee4e6602020-11-27 08:48:46 -08002367#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002368 case PSA_ALG_MD2:
2369 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2370 input, input_length );
2371 break;
2372#endif
John Durkopee4e6602020-11-27 08:48:46 -08002373#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002374 case PSA_ALG_MD4:
2375 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2376 input, input_length );
2377 break;
2378#endif
John Durkopee4e6602020-11-27 08:48:46 -08002379#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002380 case PSA_ALG_MD5:
2381 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2382 input, input_length );
2383 break;
2384#endif
John Durkopee4e6602020-11-27 08:48:46 -08002385#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002386 case PSA_ALG_RIPEMD160:
2387 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2388 input, input_length );
2389 break;
2390#endif
John Durkopee4e6602020-11-27 08:48:46 -08002391#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002392 case PSA_ALG_SHA_1:
2393 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2394 input, input_length );
2395 break;
2396#endif
John Durkop6ca23272020-12-03 06:01:32 -08002397#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002398 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002399 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2400 input, input_length );
2401 break;
2402#endif
2403#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002404 case PSA_ALG_SHA_256:
2405 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2406 input, input_length );
2407 break;
2408#endif
John Durkop6ca23272020-12-03 06:01:32 -08002409#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002410 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002411 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2412 input, input_length );
2413 break;
2414#endif
2415#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002416 case PSA_ALG_SHA_512:
2417 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2418 input, input_length );
2419 break;
2420#endif
2421 default:
John Durkopee4e6602020-11-27 08:48:46 -08002422 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002423 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002425
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002426 if( ret != 0 )
2427 psa_hash_abort( operation );
2428 return( mbedtls_to_psa_error( ret ) );
2429}
2430
2431psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2432 uint8_t *hash,
2433 size_t hash_size,
2434 size_t *hash_length )
2435{
itayzafrir40835d42018-08-02 13:14:17 +03002436 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002437 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002438 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439
2440 /* Fill the output buffer with something that isn't a valid hash
2441 * (barring an attack on the hash and deliberately-crafted input),
2442 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002443 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002444 /* If hash_size is 0 then hash may be NULL and then the
2445 * call to memset would have undefined behavior. */
2446 if( hash_size != 0 )
2447 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448
2449 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002450 {
2451 status = PSA_ERROR_BUFFER_TOO_SMALL;
2452 goto exit;
2453 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002454
2455 switch( operation->alg )
2456 {
John Durkopee4e6602020-11-27 08:48:46 -08002457#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458 case PSA_ALG_MD2:
2459 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2460 break;
2461#endif
John Durkopee4e6602020-11-27 08:48:46 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463 case PSA_ALG_MD4:
2464 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2465 break;
2466#endif
John Durkopee4e6602020-11-27 08:48:46 -08002467#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002468 case PSA_ALG_MD5:
2469 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2470 break;
2471#endif
John Durkopee4e6602020-11-27 08:48:46 -08002472#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002473 case PSA_ALG_RIPEMD160:
2474 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2475 break;
2476#endif
John Durkopee4e6602020-11-27 08:48:46 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478 case PSA_ALG_SHA_1:
2479 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2480 break;
2481#endif
John Durkop6ca23272020-12-03 06:01:32 -08002482#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002484 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2485 break;
2486#endif
2487#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002488 case PSA_ALG_SHA_256:
2489 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2490 break;
2491#endif
John Durkop6ca23272020-12-03 06:01:32 -08002492#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002493 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002494 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2495 break;
2496#endif
2497#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002498 case PSA_ALG_SHA_512:
2499 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2500 break;
2501#endif
2502 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002503 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002504 }
itayzafrir40835d42018-08-02 13:14:17 +03002505 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002506
itayzafrir40835d42018-08-02 13:14:17 +03002507exit:
2508 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002509 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002510 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002511 return( psa_hash_abort( operation ) );
2512 }
2513 else
2514 {
2515 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002516 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002517 }
2518}
2519
Gilles Peskine2d277862018-06-18 15:41:12 +02002520psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2521 const uint8_t *hash,
2522 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002523{
2524 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2525 size_t actual_hash_length;
2526 psa_status_t status = psa_hash_finish( operation,
2527 actual_hash, sizeof( actual_hash ),
2528 &actual_hash_length );
2529 if( status != PSA_SUCCESS )
2530 return( status );
2531 if( actual_hash_length != hash_length )
2532 return( PSA_ERROR_INVALID_SIGNATURE );
2533 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2534 return( PSA_ERROR_INVALID_SIGNATURE );
2535 return( PSA_SUCCESS );
2536}
2537
Gilles Peskine0a749c82019-11-28 19:33:58 +01002538psa_status_t psa_hash_compute( psa_algorithm_t alg,
2539 const uint8_t *input, size_t input_length,
2540 uint8_t *hash, size_t hash_size,
2541 size_t *hash_length )
2542{
2543 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2544 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2545
2546 *hash_length = hash_size;
2547 status = psa_hash_setup( &operation, alg );
2548 if( status != PSA_SUCCESS )
2549 goto exit;
2550 status = psa_hash_update( &operation, input, input_length );
2551 if( status != PSA_SUCCESS )
2552 goto exit;
2553 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2554 if( status != PSA_SUCCESS )
2555 goto exit;
2556
2557exit:
2558 if( status == PSA_SUCCESS )
2559 status = psa_hash_abort( &operation );
2560 else
2561 psa_hash_abort( &operation );
2562 return( status );
2563}
2564
2565psa_status_t psa_hash_compare( psa_algorithm_t alg,
2566 const uint8_t *input, size_t input_length,
2567 const uint8_t *hash, size_t hash_length )
2568{
2569 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2570 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2571
2572 status = psa_hash_setup( &operation, alg );
2573 if( status != PSA_SUCCESS )
2574 goto exit;
2575 status = psa_hash_update( &operation, input, input_length );
2576 if( status != PSA_SUCCESS )
2577 goto exit;
2578 status = psa_hash_verify( &operation, hash, hash_length );
2579 if( status != PSA_SUCCESS )
2580 goto exit;
2581
2582exit:
2583 if( status == PSA_SUCCESS )
2584 status = psa_hash_abort( &operation );
2585 else
2586 psa_hash_abort( &operation );
2587 return( status );
2588}
2589
Gilles Peskineeb35d782019-01-22 17:56:16 +01002590psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2591 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002592{
2593 if( target_operation->alg != 0 )
2594 return( PSA_ERROR_BAD_STATE );
2595
2596 switch( source_operation->alg )
2597 {
2598 case 0:
2599 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002600#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002601 case PSA_ALG_MD2:
2602 mbedtls_md2_clone( &target_operation->ctx.md2,
2603 &source_operation->ctx.md2 );
2604 break;
2605#endif
John Durkopee4e6602020-11-27 08:48:46 -08002606#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002607 case PSA_ALG_MD4:
2608 mbedtls_md4_clone( &target_operation->ctx.md4,
2609 &source_operation->ctx.md4 );
2610 break;
2611#endif
John Durkopee4e6602020-11-27 08:48:46 -08002612#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002613 case PSA_ALG_MD5:
2614 mbedtls_md5_clone( &target_operation->ctx.md5,
2615 &source_operation->ctx.md5 );
2616 break;
2617#endif
John Durkopee4e6602020-11-27 08:48:46 -08002618#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002619 case PSA_ALG_RIPEMD160:
2620 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2621 &source_operation->ctx.ripemd160 );
2622 break;
2623#endif
John Durkopee4e6602020-11-27 08:48:46 -08002624#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002625 case PSA_ALG_SHA_1:
2626 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2627 &source_operation->ctx.sha1 );
2628 break;
2629#endif
John Durkop6ca23272020-12-03 06:01:32 -08002630#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002631 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002632 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2633 &source_operation->ctx.sha256 );
2634 break;
2635#endif
2636#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002637 case PSA_ALG_SHA_256:
2638 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2639 &source_operation->ctx.sha256 );
2640 break;
2641#endif
John Durkop6ca23272020-12-03 06:01:32 -08002642#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002643 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002644 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2645 &source_operation->ctx.sha512 );
2646 break;
2647#endif
2648#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002649 case PSA_ALG_SHA_512:
2650 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2651 &source_operation->ctx.sha512 );
2652 break;
2653#endif
2654 default:
2655 return( PSA_ERROR_NOT_SUPPORTED );
2656 }
2657
2658 target_operation->alg = source_operation->alg;
2659 return( PSA_SUCCESS );
2660}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002661
2662
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002663/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664/* MAC */
2665/****************************************************************/
2666
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002667static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668 psa_algorithm_t alg,
2669 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002670 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002671 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002674 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002676 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002677 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002678
Gilles Peskine8c9def32018-02-08 10:02:12 +01002679 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2680 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002681 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002683 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684 mode = MBEDTLS_MODE_STREAM;
2685 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 case PSA_ALG_CTR:
2687 mode = MBEDTLS_MODE_CTR;
2688 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002689 case PSA_ALG_CFB:
2690 mode = MBEDTLS_MODE_CFB;
2691 break;
2692 case PSA_ALG_OFB:
2693 mode = MBEDTLS_MODE_OFB;
2694 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002695 case PSA_ALG_ECB_NO_PADDING:
2696 mode = MBEDTLS_MODE_ECB;
2697 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002698 case PSA_ALG_CBC_NO_PADDING:
2699 mode = MBEDTLS_MODE_CBC;
2700 break;
2701 case PSA_ALG_CBC_PKCS7:
2702 mode = MBEDTLS_MODE_CBC;
2703 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002704 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002705 mode = MBEDTLS_MODE_CCM;
2706 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002707 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002708 mode = MBEDTLS_MODE_GCM;
2709 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002710 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002711 mode = MBEDTLS_MODE_CHACHAPOLY;
2712 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002713 default:
2714 return( NULL );
2715 }
2716 }
2717 else if( alg == PSA_ALG_CMAC )
2718 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002719 else
2720 return( NULL );
2721
2722 switch( key_type )
2723 {
2724 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002725 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002726 break;
2727 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002728 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2729 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002731 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002732 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002733 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002734 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2735 * but two-key Triple-DES is functionally three-key Triple-DES
2736 * with K1=K3, so that's how we present it to mbedtls. */
2737 if( key_bits == 128 )
2738 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739 break;
2740 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002741 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742 break;
2743 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002744 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002745 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002746 case PSA_KEY_TYPE_CHACHA20:
2747 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2748 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002749 default:
2750 return( NULL );
2751 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002752 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002753 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002754
Jaeden Amero23bbb752018-06-26 14:16:54 +01002755 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2756 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002757}
2758
John Durkop6ba40d12020-11-10 08:50:04 -08002759#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002760static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002761{
Gilles Peskine2d277862018-06-18 15:41:12 +02002762 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002763 {
2764 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002765 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002766 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002767 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002768 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002769 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002770 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002771 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002772 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002773 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002774 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002775 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002776 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002777 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002778 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002779 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002780 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002781 return( 128 );
2782 default:
2783 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002784 }
2785}
John Durkop07cc04a2020-11-16 22:08:34 -08002786#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002787
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002788/* Initialize the MAC operation structure. Once this function has been
2789 * called, psa_mac_abort can run and will do the right thing. */
2790static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2791 psa_algorithm_t alg )
2792{
2793 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2794
Steven Cooreman15472f82021-03-02 16:16:22 +01002795 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002796 operation->key_set = 0;
2797 operation->iv_set = 0;
2798 operation->iv_required = 0;
2799 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002800 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002801
2802#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002803 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002804 {
2805 operation->iv_required = 0;
2806 mbedtls_cipher_init( &operation->ctx.cmac );
2807 status = PSA_SUCCESS;
2808 }
2809 else
2810#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002811#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002812 if( PSA_ALG_IS_HMAC( operation->alg ) )
2813 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002814 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2815 operation->ctx.hmac.hash_ctx.alg = 0;
2816 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002817 }
2818 else
John Durkopd0321952020-10-29 21:37:36 -07002819#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002820 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002821 if( ! PSA_ALG_IS_MAC( alg ) )
2822 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002823 }
2824
2825 if( status != PSA_SUCCESS )
2826 memset( operation, 0, sizeof( *operation ) );
2827 return( status );
2828}
2829
John Durkop6ba40d12020-11-10 08:50:04 -08002830#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002831static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2832{
Gilles Peskine3f108122018-12-07 18:14:53 +01002833 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002834 return( psa_hash_abort( &hmac->hash_ctx ) );
2835}
John Durkop6ba40d12020-11-10 08:50:04 -08002836#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002837
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2839{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 /* The object has (apparently) been initialized but it is not
2843 * in use. It's ok to call abort on such an object, and there's
2844 * nothing to do. */
2845 return( PSA_SUCCESS );
2846 }
2847 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002848#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002849 if( operation->alg == PSA_ALG_CMAC )
2850 {
2851 mbedtls_cipher_free( &operation->ctx.cmac );
2852 }
2853 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002854#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002855#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002856 if( PSA_ALG_IS_HMAC( operation->alg ) )
2857 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002858 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002859 }
2860 else
John Durkopd0321952020-10-29 21:37:36 -07002861#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002862 {
2863 /* Sanity check (shouldn't happen: operation->alg should
2864 * always have been initialized to a valid value). */
2865 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866 }
Moran Peker41deec42018-04-04 15:43:05 +03002867
Gilles Peskine8c9def32018-02-08 10:02:12 +01002868 operation->alg = 0;
2869 operation->key_set = 0;
2870 operation->iv_set = 0;
2871 operation->iv_required = 0;
2872 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002873 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002874
Gilles Peskine8c9def32018-02-08 10:02:12 +01002875 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002876
2877bad_state:
2878 /* If abort is called on an uninitialized object, we can't trust
2879 * anything. Wipe the object in case it contains confidential data.
2880 * This may result in a memory leak if a pointer gets overwritten,
2881 * but it's too late to do anything about this. */
2882 memset( operation, 0, sizeof( *operation ) );
2883 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002884}
2885
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002886#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002887static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2888 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002889{
Janos Follath24eed8d2019-11-22 13:21:35 +00002890 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002891 const mbedtls_cipher_info_t *cipher_info =
2892 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2893 slot->attr.type, slot->attr.bits,
2894 NULL );
2895 if( cipher_info == NULL )
2896 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897
2898 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2899 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002900 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901
2902 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002903 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002904 slot->attr.bits );
2905exit:
2906 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002907}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002908#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002909
John Durkop6ba40d12020-11-10 08:50:04 -08002910#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002911static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2912 const uint8_t *key,
2913 size_t key_length,
2914 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002915{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002916 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002917 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002918 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002919 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002920 psa_status_t status;
2921
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002922 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2923 * overflow below. This should never trigger if the hash algorithm
2924 * is implemented correctly. */
2925 /* The size checks against the ipad and opad buffers cannot be written
2926 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2927 * because that triggers -Wlogical-op on GCC 7.3. */
2928 if( block_size > sizeof( ipad ) )
2929 return( PSA_ERROR_NOT_SUPPORTED );
2930 if( block_size > sizeof( hmac->opad ) )
2931 return( PSA_ERROR_NOT_SUPPORTED );
2932 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002933 return( PSA_ERROR_NOT_SUPPORTED );
2934
Gilles Peskined223b522018-06-11 18:12:58 +02002935 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002936 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002937 status = psa_hash_compute( hash_alg, key, key_length,
2938 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002939 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002940 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002941 }
Gilles Peskine96889972018-07-12 17:07:03 +02002942 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2943 * but it is permitted. It is common when HMAC is used in HKDF, for
2944 * example. Don't call `memcpy` in the 0-length because `key` could be
2945 * an invalid pointer which would make the behavior undefined. */
2946 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002947 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002948
Gilles Peskined223b522018-06-11 18:12:58 +02002949 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2950 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002951 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002952 ipad[i] ^= 0x36;
2953 memset( ipad + key_length, 0x36, block_size - key_length );
2954
2955 /* Copy the key material from ipad to opad, flipping the requisite bits,
2956 * and filling the rest of opad with the requisite constant. */
2957 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002958 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2959 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002960
Gilles Peskine01126fa2018-07-12 17:04:55 +02002961 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002962 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002963 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002964
Gilles Peskine01126fa2018-07-12 17:04:55 +02002965 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002966
2967cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002968 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002969
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002970 return( status );
2971}
John Durkop6ba40d12020-11-10 08:50:04 -08002972#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002973
Gilles Peskine89167cb2018-07-08 20:12:23 +02002974static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002975 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002976 psa_algorithm_t alg,
2977 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002978{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002979 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002980 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002981 psa_key_slot_t *slot;
Steven Cooreman15472f82021-03-02 16:16:22 +01002982 size_t output_length = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002983 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002984 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002985
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002986 /* A context must be freshly initialized before it can be set up. */
2987 if( operation->alg != 0 )
2988 {
2989 return( PSA_ERROR_BAD_STATE );
2990 }
2991
Steven Cooreman15472f82021-03-02 16:16:22 +01002992 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002993 if( status != PSA_SUCCESS )
2994 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002995 if( is_sign )
2996 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997
Ronald Cron5c522922020-11-14 16:35:34 +01002998 status = psa_get_and_lock_transparent_key_slot_with_policy(
2999 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003000 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003001 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01003002
3003 status = psa_get_mac_output_length( alg, slot->attr.type,
3004 &output_length );
3005 if( status != PSA_SUCCESS )
3006 goto exit;
3007
3008 operation->mac_size = (uint8_t) output_length;
3009
3010 if( operation->mac_size < 4 )
3011 {
3012 /* A very short MAC is too short for security since it can be
3013 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3014 * so we make this our minimum, even though 32 bits is still
3015 * too small for security. */
3016 status = PSA_ERROR_NOT_SUPPORTED;
3017 goto exit;
3018 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003019
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01003021 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 {
Steven Cooreman15472f82021-03-02 16:16:22 +01003023 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003024 }
3025 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003026#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003027#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003028 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003030 /* Sanity check. This shouldn't fail on a valid configuration. */
3031 if( operation->mac_size == 0 ||
3032 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3033 {
3034 status = PSA_ERROR_NOT_SUPPORTED;
3035 goto exit;
3036 }
3037
Gilles Peskine8e338702019-07-30 20:06:31 +02003038 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003039 {
3040 status = PSA_ERROR_INVALID_ARGUMENT;
3041 goto exit;
3042 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003043
Gilles Peskine01126fa2018-07-12 17:04:55 +02003044 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003045 slot->key.data,
3046 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003047 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003048 }
3049 else
John Durkopd0321952020-10-29 21:37:36 -07003050#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003051 {
3052 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003054
Gilles Peskinefbfac682018-07-08 20:51:54 +02003055exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003056 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003057 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003058 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003059 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003060 else
3061 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003062 operation->key_set = 1;
3063 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003064
Ronald Cron5c522922020-11-14 16:35:34 +01003065 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003066
Ronald Cron5c522922020-11-14 16:35:34 +01003067 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003068}
3069
Gilles Peskine89167cb2018-07-08 20:12:23 +02003070psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003071 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003072 psa_algorithm_t alg )
3073{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003074 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003075}
3076
3077psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003078 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003079 psa_algorithm_t alg )
3080{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003081 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003082}
3083
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3085 const uint8_t *input,
3086 size_t input_length )
3087{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003088 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003090 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003092 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003093 operation->has_input = 1;
3094
Gilles Peskine8c9def32018-02-08 10:02:12 +01003095#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003096 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003097 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003098 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3099 input, input_length );
3100 status = mbedtls_to_psa_error( ret );
3101 }
3102 else
3103#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003104#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003105 if( PSA_ALG_IS_HMAC( operation->alg ) )
3106 {
3107 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3108 input_length );
3109 }
3110 else
John Durkopd0321952020-10-29 21:37:36 -07003111#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003112 {
3113 /* This shouldn't happen if `operation` was initialized by
3114 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003115 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003116 }
3117
Gilles Peskinefbfac682018-07-08 20:51:54 +02003118 if( status != PSA_SUCCESS )
3119 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003120 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003121}
3122
John Durkop6ba40d12020-11-10 08:50:04 -08003123#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003124static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3125 uint8_t *mac,
3126 size_t mac_size )
3127{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003128 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003129 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3130 size_t hash_size = 0;
3131 size_t block_size = psa_get_hash_block_size( hash_alg );
3132 psa_status_t status;
3133
Gilles Peskine01126fa2018-07-12 17:04:55 +02003134 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3135 if( status != PSA_SUCCESS )
3136 return( status );
3137 /* From here on, tmp needs to be wiped. */
3138
3139 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3140 if( status != PSA_SUCCESS )
3141 goto exit;
3142
3143 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3144 if( status != PSA_SUCCESS )
3145 goto exit;
3146
3147 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3148 if( status != PSA_SUCCESS )
3149 goto exit;
3150
Gilles Peskined911eb72018-08-14 15:18:45 +02003151 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3152 if( status != PSA_SUCCESS )
3153 goto exit;
3154
3155 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003156
3157exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003158 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003159 return( status );
3160}
John Durkop6ba40d12020-11-10 08:50:04 -08003161#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003162
mohammad16036df908f2018-04-02 08:34:15 -07003163static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003164 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003165 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003167 if( ! operation->key_set )
3168 return( PSA_ERROR_BAD_STATE );
3169 if( operation->iv_required && ! operation->iv_set )
3170 return( PSA_ERROR_BAD_STATE );
3171
Gilles Peskine8c9def32018-02-08 10:02:12 +01003172 if( mac_size < operation->mac_size )
3173 return( PSA_ERROR_BUFFER_TOO_SMALL );
3174
Gilles Peskine8c9def32018-02-08 10:02:12 +01003175#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003176 if( operation->alg == PSA_ALG_CMAC )
3177 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003178 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003179 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3180 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003181 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003182 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003184 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003185 else
3186#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003187#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003188 if( PSA_ALG_IS_HMAC( operation->alg ) )
3189 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003190 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003191 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003192 }
3193 else
John Durkopd0321952020-10-29 21:37:36 -07003194#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003195 {
3196 /* This shouldn't happen if `operation` was initialized by
3197 * a setup function. */
3198 return( PSA_ERROR_BAD_STATE );
3199 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003200}
3201
Gilles Peskineacd4be32018-07-08 19:56:25 +02003202psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3203 uint8_t *mac,
3204 size_t mac_size,
3205 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003206{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003207 psa_status_t status;
3208
Jaeden Amero252ef282019-02-15 14:05:35 +00003209 if( operation->alg == 0 )
3210 {
3211 return( PSA_ERROR_BAD_STATE );
3212 }
3213
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003214 /* Fill the output buffer with something that isn't a valid mac
3215 * (barring an attack on the mac and deliberately-crafted input),
3216 * in case the caller doesn't check the return status properly. */
3217 *mac_length = mac_size;
3218 /* If mac_size is 0 then mac may be NULL and then the
3219 * call to memset would have undefined behavior. */
3220 if( mac_size != 0 )
3221 memset( mac, '!', mac_size );
3222
Gilles Peskine89167cb2018-07-08 20:12:23 +02003223 if( ! operation->is_sign )
3224 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003225 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003226 }
mohammad16036df908f2018-04-02 08:34:15 -07003227
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003228 status = psa_mac_finish_internal( operation, mac, mac_size );
3229
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003230 if( status == PSA_SUCCESS )
3231 {
3232 status = psa_mac_abort( operation );
3233 if( status == PSA_SUCCESS )
3234 *mac_length = operation->mac_size;
3235 else
3236 memset( mac, '!', mac_size );
3237 }
3238 else
3239 psa_mac_abort( operation );
3240 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003241}
3242
Gilles Peskineacd4be32018-07-08 19:56:25 +02003243psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3244 const uint8_t *mac,
3245 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003246{
Gilles Peskine828ed142018-06-18 23:25:51 +02003247 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003248 psa_status_t status;
3249
Jaeden Amero252ef282019-02-15 14:05:35 +00003250 if( operation->alg == 0 )
3251 {
3252 return( PSA_ERROR_BAD_STATE );
3253 }
3254
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255 if( operation->is_sign )
3256 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003257 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003258 }
3259 if( operation->mac_size != mac_length )
3260 {
3261 status = PSA_ERROR_INVALID_SIGNATURE;
3262 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003263 }
mohammad16036df908f2018-04-02 08:34:15 -07003264
3265 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003266 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003267 if( status != PSA_SUCCESS )
3268 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003269
3270 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3271 status = PSA_ERROR_INVALID_SIGNATURE;
3272
3273cleanup:
3274 if( status == PSA_SUCCESS )
3275 status = psa_mac_abort( operation );
3276 else
3277 psa_mac_abort( operation );
3278
Gilles Peskine3f108122018-12-07 18:14:53 +01003279 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003280
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003281 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003282}
3283
3284
Gilles Peskine20035e32018-02-03 22:44:14 +01003285
Gilles Peskine20035e32018-02-03 22:44:14 +01003286/****************************************************************/
3287/* Asymmetric cryptography */
3288/****************************************************************/
3289
John Durkop6ba40d12020-11-10 08:50:04 -08003290#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3291 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003292/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003293 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003294static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3295 size_t hash_length,
3296 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003297{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003298 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003299 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003300 *md_alg = mbedtls_md_get_type( md_info );
3301
3302 /* The Mbed TLS RSA module uses an unsigned int for hash length
3303 * parameters. Validate that it fits so that we don't risk an
3304 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003305#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003306 if( hash_length > UINT_MAX )
3307 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003308#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003309
John Durkop0e005192020-10-31 22:06:54 -07003310#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003311 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3312 * must be correct. */
3313 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3314 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003315 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003316 if( md_info == NULL )
3317 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003318 if( mbedtls_md_get_size( md_info ) != hash_length )
3319 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003320 }
John Durkop0e005192020-10-31 22:06:54 -07003321#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003322
John Durkop0e005192020-10-31 22:06:54 -07003323#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003324 /* PSS requires a hash internally. */
3325 if( PSA_ALG_IS_RSA_PSS( alg ) )
3326 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003327 if( md_info == NULL )
3328 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003329 }
John Durkop0e005192020-10-31 22:06:54 -07003330#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003331
Gilles Peskine61b91d42018-06-08 16:09:36 +02003332 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003333}
3334
Gilles Peskine2b450e32018-06-27 15:42:46 +02003335static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3336 psa_algorithm_t alg,
3337 const uint8_t *hash,
3338 size_t hash_length,
3339 uint8_t *signature,
3340 size_t signature_size,
3341 size_t *signature_length )
3342{
3343 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003345 mbedtls_md_type_t md_alg;
3346
3347 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3348 if( status != PSA_SUCCESS )
3349 return( status );
3350
Gilles Peskine630a18a2018-06-29 17:49:35 +02003351 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003352 return( PSA_ERROR_BUFFER_TOO_SMALL );
3353
John Durkop0e005192020-10-31 22:06:54 -07003354#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003355 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3356 {
3357 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3358 MBEDTLS_MD_NONE );
3359 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003360 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003361 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003362 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003363 md_alg,
3364 (unsigned int) hash_length,
3365 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003366 signature );
3367 }
3368 else
John Durkop0e005192020-10-31 22:06:54 -07003369#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3370#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003371 if( PSA_ALG_IS_RSA_PSS( alg ) )
3372 {
3373 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3374 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003375 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003376 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003377 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003378 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003379 (unsigned int) hash_length,
3380 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003381 signature );
3382 }
3383 else
John Durkop0e005192020-10-31 22:06:54 -07003384#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003385 {
3386 return( PSA_ERROR_INVALID_ARGUMENT );
3387 }
3388
3389 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003390 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003391 return( mbedtls_to_psa_error( ret ) );
3392}
3393
3394static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3395 psa_algorithm_t alg,
3396 const uint8_t *hash,
3397 size_t hash_length,
3398 const uint8_t *signature,
3399 size_t signature_length )
3400{
3401 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003402 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003403 mbedtls_md_type_t md_alg;
3404
3405 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3406 if( status != PSA_SUCCESS )
3407 return( status );
3408
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003409 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3410 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003411
John Durkop0e005192020-10-31 22:06:54 -07003412#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003413 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3414 {
3415 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3416 MBEDTLS_MD_NONE );
3417 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003418 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003419 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003420 MBEDTLS_RSA_PUBLIC,
3421 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003422 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003423 hash,
3424 signature );
3425 }
3426 else
John Durkop0e005192020-10-31 22:06:54 -07003427#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3428#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003429 if( PSA_ALG_IS_RSA_PSS( alg ) )
3430 {
3431 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3432 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003433 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003434 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003436 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003437 (unsigned int) hash_length,
3438 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003439 signature );
3440 }
3441 else
John Durkop0e005192020-10-31 22:06:54 -07003442#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003443 {
3444 return( PSA_ERROR_INVALID_ARGUMENT );
3445 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003446
3447 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3448 * the rest of the signature is invalid". This has little use in
3449 * practice and PSA doesn't report this distinction. */
3450 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3451 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003452 return( mbedtls_to_psa_error( ret ) );
3453}
John Durkop6ba40d12020-11-10 08:50:04 -08003454#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3455 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003456
John Durkop6ba40d12020-11-10 08:50:04 -08003457#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3458 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003459/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3460 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3461 * (even though these functions don't modify it). */
3462static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3463 psa_algorithm_t alg,
3464 const uint8_t *hash,
3465 size_t hash_length,
3466 uint8_t *signature,
3467 size_t signature_size,
3468 size_t *signature_length )
3469{
Janos Follath24eed8d2019-11-22 13:21:35 +00003470 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003471 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003472 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003473 mbedtls_mpi_init( &r );
3474 mbedtls_mpi_init( &s );
3475
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003476 if( signature_size < 2 * curve_bytes )
3477 {
3478 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3479 goto cleanup;
3480 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003481
John Durkop0ea39e02020-10-13 19:58:20 -07003482#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003483 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3484 {
3485 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3486 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3487 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003488 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3489 &ecp->d, hash,
3490 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003491 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003492 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003493 }
3494 else
John Durkop0ea39e02020-10-13 19:58:20 -07003495#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003496 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003497 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003498 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3499 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003500 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003501 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003502 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003503
3504 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3505 signature,
3506 curve_bytes ) );
3507 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3508 signature + curve_bytes,
3509 curve_bytes ) );
3510
3511cleanup:
3512 mbedtls_mpi_free( &r );
3513 mbedtls_mpi_free( &s );
3514 if( ret == 0 )
3515 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003516 return( mbedtls_to_psa_error( ret ) );
3517}
3518
3519static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3520 const uint8_t *hash,
3521 size_t hash_length,
3522 const uint8_t *signature,
3523 size_t signature_length )
3524{
Janos Follath24eed8d2019-11-22 13:21:35 +00003525 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003526 mbedtls_mpi r, s;
3527 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3528 mbedtls_mpi_init( &r );
3529 mbedtls_mpi_init( &s );
3530
3531 if( signature_length != 2 * curve_bytes )
3532 return( PSA_ERROR_INVALID_SIGNATURE );
3533
3534 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3535 signature,
3536 curve_bytes ) );
3537 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3538 signature + curve_bytes,
3539 curve_bytes ) );
3540
Steven Cooremanacda8342020-07-24 23:09:52 +02003541 /* Check whether the public part is loaded. If not, load it. */
3542 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3543 {
3544 MBEDTLS_MPI_CHK(
3545 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003546 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003547 }
3548
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003549 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3550 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003551
3552cleanup:
3553 mbedtls_mpi_free( &r );
3554 mbedtls_mpi_free( &s );
3555 return( mbedtls_to_psa_error( ret ) );
3556}
John Durkop6ba40d12020-11-10 08:50:04 -08003557#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3558 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003559
Ronald Croncf56a0a2020-08-04 09:51:30 +02003560psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003561 psa_algorithm_t alg,
3562 const uint8_t *hash,
3563 size_t hash_length,
3564 uint8_t *signature,
3565 size_t signature_size,
3566 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003567{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003568 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003569 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003570 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003571
3572 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003573 /* Immediately reject a zero-length signature buffer. This guarantees
3574 * that signature must be a valid pointer. (On the other hand, the hash
3575 * buffer can in principle be empty since it doesn't actually have
3576 * to be a hash.) */
3577 if( signature_size == 0 )
3578 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003579
Ronald Cron5c522922020-11-14 16:35:34 +01003580 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3581 PSA_KEY_USAGE_SIGN_HASH,
3582 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003583 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003584 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003585 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003586 {
3587 status = PSA_ERROR_INVALID_ARGUMENT;
3588 goto exit;
3589 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003590
Steven Cooremancd84cb42020-07-16 20:28:36 +02003591 /* Try any of the available accelerators first */
3592 status = psa_driver_wrapper_sign_hash( slot,
3593 alg,
3594 hash,
3595 hash_length,
3596 signature,
3597 signature_size,
3598 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003599 if( status != PSA_ERROR_NOT_SUPPORTED ||
3600 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003601 goto exit;
3602
Steven Cooreman7a250572020-07-17 16:43:05 +02003603 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003604#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3605 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003606 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003607 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003608 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003609
Ronald Cron90857082020-11-25 14:58:33 +01003610 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3611 slot->key.data,
3612 slot->key.bytes,
3613 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003614 if( status != PSA_SUCCESS )
3615 goto exit;
3616
Steven Cooremana2371e52020-07-28 14:30:39 +02003617 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003618 alg,
3619 hash, hash_length,
3620 signature, signature_size,
3621 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003622
Steven Cooremana2371e52020-07-28 14:30:39 +02003623 mbedtls_rsa_free( rsa );
3624 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003625 }
3626 else
John Durkop6ba40d12020-11-10 08:50:04 -08003627#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3628 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003629 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003630 {
John Durkop9814fa22020-11-04 12:28:15 -08003631#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3632 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003633 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003634#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003635 PSA_ALG_IS_ECDSA( alg )
3636#else
3637 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3638#endif
3639 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003640 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003641 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003642 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003643 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003644 slot->key.data,
3645 slot->key.bytes,
3646 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003647 if( status != PSA_SUCCESS )
3648 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003649 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003650 alg,
3651 hash, hash_length,
3652 signature, signature_size,
3653 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003654 mbedtls_ecp_keypair_free( ecp );
3655 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003656 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003657 else
John Durkop9814fa22020-11-04 12:28:15 -08003658#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3659 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003660 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003661 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003662 }
itayzafrir5c753392018-05-08 11:18:38 +03003663 }
3664 else
itayzafrir5c753392018-05-08 11:18:38 +03003665 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003666 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003667 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003668
3669exit:
3670 /* Fill the unused part of the output buffer (the whole buffer on error,
3671 * the trailing part on success) with something that isn't a valid mac
3672 * (barring an attack on the mac and deliberately-crafted input),
3673 * in case the caller doesn't check the return status properly. */
3674 if( status == PSA_SUCCESS )
3675 memset( signature + *signature_length, '!',
3676 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003677 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003678 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003679 /* If signature_size is 0 then we have nothing to do. We must not call
3680 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003681
Ronald Cron5c522922020-11-14 16:35:34 +01003682 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003683
Ronald Cron5c522922020-11-14 16:35:34 +01003684 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003685}
3686
Ronald Croncf56a0a2020-08-04 09:51:30 +02003687psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003688 psa_algorithm_t alg,
3689 const uint8_t *hash,
3690 size_t hash_length,
3691 const uint8_t *signature,
3692 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003693{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003694 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003695 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003696 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003697
Ronald Cron5c522922020-11-14 16:35:34 +01003698 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3699 PSA_KEY_USAGE_VERIFY_HASH,
3700 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003701 if( status != PSA_SUCCESS )
3702 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003703
Steven Cooreman55ae2172020-07-17 19:46:15 +02003704 /* Try any of the available accelerators first */
3705 status = psa_driver_wrapper_verify_hash( slot,
3706 alg,
3707 hash,
3708 hash_length,
3709 signature,
3710 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003711 if( status != PSA_ERROR_NOT_SUPPORTED ||
3712 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003713 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003714
John Durkop6ba40d12020-11-10 08:50:04 -08003715#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3716 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003717 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003719 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003720
Ronald Cron90857082020-11-25 14:58:33 +01003721 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3722 slot->key.data,
3723 slot->key.bytes,
3724 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003725 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003726 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003727
Steven Cooremana2371e52020-07-28 14:30:39 +02003728 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003729 alg,
3730 hash, hash_length,
3731 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003732 mbedtls_rsa_free( rsa );
3733 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003734 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003735 }
3736 else
John Durkop6ba40d12020-11-10 08:50:04 -08003737#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3738 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003739 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003740 {
John Durkop9814fa22020-11-04 12:28:15 -08003741#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3742 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003743 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003744 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003745 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003746 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003747 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003748 slot->key.data,
3749 slot->key.bytes,
3750 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003751 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003752 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003753 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003754 hash, hash_length,
3755 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003756 mbedtls_ecp_keypair_free( ecp );
3757 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003758 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003759 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003760 else
John Durkop9814fa22020-11-04 12:28:15 -08003761#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3762 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003763 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003764 status = PSA_ERROR_INVALID_ARGUMENT;
3765 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003766 }
itayzafrir5c753392018-05-08 11:18:38 +03003767 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003768 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003769 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003770 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003771 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003772
3773exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003774 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003775
Ronald Cron5c522922020-11-14 16:35:34 +01003776 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003777}
3778
John Durkop0e005192020-10-31 22:06:54 -07003779#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003780static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3781 mbedtls_rsa_context *rsa )
3782{
3783 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3784 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3785 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3786 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3787}
John Durkop0e005192020-10-31 22:06:54 -07003788#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003789
Ronald Croncf56a0a2020-08-04 09:51:30 +02003790psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003791 psa_algorithm_t alg,
3792 const uint8_t *input,
3793 size_t input_length,
3794 const uint8_t *salt,
3795 size_t salt_length,
3796 uint8_t *output,
3797 size_t output_size,
3798 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003799{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003800 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003801 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003802 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003803
Darryl Green5cc689a2018-07-24 15:34:10 +01003804 (void) input;
3805 (void) input_length;
3806 (void) salt;
3807 (void) output;
3808 (void) output_size;
3809
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003810 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003811
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003812 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3813 return( PSA_ERROR_INVALID_ARGUMENT );
3814
Ronald Cron5c522922020-11-14 16:35:34 +01003815 status = psa_get_and_lock_transparent_key_slot_with_policy(
3816 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003817 if( status != PSA_SUCCESS )
3818 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003819 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3820 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003821 {
3822 status = PSA_ERROR_INVALID_ARGUMENT;
3823 goto exit;
3824 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825
John Durkop6ba40d12020-11-10 08:50:04 -08003826#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3827 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003828 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003829 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003830 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003831 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3832 slot->key.data,
3833 slot->key.bytes,
3834 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003835 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003836 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003837
3838 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003839 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003840 status = PSA_ERROR_BUFFER_TOO_SMALL;
3841 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003842 }
John Durkop0e005192020-10-31 22:06:54 -07003843#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003844 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003845 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003846 status = mbedtls_to_psa_error(
3847 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003848 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003849 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003850 MBEDTLS_RSA_PUBLIC,
3851 input_length,
3852 input,
3853 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003854 }
3855 else
John Durkop0e005192020-10-31 22:06:54 -07003856#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3857#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003858 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003859 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003860 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003861 status = mbedtls_to_psa_error(
3862 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003863 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003864 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003865 MBEDTLS_RSA_PUBLIC,
3866 salt, salt_length,
3867 input_length,
3868 input,
3869 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003870 }
3871 else
John Durkop0e005192020-10-31 22:06:54 -07003872#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003873 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003874 status = PSA_ERROR_INVALID_ARGUMENT;
3875 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003876 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003877rsa_exit:
3878 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003879 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003880
Steven Cooremana2371e52020-07-28 14:30:39 +02003881 mbedtls_rsa_free( rsa );
3882 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003884 else
John Durkop9814fa22020-11-04 12:28:15 -08003885#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003886 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003887 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003888 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003889 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003890
3891exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003892 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003893
Ronald Cron5c522922020-11-14 16:35:34 +01003894 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003895}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003896
Ronald Croncf56a0a2020-08-04 09:51:30 +02003897psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003898 psa_algorithm_t alg,
3899 const uint8_t *input,
3900 size_t input_length,
3901 const uint8_t *salt,
3902 size_t salt_length,
3903 uint8_t *output,
3904 size_t output_size,
3905 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003906{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003907 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003908 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003909 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003910
Darryl Green5cc689a2018-07-24 15:34:10 +01003911 (void) input;
3912 (void) input_length;
3913 (void) salt;
3914 (void) output;
3915 (void) output_size;
3916
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003917 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003918
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003919 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3920 return( PSA_ERROR_INVALID_ARGUMENT );
3921
Ronald Cron5c522922020-11-14 16:35:34 +01003922 status = psa_get_and_lock_transparent_key_slot_with_policy(
3923 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003924 if( status != PSA_SUCCESS )
3925 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003926 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003927 {
3928 status = PSA_ERROR_INVALID_ARGUMENT;
3929 goto exit;
3930 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931
John Durkop6ba40d12020-11-10 08:50:04 -08003932#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3933 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003934 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003935 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003936 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003937 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3938 slot->key.data,
3939 slot->key.bytes,
3940 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003941 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003942 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003943
Steven Cooremana2371e52020-07-28 14:30:39 +02003944 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003945 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003946 status = PSA_ERROR_INVALID_ARGUMENT;
3947 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003948 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003949
John Durkop0e005192020-10-31 22:06:54 -07003950#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003951 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003952 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003953 status = mbedtls_to_psa_error(
3954 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003955 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003956 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003957 MBEDTLS_RSA_PRIVATE,
3958 output_length,
3959 input,
3960 output,
3961 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003962 }
3963 else
John Durkop0e005192020-10-31 22:06:54 -07003964#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3965#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003966 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003967 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003968 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003969 status = mbedtls_to_psa_error(
3970 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003971 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003972 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003973 MBEDTLS_RSA_PRIVATE,
3974 salt, salt_length,
3975 output_length,
3976 input,
3977 output,
3978 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003979 }
3980 else
John Durkop0e005192020-10-31 22:06:54 -07003981#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003982 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003983 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003984 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003985
Steven Cooreman4fed4552020-08-03 14:46:03 +02003986rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003987 mbedtls_rsa_free( rsa );
3988 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003990 else
John Durkop6ba40d12020-11-10 08:50:04 -08003991#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3992 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003993 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003994 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003995 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003996
3997exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003998 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003999
Ronald Cron5c522922020-11-14 16:35:34 +01004000 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004001}
Gilles Peskine20035e32018-02-03 22:44:14 +01004002
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004003
4004
mohammad1603503973b2018-03-12 15:59:30 +02004005/****************************************************************/
4006/* Symmetric cryptography */
4007/****************************************************************/
4008
Gilles Peskinee553c652018-06-04 16:22:46 +02004009static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004010 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004011 psa_algorithm_t alg,
4012 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004013{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004014 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004015 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004016 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004017 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004018 size_t key_bits;
4019 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004020 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4021 PSA_KEY_USAGE_ENCRYPT :
4022 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004023
Steven Cooremand3feccd2020-09-01 15:56:14 +02004024 /* A context must be freshly initialized before it can be set up. */
4025 if( operation->alg != 0 )
4026 return( PSA_ERROR_BAD_STATE );
4027
Steven Cooremana07b9972020-09-10 14:54:14 +02004028 /* The requested algorithm must be one that can be processed by cipher. */
4029 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004030 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004031
Steven Cooremanef8575e2020-09-11 11:44:50 +02004032 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004033 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004034 if( status != PSA_SUCCESS )
4035 goto exit;
4036
4037 /* Initialize the operation struct members, except for alg. The alg member
4038 * is used to indicate to psa_cipher_abort that there are resources to free,
4039 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004040 operation->key_set = 0;
4041 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004042 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004043 operation->iv_size = 0;
4044 operation->block_size = 0;
4045 if( alg == PSA_ALG_ECB_NO_PADDING )
4046 operation->iv_required = 0;
4047 else
4048 operation->iv_required = 1;
4049
Steven Cooremana07b9972020-09-10 14:54:14 +02004050 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004051 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004052 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004053 slot,
4054 alg );
4055 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004056 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004057 slot,
4058 alg );
4059
Steven Cooremanef8575e2020-09-11 11:44:50 +02004060 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004061 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004062 /* Once the driver context is initialised, it needs to be freed using
4063 * psa_cipher_abort. Indicate this through setting alg. */
4064 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004065 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004066
Steven Cooremand3feccd2020-09-01 15:56:14 +02004067 if( status != PSA_ERROR_NOT_SUPPORTED ||
4068 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4069 goto exit;
4070
Steven Cooremana07b9972020-09-10 14:54:14 +02004071 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004072 * available for the given algorithm & key. */
4073 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004074
Steven Cooremana07b9972020-09-10 14:54:14 +02004075 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004076 * psa_cipher_abort. Indicate there is something to be freed through setting
4077 * alg, and indicate the operation is being done using mbedtls crypto through
4078 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004079 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004080 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004081
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004082 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004083 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004084 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004085 {
4086 status = PSA_ERROR_NOT_SUPPORTED;
4087 goto exit;
4088 }
mohammad1603503973b2018-03-12 15:59:30 +02004089
mohammad1603503973b2018-03-12 15:59:30 +02004090 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004091 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004092 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004093
David Brown12ca5032021-02-11 11:02:00 -07004094#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004095 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004096 {
4097 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004098 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004099 memcpy( keys, slot->key.data, 16 );
4100 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004101 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4102 keys,
4103 192, cipher_operation );
4104 }
4105 else
4106#endif
4107 {
4108 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004109 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004110 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004111 }
Moran Peker41deec42018-04-04 15:43:05 +03004112 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004113 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004114
David Brown63ca2602021-01-26 11:51:12 -07004115#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4116 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004117 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004118 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004119 case PSA_ALG_CBC_NO_PADDING:
4120 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4121 MBEDTLS_PADDING_NONE );
4122 break;
4123 case PSA_ALG_CBC_PKCS7:
4124 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4125 MBEDTLS_PADDING_PKCS7 );
4126 break;
4127 default:
4128 /* The algorithm doesn't involve padding. */
4129 ret = 0;
4130 break;
4131 }
4132 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004133 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004134#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004135
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004136 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004137 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004138 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4139 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004140 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004141 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004142 }
David Brown1bfe4d72021-02-16 12:54:35 -07004143#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004144 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004145 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004146 operation->iv_size = 12;
4147#endif
mohammad1603503973b2018-03-12 15:59:30 +02004148
Steven Cooreman7df02922020-09-09 15:28:49 +02004149 status = PSA_SUCCESS;
4150
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004151exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004152 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004153 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004154 if( status == PSA_SUCCESS )
4155 {
4156 /* Update operation flags for both driver and software implementations */
4157 operation->key_set = 1;
4158 }
4159 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004160 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004161
Ronald Cron5c522922020-11-14 16:35:34 +01004162 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004163
Ronald Cron5c522922020-11-14 16:35:34 +01004164 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004165}
4166
Gilles Peskinefe119512018-07-08 21:39:34 +02004167psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004168 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004169 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004170{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004171 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004172}
4173
Gilles Peskinefe119512018-07-08 21:39:34 +02004174psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004175 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004176 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004177{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004178 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004179}
4180
Gilles Peskinefe119512018-07-08 21:39:34 +02004181psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004182 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004183 size_t iv_size,
4184 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004185{
itayzafrir534bd7c2018-08-02 13:56:32 +03004186 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004187 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004188 if( operation->iv_set || ! operation->iv_required )
4189 {
4190 return( PSA_ERROR_BAD_STATE );
4191 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004192
Steven Cooremanef8575e2020-09-11 11:44:50 +02004193 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004194 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004195 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004196 iv,
4197 iv_size,
4198 iv_length );
4199 goto exit;
4200 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004201
Moran Peker41deec42018-04-04 15:43:05 +03004202 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004203 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004204 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004205 goto exit;
4206 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004207 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004208 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004209 if( ret != 0 )
4210 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004211 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004212 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004213 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004214
mohammad16038481e742018-03-18 13:57:31 +02004215 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004216 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004217
Moran Peker395db872018-05-31 14:07:14 +03004218exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004219 if( status == PSA_SUCCESS )
4220 operation->iv_set = 1;
4221 else
Moran Peker395db872018-05-31 14:07:14 +03004222 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004223 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004224}
4225
Gilles Peskinefe119512018-07-08 21:39:34 +02004226psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004227 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004228 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004229{
itayzafrir534bd7c2018-08-02 13:56:32 +03004230 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004231 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004232 if( operation->iv_set || ! operation->iv_required )
4233 {
4234 return( PSA_ERROR_BAD_STATE );
4235 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004236
Steven Cooremanef8575e2020-09-11 11:44:50 +02004237 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004238 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004239 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004240 iv,
4241 iv_length );
4242 goto exit;
4243 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004244
Moran Pekera28258c2018-05-29 16:25:04 +03004245 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004246 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004247 status = PSA_ERROR_INVALID_ARGUMENT;
4248 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004249 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004250 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4251 status = mbedtls_to_psa_error( ret );
4252exit:
4253 if( status == PSA_SUCCESS )
4254 operation->iv_set = 1;
4255 else
mohammad1603503973b2018-03-12 15:59:30 +02004256 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004257 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004258}
4259
Steven Cooreman177deba2020-09-07 17:14:14 +02004260/* Process input for which the algorithm is set to ECB mode. This requires
4261 * manual processing, since the PSA API is defined as being able to process
4262 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4263 * underlying mbedtls_cipher_update only takes full blocks. */
4264static psa_status_t psa_cipher_update_ecb_internal(
4265 mbedtls_cipher_context_t *ctx,
4266 const uint8_t *input,
4267 size_t input_length,
4268 uint8_t *output,
4269 size_t output_size,
4270 size_t *output_length )
4271{
4272 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4273 size_t block_size = ctx->cipher_info->block_size;
4274 size_t internal_output_length = 0;
4275 *output_length = 0;
4276
4277 if( input_length == 0 )
4278 {
4279 status = PSA_SUCCESS;
4280 goto exit;
4281 }
4282
4283 if( ctx->unprocessed_len > 0 )
4284 {
4285 /* Fill up to block size, and run the block if there's a full one. */
4286 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4287
4288 if( input_length < bytes_to_copy )
4289 bytes_to_copy = input_length;
4290
4291 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4292 input, bytes_to_copy );
4293 input_length -= bytes_to_copy;
4294 input += bytes_to_copy;
4295 ctx->unprocessed_len += bytes_to_copy;
4296
4297 if( ctx->unprocessed_len == block_size )
4298 {
4299 status = mbedtls_to_psa_error(
4300 mbedtls_cipher_update( ctx,
4301 ctx->unprocessed_data,
4302 block_size,
4303 output, &internal_output_length ) );
4304
4305 if( status != PSA_SUCCESS )
4306 goto exit;
4307
4308 output += internal_output_length;
4309 output_size -= internal_output_length;
4310 *output_length += internal_output_length;
4311 ctx->unprocessed_len = 0;
4312 }
4313 }
4314
4315 while( input_length >= block_size )
4316 {
4317 /* Run all full blocks we have, one by one */
4318 status = mbedtls_to_psa_error(
4319 mbedtls_cipher_update( ctx, input,
4320 block_size,
4321 output, &internal_output_length ) );
4322
4323 if( status != PSA_SUCCESS )
4324 goto exit;
4325
4326 input_length -= block_size;
4327 input += block_size;
4328
4329 output += internal_output_length;
4330 output_size -= internal_output_length;
4331 *output_length += internal_output_length;
4332 }
4333
4334 if( input_length > 0 )
4335 {
4336 /* Save unprocessed bytes for later processing */
4337 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4338 input, input_length );
4339 ctx->unprocessed_len += input_length;
4340 }
4341
4342 status = PSA_SUCCESS;
4343
4344exit:
4345 return( status );
4346}
4347
Gilles Peskinee553c652018-06-04 16:22:46 +02004348psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4349 const uint8_t *input,
4350 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004351 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004352 size_t output_size,
4353 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004354{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004355 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004356 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004357 if( operation->alg == 0 )
4358 {
4359 return( PSA_ERROR_BAD_STATE );
4360 }
4361 if( operation->iv_required && ! operation->iv_set )
4362 {
4363 return( PSA_ERROR_BAD_STATE );
4364 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004365
Steven Cooremanef8575e2020-09-11 11:44:50 +02004366 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004367 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004368 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004369 input,
4370 input_length,
4371 output,
4372 output_size,
4373 output_length );
4374 goto exit;
4375 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004376
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004377 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004378 {
4379 /* Take the unprocessed partial block left over from previous
4380 * update calls, if any, plus the input to this call. Remove
4381 * the last partial block, if any. You get the data that will be
4382 * output in this call. */
4383 expected_output_size =
4384 ( operation->ctx.cipher.unprocessed_len + input_length )
4385 / operation->block_size * operation->block_size;
4386 }
4387 else
4388 {
4389 expected_output_size = input_length;
4390 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004391
Gilles Peskine89d789c2018-06-04 17:17:16 +02004392 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004393 {
4394 status = PSA_ERROR_BUFFER_TOO_SMALL;
4395 goto exit;
4396 }
mohammad160382759612018-03-12 18:16:40 +02004397
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004398 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4399 {
4400 /* mbedtls_cipher_update has an API inconsistency: it will only
4401 * process a single block at a time in ECB mode. Abstract away that
4402 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004403 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4404 input,
4405 input_length,
4406 output,
4407 output_size,
4408 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004409 }
4410 else
4411 {
4412 status = mbedtls_to_psa_error(
4413 mbedtls_cipher_update( &operation->ctx.cipher, input,
4414 input_length, output, output_length ) );
4415 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004416exit:
4417 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004418 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004419 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004420}
4421
Gilles Peskinee553c652018-06-04 16:22:46 +02004422psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4423 uint8_t *output,
4424 size_t output_size,
4425 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004426{
David Saadab4ecc272019-02-14 13:48:10 +02004427 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004428 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004429 if( operation->alg == 0 )
4430 {
4431 return( PSA_ERROR_BAD_STATE );
4432 }
4433 if( operation->iv_required && ! operation->iv_set )
4434 {
4435 return( PSA_ERROR_BAD_STATE );
4436 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004437
Steven Cooremanef8575e2020-09-11 11:44:50 +02004438 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004439 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004440 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004441 output,
4442 output_size,
4443 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004444 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004445 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004446
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004447 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004448 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004449 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004450 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004451 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004452 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004453 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004454 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004455 }
4456
Steven Cooremanef8575e2020-09-11 11:44:50 +02004457 status = mbedtls_to_psa_error(
4458 mbedtls_cipher_finish( &operation->ctx.cipher,
4459 temp_output_buffer,
4460 output_length ) );
4461 if( status != PSA_SUCCESS )
4462 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004463
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004464 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004465 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004466 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004467 memcpy( output, temp_output_buffer, *output_length );
4468 else
Janos Follath315b51c2018-07-09 16:04:51 +01004469 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004470
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471exit:
4472 if( operation->mbedtls_in_use == 1 )
4473 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004474
Steven Cooremanef8575e2020-09-11 11:44:50 +02004475 if( status == PSA_SUCCESS )
4476 return( psa_cipher_abort( operation ) );
4477 else
4478 {
4479 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004480 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004481
Steven Cooremanef8575e2020-09-11 11:44:50 +02004482 return( status );
4483 }
mohammad1603503973b2018-03-12 15:59:30 +02004484}
4485
Gilles Peskinee553c652018-06-04 16:22:46 +02004486psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4487{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004488 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004489 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004490 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004491 * in use. It's ok to call abort on such an object, and there's
4492 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004493 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004494 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004495
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004496 /* Sanity check (shouldn't happen: operation->alg should
4497 * always have been initialized to a valid value). */
4498 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4499 return( PSA_ERROR_BAD_STATE );
4500
Steven Cooremanef8575e2020-09-11 11:44:50 +02004501 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004502 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004503 else
4504 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004505
Moran Peker41deec42018-04-04 15:43:05 +03004506 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004507 operation->key_set = 0;
4508 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004509 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004510 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004511 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004512 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004513
Moran Peker395db872018-05-31 14:07:14 +03004514 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004515}
4516
Gilles Peskinea0655c32018-04-30 17:06:50 +02004517
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004518
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004519
mohammad16035955c982018-04-26 00:53:03 +03004520/****************************************************************/
4521/* AEAD */
4522/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004523
Gilles Peskineedf9a652018-08-17 18:11:56 +02004524typedef struct
4525{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004526 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004527 const mbedtls_cipher_info_t *cipher_info;
4528 union
4529 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004530 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004531#if defined(MBEDTLS_CCM_C)
4532 mbedtls_ccm_context ccm;
4533#endif /* MBEDTLS_CCM_C */
4534#if defined(MBEDTLS_GCM_C)
4535 mbedtls_gcm_context gcm;
4536#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004537#if defined(MBEDTLS_CHACHAPOLY_C)
4538 mbedtls_chachapoly_context chachapoly;
4539#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004540 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004541 psa_algorithm_t core_alg;
4542 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004543 uint8_t tag_length;
4544} aead_operation_t;
4545
Ronald Cronf95a2b12020-10-22 15:24:49 +02004546#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4547
Gilles Peskine30a9e412019-01-14 18:36:12 +01004548static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004549{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004550 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004551 {
4552#if defined(MBEDTLS_CCM_C)
4553 case PSA_ALG_CCM:
4554 mbedtls_ccm_free( &operation->ctx.ccm );
4555 break;
4556#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004557#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004558 case PSA_ALG_GCM:
4559 mbedtls_gcm_free( &operation->ctx.gcm );
4560 break;
4561#endif /* MBEDTLS_GCM_C */
4562 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004563
Ronald Cron5c522922020-11-14 16:35:34 +01004564 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004565}
4566
4567static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004568 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004569 psa_key_usage_t usage,
4570 psa_algorithm_t alg )
4571{
4572 psa_status_t status;
4573 size_t key_bits;
4574 mbedtls_cipher_id_t cipher_id;
4575
Ronald Cron5c522922020-11-14 16:35:34 +01004576 status = psa_get_and_lock_transparent_key_slot_with_policy(
4577 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004578 if( status != PSA_SUCCESS )
4579 return( status );
4580
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004581 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004582
4583 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004584 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004585 &cipher_id );
4586 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004587 {
4588 status = PSA_ERROR_NOT_SUPPORTED;
4589 goto cleanup;
4590 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004591
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004592 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004593 {
4594#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004595 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004596 operation->core_alg = PSA_ALG_CCM;
4597 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004598 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4599 * The call to mbedtls_ccm_encrypt_and_tag or
4600 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004601 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004602 {
4603 status = PSA_ERROR_INVALID_ARGUMENT;
4604 goto cleanup;
4605 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004606 mbedtls_ccm_init( &operation->ctx.ccm );
4607 status = mbedtls_to_psa_error(
4608 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004609 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004610 (unsigned int) key_bits ) );
4611 if( status != 0 )
4612 goto cleanup;
4613 break;
4614#endif /* MBEDTLS_CCM_C */
4615
4616#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004617 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004618 operation->core_alg = PSA_ALG_GCM;
4619 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004620 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4621 * The call to mbedtls_gcm_crypt_and_tag or
4622 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004623 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004624 {
4625 status = PSA_ERROR_INVALID_ARGUMENT;
4626 goto cleanup;
4627 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004628 mbedtls_gcm_init( &operation->ctx.gcm );
4629 status = mbedtls_to_psa_error(
4630 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004631 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004632 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004633 if( status != 0 )
4634 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004635 break;
4636#endif /* MBEDTLS_GCM_C */
4637
Gilles Peskine26869f22019-05-06 15:25:00 +02004638#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004639 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004640 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4641 operation->full_tag_length = 16;
4642 /* We only support the default tag length. */
4643 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004644 {
4645 status = PSA_ERROR_NOT_SUPPORTED;
4646 goto cleanup;
4647 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004648 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4649 status = mbedtls_to_psa_error(
4650 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004651 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004652 if( status != 0 )
4653 goto cleanup;
4654 break;
4655#endif /* MBEDTLS_CHACHAPOLY_C */
4656
Gilles Peskineedf9a652018-08-17 18:11:56 +02004657 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004658 status = PSA_ERROR_NOT_SUPPORTED;
4659 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004660 }
4661
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004662 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4663 {
4664 status = PSA_ERROR_INVALID_ARGUMENT;
4665 goto cleanup;
4666 }
4667 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004668
Gilles Peskineedf9a652018-08-17 18:11:56 +02004669 return( PSA_SUCCESS );
4670
4671cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004672 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004673 return( status );
4674}
4675
Ronald Croncf56a0a2020-08-04 09:51:30 +02004676psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004677 psa_algorithm_t alg,
4678 const uint8_t *nonce,
4679 size_t nonce_length,
4680 const uint8_t *additional_data,
4681 size_t additional_data_length,
4682 const uint8_t *plaintext,
4683 size_t plaintext_length,
4684 uint8_t *ciphertext,
4685 size_t ciphertext_size,
4686 size_t *ciphertext_length )
4687{
mohammad16035955c982018-04-26 00:53:03 +03004688 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004689 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004690 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004691
mohammad1603f08a5502018-06-03 15:05:47 +03004692 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004693
Ronald Croncf56a0a2020-08-04 09:51:30 +02004694 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004695 if( status != PSA_SUCCESS )
4696 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004697
Gilles Peskineedf9a652018-08-17 18:11:56 +02004698 /* For all currently supported modes, the tag is at the end of the
4699 * ciphertext. */
4700 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4701 {
4702 status = PSA_ERROR_BUFFER_TOO_SMALL;
4703 goto exit;
4704 }
4705 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004706
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004707#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004708 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004709 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004710 status = mbedtls_to_psa_error(
4711 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4712 MBEDTLS_GCM_ENCRYPT,
4713 plaintext_length,
4714 nonce, nonce_length,
4715 additional_data, additional_data_length,
4716 plaintext, ciphertext,
4717 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004718 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004719 else
4720#endif /* MBEDTLS_GCM_C */
4721#if defined(MBEDTLS_CCM_C)
4722 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004723 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004724 status = mbedtls_to_psa_error(
4725 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4726 plaintext_length,
4727 nonce, nonce_length,
4728 additional_data,
4729 additional_data_length,
4730 plaintext, ciphertext,
4731 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004732 }
mohammad16035c8845f2018-05-09 05:40:09 -07004733 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004734#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004735#if defined(MBEDTLS_CHACHAPOLY_C)
4736 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4737 {
4738 if( nonce_length != 12 || operation.tag_length != 16 )
4739 {
4740 status = PSA_ERROR_NOT_SUPPORTED;
4741 goto exit;
4742 }
4743 status = mbedtls_to_psa_error(
4744 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4745 plaintext_length,
4746 nonce,
4747 additional_data,
4748 additional_data_length,
4749 plaintext,
4750 ciphertext,
4751 tag ) );
4752 }
4753 else
4754#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004755 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004756 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004757 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004758 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004759
Gilles Peskineedf9a652018-08-17 18:11:56 +02004760 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4761 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004762
Gilles Peskineedf9a652018-08-17 18:11:56 +02004763exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004764 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004765 if( status == PSA_SUCCESS )
4766 *ciphertext_length = plaintext_length + operation.tag_length;
4767 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004768}
4769
Gilles Peskineee652a32018-06-01 19:23:52 +02004770/* Locate the tag in a ciphertext buffer containing the encrypted data
4771 * followed by the tag. Return the length of the part preceding the tag in
4772 * *plaintext_length. This is the size of the plaintext in modes where
4773 * the encrypted data has the same size as the plaintext, such as
4774 * CCM and GCM. */
4775static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4776 const uint8_t *ciphertext,
4777 size_t ciphertext_length,
4778 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004779 const uint8_t **p_tag )
4780{
4781 size_t payload_length;
4782 if( tag_length > ciphertext_length )
4783 return( PSA_ERROR_INVALID_ARGUMENT );
4784 payload_length = ciphertext_length - tag_length;
4785 if( payload_length > plaintext_size )
4786 return( PSA_ERROR_BUFFER_TOO_SMALL );
4787 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004788 return( PSA_SUCCESS );
4789}
4790
Ronald Croncf56a0a2020-08-04 09:51:30 +02004791psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004792 psa_algorithm_t alg,
4793 const uint8_t *nonce,
4794 size_t nonce_length,
4795 const uint8_t *additional_data,
4796 size_t additional_data_length,
4797 const uint8_t *ciphertext,
4798 size_t ciphertext_length,
4799 uint8_t *plaintext,
4800 size_t plaintext_size,
4801 size_t *plaintext_length )
4802{
mohammad16035955c982018-04-26 00:53:03 +03004803 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004804 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004805 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004806
Gilles Peskineee652a32018-06-01 19:23:52 +02004807 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004808
Ronald Croncf56a0a2020-08-04 09:51:30 +02004809 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004810 if( status != PSA_SUCCESS )
4811 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004812
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004813 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4814 ciphertext, ciphertext_length,
4815 plaintext_size, &tag );
4816 if( status != PSA_SUCCESS )
4817 goto exit;
4818
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004819#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004820 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004821 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004822 status = mbedtls_to_psa_error(
4823 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4824 ciphertext_length - operation.tag_length,
4825 nonce, nonce_length,
4826 additional_data,
4827 additional_data_length,
4828 tag, operation.tag_length,
4829 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004830 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004831 else
4832#endif /* MBEDTLS_GCM_C */
4833#if defined(MBEDTLS_CCM_C)
4834 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004835 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004836 status = mbedtls_to_psa_error(
4837 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4838 ciphertext_length - operation.tag_length,
4839 nonce, nonce_length,
4840 additional_data,
4841 additional_data_length,
4842 ciphertext, plaintext,
4843 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004844 }
mohammad160339574652018-06-01 04:39:53 -07004845 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004846#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004847#if defined(MBEDTLS_CHACHAPOLY_C)
4848 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4849 {
4850 if( nonce_length != 12 || operation.tag_length != 16 )
4851 {
4852 status = PSA_ERROR_NOT_SUPPORTED;
4853 goto exit;
4854 }
4855 status = mbedtls_to_psa_error(
4856 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4857 ciphertext_length - operation.tag_length,
4858 nonce,
4859 additional_data,
4860 additional_data_length,
4861 tag,
4862 ciphertext,
4863 plaintext ) );
4864 }
4865 else
4866#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004867 {
mohammad1603554faad2018-06-03 15:07:38 +03004868 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004869 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004870
Gilles Peskineedf9a652018-08-17 18:11:56 +02004871 if( status != PSA_SUCCESS && plaintext_size != 0 )
4872 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004873
Gilles Peskineedf9a652018-08-17 18:11:56 +02004874exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004875 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004876 if( status == PSA_SUCCESS )
4877 *plaintext_length = ciphertext_length - operation.tag_length;
4878 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004879}
4880
Gilles Peskinea0655c32018-04-30 17:06:50 +02004881
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004882
Gilles Peskine20035e32018-02-03 22:44:14 +01004883/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004884/* Generators */
4885/****************************************************************/
4886
John Durkop07cc04a2020-11-16 22:08:34 -08004887#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4888 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4889 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4890#define AT_LEAST_ONE_BUILTIN_KDF
4891#endif
4892
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004893#define HKDF_STATE_INIT 0 /* no input yet */
4894#define HKDF_STATE_STARTED 1 /* got salt */
4895#define HKDF_STATE_KEYED 2 /* got key */
4896#define HKDF_STATE_OUTPUT 3 /* output started */
4897
Gilles Peskinecbe66502019-05-16 16:59:18 +02004898static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004901 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4902 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004904 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905}
4906
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004907psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004908{
4909 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004910 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004912 {
4913 /* The object has (apparently) been initialized but it is not
4914 * in use. It's ok to call abort on such an object, and there's
4915 * nothing to do. */
4916 }
4917 else
John Durkopd0321952020-10-29 21:37:36 -07004918#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004920 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 mbedtls_free( operation->ctx.hkdf.info );
4922 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004923 }
John Durkop07cc04a2020-11-16 22:08:34 -08004924 else
4925#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4926#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4927 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4928 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004929 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004930 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004931 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004932 if( operation->ctx.tls12_prf.seed != NULL )
4933 {
4934 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4935 operation->ctx.tls12_prf.seed_length );
4936 mbedtls_free( operation->ctx.tls12_prf.seed );
4937 }
4938
4939 if( operation->ctx.tls12_prf.label != NULL )
4940 {
4941 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4942 operation->ctx.tls12_prf.label_length );
4943 mbedtls_free( operation->ctx.tls12_prf.label );
4944 }
4945
4946 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4947
4948 /* We leave the fields Ai and output_block to be erased safely by the
4949 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004950 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004951 else
John Durkop07cc04a2020-11-16 22:08:34 -08004952#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4953 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004954 {
4955 status = PSA_ERROR_BAD_STATE;
4956 }
Janos Follath083036a2019-06-11 10:22:26 +01004957 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004958 return( status );
4959}
4960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004961psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004962 size_t *capacity)
4963{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004964 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004965 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004967 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004968 }
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004971 return( PSA_SUCCESS );
4972}
4973
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004974psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004975 size_t capacity )
4976{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004978 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004979 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004980 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004981 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004982 return( PSA_SUCCESS );
4983}
4984
John Durkopd0321952020-10-29 21:37:36 -07004985#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004986/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004987 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004988static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004989 psa_algorithm_t hash_alg,
4990 uint8_t *output,
4991 size_t output_length )
4992{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004993 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004994 psa_status_t status;
4995
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004996 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4997 return( PSA_ERROR_BAD_STATE );
4998 hkdf->state = HKDF_STATE_OUTPUT;
4999
Gilles Peskinebef7f142018-07-12 17:22:21 +02005000 while( output_length != 0 )
5001 {
5002 /* Copy what remains of the current block */
5003 uint8_t n = hash_length - hkdf->offset_in_block;
5004 if( n > output_length )
5005 n = (uint8_t) output_length;
5006 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5007 output += n;
5008 output_length -= n;
5009 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005010 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005011 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005012 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005013 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005014 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005015 * object was corrupted or if this function is called directly
5016 * inside the library. */
5017 if( hkdf->block_number == 0xff )
5018 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005019
5020 /* We need a new block */
5021 ++hkdf->block_number;
5022 hkdf->offset_in_block = 0;
5023 status = psa_hmac_setup_internal( &hkdf->hmac,
5024 hkdf->prk, hash_length,
5025 hash_alg );
5026 if( status != PSA_SUCCESS )
5027 return( status );
5028 if( hkdf->block_number != 1 )
5029 {
5030 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5031 hkdf->output_block,
5032 hash_length );
5033 if( status != PSA_SUCCESS )
5034 return( status );
5035 }
5036 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5037 hkdf->info,
5038 hkdf->info_length );
5039 if( status != PSA_SUCCESS )
5040 return( status );
5041 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5042 &hkdf->block_number, 1 );
5043 if( status != PSA_SUCCESS )
5044 return( status );
5045 status = psa_hmac_finish_internal( &hkdf->hmac,
5046 hkdf->output_block,
5047 sizeof( hkdf->output_block ) );
5048 if( status != PSA_SUCCESS )
5049 return( status );
5050 }
5051
5052 return( PSA_SUCCESS );
5053}
John Durkop07cc04a2020-11-16 22:08:34 -08005054#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005055
John Durkop07cc04a2020-11-16 22:08:34 -08005056#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5057 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005058static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5059 psa_tls12_prf_key_derivation_t *tls12_prf,
5060 psa_algorithm_t alg )
5061{
5062 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005063 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005064 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5065 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005066
Janos Follath7742fee2019-06-17 12:58:10 +01005067 /* We can't be wanting more output after block 0xff, otherwise
5068 * the capacity check in psa_key_derivation_output_bytes() would have
5069 * prevented this call. It could happen only if the operation
5070 * object was corrupted or if this function is called directly
5071 * inside the library. */
5072 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005073 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005074
5075 /* We need a new block */
5076 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005077 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005078
5079 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5080 *
5081 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5082 *
5083 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5084 * HMAC_hash(secret, A(2) + seed) +
5085 * HMAC_hash(secret, A(3) + seed) + ...
5086 *
5087 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005088 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005089 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005090 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005091 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005092 * is currently extracted as `output_block` and where i is
5093 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005094 */
5095
Janos Follathea29bfb2019-06-19 12:21:20 +01005096 /* Save the hash context before using it, to preserve the hash state with
5097 * only the inner padding in it. We need this, because inner padding depends
5098 * on the key (secret in the RFC's terminology). */
5099 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5100 if( status != PSA_SUCCESS )
5101 goto cleanup;
5102
5103 /* Calculate A(i) where i = tls12_prf->block_number. */
5104 if( tls12_prf->block_number == 1 )
5105 {
5106 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5107 * the variable seed and in this instance means it in the context of the
5108 * P_hash function, where seed = label + seed.) */
5109 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5110 tls12_prf->label, tls12_prf->label_length );
5111 if( status != PSA_SUCCESS )
5112 goto cleanup;
5113 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5114 tls12_prf->seed, tls12_prf->seed_length );
5115 if( status != PSA_SUCCESS )
5116 goto cleanup;
5117 }
5118 else
5119 {
5120 /* A(i) = HMAC_hash(secret, A(i-1)) */
5121 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5122 tls12_prf->Ai, hash_length );
5123 if( status != PSA_SUCCESS )
5124 goto cleanup;
5125 }
5126
5127 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5128 tls12_prf->Ai, hash_length );
5129 if( status != PSA_SUCCESS )
5130 goto cleanup;
5131 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5132 if( status != PSA_SUCCESS )
5133 goto cleanup;
5134
5135 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5136 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5137 tls12_prf->Ai, hash_length );
5138 if( status != PSA_SUCCESS )
5139 goto cleanup;
5140 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5141 tls12_prf->label, tls12_prf->label_length );
5142 if( status != PSA_SUCCESS )
5143 goto cleanup;
5144 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5145 tls12_prf->seed, tls12_prf->seed_length );
5146 if( status != PSA_SUCCESS )
5147 goto cleanup;
5148 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5149 tls12_prf->output_block, hash_length );
5150 if( status != PSA_SUCCESS )
5151 goto cleanup;
5152 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5153 if( status != PSA_SUCCESS )
5154 goto cleanup;
5155
Janos Follath7742fee2019-06-17 12:58:10 +01005156
5157cleanup:
5158
Janos Follathea29bfb2019-06-19 12:21:20 +01005159 cleanup_status = psa_hash_abort( &backup );
5160 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5161 status = cleanup_status;
5162
5163 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005164}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005165
Janos Follath844eb0e2019-06-19 12:10:49 +01005166static psa_status_t psa_key_derivation_tls12_prf_read(
5167 psa_tls12_prf_key_derivation_t *tls12_prf,
5168 psa_algorithm_t alg,
5169 uint8_t *output,
5170 size_t output_length )
5171{
5172 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005173 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005174 psa_status_t status;
5175 uint8_t offset, length;
5176
5177 while( output_length != 0 )
5178 {
5179 /* Check if we have fully processed the current block. */
5180 if( tls12_prf->left_in_block == 0 )
5181 {
5182 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5183 alg );
5184 if( status != PSA_SUCCESS )
5185 return( status );
5186
5187 continue;
5188 }
5189
5190 if( tls12_prf->left_in_block > output_length )
5191 length = (uint8_t) output_length;
5192 else
5193 length = tls12_prf->left_in_block;
5194
5195 offset = hash_length - tls12_prf->left_in_block;
5196 memcpy( output, tls12_prf->output_block + offset, length );
5197 output += length;
5198 output_length -= length;
5199 tls12_prf->left_in_block -= length;
5200 }
5201
5202 return( PSA_SUCCESS );
5203}
John Durkop07cc04a2020-11-16 22:08:34 -08005204#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5205 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005206
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005207psa_status_t psa_key_derivation_output_bytes(
5208 psa_key_derivation_operation_t *operation,
5209 uint8_t *output,
5210 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005211{
5212 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005214
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005216 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005218 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005219 }
5220
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005222 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005223 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005224 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005226 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227 goto exit;
5228 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005229 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005230 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005232 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005233 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5234 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005236 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005237 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005238 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005239 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005240
John Durkopd0321952020-10-29 21:37:36 -07005241#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005242 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005243 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005244 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005245 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005246 output, output_length );
5247 }
Janos Follathadbec812019-06-14 11:05:39 +01005248 else
John Durkop07cc04a2020-11-16 22:08:34 -08005249#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5250#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5251 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005252 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005253 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005254 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005256 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005257 output_length );
5258 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005259 else
John Durkop07cc04a2020-11-16 22:08:34 -08005260#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5261 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005263 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005264 return( PSA_ERROR_BAD_STATE );
5265 }
5266
5267exit:
5268 if( status != PSA_SUCCESS )
5269 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005270 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 * This allows us to differentiate between exhausted operations and
5272 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5273 * operations. */
5274 psa_algorithm_t alg = operation->alg;
5275 psa_key_derivation_abort( operation );
5276 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005277 memset( output, '!', output_length );
5278 }
5279 return( status );
5280}
5281
David Brown7807bf72021-02-09 16:28:23 -07005282#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005283static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5284{
5285 if( data_size >= 8 )
5286 mbedtls_des_key_set_parity( data );
5287 if( data_size >= 16 )
5288 mbedtls_des_key_set_parity( data + 8 );
5289 if( data_size >= 24 )
5290 mbedtls_des_key_set_parity( data + 16 );
5291}
David Brown7807bf72021-02-09 16:28:23 -07005292#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005293
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005294static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005295 psa_key_slot_t *slot,
5296 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005297 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005298{
5299 uint8_t *data = NULL;
5300 size_t bytes = PSA_BITS_TO_BYTES( bits );
5301 psa_status_t status;
5302
Gilles Peskine8e338702019-07-30 20:06:31 +02005303 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005304 return( PSA_ERROR_INVALID_ARGUMENT );
5305 if( bits % 8 != 0 )
5306 return( PSA_ERROR_INVALID_ARGUMENT );
5307 data = mbedtls_calloc( 1, bytes );
5308 if( data == NULL )
5309 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5310
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005311 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005312 if( status != PSA_SUCCESS )
5313 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005314#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005315 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005316 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005317#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005318
5319 status = psa_allocate_buffer_to_slot( slot, bytes );
5320 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005321 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005322
Ronald Cronbf33c932020-11-28 18:06:53 +01005323 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005324 psa_key_attributes_t attributes = {
5325 .core = slot->attr
5326 };
5327
Ronald Cronbf33c932020-11-28 18:06:53 +01005328 status = psa_driver_wrapper_import_key( &attributes,
5329 data, bytes,
5330 slot->key.data,
5331 slot->key.bytes,
5332 &slot->key.bytes, &bits );
5333 if( bits != slot->attr.bits )
5334 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005335
5336exit:
5337 mbedtls_free( data );
5338 return( status );
5339}
5340
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005341psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005342 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005343 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005344{
5345 psa_status_t status;
5346 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005347 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005348
Ronald Cron81709fc2020-11-14 12:10:32 +01005349 *key = MBEDTLS_SVC_KEY_ID_INIT;
5350
Gilles Peskine0f84d622019-09-12 19:03:13 +02005351 /* Reject any attempt to create a zero-length key so that we don't
5352 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5353 if( psa_get_key_bits( attributes ) == 0 )
5354 return( PSA_ERROR_INVALID_ARGUMENT );
5355
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005356 if( ! operation->can_output_key )
5357 return( PSA_ERROR_NOT_PERMITTED );
5358
Ronald Cron81709fc2020-11-14 12:10:32 +01005359 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5360 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005361#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5362 if( driver != NULL )
5363 {
5364 /* Deriving a key in a secure element is not implemented yet. */
5365 status = PSA_ERROR_NOT_SUPPORTED;
5366 }
5367#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005368 if( status == PSA_SUCCESS )
5369 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005370 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005371 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005372 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005373 }
5374 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005375 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005376 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005377 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005378
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005379 return( status );
5380}
5381
Gilles Peskineeab56e42018-07-12 17:12:33 +02005382
5383
5384/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005385/* Key derivation */
5386/****************************************************************/
5387
Steven Cooreman932ffb72021-02-15 12:14:32 +01005388#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005389static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005390 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005391 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005392{
John Durkop07cc04a2020-11-16 22:08:34 -08005393 int is_kdf_alg_supported;
5394
Janos Follath5fe19732019-06-20 15:09:30 +01005395 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5396 * initialisers for this union leave some bytes unspecified.) */
5397 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5398
Gilles Peskine969c5d62019-01-16 15:53:06 +01005399 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005400#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005401 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5402 is_kdf_alg_supported = 1;
5403 else
5404#endif
5405#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5406 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5407 is_kdf_alg_supported = 1;
5408 else
5409#endif
5410#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5411 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5412 is_kdf_alg_supported = 1;
5413 else
5414#endif
5415 is_kdf_alg_supported = 0;
5416
5417 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005418 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005419 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005420 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005421 if( hash_size == 0 )
5422 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005423 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5424 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005425 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005426 {
5427 return( PSA_ERROR_NOT_SUPPORTED );
5428 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005429 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005430 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005431 }
John Durkop07cc04a2020-11-16 22:08:34 -08005432
5433 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434}
John Durkop07cc04a2020-11-16 22:08:34 -08005435#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005436
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005437psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005438 psa_algorithm_t alg )
5439{
5440 psa_status_t status;
5441
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005442 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005443 return( PSA_ERROR_BAD_STATE );
5444
Gilles Peskine6843c292019-01-18 16:44:49 +01005445 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5446 return( PSA_ERROR_INVALID_ARGUMENT );
5447 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005448 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005449#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005450 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005451 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005452#else
5453 return( PSA_ERROR_NOT_SUPPORTED );
5454#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005455 }
5456 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5457 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005458#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005459 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005460#else
5461 return( PSA_ERROR_NOT_SUPPORTED );
5462#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005463 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005464 else
5465 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005466
5467 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005468 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005469 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005470}
5471
John Durkopd0321952020-10-29 21:37:36 -07005472#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005473static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005474 psa_algorithm_t hash_alg,
5475 psa_key_derivation_step_t step,
5476 const uint8_t *data,
5477 size_t data_length )
5478{
5479 psa_status_t status;
5480 switch( step )
5481 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005482 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005483 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005484 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005485 status = psa_hmac_setup_internal( &hkdf->hmac,
5486 data, data_length,
5487 hash_alg );
5488 if( status != PSA_SUCCESS )
5489 return( status );
5490 hkdf->state = HKDF_STATE_STARTED;
5491 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005492 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005493 /* If no salt was provided, use an empty salt. */
5494 if( hkdf->state == HKDF_STATE_INIT )
5495 {
5496 status = psa_hmac_setup_internal( &hkdf->hmac,
5497 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005498 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005499 if( status != PSA_SUCCESS )
5500 return( status );
5501 hkdf->state = HKDF_STATE_STARTED;
5502 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005503 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005504 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005505 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5506 data, data_length );
5507 if( status != PSA_SUCCESS )
5508 return( status );
5509 status = psa_hmac_finish_internal( &hkdf->hmac,
5510 hkdf->prk,
5511 sizeof( hkdf->prk ) );
5512 if( status != PSA_SUCCESS )
5513 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005514 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005515 hkdf->block_number = 0;
5516 hkdf->state = HKDF_STATE_KEYED;
5517 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005518 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005519 if( hkdf->state == HKDF_STATE_OUTPUT )
5520 return( PSA_ERROR_BAD_STATE );
5521 if( hkdf->info_set )
5522 return( PSA_ERROR_BAD_STATE );
5523 hkdf->info_length = data_length;
5524 if( data_length != 0 )
5525 {
5526 hkdf->info = mbedtls_calloc( 1, data_length );
5527 if( hkdf->info == NULL )
5528 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5529 memcpy( hkdf->info, data, data_length );
5530 }
5531 hkdf->info_set = 1;
5532 return( PSA_SUCCESS );
5533 default:
5534 return( PSA_ERROR_INVALID_ARGUMENT );
5535 }
5536}
John Durkop07cc04a2020-11-16 22:08:34 -08005537#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005538
John Durkop07cc04a2020-11-16 22:08:34 -08005539#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5540 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005541static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5542 const uint8_t *data,
5543 size_t data_length )
5544{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005545 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005546 return( PSA_ERROR_BAD_STATE );
5547
Janos Follathd6dce9f2019-07-04 09:11:38 +01005548 if( data_length != 0 )
5549 {
5550 prf->seed = mbedtls_calloc( 1, data_length );
5551 if( prf->seed == NULL )
5552 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005553
Janos Follathd6dce9f2019-07-04 09:11:38 +01005554 memcpy( prf->seed, data, data_length );
5555 prf->seed_length = data_length;
5556 }
Janos Follathf08e2652019-06-13 09:05:41 +01005557
Gilles Peskine43f958b2020-12-13 14:55:14 +01005558 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005559
5560 return( PSA_SUCCESS );
5561}
5562
Janos Follath81550542019-06-13 14:26:34 +01005563static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5564 psa_algorithm_t hash_alg,
5565 const uint8_t *data,
5566 size_t data_length )
5567{
5568 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005569 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005570 return( PSA_ERROR_BAD_STATE );
5571
5572 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5573 if( status != PSA_SUCCESS )
5574 return( status );
5575
Gilles Peskine43f958b2020-12-13 14:55:14 +01005576 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005577
5578 return( PSA_SUCCESS );
5579}
5580
Janos Follath63028dd2019-06-13 09:15:47 +01005581static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5582 const uint8_t *data,
5583 size_t data_length )
5584{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005585 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005586 return( PSA_ERROR_BAD_STATE );
5587
Janos Follathd6dce9f2019-07-04 09:11:38 +01005588 if( data_length != 0 )
5589 {
5590 prf->label = mbedtls_calloc( 1, data_length );
5591 if( prf->label == NULL )
5592 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005593
Janos Follathd6dce9f2019-07-04 09:11:38 +01005594 memcpy( prf->label, data, data_length );
5595 prf->label_length = data_length;
5596 }
Janos Follath63028dd2019-06-13 09:15:47 +01005597
Gilles Peskine43f958b2020-12-13 14:55:14 +01005598 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005599
5600 return( PSA_SUCCESS );
5601}
5602
Janos Follathb03233e2019-06-11 15:30:30 +01005603static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5604 psa_algorithm_t hash_alg,
5605 psa_key_derivation_step_t step,
5606 const uint8_t *data,
5607 size_t data_length )
5608{
Janos Follathb03233e2019-06-11 15:30:30 +01005609 switch( step )
5610 {
Janos Follathf08e2652019-06-13 09:05:41 +01005611 case PSA_KEY_DERIVATION_INPUT_SEED:
5612 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005613 case PSA_KEY_DERIVATION_INPUT_SECRET:
5614 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005615 case PSA_KEY_DERIVATION_INPUT_LABEL:
5616 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005617 default:
5618 return( PSA_ERROR_INVALID_ARGUMENT );
5619 }
5620}
John Durkop07cc04a2020-11-16 22:08:34 -08005621#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5622 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5623
5624#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5625static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5626 psa_tls12_prf_key_derivation_t *prf,
5627 psa_algorithm_t hash_alg,
5628 const uint8_t *data,
5629 size_t data_length )
5630{
5631 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005632 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005633 uint8_t *cur = pms;
5634
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005635 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005636 return( PSA_ERROR_INVALID_ARGUMENT );
5637
5638 /* Quoting RFC 4279, Section 2:
5639 *
5640 * The premaster secret is formed as follows: if the PSK is N octets
5641 * long, concatenate a uint16 with the value N, N zero octets, a second
5642 * uint16 with the value N, and the PSK itself.
5643 */
5644
5645 *cur++ = ( data_length >> 8 ) & 0xff;
5646 *cur++ = ( data_length >> 0 ) & 0xff;
5647 memset( cur, 0, data_length );
5648 cur += data_length;
5649 *cur++ = pms[0];
5650 *cur++ = pms[1];
5651 memcpy( cur, data, data_length );
5652 cur += data_length;
5653
5654 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5655
5656 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5657 return( status );
5658}
Janos Follath6660f0e2019-06-17 08:44:03 +01005659
5660static psa_status_t psa_tls12_prf_psk_to_ms_input(
5661 psa_tls12_prf_key_derivation_t *prf,
5662 psa_algorithm_t hash_alg,
5663 psa_key_derivation_step_t step,
5664 const uint8_t *data,
5665 size_t data_length )
5666{
5667 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005668 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005669 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5670 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005671 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005672
5673 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5674}
John Durkop07cc04a2020-11-16 22:08:34 -08005675#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005676
Gilles Peskineb8965192019-09-24 16:21:10 +02005677/** Check whether the given key type is acceptable for the given
5678 * input step of a key derivation.
5679 *
5680 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5681 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5682 * Both secret and non-secret inputs can alternatively have the type
5683 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5684 * that the input was passed as a buffer rather than via a key object.
5685 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005686static int psa_key_derivation_check_input_type(
5687 psa_key_derivation_step_t step,
5688 psa_key_type_t key_type )
5689{
5690 switch( step )
5691 {
5692 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005693 if( key_type == PSA_KEY_TYPE_DERIVE )
5694 return( PSA_SUCCESS );
5695 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005696 return( PSA_SUCCESS );
5697 break;
5698 case PSA_KEY_DERIVATION_INPUT_LABEL:
5699 case PSA_KEY_DERIVATION_INPUT_SALT:
5700 case PSA_KEY_DERIVATION_INPUT_INFO:
5701 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005702 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5703 return( PSA_SUCCESS );
5704 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005705 return( PSA_SUCCESS );
5706 break;
5707 }
5708 return( PSA_ERROR_INVALID_ARGUMENT );
5709}
5710
Janos Follathb80a94e2019-06-12 15:54:46 +01005711static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005712 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005713 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005714 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005715 const uint8_t *data,
5716 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005717{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005718 psa_status_t status;
5719 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5720
5721 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005722 if( status != PSA_SUCCESS )
5723 goto exit;
5724
John Durkopd0321952020-10-29 21:37:36 -07005725#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005726 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005727 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005728 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005729 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005730 step, data, data_length );
5731 }
John Durkop07cc04a2020-11-16 22:08:34 -08005732 else
5733#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5734#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5735 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005736 {
Janos Follathb03233e2019-06-11 15:30:30 +01005737 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5738 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5739 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005740 }
John Durkop07cc04a2020-11-16 22:08:34 -08005741 else
5742#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5743#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5744 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005745 {
5746 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5747 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5748 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005749 }
5750 else
John Durkop07cc04a2020-11-16 22:08:34 -08005751#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005753 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005754 (void) data;
5755 (void) data_length;
5756 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 return( PSA_ERROR_BAD_STATE );
5758 }
5759
Gilles Peskine224b0d62019-09-23 18:13:17 +02005760exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005761 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005762 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005763 return( status );
5764}
5765
Janos Follath51f4a0f2019-06-14 11:35:55 +01005766psa_status_t psa_key_derivation_input_bytes(
5767 psa_key_derivation_operation_t *operation,
5768 psa_key_derivation_step_t step,
5769 const uint8_t *data,
5770 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005771{
Gilles Peskineb8965192019-09-24 16:21:10 +02005772 return( psa_key_derivation_input_internal( operation, step,
5773 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005774 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005775}
5776
Janos Follath51f4a0f2019-06-14 11:35:55 +01005777psa_status_t psa_key_derivation_input_key(
5778 psa_key_derivation_operation_t *operation,
5779 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005780 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005781{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005782 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005783 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005784 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005785
Ronald Cron5c522922020-11-14 16:35:34 +01005786 status = psa_get_and_lock_transparent_key_slot_with_policy(
5787 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005788 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005789 {
5790 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005791 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005792 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005793
5794 /* Passing a key object as a SECRET input unlocks the permission
5795 * to output to a key object. */
5796 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5797 operation->can_output_key = 1;
5798
Ronald Cronf95a2b12020-10-22 15:24:49 +02005799 status = psa_key_derivation_input_internal( operation,
5800 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005801 slot->key.data,
5802 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005803
Ronald Cron5c522922020-11-14 16:35:34 +01005804 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005805
Ronald Cron5c522922020-11-14 16:35:34 +01005806 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005807}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005808
5809
5810
5811/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005812/* Key agreement */
5813/****************************************************************/
5814
John Durkop6ba40d12020-11-10 08:50:04 -08005815#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005816static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5817 size_t peer_key_length,
5818 const mbedtls_ecp_keypair *our_key,
5819 uint8_t *shared_secret,
5820 size_t shared_secret_size,
5821 size_t *shared_secret_length )
5822{
Steven Cooremand4867872020-08-05 16:31:39 +02005823 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005824 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005825 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005826 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005827 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005828 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005829
Ronald Cron90857082020-11-25 14:58:33 +01005830 status = mbedtls_psa_ecp_load_representation(
5831 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005832 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005833 peer_key,
5834 peer_key_length,
5835 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005836 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005837 goto exit;
5838
Jaeden Amero97271b32019-01-10 19:38:51 +00005839 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005840 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005841 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005842 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005843 status = mbedtls_to_psa_error(
5844 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5845 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005846 goto exit;
5847
Jaeden Amero97271b32019-01-10 19:38:51 +00005848 status = mbedtls_to_psa_error(
5849 mbedtls_ecdh_calc_secret( &ecdh,
5850 shared_secret_length,
5851 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005852 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005853 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005854 if( status != PSA_SUCCESS )
5855 goto exit;
5856 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5857 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005858
5859exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005860 if( status != PSA_SUCCESS )
5861 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005862 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005863 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005864 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005865
Jaeden Amero97271b32019-01-10 19:38:51 +00005866 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005867}
John Durkop6ba40d12020-11-10 08:50:04 -08005868#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005869
Gilles Peskine01d718c2018-09-18 12:01:02 +02005870#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5871
Gilles Peskine0216fe12019-04-11 21:23:21 +02005872static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5873 psa_key_slot_t *private_key,
5874 const uint8_t *peer_key,
5875 size_t peer_key_length,
5876 uint8_t *shared_secret,
5877 size_t shared_secret_size,
5878 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005879{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005880 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005881 {
John Durkop6ba40d12020-11-10 08:50:04 -08005882#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005883 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005884 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005885 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005886 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005887 psa_status_t status = mbedtls_psa_ecp_load_representation(
5888 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005889 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005890 private_key->key.data,
5891 private_key->key.bytes,
5892 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005893 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005894 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005895 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005896 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005897 shared_secret, shared_secret_size,
5898 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005899 mbedtls_ecp_keypair_free( ecp );
5900 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005901 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005902#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005903 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005904 (void) private_key;
5905 (void) peer_key;
5906 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005907 (void) shared_secret;
5908 (void) shared_secret_size;
5909 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005910 return( PSA_ERROR_NOT_SUPPORTED );
5911 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005912}
5913
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005914/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005915 * to potentially free embedded data structures and wipe confidential data.
5916 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005917static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005918 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005919 psa_key_slot_t *private_key,
5920 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005921 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005922{
5923 psa_status_t status;
5924 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5925 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005926 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005927
5928 /* Step 1: run the secret agreement algorithm to generate the shared
5929 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005930 status = psa_key_agreement_raw_internal( ka_alg,
5931 private_key,
5932 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005933 shared_secret,
5934 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005935 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005936 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005937 goto exit;
5938
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005939 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005940 * the shared secret. A shared secret is permitted wherever a key
5941 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005942 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005943 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005944 shared_secret,
5945 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005946exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005947 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005948 return( status );
5949}
5950
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005951psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005952 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005953 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005954 const uint8_t *peer_key,
5955 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005956{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005957 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005958 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005959 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005960
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005961 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005962 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005963 status = psa_get_and_lock_transparent_key_slot_with_policy(
5964 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005965 if( status != PSA_SUCCESS )
5966 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005968 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005969 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005970 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005971 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005972 else
5973 {
5974 /* If a private key has been added as SECRET, we allow the derived
5975 * key material to be used as a key in PSA Crypto. */
5976 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5977 operation->can_output_key = 1;
5978 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005979
Ronald Cron5c522922020-11-14 16:35:34 +01005980 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005981
Ronald Cron5c522922020-11-14 16:35:34 +01005982 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005983}
5984
Gilles Peskinebe697d82019-05-16 18:00:41 +02005985psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005986 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005987 const uint8_t *peer_key,
5988 size_t peer_key_length,
5989 uint8_t *output,
5990 size_t output_size,
5991 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005992{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005993 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005994 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005995 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005996
5997 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5998 {
5999 status = PSA_ERROR_INVALID_ARGUMENT;
6000 goto exit;
6001 }
Ronald Cron5c522922020-11-14 16:35:34 +01006002 status = psa_get_and_lock_transparent_key_slot_with_policy(
6003 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006004 if( status != PSA_SUCCESS )
6005 goto exit;
6006
6007 status = psa_key_agreement_raw_internal( alg, slot,
6008 peer_key, peer_key_length,
6009 output, output_size,
6010 output_length );
6011
6012exit:
6013 if( status != PSA_SUCCESS )
6014 {
6015 /* If an error happens and is not handled properly, the output
6016 * may be used as a key to protect sensitive data. Arrange for such
6017 * a key to be random, which is likely to result in decryption or
6018 * verification errors. This is better than filling the buffer with
6019 * some constant data such as zeros, which would result in the data
6020 * being protected with a reproducible, easily knowable key.
6021 */
6022 psa_generate_random( output, output_size );
6023 *output_length = output_size;
6024 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006025
Ronald Cron5c522922020-11-14 16:35:34 +01006026 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006027
Ronald Cron5c522922020-11-14 16:35:34 +01006028 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006029}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006030
6031
Gilles Peskine30524eb2020-11-13 17:02:26 +01006032
Gilles Peskine86a440b2018-11-12 18:39:40 +01006033/****************************************************************/
6034/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006035/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006036
Gilles Peskine30524eb2020-11-13 17:02:26 +01006037/** Initialize the PSA random generator.
6038 */
6039static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6040{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006041#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6042 memset( rng, 0, sizeof( *rng ) );
6043#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6044
Gilles Peskine30524eb2020-11-13 17:02:26 +01006045 /* Set default configuration if
6046 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6047 if( rng->entropy_init == NULL )
6048 rng->entropy_init = mbedtls_entropy_init;
6049 if( rng->entropy_free == NULL )
6050 rng->entropy_free = mbedtls_entropy_free;
6051
6052 rng->entropy_init( &rng->entropy );
6053#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6054 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6055 /* The PSA entropy injection feature depends on using NV seed as an entropy
6056 * source. Add NV seed as an entropy source for PSA entropy injection. */
6057 mbedtls_entropy_add_source( &rng->entropy,
6058 mbedtls_nv_seed_poll, NULL,
6059 MBEDTLS_ENTROPY_BLOCK_SIZE,
6060 MBEDTLS_ENTROPY_SOURCE_STRONG );
6061#endif
6062
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006063 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006064#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006065}
6066
6067/** Deinitialize the PSA random generator.
6068 */
6069static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6070{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006071#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6072 memset( rng, 0, sizeof( *rng ) );
6073#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006074 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006075 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006076#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006077}
6078
6079/** Seed the PSA random generator.
6080 */
6081static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6082{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006083#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6084 /* Do nothing: the external RNG seeds itself. */
6085 (void) rng;
6086 return( PSA_SUCCESS );
6087#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006088 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006089 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6090 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006091 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006092#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006093}
6094
Gilles Peskinee59236f2018-01-27 23:32:46 +01006095psa_status_t psa_generate_random( uint8_t *output,
6096 size_t output_size )
6097{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006098 GUARD_MODULE_INITIALIZED;
6099
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006100#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6101
6102 size_t output_length = 0;
6103 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6104 output, output_size,
6105 &output_length );
6106 if( status != PSA_SUCCESS )
6107 return( status );
6108 /* Breaking up a request into smaller chunks is currently not supported
6109 * for the extrernal RNG interface. */
6110 if( output_length != output_size )
6111 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6112 return( PSA_SUCCESS );
6113
6114#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6115
Gilles Peskine71ddab92021-01-04 21:01:07 +01006116 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006117 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006118 size_t request_size =
6119 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6120 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6121 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006122 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6123 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006124 if( ret != 0 )
6125 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006126 output_size -= request_size;
6127 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006128 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006129 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006130#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006131}
6132
Gilles Peskine8814fc42020-12-14 15:33:44 +01006133/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006134 *
6135 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6136 * `psa_generate_random(...)`. The state parameter is ignored since the
6137 * PSA API doesn't support passing an explicit state.
6138 *
6139 * In the non-external case, psa_generate_random() calls an
6140 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6141 * and semantics as mbedtls_psa_get_random(). As an optimization,
6142 * instead of doing this back-and-forth between the PSA API and the
6143 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6144 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006145 */
6146#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6147int mbedtls_psa_get_random( void *p_rng,
6148 unsigned char *output,
6149 size_t output_size )
6150{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006151 /* This function takes a pointer to the RNG state because that's what
6152 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6153 * its own state internally and doesn't let the caller access that state.
6154 * So we just ignore the state parameter, and in practice we'll pass
6155 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006156 (void) p_rng;
6157 psa_status_t status = psa_generate_random( output, output_size );
6158 if( status == PSA_SUCCESS )
6159 return( 0 );
6160 else
6161 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6162}
6163#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6164
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006165#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6166#include "mbedtls/entropy_poll.h"
6167
Gilles Peskine7228da22019-07-15 11:06:15 +02006168psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006169 size_t seed_size )
6170{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006171 if( global_data.initialized )
6172 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006173
6174 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6175 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6176 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6177 return( PSA_ERROR_INVALID_ARGUMENT );
6178
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006179 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006180}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006181#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006182
Ronald Cron3772afe2021-02-08 16:10:05 +01006183/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006184 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006185 * \param type The key type
6186 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006187 *
6188 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006189 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006190 * \retval #PSA_ERROR_INVALID_ARGUMENT
6191 * The size in bits of the key is not valid.
6192 * \retval #PSA_ERROR_NOT_SUPPORTED
6193 * The type and/or the size in bits of the key or the combination of
6194 * the two is not supported.
6195 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006196static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6197 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006198{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006199 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200
Gilles Peskinee59236f2018-01-27 23:32:46 +01006201 if( key_type_is_raw_bytes( type ) )
6202 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006203 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006204 if( status != PSA_SUCCESS )
6205 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006206 }
6207 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006208#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006209 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6210 {
6211 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6212 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006213
Ronald Cron01b2aba2020-10-05 09:42:02 +02006214 /* Accept only byte-aligned keys, for the same reasons as
6215 * in psa_import_rsa_key(). */
6216 if( bits % 8 != 0 )
6217 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006218 }
6219 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006220#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006221
Ronald Cron5c4d3862020-12-07 11:07:24 +01006222#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006223 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6224 {
Ronald Crond81ab562021-02-16 09:01:16 +01006225 /* To avoid empty block, return successfully here. */
6226 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006227 }
6228 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006229#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006230 {
6231 return( PSA_ERROR_NOT_SUPPORTED );
6232 }
6233
6234 return( PSA_SUCCESS );
6235}
6236
Ronald Cron55ed0592020-10-05 10:30:40 +02006237psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006238 const psa_key_attributes_t *attributes,
6239 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006240{
6241 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006242 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006243
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006244 if( ( attributes->domain_parameters == NULL ) &&
6245 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006246 return( PSA_ERROR_INVALID_ARGUMENT );
6247
Ronald Cron01b2aba2020-10-05 09:42:02 +02006248 if( key_type_is_raw_bytes( type ) )
6249 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006250 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006251 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006252 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006253
David Brown12ca5032021-02-11 11:02:00 -07006254#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006255 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006256 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006257#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006258 }
6259 else
6260
John Durkop07cc04a2020-11-16 22:08:34 -08006261#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006262 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006263 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006264 return( mbedtls_psa_rsa_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 Durkop07cc04a2020-11-16 22:08:34 -08006270#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006271
John Durkop9814fa22020-11-04 12:28:15 -08006272#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006273 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006274 {
Ronald Cron7023db52020-11-20 18:17:42 +01006275 return( mbedtls_psa_ecp_generate_key( attributes,
6276 key_buffer,
6277 key_buffer_size,
6278 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006279 }
6280 else
John Durkop9814fa22020-11-04 12:28:15 -08006281#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006282 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006283 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006284 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006285 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006286
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006287 return( PSA_SUCCESS );
6288}
Darryl Green0c6575a2018-11-07 16:05:30 +00006289
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006290psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006291 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006292{
6293 psa_status_t status;
6294 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006295 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006296 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006297
Ronald Cron81709fc2020-11-14 12:10:32 +01006298 *key = MBEDTLS_SVC_KEY_ID_INIT;
6299
Gilles Peskine0f84d622019-09-12 19:03:13 +02006300 /* Reject any attempt to create a zero-length key so that we don't
6301 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6302 if( psa_get_key_bits( attributes ) == 0 )
6303 return( PSA_ERROR_INVALID_ARGUMENT );
6304
Ronald Cron81709fc2020-11-14 12:10:32 +01006305 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6306 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006307 if( status != PSA_SUCCESS )
6308 goto exit;
6309
Ronald Cron977c2472020-10-13 08:32:21 +02006310 /* In the case of a transparent key or an opaque key stored in local
6311 * storage (thus not in the case of generating a key in a secure element
6312 * or cryptoprocessor with storage), we have to allocate a buffer to
6313 * hold the generated key material. */
6314 if( slot->key.data == NULL )
6315 {
6316 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6317 PSA_KEY_LOCATION_LOCAL_STORAGE )
6318 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006319 status = psa_validate_key_type_and_size_for_key_generation(
6320 attributes->core.type, attributes->core.bits );
6321 if( status != PSA_SUCCESS )
6322 goto exit;
6323
6324 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6325 attributes->core.type,
6326 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006327 }
6328 else
6329 {
6330 status = psa_driver_wrapper_get_key_buffer_size(
6331 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006332 if( status != PSA_SUCCESS )
6333 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006334 }
Ronald Cron977c2472020-10-13 08:32:21 +02006335
6336 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6337 if( status != PSA_SUCCESS )
6338 goto exit;
6339 }
6340
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006341 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006342 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006343
Ronald Cron2b56bc82020-10-05 10:02:26 +02006344 if( status != PSA_SUCCESS )
6345 psa_remove_key_data_from_memory( slot );
6346
Gilles Peskine11792082019-08-06 18:36:36 +02006347exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006348 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006349 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006350 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006351 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006352
Darryl Green0c6575a2018-11-07 16:05:30 +00006353 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006354}
6355
Gilles Peskinee59236f2018-01-27 23:32:46 +01006356/****************************************************************/
6357/* Module setup */
6358/****************************************************************/
6359
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006360#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006361psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6362 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6363 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6364{
6365 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6366 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006367 global_data.rng.entropy_init = entropy_init;
6368 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006369 return( PSA_SUCCESS );
6370}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006371#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006372
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373void mbedtls_psa_crypto_free( void )
6374{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006375 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006376 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6377 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006378 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006379 }
6380 /* Wipe all remaining data, including configuration.
6381 * In particular, this sets all state indicator to the value
6382 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006383 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006384#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006385 /* Unregister all secure element drivers, so that we restart from
6386 * a pristine state. */
6387 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006388#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006389}
6390
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006391#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6392/** Recover a transaction that was interrupted by a power failure.
6393 *
6394 * This function is called during initialization, before psa_crypto_init()
6395 * returns. If this function returns a failure status, the initialization
6396 * fails.
6397 */
6398static psa_status_t psa_crypto_recover_transaction(
6399 const psa_crypto_transaction_t *transaction )
6400{
6401 switch( transaction->unknown.type )
6402 {
6403 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6404 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006405 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006406 * is implemented.
6407 * https://github.com/ARMmbed/mbed-crypto/issues/218
6408 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006409 default:
6410 /* We found an unsupported transaction in the storage.
6411 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006412 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006413 }
6414}
6415#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6416
Gilles Peskinee59236f2018-01-27 23:32:46 +01006417psa_status_t psa_crypto_init( void )
6418{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006419 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006420
Gilles Peskinec6b69072018-11-20 21:42:52 +01006421 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006422 if( global_data.initialized != 0 )
6423 return( PSA_SUCCESS );
6424
Gilles Peskine30524eb2020-11-13 17:02:26 +01006425 /* Initialize and seed the random generator. */
6426 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006427 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006428 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006429 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006430 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006431 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432
Gilles Peskine66fb1262018-12-10 16:29:04 +01006433 status = psa_initialize_key_slots( );
6434 if( status != PSA_SUCCESS )
6435 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006436
Gilles Peskined9348f22019-10-01 15:22:29 +02006437#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6438 status = psa_init_all_se_drivers( );
6439 if( status != PSA_SUCCESS )
6440 goto exit;
6441#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6442
Gilles Peskine4b734222019-07-24 15:56:31 +02006443#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006444 status = psa_crypto_load_transaction( );
6445 if( status == PSA_SUCCESS )
6446 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006447 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6448 if( status != PSA_SUCCESS )
6449 goto exit;
6450 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006451 }
6452 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6453 {
6454 /* There's no transaction to complete. It's all good. */
6455 status = PSA_SUCCESS;
6456 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006457#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006458
Gilles Peskinec6b69072018-11-20 21:42:52 +01006459 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006460 global_data.initialized = 1;
6461
6462exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006463 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006464 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006465 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466}
6467
6468#endif /* MBEDTLS_PSA_CRYPTO_C */