blob: 6b05715121c69afea423e6653490fa65266adbe5 [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
Bence Szépkúti86974652020-06-15 11:59:37 +02004/*
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Gilles Peskinee59236f2018-01-27 23:32:46 +01006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Gilles Peskinee59236f2018-01-27 23:32:46 +010019 */
20
Gilles Peskinedb09ef62020-06-03 01:43:33 +020021#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010022
23#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030024
John Durkop07cc04a2020-11-16 22:08:34 -080025#if defined(MBEDTLS_PSA_CRYPTO_CONFIG)
26#include "check_crypto_config.h"
27#endif
28
itayzafrir7723ab12019-02-14 10:28:02 +020029#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010030#include "psa/crypto.h"
31
Gilles Peskine039b90c2018-12-07 18:24:41 +010032#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010033#include "psa_crypto_invasive.h"
Steven Cooremancd84cb42020-07-16 20:28:36 +020034#include "psa_crypto_driver_wrappers.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010035#include "psa_crypto_ecp.h"
36#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010037#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020039#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020040#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010041#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010042/* Include internal declarations that are useful for implementing persistently
43 * stored keys. */
44#include "psa_crypto_storage.h"
45
Gilles Peskineb2b64d32020-12-14 16:43:58 +010046#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010047
Gilles Peskineb46bef22019-07-30 21:32:04 +020048#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#include <stdlib.h>
50#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010051#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020052#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010053#define mbedtls_calloc calloc
54#define mbedtls_free free
55#endif
56
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010057#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010058#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020059#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000060#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020061#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010062#include "mbedtls/blowfish.h"
63#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020064#include "mbedtls/chacha20.h"
65#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/cipher.h"
67#include "mbedtls/ccm.h"
68#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020070#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010071#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010072#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010073#include "mbedtls/error.h"
74#include "mbedtls/gcm.h"
75#include "mbedtls/md2.h"
76#include "mbedtls/md4.h"
77#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010078#include "mbedtls/md.h"
79#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010080#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010082#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000083#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010084#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010085#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010086#include "mbedtls/sha1.h"
87#include "mbedtls/sha256.h"
88#include "mbedtls/sha512.h"
89#include "mbedtls/xtea.h"
90
Gilles Peskine996deb12018-08-01 15:45:45 +020091#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
92
Gilles Peskine9ef733f2018-02-07 21:05:37 +010093/* constant-time buffer comparison */
94static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
95{
96 size_t i;
97 unsigned char diff = 0;
98
99 for( i = 0; i < n; i++ )
100 diff |= a[i] ^ b[i];
101
102 return( diff );
103}
104
105
106
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100107/****************************************************************/
108/* Global data, support functions and library management */
109/****************************************************************/
110
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111static int key_type_is_raw_bytes( psa_key_type_t type )
112{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200113 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200114}
115
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100116/* Values for psa_global_data_t::rng_state */
117#define RNG_NOT_INITIALIZED 0
118#define RNG_INITIALIZED 1
119#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100120
Gilles Peskine2d277862018-06-18 15:41:12 +0200121typedef struct
122{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100123 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100124 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100125 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100126} psa_global_data_t;
127
128static psa_global_data_t global_data;
129
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100130#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
131mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
132 &global_data.rng.drbg;
133#endif
134
itayzafrir0adf0fc2018-09-06 16:24:41 +0300135#define GUARD_MODULE_INITIALIZED \
136 if( global_data.initialized == 0 ) \
137 return( PSA_ERROR_BAD_STATE );
138
Steven Cooreman01164162020-07-20 15:31:37 +0200139psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100140{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100141 /* Mbed TLS error codes can combine a high-level error code and a
142 * low-level error code. The low-level error usually reflects the
143 * root cause better, so dispatch on that preferably. */
144 int low_level_ret = - ( -ret & 0x007f );
145 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100146 {
147 case 0:
148 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100149
150 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
151 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
152 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
153 return( PSA_ERROR_NOT_SUPPORTED );
154 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
155 return( PSA_ERROR_HARDWARE_FAILURE );
156
157 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
158 return( PSA_ERROR_HARDWARE_FAILURE );
159
Gilles Peskine9a944802018-06-21 09:35:35 +0200160 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
161 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
162 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
163 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
164 case MBEDTLS_ERR_ASN1_INVALID_DATA:
165 return( PSA_ERROR_INVALID_ARGUMENT );
166 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
167 return( PSA_ERROR_INSUFFICIENT_MEMORY );
168 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
169 return( PSA_ERROR_BUFFER_TOO_SMALL );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
Jaeden Amero93e21112019-02-20 13:57:28 +0000181#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000182 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000183#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
184 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
185#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100186 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
187 return( PSA_ERROR_NOT_SUPPORTED );
188 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
189 return( PSA_ERROR_HARDWARE_FAILURE );
190
191 case MBEDTLS_ERR_CCM_BAD_INPUT:
192 return( PSA_ERROR_INVALID_ARGUMENT );
193 case MBEDTLS_ERR_CCM_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
196 return( PSA_ERROR_HARDWARE_FAILURE );
197
Gilles Peskine26869f22019-05-06 15:25:00 +0200198 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200
201 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
202 return( PSA_ERROR_BAD_STATE );
203 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
204 return( PSA_ERROR_INVALID_SIGNATURE );
205
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
207 return( PSA_ERROR_NOT_SUPPORTED );
208 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
209 return( PSA_ERROR_INVALID_ARGUMENT );
210 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
211 return( PSA_ERROR_INSUFFICIENT_MEMORY );
212 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
213 return( PSA_ERROR_INVALID_PADDING );
214 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200215 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100216 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
217 return( PSA_ERROR_INVALID_SIGNATURE );
218 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200219 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
221 return( PSA_ERROR_HARDWARE_FAILURE );
222
223 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
224 return( PSA_ERROR_HARDWARE_FAILURE );
225
Gilles Peskine82e57d12020-11-13 21:31:17 +0100226#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
227 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100228 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
229 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100230 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
232 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
233 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
234 return( PSA_ERROR_NOT_SUPPORTED );
235 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
236 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100237#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
239 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
240 return( PSA_ERROR_NOT_SUPPORTED );
241 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskinee59236f2018-01-27 23:32:46 +0100244 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
245 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
246 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248
249 case MBEDTLS_ERR_GCM_AUTH_FAILED:
250 return( PSA_ERROR_INVALID_SIGNATURE );
251 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200252 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100253 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
254 return( PSA_ERROR_HARDWARE_FAILURE );
255
Gilles Peskine82e57d12020-11-13 21:31:17 +0100256#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
257 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100258 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
259 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100260 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
263 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
264 return( PSA_ERROR_NOT_SUPPORTED );
265 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
266 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
267#endif
268
Gilles Peskinea5905292018-02-07 20:59:33 +0100269 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
270 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
272 return( PSA_ERROR_HARDWARE_FAILURE );
273
274 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
275 return( PSA_ERROR_NOT_SUPPORTED );
276 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
277 return( PSA_ERROR_INVALID_ARGUMENT );
278 case MBEDTLS_ERR_MD_ALLOC_FAILED:
279 return( PSA_ERROR_INSUFFICIENT_MEMORY );
280 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
283 return( PSA_ERROR_HARDWARE_FAILURE );
284
Gilles Peskinef76aa772018-10-29 19:24:33 +0100285 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
286 return( PSA_ERROR_STORAGE_FAILURE );
287 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
290 return( PSA_ERROR_INVALID_ARGUMENT );
291 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
292 return( PSA_ERROR_BUFFER_TOO_SMALL );
293 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
294 return( PSA_ERROR_INVALID_ARGUMENT );
295 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
298 return( PSA_ERROR_INVALID_ARGUMENT );
299 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
300 return( PSA_ERROR_INSUFFICIENT_MEMORY );
301
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100302 case MBEDTLS_ERR_PK_ALLOC_FAILED:
303 return( PSA_ERROR_INSUFFICIENT_MEMORY );
304 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
305 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100308 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100309 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
310 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
313 return( PSA_ERROR_NOT_SUPPORTED );
314 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
315 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
316 return( PSA_ERROR_NOT_PERMITTED );
317 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
318 return( PSA_ERROR_INVALID_ARGUMENT );
319 case MBEDTLS_ERR_PK_INVALID_ALG:
320 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
321 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
322 return( PSA_ERROR_NOT_SUPPORTED );
323 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
324 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100325 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
Gilles Peskineff2d2002019-05-06 15:26:23 +0200328 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
331 return( PSA_ERROR_NOT_SUPPORTED );
332
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_RSA_INVALID_PADDING:
339 return( PSA_ERROR_INVALID_PADDING );
340 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
341 return( PSA_ERROR_HARDWARE_FAILURE );
342 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
343 return( PSA_ERROR_INVALID_ARGUMENT );
344 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
345 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200346 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100347 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
348 return( PSA_ERROR_INVALID_SIGNATURE );
349 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
350 return( PSA_ERROR_BUFFER_TOO_SMALL );
351 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100352 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100353 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
354 return( PSA_ERROR_NOT_SUPPORTED );
355 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
359 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
360 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
363 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
364 return( PSA_ERROR_INVALID_ARGUMENT );
365 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
366 return( PSA_ERROR_HARDWARE_FAILURE );
367
itayzafrir5c753392018-05-08 11:18:38 +0300368 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300369 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300370 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300371 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
372 return( PSA_ERROR_BUFFER_TOO_SMALL );
373 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
374 return( PSA_ERROR_NOT_SUPPORTED );
375 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
376 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
377 return( PSA_ERROR_INVALID_SIGNATURE );
378 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
379 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100380 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
381 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300382 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
383 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100384
Janos Follatha13b9052019-11-22 12:48:59 +0000385 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
386 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300387
Gilles Peskinee59236f2018-01-27 23:32:46 +0100388 default:
David Saadab4ecc272019-02-14 13:48:10 +0200389 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100390 }
391}
392
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200393
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200394
395
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100396/****************************************************************/
397/* Key management */
398/****************************************************************/
399
Gilles Peskine73167e12019-07-12 23:44:37 +0200400#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
401static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
402{
Gilles Peskine8e338702019-07-30 20:06:31 +0200403 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200404}
405#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
406
John Durkop07cc04a2020-11-16 22:08:34 -0800407/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
408 * current test driver in key_management.c is using this function
409 * when accelerators are used for ECC key pair and public key.
410 * Once that dependency is resolved these guards can be removed.
411 */
John Durkop9814fa22020-11-04 12:28:15 -0800412#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800413 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
414 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
415 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100417 size_t bits,
418 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200419{
420 switch( curve )
421 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100423 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100424 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100425 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100426 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100429 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100430 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100433 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100434 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 528:
436 if( bits_is_sloppy )
437 return( MBEDTLS_ECP_DP_SECP521R1 );
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439 }
440 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100441
Paul Elliott8ff510a2020-06-02 17:19:28 +0100442 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100447 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100448 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100449 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100451 }
452 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100453
Paul Elliott8ff510a2020-06-02 17:19:28 +0100454 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100455 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100456 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100459 case 256:
460 if( bits_is_sloppy )
461 return( MBEDTLS_ECP_DP_CURVE25519 );
462 break;
463 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100464 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100465 }
466 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100467
Paul Elliott8ff510a2020-06-02 17:19:28 +0100468 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100469 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100470 {
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100471 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100472 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100473 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100474 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskine228abc52019-12-03 17:24:19 +0100477 }
478 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100480
481 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200482}
John Durkop9814fa22020-11-04 12:28:15 -0800483#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800484 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
485 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
486 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200487
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200488static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
489 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490{
491 /* Check that the bit size is acceptable for the key type */
492 switch( type )
493 {
494 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200495 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200496 case PSA_KEY_TYPE_DERIVE:
497 break;
David Browne04acc22021-01-26 11:39:16 -0700498#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 case PSA_KEY_TYPE_AES:
500 if( bits != 128 && bits != 192 && bits != 256 )
501 return( PSA_ERROR_INVALID_ARGUMENT );
502 break;
503#endif
David Browne04acc22021-01-26 11:39:16 -0700504#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200505 case PSA_KEY_TYPE_CAMELLIA:
506 if( bits != 128 && bits != 192 && bits != 256 )
507 return( PSA_ERROR_INVALID_ARGUMENT );
508 break;
509#endif
David Browne04acc22021-01-26 11:39:16 -0700510#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 case PSA_KEY_TYPE_DES:
512 if( bits != 64 && bits != 128 && bits != 192 )
513 return( PSA_ERROR_INVALID_ARGUMENT );
514 break;
515#endif
David Browne04acc22021-01-26 11:39:16 -0700516#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 case PSA_KEY_TYPE_ARC4:
518 if( bits < 8 || bits > 2048 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
520 break;
521#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700522#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200523 case PSA_KEY_TYPE_CHACHA20:
524 if( bits != 256 )
525 return( PSA_ERROR_INVALID_ARGUMENT );
526 break;
527#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200528 default:
529 return( PSA_ERROR_NOT_SUPPORTED );
530 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200531 if( bits % 8 != 0 )
532 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200533
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200534 return( PSA_SUCCESS );
535}
536
Gilles Peskineb46bef22019-07-30 21:32:04 +0200537/** Return the size of the key in the given slot, in bits.
538 *
539 * \param[in] slot A key slot.
540 *
541 * \return The key size in bits, read from the metadata in the slot.
542 */
543static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
544{
545 return( slot->attr.bits );
546}
547
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100548/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100549 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100550 * Upon successful return of this function, the behavioud of #PSA_MAC_LENGTH
551 * will be defined when called with the validated \p algorithm and \p key_type
552 *
553 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100554 * \param[in] key_type The key type of the key to be used with the
555 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100556 *
557 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100558 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100559 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100560 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100561 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100562MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100563 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100564 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100565{
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100566 if( PSA_ALG_IS_HMAC( algorithm ) ) {
567 if( key_type == PSA_KEY_TYPE_HMAC )
568 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100569 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100570
571 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100572 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100573 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
574 * key. */
575 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
576 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
577 {
578 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
579 * the block length (larger than 1) for block ciphers. */
580 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
581 return( PSA_SUCCESS );
582 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100584
585 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100586}
587
Steven Cooreman75b74362020-07-28 14:30:13 +0200588/** Try to allocate a buffer to an empty key slot.
589 *
590 * \param[in,out] slot Key slot to attach buffer to.
591 * \param[in] buffer_length Requested size of the buffer.
592 *
593 * \retval #PSA_SUCCESS
594 * The buffer has been successfully allocated.
595 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
596 * Not enough memory was available for allocation.
597 * \retval #PSA_ERROR_ALREADY_EXISTS
598 * Trying to allocate a buffer to a non-empty key slot.
599 */
600static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
601 size_t buffer_length )
602{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100603 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200604 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200605
Ronald Cronea0f8a62020-11-25 17:52:23 +0100606 slot->key.data = mbedtls_calloc( 1, buffer_length );
607 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200608 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200609
Ronald Cronea0f8a62020-11-25 17:52:23 +0100610 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200611 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200612}
613
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200614psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
615 const uint8_t* data,
616 size_t data_length )
617{
618 psa_status_t status = psa_allocate_buffer_to_slot( slot,
619 data_length );
620 if( status != PSA_SUCCESS )
621 return( status );
622
Ronald Cronea0f8a62020-11-25 17:52:23 +0100623 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200624 return( PSA_SUCCESS );
625}
626
Ronald Crona0fe59f2020-11-29 11:14:53 +0100627psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100628 const psa_key_attributes_t *attributes,
629 const uint8_t *data, size_t data_length,
630 uint8_t *key_buffer, size_t key_buffer_size,
631 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100632{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100633 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
634 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100635
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200636 /* zero-length keys are never supported. */
637 if( data_length == 0 )
638 return( PSA_ERROR_NOT_SUPPORTED );
639
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100640 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100641 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100642 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200643
Steven Cooreman75b74362020-07-28 14:30:13 +0200644 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
645 if( data_length > SIZE_MAX / 8 )
646 return( PSA_ERROR_NOT_SUPPORTED );
647
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200648 /* Enforce a size limit, and in particular ensure that the bit
649 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100650 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200651 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200652
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100653 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200654 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200655 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200656
Ronald Crondd04d422020-11-28 15:14:42 +0100657 /* Copy the key material. */
658 memcpy( key_buffer, data, data_length );
659 *key_buffer_length = data_length;
660 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200661
Steven Cooreman40120f62020-10-29 11:42:22 +0100662 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100664 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200665 {
John Durkop9814fa22020-11-04 12:28:15 -0800666#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
667 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100668 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200669 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100670 return( mbedtls_psa_ecp_import_key( attributes,
671 data, data_length,
672 key_buffer, key_buffer_size,
673 key_buffer_length,
674 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100675 }
John Durkop9814fa22020-11-04 12:28:15 -0800676#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
677 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700678#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
679 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100680 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200681 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100682 return( mbedtls_psa_rsa_import_key( attributes,
683 data, data_length,
684 key_buffer, key_buffer_size,
685 key_buffer_length,
686 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200687 }
John Durkop9814fa22020-11-04 12:28:15 -0800688#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
689 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100690 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100691
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100692 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100693}
694
Gilles Peskinef603c712019-01-19 13:40:11 +0100695/** Calculate the intersection of two algorithm usage policies.
696 *
697 * Return 0 (which allows no operation) on incompatibility.
698 */
699static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100700 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100701 psa_algorithm_t alg1,
702 psa_algorithm_t alg2 )
703{
Gilles Peskine549ea862019-05-22 11:45:59 +0200704 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100705 if( alg1 == alg2 )
706 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100707 /* If the policies are from the same hash-and-sign family, check
708 * if one is a wildcard. If so the other has the specific algorithm. */
709 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
710 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
711 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100712 {
Steven Cooreman03488022021-02-18 12:07:20 +0100713 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
714 return( alg2 );
715 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
716 return( alg1 );
717 }
718 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100719 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100720 * restrictive tag length. */
721 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
722 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
723 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
724 {
725 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
726 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
727 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100728
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100729 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100730 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
731 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100732 {
Steven Cooreman5d814812021-02-18 12:11:39 +0100733 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100734 }
735 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100736 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100737 ( alg1_len <= alg2_len ) )
738 {
739 return( alg2 );
740 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100741 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100742 ( alg2_len <= alg1_len ) )
743 {
744 return( alg1 );
745 }
746 }
747 /* If the policies are from the same MAC family, check whether one
748 * of them is a minimum-MAC-length policy. Calculate the most
749 * restrictive tag length. */
750 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
751 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
752 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
753 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100754 /* Validate the combination of key type and algorithm. Since the base
755 * algorithm of alg1 and alg2 are the same, we only need this once. */
756 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100757 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100758
Steven Cooreman31a876d2021-03-03 20:47:40 +0100759 /* Get the (exact or at-least) output lengths for both sides of the
760 * requested intersection. None of the currently supported algorithms
761 * have an output length dependent on the actual key size, so setting it
762 * to a bogus value of 0 is currently OK.
763 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100764 * Note that for at-least-this-length wildcard algorithms, the output
765 * length is set to the shortest allowed length, which allows us to
766 * calculate the most restrictive tag length for the intersection. */
767 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
768 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooreman03488022021-02-18 12:07:20 +0100769 size_t max_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100770
Steven Cooremana1d83222021-02-25 10:20:29 +0100771 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100772 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
773 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100774 {
Steven Cooremancaad4932021-02-18 11:28:17 +0100775 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, max_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100776 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100777
778 /* If only one is an at-least-this-length policy, the intersection would
779 * be the other (fixed-length) policy as long as said fixed length is
780 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100781 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100782 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100783 if( alg1_len <= alg2_len )
784 return( alg2 );
785 else
786 return( 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100787 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100788 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100789 {
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100790 if( alg2_len <= alg1_len )
791 return( alg1 );
792 else
793 return( 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100794 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100795
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100796 /* If none of them are wildcards, check whether this is a case of one
797 * specifying the default length and the other a specific length. If the
798 * specific length equals the default length for this key type, the
799 * intersection would be the specific-length algorithm. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100800 if( alg1_len == alg2_len )
801 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100802 }
803 /* If the policies are incompatible, allow nothing. */
804 return( 0 );
805}
806
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100807static int psa_key_algorithm_permits( psa_key_type_t key_type,
808 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200809 psa_algorithm_t requested_alg )
810{
Gilles Peskine549ea862019-05-22 11:45:59 +0200811 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200812 if( requested_alg == policy_alg )
813 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100814 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
815 * and requested_alg is the same hash-and-sign family with any hash,
816 * then requested_alg is compliant with policy_alg. */
817 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
818 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200819 {
Steven Cooreman03488022021-02-18 12:07:20 +0100820 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
821 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
822 }
823 /* If policy_alg is a wildcard AEAD algorithm of the same base as
824 * the requested algorithm, check the requested tag length to be
825 * equal-length or longer than the wildcard-specified length. */
826 if( PSA_ALG_IS_AEAD( policy_alg ) &&
827 PSA_ALG_IS_AEAD( requested_alg ) &&
828 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
829 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100830 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100831 {
832 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
833 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
834 }
Steven Cooreman03488022021-02-18 12:07:20 +0100835 if( PSA_ALG_IS_MAC( policy_alg ) &&
836 PSA_ALG_IS_MAC( requested_alg ) &&
837 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100838 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100839 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100840 /* Validate the combination of key type and algorithm. Since the policy
841 * and requested algorithms are the same, we only need this once. */
842 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100843 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100844
Steven Cooreman31a876d2021-03-03 20:47:40 +0100845 /* Get both the requested output length for the algorithm which is to be
846 * verified, and the default output length for the base algorithm.
847 * Note that none of the currently supported algorithms have an output
848 * length dependent on actual key size, so setting it to a bogus value
849 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100850 size_t requested_output_length = PSA_MAC_LENGTH(
851 key_type, 0, requested_alg );
852 size_t default_output_length = PSA_MAC_LENGTH(
853 key_type, 0,
854 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100855
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100856 /* If the policy is default-length, only allow an algorithm with
857 * a declared exact-length matching the default. */
858 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100859 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100860
861 /* If the requested algorithm is default-length, allow it if the policy
862 * is exactly the default length. */
863 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
864 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
865 {
866 return( 1 );
867 }
868
Steven Cooreman31a876d2021-03-03 20:47:40 +0100869 /* If policy_alg is an at-least-this-length wildcard MAC algorithm of
870 * the same base as the requested algorithm, check for the requested MAC
871 * length to be equal to or longer than the minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100872 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
873 {
874 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100875 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100876 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 }
Steven Cooremance48e852020-10-05 16:02:45 +0200878 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200879 * a key derivation with that key agreement should also be allowed. This
880 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200881 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
882 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
883 {
884 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
885 policy_alg );
886 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100887 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200888 return( 0 );
889}
890
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100891/** Test whether a policy permits an algorithm.
892 *
893 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100894 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100895 * \note This function requires providing the key type for which the policy is
896 * being validated, since some algorithm policy definitions (e.g. MAC)
897 * have different properties depending on what kind of cipher it is
898 * combined with.
899 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100900 * \retval PSA_SUCCESS When \p alg is a specific algorithm
901 * allowed by the \p policy.
902 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
903 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
904 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100905 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100906static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100907 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100908 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100909{
Steven Cooremand788fab2021-02-25 11:29:17 +0100910 /* '0' is not a valid algorithm */
911 if( alg == 0 )
912 return( PSA_ERROR_INVALID_ARGUMENT );
913
Steven Cooreman7e39f052021-02-23 14:18:32 +0100914 /* A requested algorithm cannot be a wildcard. */
915 if( PSA_ALG_IS_WILDCARD( alg ) )
916 return( PSA_ERROR_INVALID_ARGUMENT );
917
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100918 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
919 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100920 return( PSA_SUCCESS );
921 else
922 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100923}
924
Gilles Peskinef603c712019-01-19 13:40:11 +0100925/** Restrict a key policy based on a constraint.
926 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100927 * \note This function requires providing the key type for which the policy is
928 * being restricted, since some algorithm policy definitions (e.g. MAC)
929 * have different properties depending on what kind of cipher it is
930 * combined with.
931 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100932 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100933 * \param[in,out] policy The policy to restrict.
934 * \param[in] constraint The policy constraint to apply.
935 *
936 * \retval #PSA_SUCCESS
937 * \c *policy contains the intersection of the original value of
938 * \c *policy and \c *constraint.
939 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100940 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100941 * \c *policy is unchanged.
942 */
943static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100944 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100945 psa_key_policy_t *policy,
946 const psa_key_policy_t *constraint )
947{
948 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100949 psa_key_policy_algorithm_intersection( key_type, policy->alg,
950 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200951 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100952 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
953 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100954 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
955 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200956 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
957 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100958 policy->usage &= constraint->usage;
959 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200960 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100961 return( PSA_SUCCESS );
962}
963
Ronald Cron5c522922020-11-14 16:35:34 +0100964/** Get the description of a key given its identifier and policy constraints
965 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200966 *
Ronald Cron5c522922020-11-14 16:35:34 +0100967 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100968 * nonzero, the key must allow operations with this algorithm. If \p alg is
969 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100970 *
Ronald Cron5c522922020-11-14 16:35:34 +0100971 * In case of a persistent key, the function loads the description of the key
972 * into a key slot if not already done.
973 *
974 * On success, the returned key slot is locked. It is the responsibility of
975 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200976 */
Ronald Cron5c522922020-11-14 16:35:34 +0100977static psa_status_t psa_get_and_lock_key_slot_with_policy(
978 mbedtls_svc_key_id_t key,
979 psa_key_slot_t **p_slot,
980 psa_key_usage_t usage,
981 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +0100982{
Ronald Cronf95a2b12020-10-22 15:24:49 +0200983 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
984 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100985
Ronald Cron5c522922020-11-14 16:35:34 +0100986 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100987 if( status != PSA_SUCCESS )
988 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +0200989 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +0100990
991 /* Enforce that usage policy for the key slot contains all the flags
992 * required by the usage parameter. There is one exception: public
993 * keys can always be exported, so we treat public key objects as
994 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200995 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100996 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +0200997
Gilles Peskine8e338702019-07-30 20:06:31 +0200998 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +0100999 {
1000 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001002 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001003
1004 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001005 if( alg != 0 )
1006 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001007 status = psa_key_policy_permits( &slot->attr.policy,
1008 slot->attr.type,
1009 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001010 if( status != PSA_SUCCESS )
1011 goto error;
1012 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001013
Darryl Green06fd18d2018-07-16 11:21:11 +01001014 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001015
1016error:
1017 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001018 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001019
1020 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001021}
Darryl Green940d72c2018-07-13 13:18:51 +01001022
Ronald Cron5c522922020-11-14 16:35:34 +01001023/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001024 *
1025 * A transparent key is a key for which the key material is directly
1026 * available, as opposed to a key in a secure element.
1027 *
Ronald Cron5c522922020-11-14 16:35:34 +01001028 * This is a temporary function to use instead of
1029 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1030 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001031 *
Ronald Cron5c522922020-11-14 16:35:34 +01001032 * On success, the returned key slot is locked. It is the responsibility of the
1033 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001034 */
1035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001036static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1037 mbedtls_svc_key_id_t key,
1038 psa_key_slot_t **p_slot,
1039 psa_key_usage_t usage,
1040 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001041{
Ronald Cron5c522922020-11-14 16:35:34 +01001042 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1043 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001044 if( status != PSA_SUCCESS )
1045 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001046
Gilles Peskineadad8132019-07-25 11:31:23 +02001047 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001048 {
Ronald Cron5c522922020-11-14 16:35:34 +01001049 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001050 *p_slot = NULL;
1051 return( PSA_ERROR_NOT_SUPPORTED );
1052 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001053
Gilles Peskine28f8f302019-07-24 13:30:31 +02001054 return( PSA_SUCCESS );
1055}
1056#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1057/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001058#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1059 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001060#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1061
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001062/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001063static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001064{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001065 /* Data pointer will always be either a valid pointer or NULL in an
1066 * initialized slot, so we can just free it. */
1067 if( slot->key.data != NULL )
1068 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1069
1070 mbedtls_free( slot->key.data );
1071 slot->key.data = NULL;
1072 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001073
1074 return( PSA_SUCCESS );
1075}
1076
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001077/** Completely wipe a slot in memory, including its policy.
1078 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001079psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001080{
1081 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001082
1083 /*
1084 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001085 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001086 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1087 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001088 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001089 */
Ronald Cron5c522922020-11-14 16:35:34 +01001090 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001091 {
1092#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001093 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001094#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001095 status = PSA_ERROR_CORRUPTION_DETECTED;
1096 }
1097
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001098 /* Multipart operations may still be using the key. This is safe
1099 * because all multipart operation objects are independent from
1100 * the key slot: if they need to access the key after the setup
1101 * phase, they have a copy of the key. Note that this means that
1102 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001103 /* At this point, key material and other type-specific content has
1104 * been wiped. Clear remaining metadata. We can call memset and not
1105 * zeroize because the metadata is not particularly sensitive. */
1106 memset( slot, 0, sizeof( *slot ) );
1107 return( status );
1108}
1109
Ronald Croncf56a0a2020-08-04 09:51:30 +02001110psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001111{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001112 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001113 psa_status_t status; /* status of the last operation */
1114 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001115#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1116 psa_se_drv_table_entry_t *driver;
1117#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001118
Ronald Croncf56a0a2020-08-04 09:51:30 +02001119 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001120 return( PSA_SUCCESS );
1121
Ronald Cronf2911112020-10-29 17:51:10 +01001122 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001123 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001124 * key, this will load the key description from persistent memory if not
1125 * done yet. We cannot avoid this loading as without it we don't know if
1126 * the key is operated by an SE or not and this information is needed by
1127 * the current implementation.
1128 */
Ronald Cron5c522922020-11-14 16:35:34 +01001129 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001130 if( status != PSA_SUCCESS )
1131 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001132
Ronald Cronf2911112020-10-29 17:51:10 +01001133 /*
1134 * If the key slot containing the key description is under access by the
1135 * library (apart from the present access), the key cannot be destroyed
1136 * yet. For the time being, just return in error. Eventually (to be
1137 * implemented), the key should be destroyed when all accesses have
1138 * stopped.
1139 */
Ronald Cron5c522922020-11-14 16:35:34 +01001140 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001141 {
Ronald Cron5c522922020-11-14 16:35:34 +01001142 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001143 return( PSA_ERROR_GENERIC_ERROR );
1144 }
1145
Gilles Peskine354f7672019-07-12 23:46:38 +02001146#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001147 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001148 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001149 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001150 /* For a key in a secure element, we need to do three things:
1151 * remove the key file in internal storage, destroy the
1152 * key inside the secure element, and update the driver's
1153 * persistent data. Start a transaction that will encompass these
1154 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001155 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001156 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001157 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001158 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001159 status = psa_crypto_save_transaction( );
1160 if( status != PSA_SUCCESS )
1161 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001162 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001163 /* We should still try to destroy the key in the secure
1164 * element and the key metadata in storage. This is especially
1165 * important if the error is that the storage is full.
1166 * But how to do it exactly without risking an inconsistent
1167 * state after a reset?
1168 * https://github.com/ARMmbed/mbed-crypto/issues/215
1169 */
1170 overall_status = status;
1171 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001172 }
1173
Ronald Cronea0f8a62020-11-25 17:52:23 +01001174 status = psa_destroy_se_key( driver,
1175 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001176 if( overall_status == PSA_SUCCESS )
1177 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001178 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001179#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1180
Darryl Greend49a4992018-06-18 17:27:26 +01001181#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001182 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001183 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001184 status = psa_destroy_persistent_key( slot->attr.id );
1185 if( overall_status == PSA_SUCCESS )
1186 overall_status = status;
1187
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001188 /* TODO: other slots may have a copy of the same key. We should
1189 * invalidate them.
1190 * https://github.com/ARMmbed/mbed-crypto/issues/214
1191 */
Darryl Greend49a4992018-06-18 17:27:26 +01001192 }
1193#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001194
Gilles Peskinefc762652019-07-22 19:30:34 +02001195#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1196 if( driver != NULL )
1197 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001198 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001199 if( overall_status == PSA_SUCCESS )
1200 overall_status = status;
1201 status = psa_crypto_stop_transaction( );
1202 if( overall_status == PSA_SUCCESS )
1203 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001204 }
1205#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1206
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001207#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1208exit:
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001210 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001211 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1212 if( overall_status == PSA_SUCCESS )
1213 overall_status = status;
1214 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001215}
1216
John Durkop07cc04a2020-11-16 22:08:34 -08001217#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001218 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001219static psa_status_t psa_get_rsa_public_exponent(
1220 const mbedtls_rsa_context *rsa,
1221 psa_key_attributes_t *attributes )
1222{
1223 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001224 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001225 uint8_t *buffer = NULL;
1226 size_t buflen;
1227 mbedtls_mpi_init( &mpi );
1228
1229 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1230 if( ret != 0 )
1231 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001232 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1233 {
1234 /* It's the default value, which is reported as an empty string,
1235 * so there's nothing to do. */
1236 goto exit;
1237 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001238
1239 buflen = mbedtls_mpi_size( &mpi );
1240 buffer = mbedtls_calloc( 1, buflen );
1241 if( buffer == NULL )
1242 {
1243 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1244 goto exit;
1245 }
1246 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1247 if( ret != 0 )
1248 goto exit;
1249 attributes->domain_parameters = buffer;
1250 attributes->domain_parameters_size = buflen;
1251
1252exit:
1253 mbedtls_mpi_free( &mpi );
1254 if( ret != 0 )
1255 mbedtls_free( buffer );
1256 return( mbedtls_to_psa_error( ret ) );
1257}
John Durkop07cc04a2020-11-16 22:08:34 -08001258#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001259 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001260
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001261/** Retrieve all the publicly-accessible attributes of a key.
1262 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001263psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001264 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001265{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001267 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001268 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001269
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001270 psa_reset_key_attributes( attributes );
1271
Ronald Cron5c522922020-11-14 16:35:34 +01001272 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001273 if( status != PSA_SUCCESS )
1274 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001275
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001276 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001277 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1278 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1279
1280#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1281 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001282 psa_set_key_slot_number( attributes,
1283 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001284#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001285
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001287 {
John Durkop07cc04a2020-11-16 22:08:34 -08001288#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001289 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001290 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001291 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001292#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001293 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001294 * is not yet implemented.
1295 * https://github.com/ARMmbed/mbed-crypto/issues/216
1296 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001297 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001298 break;
1299#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001300 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001301 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001302
Ronald Cron90857082020-11-25 14:58:33 +01001303 status = mbedtls_psa_rsa_load_representation(
1304 slot->attr.type,
1305 slot->key.data,
1306 slot->key.bytes,
1307 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001308 if( status != PSA_SUCCESS )
1309 break;
1310
Steven Cooremana2371e52020-07-28 14:30:39 +02001311 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001312 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001313 mbedtls_rsa_free( rsa );
1314 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001315 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001317#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001318 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001319 default:
1320 /* Nothing else to do. */
1321 break;
1322 }
1323
1324 if( status != PSA_SUCCESS )
1325 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001326
Ronald Cron5c522922020-11-14 16:35:34 +01001327 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001328
Ronald Cron5c522922020-11-14 16:35:34 +01001329 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001330}
1331
Gilles Peskinec8000c02019-08-02 20:15:51 +02001332#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1333psa_status_t psa_get_key_slot_number(
1334 const psa_key_attributes_t *attributes,
1335 psa_key_slot_number_t *slot_number )
1336{
1337 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1338 {
1339 *slot_number = attributes->slot_number;
1340 return( PSA_SUCCESS );
1341 }
1342 else
1343 return( PSA_ERROR_INVALID_ARGUMENT );
1344}
1345#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1346
Ronald Crond18b5f82020-11-25 16:46:05 +01001347static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1348 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001349 uint8_t *data,
1350 size_t data_size,
1351 size_t *data_length )
1352{
Ronald Crond18b5f82020-11-25 16:46:05 +01001353 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001354 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001355 memcpy( data, key_buffer, key_buffer_size );
1356 memset( data + key_buffer_size, 0,
1357 data_size - key_buffer_size );
1358 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001359 return( PSA_SUCCESS );
1360}
1361
Ronald Cron7285cda2020-11-26 14:46:37 +01001362psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001363 const psa_key_attributes_t *attributes,
1364 const uint8_t *key_buffer, size_t key_buffer_size,
1365 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001366{
Ronald Crond18b5f82020-11-25 16:46:05 +01001367 psa_key_type_t type = attributes->core.type;
1368
Ronald Crond18b5f82020-11-25 16:46:05 +01001369 if( key_type_is_raw_bytes( type ) ||
1370 PSA_KEY_TYPE_IS_RSA( type ) ||
1371 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001372 {
1373 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001374 key_buffer, key_buffer_size,
1375 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001376 }
1377 else
1378 {
1379 /* This shouldn't happen in the reference implementation, but
1380 it is valid for a special-purpose implementation to omit
1381 support for exporting certain key types. */
1382 return( PSA_ERROR_NOT_SUPPORTED );
1383 }
1384}
1385
1386psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1387 uint8_t *data,
1388 size_t data_size,
1389 size_t *data_length )
1390{
1391 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1392 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1393 psa_key_slot_t *slot;
1394
Ronald Crone907e552021-01-18 13:22:38 +01001395 /* Reject a zero-length output buffer now, since this can never be a
1396 * valid key representation. This way we know that data must be a valid
1397 * pointer and we can do things like memset(data, ..., data_size). */
1398 if( data_size == 0 )
1399 return( PSA_ERROR_BUFFER_TOO_SMALL );
1400
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401 /* Set the key to empty now, so that even when there are errors, we always
1402 * set data_length to a value between 0 and data_size. On error, setting
1403 * the key to empty is a good choice because an empty key representation is
1404 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001405 *data_length = 0;
1406
Ronald Cron9486f9d2020-11-26 13:36:23 +01001407 /* Export requires the EXPORT flag. There is an exception for public keys,
1408 * which don't require any flag, but
1409 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1410 */
1411 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1412 PSA_KEY_USAGE_EXPORT, 0 );
1413 if( status != PSA_SUCCESS )
1414 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001415
Ronald Crond18b5f82020-11-25 16:46:05 +01001416 psa_key_attributes_t attributes = {
1417 .core = slot->attr
1418 };
Ronald Cron67227982020-11-26 15:16:05 +01001419 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001420 slot->key.data, slot->key.bytes,
1421 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001422
Ronald Cron9486f9d2020-11-26 13:36:23 +01001423 unlock_status = psa_unlock_key_slot( slot );
1424
1425 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1426}
1427
Ronald Cron7285cda2020-11-26 14:46:37 +01001428psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001429 const psa_key_attributes_t *attributes,
1430 const uint8_t *key_buffer,
1431 size_t key_buffer_size,
1432 uint8_t *data,
1433 size_t data_size,
1434 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001435{
Ronald Crond18b5f82020-11-25 16:46:05 +01001436 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001437
Ronald Crond18b5f82020-11-25 16:46:05 +01001438 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001439 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001440 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001441 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001442 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001443 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001444 key_buffer, key_buffer_size,
1445 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001446 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001447
Ronald Crond18b5f82020-11-25 16:46:05 +01001448 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001449 {
John Durkop0e005192020-10-31 22:06:54 -07001450#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1451 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001452 return( mbedtls_psa_rsa_export_public_key( attributes,
1453 key_buffer,
1454 key_buffer_size,
1455 data,
1456 data_size,
1457 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001458#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001459 /* We don't know how to convert a private RSA key to public. */
1460 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001461#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1462 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001463 }
1464 else
Moran Pekera998bc62018-04-16 18:16:20 +03001465 {
John Durkop6ba40d12020-11-10 08:50:04 -08001466#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1467 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001468 return( mbedtls_psa_ecp_export_public_key( attributes,
1469 key_buffer,
1470 key_buffer_size,
1471 data,
1472 data_size,
1473 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474#else
1475 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001476 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001477#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1478 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001479 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001480 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001481 else
1482 {
1483 /* This shouldn't happen in the reference implementation, but
1484 it is valid for a special-purpose implementation to omit
1485 support for exporting certain key types. */
1486 return( PSA_ERROR_NOT_SUPPORTED );
1487 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488}
Ronald Crond18b5f82020-11-25 16:46:05 +01001489
Ronald Croncf56a0a2020-08-04 09:51:30 +02001490psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001491 uint8_t *data,
1492 size_t data_size,
1493 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001494{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001495 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001496 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001497 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001498
Ronald Crone907e552021-01-18 13:22:38 +01001499 /* Reject a zero-length output buffer now, since this can never be a
1500 * valid key representation. This way we know that data must be a valid
1501 * pointer and we can do things like memset(data, ..., data_size). */
1502 if( data_size == 0 )
1503 return( PSA_ERROR_BUFFER_TOO_SMALL );
1504
Darryl Greendd8fb772018-11-07 16:00:44 +00001505 /* Set the key to empty now, so that even when there are errors, we always
1506 * set data_length to a value between 0 and data_size. On error, setting
1507 * the key to empty is a good choice because an empty key representation is
1508 * unlikely to be accepted anywhere. */
1509 *data_length = 0;
1510
1511 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001512 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001513 if( status != PSA_SUCCESS )
1514 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001515
Ronald Cron9486f9d2020-11-26 13:36:23 +01001516 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1517 {
1518 status = PSA_ERROR_INVALID_ARGUMENT;
1519 goto exit;
1520 }
1521
Ronald Crond18b5f82020-11-25 16:46:05 +01001522 psa_key_attributes_t attributes = {
1523 .core = slot->attr
1524 };
Ronald Cron67227982020-11-26 15:16:05 +01001525 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001526 &attributes, slot->key.data, slot->key.bytes,
1527 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001528
1529exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001530 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001531
Ronald Cron5c522922020-11-14 16:35:34 +01001532 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001533}
1534
Gilles Peskine91e8c332019-08-02 19:19:39 +02001535#if defined(static_assert)
1536static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1537 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001538static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001539 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001540static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001541 "One or more key attribute flag is listed as both internal-only and external-only" );
1542#endif
1543
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001544/** Validate that a key policy is internally well-formed.
1545 *
1546 * This function only rejects invalid policies. It does not validate the
1547 * consistency of the policy with respect to other attributes of the key
1548 * such as the key type.
1549 */
1550static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001551{
1552 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001553 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 PSA_KEY_USAGE_ENCRYPT |
1555 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001556 PSA_KEY_USAGE_SIGN_HASH |
1557 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1559 return( PSA_ERROR_INVALID_ARGUMENT );
1560
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 return( PSA_SUCCESS );
1562}
1563
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001564/** Validate the internal consistency of key attributes.
1565 *
1566 * This function only rejects invalid attribute values. If does not
1567 * validate the consistency of the attributes with any key data that may
1568 * be involved in the creation of the key.
1569 *
1570 * Call this function early in the key creation process.
1571 *
1572 * \param[in] attributes Key attributes for the new key.
1573 * \param[out] p_drv On any return, the driver for the key, if any.
1574 * NULL for a transparent key.
1575 *
1576 */
1577static psa_status_t psa_validate_key_attributes(
1578 const psa_key_attributes_t *attributes,
1579 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001580{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001581 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001582 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001583 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001584
Ronald Cron54b90082020-10-29 15:26:43 +01001585 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001586 if( status != PSA_SUCCESS )
1587 return( status );
1588
Ronald Crond2ed4812020-07-17 16:11:30 +02001589 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001590 if( status != PSA_SUCCESS )
1591 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001592
Ronald Cron65f38a32020-10-23 17:11:13 +02001593 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1594 {
1595 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1596 return( PSA_ERROR_INVALID_ARGUMENT );
1597 }
1598 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001599 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001600 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001601 if( status != PSA_SUCCESS )
1602 return( status );
1603 }
1604
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001605 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001606 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001607 return( status );
1608
1609 /* Refuse to create overly large keys.
1610 * Note that this doesn't trigger on import if the attributes don't
1611 * explicitly specify a size (so psa_get_key_bits returns 0), so
1612 * psa_import_key() needs its own checks. */
1613 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1614 return( PSA_ERROR_NOT_SUPPORTED );
1615
Gilles Peskine91e8c332019-08-02 19:19:39 +02001616 /* Reject invalid flags. These should not be reachable through the API. */
1617 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1618 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1619 return( PSA_ERROR_INVALID_ARGUMENT );
1620
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001621 return( PSA_SUCCESS );
1622}
1623
Gilles Peskine4747d192019-04-17 15:05:45 +02001624/** Prepare a key slot to receive key material.
1625 *
1626 * This function allocates a key slot and sets its metadata.
1627 *
1628 * If this function fails, call psa_fail_key_creation().
1629 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * This function is intended to be used as follows:
1631 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001632 * it with the specified attributes, and in case of a volatile key assign it
1633 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001634 * -# Populate the slot with the key material.
1635 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1636 * In case of failure at any step, stop the sequence and call
1637 * psa_fail_key_creation().
1638 *
Ronald Cron5c522922020-11-14 16:35:34 +01001639 * On success, the key slot is locked. It is the responsibility of the caller
1640 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001641 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001642 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001643 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001644 * \param[out] p_slot On success, a pointer to the prepared slot.
1645 * \param[out] p_drv On any return, the driver for the key, if any.
1646 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001647 *
1648 * \retval #PSA_SUCCESS
1649 * The key slot is ready to receive key material.
1650 * \return If this function fails, the key slot is an invalid state.
1651 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 */
1653static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001654 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001656 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001657 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001660 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001661 psa_key_slot_t *slot;
1662
Gilles Peskinedf179142019-07-15 22:02:14 +02001663 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001664 *p_drv = NULL;
1665
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001666 status = psa_validate_key_attributes( attributes, p_drv );
1667 if( status != PSA_SUCCESS )
1668 return( status );
1669
Ronald Cronc4d1b512020-07-31 11:26:37 +02001670 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001671 if( status != PSA_SUCCESS )
1672 return( status );
1673 slot = *p_slot;
1674
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001675 /* We're storing the declared bit-size of the key. It's up to each
1676 * creation mechanism to verify that this information is correct.
1677 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001678 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001679 * is optional (import, copy). In case of a volatile key, assign it the
1680 * volatile key identifier associated to the slot returned to contain its
1681 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001682
1683 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001684 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1685 {
1686#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1687 slot->attr.id = volatile_key_id;
1688#else
1689 slot->attr.id.key_id = volatile_key_id;
1690#endif
1691 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001692
Gilles Peskine91e8c332019-08-02 19:19:39 +02001693 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001694 * external-only flags, query `attributes`. Thanks to the check
1695 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001696 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001697 * may have set. */
1698 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001699
Gilles Peskinecbaff462019-07-12 23:46:04 +02001700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001701 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001702 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001703 * create the key file in internal storage, create the
1704 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001705 * persistent data. This is done by starting a transaction that will
1706 * encompass these three actions.
1707 * For registering a volatile key, we just need to find an appropriate
1708 * slot number inside the SE. Since the key is designated volatile, creating
1709 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001710 /* The first thing to do is to find a slot number for the new key.
1711 * We save the slot number in persistent storage as part of the
1712 * transaction data. It will be needed to recover if the power
1713 * fails during the key creation process, to clean up on the secure
1714 * element side after restarting. Obtaining a slot number from the
1715 * secure element driver updates its persistent state, but we do not yet
1716 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001717 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001718 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001719 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001720 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001721 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001722 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001723 if( status != PSA_SUCCESS )
1724 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001725
1726 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001727 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001728 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1729 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001730 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001731 psa_crypto_transaction.key.id = slot->attr.id;
1732 status = psa_crypto_save_transaction( );
1733 if( status != PSA_SUCCESS )
1734 {
1735 (void) psa_crypto_stop_transaction( );
1736 return( status );
1737 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001738 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001739
1740 status = psa_copy_key_material_into_slot(
1741 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001742 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001743
1744 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1745 {
1746 /* Key registration only makes sense with a secure element. */
1747 return( PSA_ERROR_INVALID_ARGUMENT );
1748 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001749#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1750
Ronald Cronc4d1b512020-07-31 11:26:37 +02001751 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001752}
Gilles Peskine4747d192019-04-17 15:05:45 +02001753
1754/** Finalize the creation of a key once its key material has been set.
1755 *
1756 * This entails writing the key to persistent storage.
1757 *
1758 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001759 * See the documentation of psa_start_key_creation() for the intended use
1760 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001761 *
Ronald Cron5c522922020-11-14 16:35:34 +01001762 * If the finalization succeeds, the function unlocks the key slot (it was
1763 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1764 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001765 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001766 * \param[in,out] slot Pointer to the slot with key material.
1767 * \param[in] driver The secure element driver for the key,
1768 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001769 * \param[out] key On success, identifier of the key. Note that the
1770 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001771 *
1772 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001773 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001774 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1775 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1776 * \retval #PSA_ERROR_ALREADY_EXISTS
1777 * \retval #PSA_ERROR_DATA_INVALID
1778 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001779 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001780 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001781 * \return If this function fails, the key slot is an invalid state.
1782 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001783 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001784static psa_status_t psa_finish_key_creation(
1785 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001786 psa_se_drv_table_entry_t *driver,
1787 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001788{
1789 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001790 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001792
1793#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001794 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001795 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1797 if( driver != NULL )
1798 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001799 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001800 psa_key_slot_number_t slot_number =
1801 psa_key_slot_get_slot_number( slot ) ;
1802
Gilles Peskineb46bef22019-07-30 21:32:04 +02001803#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001804 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001805 sizeof( data.slot_number ),
1806 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001807#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001808 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001809 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001810 (uint8_t*) &data,
1811 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001812 }
1813 else
1814#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1815 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001816 /* Key material is saved in export representation in the slot, so
1817 * just pass the slot buffer for storage. */
1818 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001819 slot->key.data,
1820 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001821 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001822 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001823#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1824
Gilles Peskinecbaff462019-07-12 23:46:04 +02001825#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001826 /* Finish the transaction for a key creation. This does not
1827 * happen when registering an existing key. Detect this case
1828 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001829 * creation of a persistent key in a secure element requires a transaction,
1830 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001831 if( driver != NULL &&
1832 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001833 {
1834 status = psa_save_se_persistent_data( driver );
1835 if( status != PSA_SUCCESS )
1836 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001837 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001838 return( status );
1839 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001840 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001841 }
1842#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1843
Ronald Cron50972942020-11-14 11:28:25 +01001844 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001845 {
1846 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001847 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001848 if( status != PSA_SUCCESS )
1849 *key = MBEDTLS_SVC_KEY_ID_INIT;
1850 }
Ronald Cron50972942020-11-14 11:28:25 +01001851
Gilles Peskine4747d192019-04-17 15:05:45 +02001852 return( status );
1853}
1854
1855/** Abort the creation of a key.
1856 *
1857 * You may call this function after calling psa_start_key_creation(),
1858 * or after psa_finish_key_creation() fails. In other circumstances, this
1859 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001860 * See the documentation of psa_start_key_creation() for the intended use
1861 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001862 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001863 * \param[in,out] slot Pointer to the slot with key material.
1864 * \param[in] driver The secure element driver for the key,
1865 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001866 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001867static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001868 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001869{
Gilles Peskine011e4282019-06-26 18:34:38 +02001870 (void) driver;
1871
Gilles Peskine4747d192019-04-17 15:05:45 +02001872 if( slot == NULL )
1873 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001874
1875#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001876 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001877 * element, and the failure happened later (when saving metadata
1878 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001879 * element.
1880 * https://github.com/ARMmbed/mbed-crypto/issues/217
1881 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001882
Gilles Peskined7729582019-08-05 15:55:54 +02001883 /* Abort the ongoing transaction if any (there may not be one if
1884 * the creation process failed before starting one, or if the
1885 * key creation is a registration of a key in a secure element).
1886 * Earlier functions must already have done what it takes to undo any
1887 * partial creation. All that's left is to update the transaction data
1888 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001889 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001890#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1891
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 psa_wipe_key_slot( slot );
1893}
1894
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001895/** Validate optional attributes during key creation.
1896 *
1897 * Some key attributes are optional during key creation. If they are
1898 * specified in the attributes structure, check that they are consistent
1899 * with the data in the slot.
1900 *
1901 * This function should be called near the end of key creation, after
1902 * the slot in memory is fully populated but before saving persistent data.
1903 */
1904static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001905 const psa_key_slot_t *slot,
1906 const psa_key_attributes_t *attributes )
1907{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001908 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001909 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001910 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001911 return( PSA_ERROR_INVALID_ARGUMENT );
1912 }
1913
1914 if( attributes->domain_parameters_size != 0 )
1915 {
John Durkop0e005192020-10-31 22:06:54 -07001916#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1917 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001918 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001919 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001920 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001921 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001922 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001923
Ronald Cron90857082020-11-25 14:58:33 +01001924 psa_status_t status = mbedtls_psa_rsa_load_representation(
1925 slot->attr.type,
1926 slot->key.data,
1927 slot->key.bytes,
1928 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001929 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001930 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001931
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001932 mbedtls_mpi_init( &actual );
1933 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001934 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001935 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001936 mbedtls_rsa_free( rsa );
1937 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001938 if( ret != 0 )
1939 goto rsa_exit;
1940 ret = mbedtls_mpi_read_binary( &required,
1941 attributes->domain_parameters,
1942 attributes->domain_parameters_size );
1943 if( ret != 0 )
1944 goto rsa_exit;
1945 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1946 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1947 rsa_exit:
1948 mbedtls_mpi_free( &actual );
1949 mbedtls_mpi_free( &required );
1950 if( ret != 0)
1951 return( mbedtls_to_psa_error( ret ) );
1952 }
1953 else
John Durkop9814fa22020-11-04 12:28:15 -08001954#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1955 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001956 {
1957 return( PSA_ERROR_INVALID_ARGUMENT );
1958 }
1959 }
1960
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001961 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001962 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001963 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001964 return( PSA_ERROR_INVALID_ARGUMENT );
1965 }
1966
1967 return( PSA_SUCCESS );
1968}
1969
Gilles Peskine4747d192019-04-17 15:05:45 +02001970psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001971 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001972 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001973 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001974{
1975 psa_status_t status;
1976 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001977 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001978 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979
Ronald Cron81709fc2020-11-14 12:10:32 +01001980 *key = MBEDTLS_SVC_KEY_ID_INIT;
1981
Gilles Peskine0f84d622019-09-12 19:03:13 +02001982 /* Reject zero-length symmetric keys (including raw data key objects).
1983 * This also rejects any key which might be encoded as an empty string,
1984 * which is never valid. */
1985 if( data_length == 0 )
1986 return( PSA_ERROR_INVALID_ARGUMENT );
1987
Gilles Peskinedf179142019-07-15 22:02:14 +02001988 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01001989 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001993 /* In the case of a transparent key or an opaque key stored in local
1994 * storage (thus not in the case of generating a key in a secure element
1995 * or cryptoprocessor with storage), we have to allocate a buffer to
1996 * hold the generated key material. */
1997 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02001998 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01001999 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002000 if( status != PSA_SUCCESS )
2001 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002002 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003
2004 bits = slot->attr.bits;
2005 status = psa_driver_wrapper_import_key( attributes,
2006 data, data_length,
2007 slot->key.data,
2008 slot->key.bytes,
2009 &slot->key.bytes, &bits );
2010 if( status != PSA_SUCCESS )
2011 goto exit;
2012
2013 if( slot->attr.bits == 0 )
2014 slot->attr.bits = (psa_key_bits_t) bits;
2015 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002016 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002017 status = PSA_ERROR_INVALID_ARGUMENT;
2018 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002019 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002020
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002021 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002022 if( status != PSA_SUCCESS )
2023 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002024
Ronald Cron81709fc2020-11-14 12:10:32 +01002025 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002026exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002027 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002028 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002029
Gilles Peskine4747d192019-04-17 15:05:45 +02002030 return( status );
2031}
2032
Gilles Peskined7729582019-08-05 15:55:54 +02002033#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2034psa_status_t mbedtls_psa_register_se_key(
2035 const psa_key_attributes_t *attributes )
2036{
2037 psa_status_t status;
2038 psa_key_slot_t *slot = NULL;
2039 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002040 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002041
2042 /* Leaving attributes unspecified is not currently supported.
2043 * It could make sense to query the key type and size from the
2044 * secure element, but not all secure elements support this
2045 * and the driver HAL doesn't currently support it. */
2046 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2047 return( PSA_ERROR_NOT_SUPPORTED );
2048 if( psa_get_key_bits( attributes ) == 0 )
2049 return( PSA_ERROR_NOT_SUPPORTED );
2050
2051 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002052 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002053 if( status != PSA_SUCCESS )
2054 goto exit;
2055
Ronald Cron81709fc2020-11-14 12:10:32 +01002056 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002057
2058exit:
2059 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002060 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002061
Gilles Peskined7729582019-08-05 15:55:54 +02002062 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002063 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002064 return( status );
2065}
2066#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2067
Gilles Peskinef603c712019-01-19 13:40:11 +01002068static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002069 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002070{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002071 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002072 source->key.data,
2073 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002074 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002075 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002076
Steven Cooreman398aee52020-10-13 14:35:45 +02002077 target->attr.type = source->attr.type;
2078 target->attr.bits = source->attr.bits;
2079
2080 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002081}
2082
Ronald Croncf56a0a2020-08-04 09:51:30 +02002083psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002084 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002085 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002086{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002087 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002088 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002089 psa_key_slot_t *source_slot = NULL;
2090 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002091 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002092 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002093
Ronald Cron81709fc2020-11-14 12:10:32 +01002094 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2095
Ronald Cron5c522922020-11-14 16:35:34 +01002096 status = psa_get_and_lock_transparent_key_slot_with_policy(
2097 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002098 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002099 goto exit;
2100
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002101 status = psa_validate_optional_attributes( source_slot,
2102 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002103 if( status != PSA_SUCCESS )
2104 goto exit;
2105
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002106 status = psa_restrict_key_policy( source_slot->attr.type,
2107 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002108 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 if( status != PSA_SUCCESS )
2110 goto exit;
2111
Ronald Cron81709fc2020-11-14 12:10:32 +01002112 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2113 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002114 if( status != PSA_SUCCESS )
2115 goto exit;
2116
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002117#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2118 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002119 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002120 /* Copying to a secure element is not implemented yet. */
2121 status = PSA_ERROR_NOT_SUPPORTED;
2122 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002123 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002124#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002125
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002126 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002127 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002129
Ronald Cron81709fc2020-11-14 12:10:32 +01002130 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002131exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002132 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002133 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002134
Ronald Cron5c522922020-11-14 16:35:34 +01002135 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002136
Ronald Cron5c522922020-11-14 16:35:34 +01002137 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002138}
2139
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002140
2141
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002142/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002143/* Message digests */
2144/****************************************************************/
2145
John Durkop07cc04a2020-11-16 22:08:34 -08002146#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002147 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2148 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002149 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002150static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002151{
2152 switch( alg )
2153 {
John Durkopee4e6602020-11-27 08:48:46 -08002154#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002155 case PSA_ALG_MD2:
2156 return( &mbedtls_md2_info );
2157#endif
John Durkopee4e6602020-11-27 08:48:46 -08002158#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002159 case PSA_ALG_MD4:
2160 return( &mbedtls_md4_info );
2161#endif
John Durkopee4e6602020-11-27 08:48:46 -08002162#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002163 case PSA_ALG_MD5:
2164 return( &mbedtls_md5_info );
2165#endif
John Durkopee4e6602020-11-27 08:48:46 -08002166#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002167 case PSA_ALG_RIPEMD160:
2168 return( &mbedtls_ripemd160_info );
2169#endif
John Durkopee4e6602020-11-27 08:48:46 -08002170#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002171 case PSA_ALG_SHA_1:
2172 return( &mbedtls_sha1_info );
2173#endif
John Durkopee4e6602020-11-27 08:48:46 -08002174#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002175 case PSA_ALG_SHA_224:
2176 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002177#endif
2178#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002179 case PSA_ALG_SHA_256:
2180 return( &mbedtls_sha256_info );
2181#endif
John Durkopee4e6602020-11-27 08:48:46 -08002182#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002183 case PSA_ALG_SHA_384:
2184 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002185#endif
John Durkopee4e6602020-11-27 08:48:46 -08002186#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002187 case PSA_ALG_SHA_512:
2188 return( &mbedtls_sha512_info );
2189#endif
2190 default:
2191 return( NULL );
2192 }
2193}
John Durkop07cc04a2020-11-16 22:08:34 -08002194#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002195 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2196 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2197 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002198
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002199psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2200{
2201 switch( operation->alg )
2202 {
Gilles Peskine81736312018-06-26 15:04:31 +02002203 case 0:
2204 /* The object has (apparently) been initialized but it is not
2205 * in use. It's ok to call abort on such an object, and there's
2206 * nothing to do. */
2207 break;
John Durkopee4e6602020-11-27 08:48:46 -08002208#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209 case PSA_ALG_MD2:
2210 mbedtls_md2_free( &operation->ctx.md2 );
2211 break;
2212#endif
John Durkopee4e6602020-11-27 08:48:46 -08002213#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002214 case PSA_ALG_MD4:
2215 mbedtls_md4_free( &operation->ctx.md4 );
2216 break;
2217#endif
John Durkopee4e6602020-11-27 08:48:46 -08002218#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002219 case PSA_ALG_MD5:
2220 mbedtls_md5_free( &operation->ctx.md5 );
2221 break;
2222#endif
John Durkopee4e6602020-11-27 08:48:46 -08002223#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002224 case PSA_ALG_RIPEMD160:
2225 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2226 break;
2227#endif
John Durkopee4e6602020-11-27 08:48:46 -08002228#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002229 case PSA_ALG_SHA_1:
2230 mbedtls_sha1_free( &operation->ctx.sha1 );
2231 break;
2232#endif
John Durkop6ca23272020-12-03 06:01:32 -08002233#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002234 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002235 mbedtls_sha256_free( &operation->ctx.sha256 );
2236 break;
2237#endif
2238#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_SHA_256:
2240 mbedtls_sha256_free( &operation->ctx.sha256 );
2241 break;
2242#endif
John Durkop6ca23272020-12-03 06:01:32 -08002243#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002244 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002245 mbedtls_sha512_free( &operation->ctx.sha512 );
2246 break;
2247#endif
2248#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002249 case PSA_ALG_SHA_512:
2250 mbedtls_sha512_free( &operation->ctx.sha512 );
2251 break;
2252#endif
2253 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002254 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002255 }
2256 operation->alg = 0;
2257 return( PSA_SUCCESS );
2258}
2259
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002260psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261 psa_algorithm_t alg )
2262{
Janos Follath24eed8d2019-11-22 13:21:35 +00002263 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002264
2265 /* A context must be freshly initialized before it can be set up. */
2266 if( operation->alg != 0 )
2267 {
2268 return( PSA_ERROR_BAD_STATE );
2269 }
2270
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002271 switch( alg )
2272 {
John Durkopee4e6602020-11-27 08:48:46 -08002273#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274 case PSA_ALG_MD2:
2275 mbedtls_md2_init( &operation->ctx.md2 );
2276 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2277 break;
2278#endif
John Durkopee4e6602020-11-27 08:48:46 -08002279#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002280 case PSA_ALG_MD4:
2281 mbedtls_md4_init( &operation->ctx.md4 );
2282 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2283 break;
2284#endif
John Durkopee4e6602020-11-27 08:48:46 -08002285#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002286 case PSA_ALG_MD5:
2287 mbedtls_md5_init( &operation->ctx.md5 );
2288 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2289 break;
2290#endif
John Durkopee4e6602020-11-27 08:48:46 -08002291#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002292 case PSA_ALG_RIPEMD160:
2293 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2294 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2295 break;
2296#endif
John Durkopee4e6602020-11-27 08:48:46 -08002297#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002298 case PSA_ALG_SHA_1:
2299 mbedtls_sha1_init( &operation->ctx.sha1 );
2300 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2301 break;
2302#endif
John Durkopee4e6602020-11-27 08:48:46 -08002303#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002304 case PSA_ALG_SHA_224:
2305 mbedtls_sha256_init( &operation->ctx.sha256 );
2306 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2307 break;
John Durkopee4e6602020-11-27 08:48:46 -08002308#endif
2309#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002310 case PSA_ALG_SHA_256:
2311 mbedtls_sha256_init( &operation->ctx.sha256 );
2312 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2313 break;
2314#endif
John Durkopee4e6602020-11-27 08:48:46 -08002315#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002316 case PSA_ALG_SHA_384:
2317 mbedtls_sha512_init( &operation->ctx.sha512 );
2318 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2319 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002320#endif
John Durkopee4e6602020-11-27 08:48:46 -08002321#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322 case PSA_ALG_SHA_512:
2323 mbedtls_sha512_init( &operation->ctx.sha512 );
2324 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2325 break;
2326#endif
2327 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002328 return( PSA_ALG_IS_HASH( alg ) ?
2329 PSA_ERROR_NOT_SUPPORTED :
2330 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002331 }
2332 if( ret == 0 )
2333 operation->alg = alg;
2334 else
2335 psa_hash_abort( operation );
2336 return( mbedtls_to_psa_error( ret ) );
2337}
2338
2339psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2340 const uint8_t *input,
2341 size_t input_length )
2342{
Janos Follath24eed8d2019-11-22 13:21:35 +00002343 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002344
2345 /* Don't require hash implementations to behave correctly on a
2346 * zero-length input, which may have an invalid pointer. */
2347 if( input_length == 0 )
2348 return( PSA_SUCCESS );
2349
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002350 switch( operation->alg )
2351 {
John Durkopee4e6602020-11-27 08:48:46 -08002352#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353 case PSA_ALG_MD2:
2354 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2355 input, input_length );
2356 break;
2357#endif
John Durkopee4e6602020-11-27 08:48:46 -08002358#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002359 case PSA_ALG_MD4:
2360 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2361 input, input_length );
2362 break;
2363#endif
John Durkopee4e6602020-11-27 08:48:46 -08002364#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002365 case PSA_ALG_MD5:
2366 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2367 input, input_length );
2368 break;
2369#endif
John Durkopee4e6602020-11-27 08:48:46 -08002370#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002371 case PSA_ALG_RIPEMD160:
2372 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2373 input, input_length );
2374 break;
2375#endif
John Durkopee4e6602020-11-27 08:48:46 -08002376#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002377 case PSA_ALG_SHA_1:
2378 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2379 input, input_length );
2380 break;
2381#endif
John Durkop6ca23272020-12-03 06:01:32 -08002382#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002383 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002384 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2385 input, input_length );
2386 break;
2387#endif
2388#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002389 case PSA_ALG_SHA_256:
2390 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2391 input, input_length );
2392 break;
2393#endif
John Durkop6ca23272020-12-03 06:01:32 -08002394#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002395 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002396 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2397 input, input_length );
2398 break;
2399#endif
2400#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002401 case PSA_ALG_SHA_512:
2402 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2403 input, input_length );
2404 break;
2405#endif
2406 default:
John Durkopee4e6602020-11-27 08:48:46 -08002407 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002408 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002409 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002410
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002411 if( ret != 0 )
2412 psa_hash_abort( operation );
2413 return( mbedtls_to_psa_error( ret ) );
2414}
2415
2416psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2417 uint8_t *hash,
2418 size_t hash_size,
2419 size_t *hash_length )
2420{
itayzafrir40835d42018-08-02 13:14:17 +03002421 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002422 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002423 size_t actual_hash_length = PSA_HASH_LENGTH( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002424
2425 /* Fill the output buffer with something that isn't a valid hash
2426 * (barring an attack on the hash and deliberately-crafted input),
2427 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002428 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002429 /* If hash_size is 0 then hash may be NULL and then the
2430 * call to memset would have undefined behavior. */
2431 if( hash_size != 0 )
2432 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002433
2434 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002435 {
2436 status = PSA_ERROR_BUFFER_TOO_SMALL;
2437 goto exit;
2438 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002439
2440 switch( operation->alg )
2441 {
John Durkopee4e6602020-11-27 08:48:46 -08002442#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002443 case PSA_ALG_MD2:
2444 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2445 break;
2446#endif
John Durkopee4e6602020-11-27 08:48:46 -08002447#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002448 case PSA_ALG_MD4:
2449 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2450 break;
2451#endif
John Durkopee4e6602020-11-27 08:48:46 -08002452#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002453 case PSA_ALG_MD5:
2454 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2455 break;
2456#endif
John Durkopee4e6602020-11-27 08:48:46 -08002457#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002458 case PSA_ALG_RIPEMD160:
2459 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2460 break;
2461#endif
John Durkopee4e6602020-11-27 08:48:46 -08002462#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002463 case PSA_ALG_SHA_1:
2464 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2465 break;
2466#endif
John Durkop6ca23272020-12-03 06:01:32 -08002467#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002468 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002469 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2470 break;
2471#endif
2472#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002473 case PSA_ALG_SHA_256:
2474 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2475 break;
2476#endif
John Durkop6ca23272020-12-03 06:01:32 -08002477#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002478 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002479 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2480 break;
2481#endif
2482#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483 case PSA_ALG_SHA_512:
2484 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2485 break;
2486#endif
2487 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002488 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002489 }
itayzafrir40835d42018-08-02 13:14:17 +03002490 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002491
itayzafrir40835d42018-08-02 13:14:17 +03002492exit:
2493 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002494 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002495 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002496 return( psa_hash_abort( operation ) );
2497 }
2498 else
2499 {
2500 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002501 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002502 }
2503}
2504
Gilles Peskine2d277862018-06-18 15:41:12 +02002505psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2506 const uint8_t *hash,
2507 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002508{
2509 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2510 size_t actual_hash_length;
2511 psa_status_t status = psa_hash_finish( operation,
2512 actual_hash, sizeof( actual_hash ),
2513 &actual_hash_length );
2514 if( status != PSA_SUCCESS )
2515 return( status );
2516 if( actual_hash_length != hash_length )
2517 return( PSA_ERROR_INVALID_SIGNATURE );
2518 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2519 return( PSA_ERROR_INVALID_SIGNATURE );
2520 return( PSA_SUCCESS );
2521}
2522
Gilles Peskine0a749c82019-11-28 19:33:58 +01002523psa_status_t psa_hash_compute( psa_algorithm_t alg,
2524 const uint8_t *input, size_t input_length,
2525 uint8_t *hash, size_t hash_size,
2526 size_t *hash_length )
2527{
2528 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2529 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2530
2531 *hash_length = hash_size;
2532 status = psa_hash_setup( &operation, alg );
2533 if( status != PSA_SUCCESS )
2534 goto exit;
2535 status = psa_hash_update( &operation, input, input_length );
2536 if( status != PSA_SUCCESS )
2537 goto exit;
2538 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2539 if( status != PSA_SUCCESS )
2540 goto exit;
2541
2542exit:
2543 if( status == PSA_SUCCESS )
2544 status = psa_hash_abort( &operation );
2545 else
2546 psa_hash_abort( &operation );
2547 return( status );
2548}
2549
2550psa_status_t psa_hash_compare( psa_algorithm_t alg,
2551 const uint8_t *input, size_t input_length,
2552 const uint8_t *hash, size_t hash_length )
2553{
2554 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2555 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2556
2557 status = psa_hash_setup( &operation, alg );
2558 if( status != PSA_SUCCESS )
2559 goto exit;
2560 status = psa_hash_update( &operation, input, input_length );
2561 if( status != PSA_SUCCESS )
2562 goto exit;
2563 status = psa_hash_verify( &operation, hash, hash_length );
2564 if( status != PSA_SUCCESS )
2565 goto exit;
2566
2567exit:
2568 if( status == PSA_SUCCESS )
2569 status = psa_hash_abort( &operation );
2570 else
2571 psa_hash_abort( &operation );
2572 return( status );
2573}
2574
Gilles Peskineeb35d782019-01-22 17:56:16 +01002575psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2576 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002577{
2578 if( target_operation->alg != 0 )
2579 return( PSA_ERROR_BAD_STATE );
2580
2581 switch( source_operation->alg )
2582 {
2583 case 0:
2584 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002585#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002586 case PSA_ALG_MD2:
2587 mbedtls_md2_clone( &target_operation->ctx.md2,
2588 &source_operation->ctx.md2 );
2589 break;
2590#endif
John Durkopee4e6602020-11-27 08:48:46 -08002591#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002592 case PSA_ALG_MD4:
2593 mbedtls_md4_clone( &target_operation->ctx.md4,
2594 &source_operation->ctx.md4 );
2595 break;
2596#endif
John Durkopee4e6602020-11-27 08:48:46 -08002597#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002598 case PSA_ALG_MD5:
2599 mbedtls_md5_clone( &target_operation->ctx.md5,
2600 &source_operation->ctx.md5 );
2601 break;
2602#endif
John Durkopee4e6602020-11-27 08:48:46 -08002603#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002604 case PSA_ALG_RIPEMD160:
2605 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2606 &source_operation->ctx.ripemd160 );
2607 break;
2608#endif
John Durkopee4e6602020-11-27 08:48:46 -08002609#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002610 case PSA_ALG_SHA_1:
2611 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2612 &source_operation->ctx.sha1 );
2613 break;
2614#endif
John Durkop6ca23272020-12-03 06:01:32 -08002615#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002616 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002617 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2618 &source_operation->ctx.sha256 );
2619 break;
2620#endif
2621#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002622 case PSA_ALG_SHA_256:
2623 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2624 &source_operation->ctx.sha256 );
2625 break;
2626#endif
John Durkop6ca23272020-12-03 06:01:32 -08002627#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002628 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002629 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2630 &source_operation->ctx.sha512 );
2631 break;
2632#endif
2633#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002634 case PSA_ALG_SHA_512:
2635 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2636 &source_operation->ctx.sha512 );
2637 break;
2638#endif
2639 default:
2640 return( PSA_ERROR_NOT_SUPPORTED );
2641 }
2642
2643 target_operation->alg = source_operation->alg;
2644 return( PSA_SUCCESS );
2645}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002646
2647
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002648/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002649/* MAC */
2650/****************************************************************/
2651
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002652static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002653 psa_algorithm_t alg,
2654 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002655 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002656 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002659 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002661 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002662 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002663
Gilles Peskine8c9def32018-02-08 10:02:12 +01002664 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2665 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002666 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002668 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002669 mode = MBEDTLS_MODE_STREAM;
2670 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002671 case PSA_ALG_CTR:
2672 mode = MBEDTLS_MODE_CTR;
2673 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002674 case PSA_ALG_CFB:
2675 mode = MBEDTLS_MODE_CFB;
2676 break;
2677 case PSA_ALG_OFB:
2678 mode = MBEDTLS_MODE_OFB;
2679 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002680 case PSA_ALG_ECB_NO_PADDING:
2681 mode = MBEDTLS_MODE_ECB;
2682 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002683 case PSA_ALG_CBC_NO_PADDING:
2684 mode = MBEDTLS_MODE_CBC;
2685 break;
2686 case PSA_ALG_CBC_PKCS7:
2687 mode = MBEDTLS_MODE_CBC;
2688 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002689 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690 mode = MBEDTLS_MODE_CCM;
2691 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002692 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 mode = MBEDTLS_MODE_GCM;
2694 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002695 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002696 mode = MBEDTLS_MODE_CHACHAPOLY;
2697 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002698 default:
2699 return( NULL );
2700 }
2701 }
2702 else if( alg == PSA_ALG_CMAC )
2703 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704 else
2705 return( NULL );
2706
2707 switch( key_type )
2708 {
2709 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002710 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711 break;
2712 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002713 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2714 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002715 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002716 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002717 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002718 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002719 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2720 * but two-key Triple-DES is functionally three-key Triple-DES
2721 * with K1=K3, so that's how we present it to mbedtls. */
2722 if( key_bits == 128 )
2723 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002724 break;
2725 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002726 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002727 break;
2728 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002729 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002730 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002731 case PSA_KEY_TYPE_CHACHA20:
2732 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2733 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002734 default:
2735 return( NULL );
2736 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002737 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002738 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002739
Jaeden Amero23bbb752018-06-26 14:16:54 +01002740 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2741 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002742}
2743
John Durkop6ba40d12020-11-10 08:50:04 -08002744#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002745static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002746{
Gilles Peskine2d277862018-06-18 15:41:12 +02002747 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002748 {
2749 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002750 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002751 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002752 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002753 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002754 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002755 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002756 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002757 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002758 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002759 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002760 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002761 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002762 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002763 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002764 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002765 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002766 return( 128 );
2767 default:
2768 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002769 }
2770}
John Durkop07cc04a2020-11-16 22:08:34 -08002771#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002772
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002773/* Initialize the MAC operation structure. Once this function has been
2774 * called, psa_mac_abort can run and will do the right thing. */
2775static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2776 psa_algorithm_t alg )
2777{
2778 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2779
Steven Cooreman15472f82021-03-02 16:16:22 +01002780 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002781 operation->key_set = 0;
2782 operation->iv_set = 0;
2783 operation->iv_required = 0;
2784 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002785 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002786
2787#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002788 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002789 {
2790 operation->iv_required = 0;
2791 mbedtls_cipher_init( &operation->ctx.cmac );
2792 status = PSA_SUCCESS;
2793 }
2794 else
2795#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002796#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002797 if( PSA_ALG_IS_HMAC( operation->alg ) )
2798 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002799 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2800 operation->ctx.hmac.hash_ctx.alg = 0;
2801 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002802 }
2803 else
John Durkopd0321952020-10-29 21:37:36 -07002804#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002805 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002806 if( ! PSA_ALG_IS_MAC( alg ) )
2807 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002808 }
2809
2810 if( status != PSA_SUCCESS )
2811 memset( operation, 0, sizeof( *operation ) );
2812 return( status );
2813}
2814
John Durkop6ba40d12020-11-10 08:50:04 -08002815#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002816static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2817{
Gilles Peskine3f108122018-12-07 18:14:53 +01002818 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002819 return( psa_hash_abort( &hmac->hash_ctx ) );
2820}
John Durkop6ba40d12020-11-10 08:50:04 -08002821#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002822
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2824{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002825 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002826 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 /* The object has (apparently) been initialized but it is not
2828 * in use. It's ok to call abort on such an object, and there's
2829 * nothing to do. */
2830 return( PSA_SUCCESS );
2831 }
2832 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002833#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002834 if( operation->alg == PSA_ALG_CMAC )
2835 {
2836 mbedtls_cipher_free( &operation->ctx.cmac );
2837 }
2838 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002840#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841 if( PSA_ALG_IS_HMAC( operation->alg ) )
2842 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002843 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002844 }
2845 else
John Durkopd0321952020-10-29 21:37:36 -07002846#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002847 {
2848 /* Sanity check (shouldn't happen: operation->alg should
2849 * always have been initialized to a valid value). */
2850 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002851 }
Moran Peker41deec42018-04-04 15:43:05 +03002852
Gilles Peskine8c9def32018-02-08 10:02:12 +01002853 operation->alg = 0;
2854 operation->key_set = 0;
2855 operation->iv_set = 0;
2856 operation->iv_required = 0;
2857 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002858 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002859
Gilles Peskine8c9def32018-02-08 10:02:12 +01002860 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002861
2862bad_state:
2863 /* If abort is called on an uninitialized object, we can't trust
2864 * anything. Wipe the object in case it contains confidential data.
2865 * This may result in a memory leak if a pointer gets overwritten,
2866 * but it's too late to do anything about this. */
2867 memset( operation, 0, sizeof( *operation ) );
2868 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869}
2870
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002871#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002872static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2873 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002874{
Janos Follath24eed8d2019-11-22 13:21:35 +00002875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002876 const mbedtls_cipher_info_t *cipher_info =
2877 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2878 slot->attr.type, slot->attr.bits,
2879 NULL );
2880 if( cipher_info == NULL )
2881 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002882
2883 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2884 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002885 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002886
2887 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002888 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002889 slot->attr.bits );
2890exit:
2891 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002892}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002893#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002894
John Durkop6ba40d12020-11-10 08:50:04 -08002895#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002896static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2897 const uint8_t *key,
2898 size_t key_length,
2899 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002900{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002901 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002902 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002903 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002904 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002905 psa_status_t status;
2906
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002907 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2908 * overflow below. This should never trigger if the hash algorithm
2909 * is implemented correctly. */
2910 /* The size checks against the ipad and opad buffers cannot be written
2911 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2912 * because that triggers -Wlogical-op on GCC 7.3. */
2913 if( block_size > sizeof( ipad ) )
2914 return( PSA_ERROR_NOT_SUPPORTED );
2915 if( block_size > sizeof( hmac->opad ) )
2916 return( PSA_ERROR_NOT_SUPPORTED );
2917 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002918 return( PSA_ERROR_NOT_SUPPORTED );
2919
Gilles Peskined223b522018-06-11 18:12:58 +02002920 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002921 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002922 status = psa_hash_compute( hash_alg, key, key_length,
2923 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002924 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002925 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002926 }
Gilles Peskine96889972018-07-12 17:07:03 +02002927 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2928 * but it is permitted. It is common when HMAC is used in HKDF, for
2929 * example. Don't call `memcpy` in the 0-length because `key` could be
2930 * an invalid pointer which would make the behavior undefined. */
2931 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002932 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002933
Gilles Peskined223b522018-06-11 18:12:58 +02002934 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2935 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002936 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002937 ipad[i] ^= 0x36;
2938 memset( ipad + key_length, 0x36, block_size - key_length );
2939
2940 /* Copy the key material from ipad to opad, flipping the requisite bits,
2941 * and filling the rest of opad with the requisite constant. */
2942 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002943 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2944 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002945
Gilles Peskine01126fa2018-07-12 17:04:55 +02002946 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002947 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002948 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002949
Gilles Peskine01126fa2018-07-12 17:04:55 +02002950 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002951
2952cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002953 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002954
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002955 return( status );
2956}
John Durkop6ba40d12020-11-10 08:50:04 -08002957#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002958
Gilles Peskine89167cb2018-07-08 20:12:23 +02002959static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002960 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002961 psa_algorithm_t alg,
2962 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002964 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002965 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002966 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002967 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002968 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002969
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002970 /* A context must be freshly initialized before it can be set up. */
2971 if( operation->alg != 0 )
2972 {
2973 return( PSA_ERROR_BAD_STATE );
2974 }
2975
Steven Cooreman15472f82021-03-02 16:16:22 +01002976 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002977 if( status != PSA_SUCCESS )
2978 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002979 if( is_sign )
2980 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002981
Ronald Cron5c522922020-11-14 16:35:34 +01002982 status = psa_get_and_lock_transparent_key_slot_with_policy(
2983 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002984 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002985 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002986
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002987 /* Validate the combination of key type and algorithm */
2988 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002989 if( status != PSA_SUCCESS )
2990 goto exit;
2991
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002992 /* Get the output length for the algorithm and key combination. None of the
2993 * currently supported algorithms have an output length dependent on actual
2994 * key size, so setting it to a bogus value is currently OK. */
2995 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002996
2997 if( operation->mac_size < 4 )
2998 {
2999 /* A very short MAC is too short for security since it can be
3000 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3001 * so we make this our minimum, even though 32 bits is still
3002 * too small for security. */
3003 status = PSA_ERROR_NOT_SUPPORTED;
3004 goto exit;
3005 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003006
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01003007 if( operation->mac_size >
3008 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
3009 {
3010 /* It's impossible to "truncate" to a larger length than the full length
3011 * of the algorithm. */
3012 status = PSA_ERROR_INVALID_ARGUMENT;
3013 goto exit;
3014 }
3015
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01003017 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003018 {
Steven Cooreman15472f82021-03-02 16:16:22 +01003019 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 }
3021 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003023#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01003024 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003025 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003026 /* Sanity check. This shouldn't fail on a valid configuration. */
3027 if( operation->mac_size == 0 ||
3028 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3029 {
3030 status = PSA_ERROR_NOT_SUPPORTED;
3031 goto exit;
3032 }
3033
Gilles Peskine8e338702019-07-30 20:06:31 +02003034 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003035 {
3036 status = PSA_ERROR_INVALID_ARGUMENT;
3037 goto exit;
3038 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003039
Gilles Peskine01126fa2018-07-12 17:04:55 +02003040 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003041 slot->key.data,
3042 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01003043 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003044 }
3045 else
John Durkopd0321952020-10-29 21:37:36 -07003046#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003047 {
3048 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003049 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003050
Gilles Peskinefbfac682018-07-08 20:51:54 +02003051exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003052 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003053 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003054 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003055 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003056 else
3057 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003058 operation->key_set = 1;
3059 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003060
Ronald Cron5c522922020-11-14 16:35:34 +01003061 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003062
Ronald Cron5c522922020-11-14 16:35:34 +01003063 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003064}
3065
Gilles Peskine89167cb2018-07-08 20:12:23 +02003066psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003067 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003068 psa_algorithm_t alg )
3069{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003070 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003071}
3072
3073psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003074 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003075 psa_algorithm_t alg )
3076{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003077 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003078}
3079
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3081 const uint8_t *input,
3082 size_t input_length )
3083{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003084 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003085 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003086 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003087 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003088 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089 operation->has_input = 1;
3090
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003092 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003093 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003094 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3095 input, input_length );
3096 status = mbedtls_to_psa_error( ret );
3097 }
3098 else
3099#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003100#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003101 if( PSA_ALG_IS_HMAC( operation->alg ) )
3102 {
3103 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3104 input_length );
3105 }
3106 else
John Durkopd0321952020-10-29 21:37:36 -07003107#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003108 {
3109 /* This shouldn't happen if `operation` was initialized by
3110 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003111 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003112 }
3113
Gilles Peskinefbfac682018-07-08 20:51:54 +02003114 if( status != PSA_SUCCESS )
3115 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003116 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003117}
3118
John Durkop6ba40d12020-11-10 08:50:04 -08003119#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003120static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3121 uint8_t *mac,
3122 size_t mac_size )
3123{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003124 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003125 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3126 size_t hash_size = 0;
3127 size_t block_size = psa_get_hash_block_size( hash_alg );
3128 psa_status_t status;
3129
Gilles Peskine01126fa2018-07-12 17:04:55 +02003130 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3131 if( status != PSA_SUCCESS )
3132 return( status );
3133 /* From here on, tmp needs to be wiped. */
3134
3135 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3136 if( status != PSA_SUCCESS )
3137 goto exit;
3138
3139 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3140 if( status != PSA_SUCCESS )
3141 goto exit;
3142
3143 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3144 if( status != PSA_SUCCESS )
3145 goto exit;
3146
Gilles Peskined911eb72018-08-14 15:18:45 +02003147 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3148 if( status != PSA_SUCCESS )
3149 goto exit;
3150
3151 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003152
3153exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003154 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003155 return( status );
3156}
John Durkop6ba40d12020-11-10 08:50:04 -08003157#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003158
mohammad16036df908f2018-04-02 08:34:15 -07003159static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003160 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003161 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003162{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003163 if( ! operation->key_set )
3164 return( PSA_ERROR_BAD_STATE );
3165 if( operation->iv_required && ! operation->iv_set )
3166 return( PSA_ERROR_BAD_STATE );
3167
Gilles Peskine8c9def32018-02-08 10:02:12 +01003168 if( mac_size < operation->mac_size )
3169 return( PSA_ERROR_BUFFER_TOO_SMALL );
3170
Gilles Peskine8c9def32018-02-08 10:02:12 +01003171#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003172 if( operation->alg == PSA_ALG_CMAC )
3173 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003174 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02003175 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3176 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003177 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003178 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003179 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003180 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003181 else
3182#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003183#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003184 if( PSA_ALG_IS_HMAC( operation->alg ) )
3185 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003186 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003187 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003188 }
3189 else
John Durkopd0321952020-10-29 21:37:36 -07003190#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003191 {
3192 /* This shouldn't happen if `operation` was initialized by
3193 * a setup function. */
3194 return( PSA_ERROR_BAD_STATE );
3195 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003196}
3197
Gilles Peskineacd4be32018-07-08 19:56:25 +02003198psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3199 uint8_t *mac,
3200 size_t mac_size,
3201 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003202{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003203 psa_status_t status;
3204
Jaeden Amero252ef282019-02-15 14:05:35 +00003205 if( operation->alg == 0 )
3206 {
3207 return( PSA_ERROR_BAD_STATE );
3208 }
3209
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003210 /* Fill the output buffer with something that isn't a valid mac
3211 * (barring an attack on the mac and deliberately-crafted input),
3212 * in case the caller doesn't check the return status properly. */
3213 *mac_length = mac_size;
3214 /* If mac_size is 0 then mac may be NULL and then the
3215 * call to memset would have undefined behavior. */
3216 if( mac_size != 0 )
3217 memset( mac, '!', mac_size );
3218
Gilles Peskine89167cb2018-07-08 20:12:23 +02003219 if( ! operation->is_sign )
3220 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003221 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003222 }
mohammad16036df908f2018-04-02 08:34:15 -07003223
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003224 status = psa_mac_finish_internal( operation, mac, mac_size );
3225
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003226 if( status == PSA_SUCCESS )
3227 {
3228 status = psa_mac_abort( operation );
3229 if( status == PSA_SUCCESS )
3230 *mac_length = operation->mac_size;
3231 else
3232 memset( mac, '!', mac_size );
3233 }
3234 else
3235 psa_mac_abort( operation );
3236 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003237}
3238
Gilles Peskineacd4be32018-07-08 19:56:25 +02003239psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3240 const uint8_t *mac,
3241 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003242{
Gilles Peskine828ed142018-06-18 23:25:51 +02003243 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003244 psa_status_t status;
3245
Jaeden Amero252ef282019-02-15 14:05:35 +00003246 if( operation->alg == 0 )
3247 {
3248 return( PSA_ERROR_BAD_STATE );
3249 }
3250
Gilles Peskine89167cb2018-07-08 20:12:23 +02003251 if( operation->is_sign )
3252 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003253 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003254 }
3255 if( operation->mac_size != mac_length )
3256 {
3257 status = PSA_ERROR_INVALID_SIGNATURE;
3258 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003259 }
mohammad16036df908f2018-04-02 08:34:15 -07003260
3261 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003262 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003263 if( status != PSA_SUCCESS )
3264 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003265
3266 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3267 status = PSA_ERROR_INVALID_SIGNATURE;
3268
3269cleanup:
3270 if( status == PSA_SUCCESS )
3271 status = psa_mac_abort( operation );
3272 else
3273 psa_mac_abort( operation );
3274
Gilles Peskine3f108122018-12-07 18:14:53 +01003275 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003276
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003277 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278}
3279
3280
Gilles Peskine20035e32018-02-03 22:44:14 +01003281
Gilles Peskine20035e32018-02-03 22:44:14 +01003282/****************************************************************/
3283/* Asymmetric cryptography */
3284/****************************************************************/
3285
John Durkop6ba40d12020-11-10 08:50:04 -08003286#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3287 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003288/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003289 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003290static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3291 size_t hash_length,
3292 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003293{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003294 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003295 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003296 *md_alg = mbedtls_md_get_type( md_info );
3297
3298 /* The Mbed TLS RSA module uses an unsigned int for hash length
3299 * parameters. Validate that it fits so that we don't risk an
3300 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003301#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003302 if( hash_length > UINT_MAX )
3303 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003304#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003305
John Durkop0e005192020-10-31 22:06:54 -07003306#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003307 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3308 * must be correct. */
3309 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3310 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003311 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003312 if( md_info == NULL )
3313 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003314 if( mbedtls_md_get_size( md_info ) != hash_length )
3315 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003316 }
John Durkop0e005192020-10-31 22:06:54 -07003317#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003318
John Durkop0e005192020-10-31 22:06:54 -07003319#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003320 /* PSS requires a hash internally. */
3321 if( PSA_ALG_IS_RSA_PSS( alg ) )
3322 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003323 if( md_info == NULL )
3324 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003325 }
John Durkop0e005192020-10-31 22:06:54 -07003326#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003327
Gilles Peskine61b91d42018-06-08 16:09:36 +02003328 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003329}
3330
Gilles Peskine2b450e32018-06-27 15:42:46 +02003331static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3332 psa_algorithm_t alg,
3333 const uint8_t *hash,
3334 size_t hash_length,
3335 uint8_t *signature,
3336 size_t signature_size,
3337 size_t *signature_length )
3338{
3339 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003340 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003341 mbedtls_md_type_t md_alg;
3342
3343 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3344 if( status != PSA_SUCCESS )
3345 return( status );
3346
Gilles Peskine630a18a2018-06-29 17:49:35 +02003347 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003348 return( PSA_ERROR_BUFFER_TOO_SMALL );
3349
John Durkop0e005192020-10-31 22:06:54 -07003350#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003351 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3352 {
3353 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3354 MBEDTLS_MD_NONE );
3355 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003356 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003357 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003358 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003359 md_alg,
3360 (unsigned int) hash_length,
3361 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003362 signature );
3363 }
3364 else
John Durkop0e005192020-10-31 22:06:54 -07003365#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3366#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003367 if( PSA_ALG_IS_RSA_PSS( alg ) )
3368 {
3369 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3370 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003371 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003372 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003373 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003374 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003375 (unsigned int) hash_length,
3376 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003377 signature );
3378 }
3379 else
John Durkop0e005192020-10-31 22:06:54 -07003380#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003381 {
3382 return( PSA_ERROR_INVALID_ARGUMENT );
3383 }
3384
3385 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003386 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003387 return( mbedtls_to_psa_error( ret ) );
3388}
3389
3390static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3391 psa_algorithm_t alg,
3392 const uint8_t *hash,
3393 size_t hash_length,
3394 const uint8_t *signature,
3395 size_t signature_length )
3396{
3397 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003398 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003399 mbedtls_md_type_t md_alg;
3400
3401 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3402 if( status != PSA_SUCCESS )
3403 return( status );
3404
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003405 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3406 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003407
John Durkop0e005192020-10-31 22:06:54 -07003408#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003409 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3410 {
3411 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3412 MBEDTLS_MD_NONE );
3413 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003414 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003415 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003416 MBEDTLS_RSA_PUBLIC,
3417 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003418 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003419 hash,
3420 signature );
3421 }
3422 else
John Durkop0e005192020-10-31 22:06:54 -07003423#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3424#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003425 if( PSA_ALG_IS_RSA_PSS( alg ) )
3426 {
3427 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3428 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003429 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003430 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003431 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003432 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003433 (unsigned int) hash_length,
3434 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003435 signature );
3436 }
3437 else
John Durkop0e005192020-10-31 22:06:54 -07003438#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003439 {
3440 return( PSA_ERROR_INVALID_ARGUMENT );
3441 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003442
3443 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3444 * the rest of the signature is invalid". This has little use in
3445 * practice and PSA doesn't report this distinction. */
3446 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3447 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003448 return( mbedtls_to_psa_error( ret ) );
3449}
John Durkop6ba40d12020-11-10 08:50:04 -08003450#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3451 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003452
John Durkop6ba40d12020-11-10 08:50:04 -08003453#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3454 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003455/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3456 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3457 * (even though these functions don't modify it). */
3458static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3459 psa_algorithm_t alg,
3460 const uint8_t *hash,
3461 size_t hash_length,
3462 uint8_t *signature,
3463 size_t signature_size,
3464 size_t *signature_length )
3465{
Janos Follath24eed8d2019-11-22 13:21:35 +00003466 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003468 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003469 mbedtls_mpi_init( &r );
3470 mbedtls_mpi_init( &s );
3471
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003472 if( signature_size < 2 * curve_bytes )
3473 {
3474 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3475 goto cleanup;
3476 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003477
John Durkop0ea39e02020-10-13 19:58:20 -07003478#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003479 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3480 {
3481 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3482 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3483 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003484 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3485 &ecp->d, hash,
3486 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003487 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003488 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003489 }
3490 else
John Durkop0ea39e02020-10-13 19:58:20 -07003491#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003492 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003493 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003494 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3495 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003496 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003497 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003498 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003499
3500 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3501 signature,
3502 curve_bytes ) );
3503 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3504 signature + curve_bytes,
3505 curve_bytes ) );
3506
3507cleanup:
3508 mbedtls_mpi_free( &r );
3509 mbedtls_mpi_free( &s );
3510 if( ret == 0 )
3511 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003512 return( mbedtls_to_psa_error( ret ) );
3513}
3514
3515static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3516 const uint8_t *hash,
3517 size_t hash_length,
3518 const uint8_t *signature,
3519 size_t signature_length )
3520{
Janos Follath24eed8d2019-11-22 13:21:35 +00003521 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003522 mbedtls_mpi r, s;
3523 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3524 mbedtls_mpi_init( &r );
3525 mbedtls_mpi_init( &s );
3526
3527 if( signature_length != 2 * curve_bytes )
3528 return( PSA_ERROR_INVALID_SIGNATURE );
3529
3530 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3531 signature,
3532 curve_bytes ) );
3533 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3534 signature + curve_bytes,
3535 curve_bytes ) );
3536
Steven Cooremanacda8342020-07-24 23:09:52 +02003537 /* Check whether the public part is loaded. If not, load it. */
3538 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3539 {
3540 MBEDTLS_MPI_CHK(
3541 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003542 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003543 }
3544
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003545 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3546 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003547
3548cleanup:
3549 mbedtls_mpi_free( &r );
3550 mbedtls_mpi_free( &s );
3551 return( mbedtls_to_psa_error( ret ) );
3552}
John Durkop6ba40d12020-11-10 08:50:04 -08003553#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3554 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003555
Ronald Croncf56a0a2020-08-04 09:51:30 +02003556psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003557 psa_algorithm_t alg,
3558 const uint8_t *hash,
3559 size_t hash_length,
3560 uint8_t *signature,
3561 size_t signature_size,
3562 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003563{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003564 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003565 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003566 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003567
3568 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003569 /* Immediately reject a zero-length signature buffer. This guarantees
3570 * that signature must be a valid pointer. (On the other hand, the hash
3571 * buffer can in principle be empty since it doesn't actually have
3572 * to be a hash.) */
3573 if( signature_size == 0 )
3574 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003575
Ronald Cron5c522922020-11-14 16:35:34 +01003576 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3577 PSA_KEY_USAGE_SIGN_HASH,
3578 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003580 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003581 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003582 {
3583 status = PSA_ERROR_INVALID_ARGUMENT;
3584 goto exit;
3585 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003586
Steven Cooremancd84cb42020-07-16 20:28:36 +02003587 /* Try any of the available accelerators first */
3588 status = psa_driver_wrapper_sign_hash( slot,
3589 alg,
3590 hash,
3591 hash_length,
3592 signature,
3593 signature_size,
3594 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003595 if( status != PSA_ERROR_NOT_SUPPORTED ||
3596 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003597 goto exit;
3598
Steven Cooreman7a250572020-07-17 16:43:05 +02003599 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003600#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3601 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003602 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003603 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003604 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003605
Ronald Cron90857082020-11-25 14:58:33 +01003606 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3607 slot->key.data,
3608 slot->key.bytes,
3609 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003610 if( status != PSA_SUCCESS )
3611 goto exit;
3612
Steven Cooremana2371e52020-07-28 14:30:39 +02003613 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003614 alg,
3615 hash, hash_length,
3616 signature, signature_size,
3617 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003618
Steven Cooremana2371e52020-07-28 14:30:39 +02003619 mbedtls_rsa_free( rsa );
3620 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003621 }
3622 else
John Durkop6ba40d12020-11-10 08:50:04 -08003623#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3624 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003625 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003626 {
John Durkop9814fa22020-11-04 12:28:15 -08003627#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3628 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003629 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003630#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003631 PSA_ALG_IS_ECDSA( alg )
3632#else
3633 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3634#endif
3635 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003636 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003637 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003638 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003639 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003640 slot->key.data,
3641 slot->key.bytes,
3642 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003643 if( status != PSA_SUCCESS )
3644 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003645 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003646 alg,
3647 hash, hash_length,
3648 signature, signature_size,
3649 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003650 mbedtls_ecp_keypair_free( ecp );
3651 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003652 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003653 else
John Durkop9814fa22020-11-04 12:28:15 -08003654#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3655 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003656 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003657 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003658 }
itayzafrir5c753392018-05-08 11:18:38 +03003659 }
3660 else
itayzafrir5c753392018-05-08 11:18:38 +03003661 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003662 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003663 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003664
3665exit:
3666 /* Fill the unused part of the output buffer (the whole buffer on error,
3667 * the trailing part on success) with something that isn't a valid mac
3668 * (barring an attack on the mac and deliberately-crafted input),
3669 * in case the caller doesn't check the return status properly. */
3670 if( status == PSA_SUCCESS )
3671 memset( signature + *signature_length, '!',
3672 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003673 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003674 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003675 /* If signature_size is 0 then we have nothing to do. We must not call
3676 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003677
Ronald Cron5c522922020-11-14 16:35:34 +01003678 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003679
Ronald Cron5c522922020-11-14 16:35:34 +01003680 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003681}
3682
Ronald Croncf56a0a2020-08-04 09:51:30 +02003683psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003684 psa_algorithm_t alg,
3685 const uint8_t *hash,
3686 size_t hash_length,
3687 const uint8_t *signature,
3688 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003689{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003690 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003691 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003692 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003693
Ronald Cron5c522922020-11-14 16:35:34 +01003694 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3695 PSA_KEY_USAGE_VERIFY_HASH,
3696 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003697 if( status != PSA_SUCCESS )
3698 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003699
Steven Cooreman55ae2172020-07-17 19:46:15 +02003700 /* Try any of the available accelerators first */
3701 status = psa_driver_wrapper_verify_hash( slot,
3702 alg,
3703 hash,
3704 hash_length,
3705 signature,
3706 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003707 if( status != PSA_ERROR_NOT_SUPPORTED ||
3708 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003709 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02003710
John Durkop6ba40d12020-11-10 08:50:04 -08003711#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3712 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003713 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003714 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003715 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003716
Ronald Cron90857082020-11-25 14:58:33 +01003717 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3718 slot->key.data,
3719 slot->key.bytes,
3720 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003721 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003722 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003723
Steven Cooremana2371e52020-07-28 14:30:39 +02003724 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02003725 alg,
3726 hash, hash_length,
3727 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003728 mbedtls_rsa_free( rsa );
3729 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003730 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003731 }
3732 else
John Durkop6ba40d12020-11-10 08:50:04 -08003733#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3734 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003735 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003736 {
John Durkop9814fa22020-11-04 12:28:15 -08003737#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3738 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003739 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003740 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003741 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003742 status = mbedtls_psa_ecp_load_representation( slot->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01003743 slot->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01003744 slot->key.data,
3745 slot->key.bytes,
3746 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003747 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003748 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003749 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02003750 hash, hash_length,
3751 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003752 mbedtls_ecp_keypair_free( ecp );
3753 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003754 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02003755 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003756 else
John Durkop9814fa22020-11-04 12:28:15 -08003757#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3758 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003759 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003760 status = PSA_ERROR_INVALID_ARGUMENT;
3761 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003762 }
itayzafrir5c753392018-05-08 11:18:38 +03003763 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003764 else
Gilles Peskine20035e32018-02-03 22:44:14 +01003765 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003766 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003767 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003768
3769exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003770 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003771
Ronald Cron5c522922020-11-14 16:35:34 +01003772 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01003773}
3774
John Durkop0e005192020-10-31 22:06:54 -07003775#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003776static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3777 mbedtls_rsa_context *rsa )
3778{
3779 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3780 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3781 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3782 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3783}
John Durkop0e005192020-10-31 22:06:54 -07003784#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003785
Ronald Croncf56a0a2020-08-04 09:51:30 +02003786psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003787 psa_algorithm_t alg,
3788 const uint8_t *input,
3789 size_t input_length,
3790 const uint8_t *salt,
3791 size_t salt_length,
3792 uint8_t *output,
3793 size_t output_size,
3794 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003795{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003796 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003797 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003798 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003799
Darryl Green5cc689a2018-07-24 15:34:10 +01003800 (void) input;
3801 (void) input_length;
3802 (void) salt;
3803 (void) output;
3804 (void) output_size;
3805
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003806 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003807
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003808 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3809 return( PSA_ERROR_INVALID_ARGUMENT );
3810
Ronald Cron5c522922020-11-14 16:35:34 +01003811 status = psa_get_and_lock_transparent_key_slot_with_policy(
3812 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003813 if( status != PSA_SUCCESS )
3814 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003815 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3816 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003817 {
3818 status = PSA_ERROR_INVALID_ARGUMENT;
3819 goto exit;
3820 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003821
John Durkop6ba40d12020-11-10 08:50:04 -08003822#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3823 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003824 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003825 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003826 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003827 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3828 slot->key.data,
3829 slot->key.bytes,
3830 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003831 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003832 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003833
3834 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003835 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003836 status = PSA_ERROR_BUFFER_TOO_SMALL;
3837 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003838 }
John Durkop0e005192020-10-31 22:06:54 -07003839#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003840 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003841 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003842 status = mbedtls_to_psa_error(
3843 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003844 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003845 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003846 MBEDTLS_RSA_PUBLIC,
3847 input_length,
3848 input,
3849 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003850 }
3851 else
John Durkop0e005192020-10-31 22:06:54 -07003852#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3853#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003854 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003855 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003856 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003857 status = mbedtls_to_psa_error(
3858 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003859 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003860 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003861 MBEDTLS_RSA_PUBLIC,
3862 salt, salt_length,
3863 input_length,
3864 input,
3865 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003866 }
3867 else
John Durkop0e005192020-10-31 22:06:54 -07003868#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003869 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003870 status = PSA_ERROR_INVALID_ARGUMENT;
3871 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003872 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003873rsa_exit:
3874 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003875 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003876
Steven Cooremana2371e52020-07-28 14:30:39 +02003877 mbedtls_rsa_free( rsa );
3878 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003879 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003880 else
John Durkop9814fa22020-11-04 12:28:15 -08003881#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003882 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003883 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003884 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003885 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003886
3887exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003888 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003889
Ronald Cron5c522922020-11-14 16:35:34 +01003890 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003891}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003892
Ronald Croncf56a0a2020-08-04 09:51:30 +02003893psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003894 psa_algorithm_t alg,
3895 const uint8_t *input,
3896 size_t input_length,
3897 const uint8_t *salt,
3898 size_t salt_length,
3899 uint8_t *output,
3900 size_t output_size,
3901 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003902{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003903 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003904 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003905 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003906
Darryl Green5cc689a2018-07-24 15:34:10 +01003907 (void) input;
3908 (void) input_length;
3909 (void) salt;
3910 (void) output;
3911 (void) output_size;
3912
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003913 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003914
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003915 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3916 return( PSA_ERROR_INVALID_ARGUMENT );
3917
Ronald Cron5c522922020-11-14 16:35:34 +01003918 status = psa_get_and_lock_transparent_key_slot_with_policy(
3919 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003920 if( status != PSA_SUCCESS )
3921 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003922 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003923 {
3924 status = PSA_ERROR_INVALID_ARGUMENT;
3925 goto exit;
3926 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003927
John Durkop6ba40d12020-11-10 08:50:04 -08003928#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3929 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003930 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003931 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003932 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003933 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3934 slot->key.data,
3935 slot->key.bytes,
3936 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003937 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003938 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003939
Steven Cooremana2371e52020-07-28 14:30:39 +02003940 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003941 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003942 status = PSA_ERROR_INVALID_ARGUMENT;
3943 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003944 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003945
John Durkop0e005192020-10-31 22:06:54 -07003946#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003947 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003948 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003949 status = mbedtls_to_psa_error(
3950 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003951 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003952 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003953 MBEDTLS_RSA_PRIVATE,
3954 output_length,
3955 input,
3956 output,
3957 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003958 }
3959 else
John Durkop0e005192020-10-31 22:06:54 -07003960#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3961#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003962 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003963 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003964 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003965 status = mbedtls_to_psa_error(
3966 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003967 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003968 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003969 MBEDTLS_RSA_PRIVATE,
3970 salt, salt_length,
3971 output_length,
3972 input,
3973 output,
3974 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003975 }
3976 else
John Durkop0e005192020-10-31 22:06:54 -07003977#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003978 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003979 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003980 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003981
Steven Cooreman4fed4552020-08-03 14:46:03 +02003982rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003983 mbedtls_rsa_free( rsa );
3984 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003985 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003986 else
John Durkop6ba40d12020-11-10 08:50:04 -08003987#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3988 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003989 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003990 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003991 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003992
3993exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003994 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003995
Ronald Cron5c522922020-11-14 16:35:34 +01003996 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003997}
Gilles Peskine20035e32018-02-03 22:44:14 +01003998
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003999
4000
mohammad1603503973b2018-03-12 15:59:30 +02004001/****************************************************************/
4002/* Symmetric cryptography */
4003/****************************************************************/
4004
Gilles Peskinee553c652018-06-04 16:22:46 +02004005static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004006 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004007 psa_algorithm_t alg,
4008 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004009{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004010 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004011 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004012 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004013 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004014 size_t key_bits;
4015 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004016 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4017 PSA_KEY_USAGE_ENCRYPT :
4018 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004019
Steven Cooremand3feccd2020-09-01 15:56:14 +02004020 /* A context must be freshly initialized before it can be set up. */
4021 if( operation->alg != 0 )
4022 return( PSA_ERROR_BAD_STATE );
4023
Steven Cooremana07b9972020-09-10 14:54:14 +02004024 /* The requested algorithm must be one that can be processed by cipher. */
4025 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004026 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004027
Steven Cooremanef8575e2020-09-11 11:44:50 +02004028 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004029 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004030 if( status != PSA_SUCCESS )
4031 goto exit;
4032
4033 /* Initialize the operation struct members, except for alg. The alg member
4034 * is used to indicate to psa_cipher_abort that there are resources to free,
4035 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004036 operation->key_set = 0;
4037 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004038 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004039 operation->iv_size = 0;
4040 operation->block_size = 0;
4041 if( alg == PSA_ALG_ECB_NO_PADDING )
4042 operation->iv_required = 0;
4043 else
4044 operation->iv_required = 1;
4045
Steven Cooremana07b9972020-09-10 14:54:14 +02004046 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004047 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004048 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004049 slot,
4050 alg );
4051 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004052 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004053 slot,
4054 alg );
4055
Steven Cooremanef8575e2020-09-11 11:44:50 +02004056 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004057 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004058 /* Once the driver context is initialised, it needs to be freed using
4059 * psa_cipher_abort. Indicate this through setting alg. */
4060 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004061 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004062
Steven Cooremand3feccd2020-09-01 15:56:14 +02004063 if( status != PSA_ERROR_NOT_SUPPORTED ||
4064 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4065 goto exit;
4066
Steven Cooremana07b9972020-09-10 14:54:14 +02004067 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004068 * available for the given algorithm & key. */
4069 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004070
Steven Cooremana07b9972020-09-10 14:54:14 +02004071 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004072 * psa_cipher_abort. Indicate there is something to be freed through setting
4073 * alg, and indicate the operation is being done using mbedtls crypto through
4074 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004075 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004076 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004077
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004078 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004079 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004080 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004081 {
4082 status = PSA_ERROR_NOT_SUPPORTED;
4083 goto exit;
4084 }
mohammad1603503973b2018-03-12 15:59:30 +02004085
mohammad1603503973b2018-03-12 15:59:30 +02004086 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004087 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004088 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004089
David Brown12ca5032021-02-11 11:02:00 -07004090#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004091 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004092 {
4093 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004094 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01004095 memcpy( keys, slot->key.data, 16 );
4096 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004097 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4098 keys,
4099 192, cipher_operation );
4100 }
4101 else
4102#endif
4103 {
4104 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004105 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004106 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004107 }
Moran Peker41deec42018-04-04 15:43:05 +03004108 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004109 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004110
David Brown63ca2602021-01-26 11:51:12 -07004111#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
4112 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004113 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004114 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004115 case PSA_ALG_CBC_NO_PADDING:
4116 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4117 MBEDTLS_PADDING_NONE );
4118 break;
4119 case PSA_ALG_CBC_PKCS7:
4120 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4121 MBEDTLS_PADDING_PKCS7 );
4122 break;
4123 default:
4124 /* The algorithm doesn't involve padding. */
4125 ret = 0;
4126 break;
4127 }
4128 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004129 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07004130#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02004131
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004132 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004133 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004134 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4135 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004136 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004137 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004138 }
David Brown1bfe4d72021-02-16 12:54:35 -07004139#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02004140 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004141 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004142 operation->iv_size = 12;
4143#endif
mohammad1603503973b2018-03-12 15:59:30 +02004144
Steven Cooreman7df02922020-09-09 15:28:49 +02004145 status = PSA_SUCCESS;
4146
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004147exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004148 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004149 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004150 if( status == PSA_SUCCESS )
4151 {
4152 /* Update operation flags for both driver and software implementations */
4153 operation->key_set = 1;
4154 }
4155 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004156 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004157
Ronald Cron5c522922020-11-14 16:35:34 +01004158 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004159
Ronald Cron5c522922020-11-14 16:35:34 +01004160 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004161}
4162
Gilles Peskinefe119512018-07-08 21:39:34 +02004163psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004164 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004165 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004166{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004167 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004168}
4169
Gilles Peskinefe119512018-07-08 21:39:34 +02004170psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004171 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004172 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004173{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004174 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004175}
4176
Gilles Peskinefe119512018-07-08 21:39:34 +02004177psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004178 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004179 size_t iv_size,
4180 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004181{
itayzafrir534bd7c2018-08-02 13:56:32 +03004182 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004183 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004184 if( operation->iv_set || ! operation->iv_required )
4185 {
4186 return( PSA_ERROR_BAD_STATE );
4187 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004188
Steven Cooremanef8575e2020-09-11 11:44:50 +02004189 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004190 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004191 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004192 iv,
4193 iv_size,
4194 iv_length );
4195 goto exit;
4196 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004197
Moran Peker41deec42018-04-04 15:43:05 +03004198 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004199 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004200 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004201 goto exit;
4202 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004203 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004204 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004205 if( ret != 0 )
4206 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004207 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004208 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004209 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004210
mohammad16038481e742018-03-18 13:57:31 +02004211 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004212 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004213
Moran Peker395db872018-05-31 14:07:14 +03004214exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004215 if( status == PSA_SUCCESS )
4216 operation->iv_set = 1;
4217 else
Moran Peker395db872018-05-31 14:07:14 +03004218 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004219 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004220}
4221
Gilles Peskinefe119512018-07-08 21:39:34 +02004222psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004223 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004224 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004225{
itayzafrir534bd7c2018-08-02 13:56:32 +03004226 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004228 if( operation->iv_set || ! operation->iv_required )
4229 {
4230 return( PSA_ERROR_BAD_STATE );
4231 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004232
Steven Cooremanef8575e2020-09-11 11:44:50 +02004233 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004234 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004235 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004236 iv,
4237 iv_length );
4238 goto exit;
4239 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004240
Moran Pekera28258c2018-05-29 16:25:04 +03004241 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004242 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004243 status = PSA_ERROR_INVALID_ARGUMENT;
4244 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004245 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004246 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4247 status = mbedtls_to_psa_error( ret );
4248exit:
4249 if( status == PSA_SUCCESS )
4250 operation->iv_set = 1;
4251 else
mohammad1603503973b2018-03-12 15:59:30 +02004252 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004253 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004254}
4255
Steven Cooreman177deba2020-09-07 17:14:14 +02004256/* Process input for which the algorithm is set to ECB mode. This requires
4257 * manual processing, since the PSA API is defined as being able to process
4258 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4259 * underlying mbedtls_cipher_update only takes full blocks. */
4260static psa_status_t psa_cipher_update_ecb_internal(
4261 mbedtls_cipher_context_t *ctx,
4262 const uint8_t *input,
4263 size_t input_length,
4264 uint8_t *output,
4265 size_t output_size,
4266 size_t *output_length )
4267{
4268 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4269 size_t block_size = ctx->cipher_info->block_size;
4270 size_t internal_output_length = 0;
4271 *output_length = 0;
4272
4273 if( input_length == 0 )
4274 {
4275 status = PSA_SUCCESS;
4276 goto exit;
4277 }
4278
4279 if( ctx->unprocessed_len > 0 )
4280 {
4281 /* Fill up to block size, and run the block if there's a full one. */
4282 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4283
4284 if( input_length < bytes_to_copy )
4285 bytes_to_copy = input_length;
4286
4287 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4288 input, bytes_to_copy );
4289 input_length -= bytes_to_copy;
4290 input += bytes_to_copy;
4291 ctx->unprocessed_len += bytes_to_copy;
4292
4293 if( ctx->unprocessed_len == block_size )
4294 {
4295 status = mbedtls_to_psa_error(
4296 mbedtls_cipher_update( ctx,
4297 ctx->unprocessed_data,
4298 block_size,
4299 output, &internal_output_length ) );
4300
4301 if( status != PSA_SUCCESS )
4302 goto exit;
4303
4304 output += internal_output_length;
4305 output_size -= internal_output_length;
4306 *output_length += internal_output_length;
4307 ctx->unprocessed_len = 0;
4308 }
4309 }
4310
4311 while( input_length >= block_size )
4312 {
4313 /* Run all full blocks we have, one by one */
4314 status = mbedtls_to_psa_error(
4315 mbedtls_cipher_update( ctx, input,
4316 block_size,
4317 output, &internal_output_length ) );
4318
4319 if( status != PSA_SUCCESS )
4320 goto exit;
4321
4322 input_length -= block_size;
4323 input += block_size;
4324
4325 output += internal_output_length;
4326 output_size -= internal_output_length;
4327 *output_length += internal_output_length;
4328 }
4329
4330 if( input_length > 0 )
4331 {
4332 /* Save unprocessed bytes for later processing */
4333 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4334 input, input_length );
4335 ctx->unprocessed_len += input_length;
4336 }
4337
4338 status = PSA_SUCCESS;
4339
4340exit:
4341 return( status );
4342}
4343
Gilles Peskinee553c652018-06-04 16:22:46 +02004344psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4345 const uint8_t *input,
4346 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004347 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004348 size_t output_size,
4349 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004350{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004351 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004352 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004353 if( operation->alg == 0 )
4354 {
4355 return( PSA_ERROR_BAD_STATE );
4356 }
4357 if( operation->iv_required && ! operation->iv_set )
4358 {
4359 return( PSA_ERROR_BAD_STATE );
4360 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004361
Steven Cooremanef8575e2020-09-11 11:44:50 +02004362 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004363 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004364 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004365 input,
4366 input_length,
4367 output,
4368 output_size,
4369 output_length );
4370 goto exit;
4371 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004372
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004373 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004374 {
4375 /* Take the unprocessed partial block left over from previous
4376 * update calls, if any, plus the input to this call. Remove
4377 * the last partial block, if any. You get the data that will be
4378 * output in this call. */
4379 expected_output_size =
4380 ( operation->ctx.cipher.unprocessed_len + input_length )
4381 / operation->block_size * operation->block_size;
4382 }
4383 else
4384 {
4385 expected_output_size = input_length;
4386 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004387
Gilles Peskine89d789c2018-06-04 17:17:16 +02004388 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004389 {
4390 status = PSA_ERROR_BUFFER_TOO_SMALL;
4391 goto exit;
4392 }
mohammad160382759612018-03-12 18:16:40 +02004393
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004394 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4395 {
4396 /* mbedtls_cipher_update has an API inconsistency: it will only
4397 * process a single block at a time in ECB mode. Abstract away that
4398 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004399 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4400 input,
4401 input_length,
4402 output,
4403 output_size,
4404 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004405 }
4406 else
4407 {
4408 status = mbedtls_to_psa_error(
4409 mbedtls_cipher_update( &operation->ctx.cipher, input,
4410 input_length, output, output_length ) );
4411 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004412exit:
4413 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004414 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004415 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004416}
4417
Gilles Peskinee553c652018-06-04 16:22:46 +02004418psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4419 uint8_t *output,
4420 size_t output_size,
4421 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004422{
David Saadab4ecc272019-02-14 13:48:10 +02004423 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004424 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004425 if( operation->alg == 0 )
4426 {
4427 return( PSA_ERROR_BAD_STATE );
4428 }
4429 if( operation->iv_required && ! operation->iv_set )
4430 {
4431 return( PSA_ERROR_BAD_STATE );
4432 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004433
Steven Cooremanef8575e2020-09-11 11:44:50 +02004434 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004435 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004436 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004437 output,
4438 output_size,
4439 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004440 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004441 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004442
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004443 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004444 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004445 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004446 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004447 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004448 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004449 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004450 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004451 }
4452
Steven Cooremanef8575e2020-09-11 11:44:50 +02004453 status = mbedtls_to_psa_error(
4454 mbedtls_cipher_finish( &operation->ctx.cipher,
4455 temp_output_buffer,
4456 output_length ) );
4457 if( status != PSA_SUCCESS )
4458 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004459
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004460 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004461 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004462 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004463 memcpy( output, temp_output_buffer, *output_length );
4464 else
Janos Follath315b51c2018-07-09 16:04:51 +01004465 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004466
Steven Cooremanef8575e2020-09-11 11:44:50 +02004467exit:
4468 if( operation->mbedtls_in_use == 1 )
4469 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004470
Steven Cooremanef8575e2020-09-11 11:44:50 +02004471 if( status == PSA_SUCCESS )
4472 return( psa_cipher_abort( operation ) );
4473 else
4474 {
4475 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004476 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004477
Steven Cooremanef8575e2020-09-11 11:44:50 +02004478 return( status );
4479 }
mohammad1603503973b2018-03-12 15:59:30 +02004480}
4481
Gilles Peskinee553c652018-06-04 16:22:46 +02004482psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4483{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004484 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004485 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004486 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004487 * in use. It's ok to call abort on such an object, and there's
4488 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004489 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004490 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004491
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004492 /* Sanity check (shouldn't happen: operation->alg should
4493 * always have been initialized to a valid value). */
4494 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4495 return( PSA_ERROR_BAD_STATE );
4496
Steven Cooremanef8575e2020-09-11 11:44:50 +02004497 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004498 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004499 else
4500 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004501
Moran Peker41deec42018-04-04 15:43:05 +03004502 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004503 operation->key_set = 0;
4504 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004505 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004506 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004507 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004508 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004509
Moran Peker395db872018-05-31 14:07:14 +03004510 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004511}
4512
Gilles Peskinea0655c32018-04-30 17:06:50 +02004513
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004514
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004515
mohammad16035955c982018-04-26 00:53:03 +03004516/****************************************************************/
4517/* AEAD */
4518/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004519
Gilles Peskineedf9a652018-08-17 18:11:56 +02004520typedef struct
4521{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004522 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004523 const mbedtls_cipher_info_t *cipher_info;
4524 union
4525 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004526 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004527#if defined(MBEDTLS_CCM_C)
4528 mbedtls_ccm_context ccm;
4529#endif /* MBEDTLS_CCM_C */
4530#if defined(MBEDTLS_GCM_C)
4531 mbedtls_gcm_context gcm;
4532#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004533#if defined(MBEDTLS_CHACHAPOLY_C)
4534 mbedtls_chachapoly_context chachapoly;
4535#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004536 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004537 psa_algorithm_t core_alg;
4538 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004539 uint8_t tag_length;
4540} aead_operation_t;
4541
Ronald Cronf95a2b12020-10-22 15:24:49 +02004542#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4543
Gilles Peskine30a9e412019-01-14 18:36:12 +01004544static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004545{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004546 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004547 {
4548#if defined(MBEDTLS_CCM_C)
4549 case PSA_ALG_CCM:
4550 mbedtls_ccm_free( &operation->ctx.ccm );
4551 break;
4552#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004553#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004554 case PSA_ALG_GCM:
4555 mbedtls_gcm_free( &operation->ctx.gcm );
4556 break;
4557#endif /* MBEDTLS_GCM_C */
4558 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004559
Ronald Cron5c522922020-11-14 16:35:34 +01004560 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004561}
4562
4563static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004564 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004565 psa_key_usage_t usage,
4566 psa_algorithm_t alg )
4567{
4568 psa_status_t status;
4569 size_t key_bits;
4570 mbedtls_cipher_id_t cipher_id;
4571
Ronald Cron5c522922020-11-14 16:35:34 +01004572 status = psa_get_and_lock_transparent_key_slot_with_policy(
4573 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004574 if( status != PSA_SUCCESS )
4575 return( status );
4576
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004577 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004578
4579 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004580 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004581 &cipher_id );
4582 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004583 {
4584 status = PSA_ERROR_NOT_SUPPORTED;
4585 goto cleanup;
4586 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004587
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004588 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004589 {
4590#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004591 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004592 operation->core_alg = PSA_ALG_CCM;
4593 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004594 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4595 * The call to mbedtls_ccm_encrypt_and_tag or
4596 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004597 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004598 {
4599 status = PSA_ERROR_INVALID_ARGUMENT;
4600 goto cleanup;
4601 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004602 mbedtls_ccm_init( &operation->ctx.ccm );
4603 status = mbedtls_to_psa_error(
4604 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004605 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004606 (unsigned int) key_bits ) );
4607 if( status != 0 )
4608 goto cleanup;
4609 break;
4610#endif /* MBEDTLS_CCM_C */
4611
4612#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004613 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004614 operation->core_alg = PSA_ALG_GCM;
4615 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004616 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4617 * The call to mbedtls_gcm_crypt_and_tag or
4618 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004619 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004620 {
4621 status = PSA_ERROR_INVALID_ARGUMENT;
4622 goto cleanup;
4623 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004624 mbedtls_gcm_init( &operation->ctx.gcm );
4625 status = mbedtls_to_psa_error(
4626 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004627 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004628 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004629 if( status != 0 )
4630 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004631 break;
4632#endif /* MBEDTLS_GCM_C */
4633
Gilles Peskine26869f22019-05-06 15:25:00 +02004634#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004635 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004636 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4637 operation->full_tag_length = 16;
4638 /* We only support the default tag length. */
4639 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004640 {
4641 status = PSA_ERROR_NOT_SUPPORTED;
4642 goto cleanup;
4643 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004644 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4645 status = mbedtls_to_psa_error(
4646 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004647 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004648 if( status != 0 )
4649 goto cleanup;
4650 break;
4651#endif /* MBEDTLS_CHACHAPOLY_C */
4652
Gilles Peskineedf9a652018-08-17 18:11:56 +02004653 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004654 status = PSA_ERROR_NOT_SUPPORTED;
4655 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004656 }
4657
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004658 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4659 {
4660 status = PSA_ERROR_INVALID_ARGUMENT;
4661 goto cleanup;
4662 }
4663 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004664
Gilles Peskineedf9a652018-08-17 18:11:56 +02004665 return( PSA_SUCCESS );
4666
4667cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004668 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004669 return( status );
4670}
4671
Ronald Croncf56a0a2020-08-04 09:51:30 +02004672psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004673 psa_algorithm_t alg,
4674 const uint8_t *nonce,
4675 size_t nonce_length,
4676 const uint8_t *additional_data,
4677 size_t additional_data_length,
4678 const uint8_t *plaintext,
4679 size_t plaintext_length,
4680 uint8_t *ciphertext,
4681 size_t ciphertext_size,
4682 size_t *ciphertext_length )
4683{
mohammad16035955c982018-04-26 00:53:03 +03004684 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004685 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004686 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004687
mohammad1603f08a5502018-06-03 15:05:47 +03004688 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004689
Ronald Croncf56a0a2020-08-04 09:51:30 +02004690 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004691 if( status != PSA_SUCCESS )
4692 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004693
Gilles Peskineedf9a652018-08-17 18:11:56 +02004694 /* For all currently supported modes, the tag is at the end of the
4695 * ciphertext. */
4696 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4697 {
4698 status = PSA_ERROR_BUFFER_TOO_SMALL;
4699 goto exit;
4700 }
4701 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004702
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004703#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004704 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004705 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004706 status = mbedtls_to_psa_error(
4707 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4708 MBEDTLS_GCM_ENCRYPT,
4709 plaintext_length,
4710 nonce, nonce_length,
4711 additional_data, additional_data_length,
4712 plaintext, ciphertext,
4713 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004714 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004715 else
4716#endif /* MBEDTLS_GCM_C */
4717#if defined(MBEDTLS_CCM_C)
4718 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004719 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004720 status = mbedtls_to_psa_error(
4721 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4722 plaintext_length,
4723 nonce, nonce_length,
4724 additional_data,
4725 additional_data_length,
4726 plaintext, ciphertext,
4727 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004728 }
mohammad16035c8845f2018-05-09 05:40:09 -07004729 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004730#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004731#if defined(MBEDTLS_CHACHAPOLY_C)
4732 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4733 {
4734 if( nonce_length != 12 || operation.tag_length != 16 )
4735 {
4736 status = PSA_ERROR_NOT_SUPPORTED;
4737 goto exit;
4738 }
4739 status = mbedtls_to_psa_error(
4740 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4741 plaintext_length,
4742 nonce,
4743 additional_data,
4744 additional_data_length,
4745 plaintext,
4746 ciphertext,
4747 tag ) );
4748 }
4749 else
4750#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004751 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004752 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004753 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004754 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004755
Gilles Peskineedf9a652018-08-17 18:11:56 +02004756 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4757 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004758
Gilles Peskineedf9a652018-08-17 18:11:56 +02004759exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004760 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004761 if( status == PSA_SUCCESS )
4762 *ciphertext_length = plaintext_length + operation.tag_length;
4763 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004764}
4765
Gilles Peskineee652a32018-06-01 19:23:52 +02004766/* Locate the tag in a ciphertext buffer containing the encrypted data
4767 * followed by the tag. Return the length of the part preceding the tag in
4768 * *plaintext_length. This is the size of the plaintext in modes where
4769 * the encrypted data has the same size as the plaintext, such as
4770 * CCM and GCM. */
4771static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4772 const uint8_t *ciphertext,
4773 size_t ciphertext_length,
4774 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004775 const uint8_t **p_tag )
4776{
4777 size_t payload_length;
4778 if( tag_length > ciphertext_length )
4779 return( PSA_ERROR_INVALID_ARGUMENT );
4780 payload_length = ciphertext_length - tag_length;
4781 if( payload_length > plaintext_size )
4782 return( PSA_ERROR_BUFFER_TOO_SMALL );
4783 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004784 return( PSA_SUCCESS );
4785}
4786
Ronald Croncf56a0a2020-08-04 09:51:30 +02004787psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004788 psa_algorithm_t alg,
4789 const uint8_t *nonce,
4790 size_t nonce_length,
4791 const uint8_t *additional_data,
4792 size_t additional_data_length,
4793 const uint8_t *ciphertext,
4794 size_t ciphertext_length,
4795 uint8_t *plaintext,
4796 size_t plaintext_size,
4797 size_t *plaintext_length )
4798{
mohammad16035955c982018-04-26 00:53:03 +03004799 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004800 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004801 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004802
Gilles Peskineee652a32018-06-01 19:23:52 +02004803 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004804
Ronald Croncf56a0a2020-08-04 09:51:30 +02004805 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004806 if( status != PSA_SUCCESS )
4807 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004808
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004809 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4810 ciphertext, ciphertext_length,
4811 plaintext_size, &tag );
4812 if( status != PSA_SUCCESS )
4813 goto exit;
4814
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004815#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004816 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004817 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004818 status = mbedtls_to_psa_error(
4819 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4820 ciphertext_length - operation.tag_length,
4821 nonce, nonce_length,
4822 additional_data,
4823 additional_data_length,
4824 tag, operation.tag_length,
4825 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004826 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004827 else
4828#endif /* MBEDTLS_GCM_C */
4829#if defined(MBEDTLS_CCM_C)
4830 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004831 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004832 status = mbedtls_to_psa_error(
4833 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4834 ciphertext_length - operation.tag_length,
4835 nonce, nonce_length,
4836 additional_data,
4837 additional_data_length,
4838 ciphertext, plaintext,
4839 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004840 }
mohammad160339574652018-06-01 04:39:53 -07004841 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004842#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004843#if defined(MBEDTLS_CHACHAPOLY_C)
4844 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4845 {
4846 if( nonce_length != 12 || operation.tag_length != 16 )
4847 {
4848 status = PSA_ERROR_NOT_SUPPORTED;
4849 goto exit;
4850 }
4851 status = mbedtls_to_psa_error(
4852 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4853 ciphertext_length - operation.tag_length,
4854 nonce,
4855 additional_data,
4856 additional_data_length,
4857 tag,
4858 ciphertext,
4859 plaintext ) );
4860 }
4861 else
4862#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004863 {
mohammad1603554faad2018-06-03 15:07:38 +03004864 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004865 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004866
Gilles Peskineedf9a652018-08-17 18:11:56 +02004867 if( status != PSA_SUCCESS && plaintext_size != 0 )
4868 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004869
Gilles Peskineedf9a652018-08-17 18:11:56 +02004870exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004871 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004872 if( status == PSA_SUCCESS )
4873 *plaintext_length = ciphertext_length - operation.tag_length;
4874 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004875}
4876
Gilles Peskinea0655c32018-04-30 17:06:50 +02004877
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004878
Gilles Peskine20035e32018-02-03 22:44:14 +01004879/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004880/* Generators */
4881/****************************************************************/
4882
John Durkop07cc04a2020-11-16 22:08:34 -08004883#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4884 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4885 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4886#define AT_LEAST_ONE_BUILTIN_KDF
4887#endif
4888
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889#define HKDF_STATE_INIT 0 /* no input yet */
4890#define HKDF_STATE_STARTED 1 /* got salt */
4891#define HKDF_STATE_KEYED 2 /* got key */
4892#define HKDF_STATE_OUTPUT 3 /* output started */
4893
Gilles Peskinecbe66502019-05-16 16:59:18 +02004894static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004895 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004897 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4898 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901}
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004904{
4905 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004906 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004907 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004908 {
4909 /* The object has (apparently) been initialized but it is not
4910 * in use. It's ok to call abort on such an object, and there's
4911 * nothing to do. */
4912 }
4913 else
John Durkopd0321952020-10-29 21:37:36 -07004914#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004915 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004916 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004917 mbedtls_free( operation->ctx.hkdf.info );
4918 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004919 }
John Durkop07cc04a2020-11-16 22:08:34 -08004920 else
4921#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4922#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4923 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4924 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004927 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004928 if( operation->ctx.tls12_prf.seed != NULL )
4929 {
4930 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4931 operation->ctx.tls12_prf.seed_length );
4932 mbedtls_free( operation->ctx.tls12_prf.seed );
4933 }
4934
4935 if( operation->ctx.tls12_prf.label != NULL )
4936 {
4937 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4938 operation->ctx.tls12_prf.label_length );
4939 mbedtls_free( operation->ctx.tls12_prf.label );
4940 }
4941
4942 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4943
4944 /* We leave the fields Ai and output_block to be erased safely by the
4945 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004946 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004947 else
John Durkop07cc04a2020-11-16 22:08:34 -08004948#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4949 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004950 {
4951 status = PSA_ERROR_BAD_STATE;
4952 }
Janos Follath083036a2019-06-11 10:22:26 +01004953 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004954 return( status );
4955}
4956
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004957psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004958 size_t *capacity)
4959{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004960 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004961 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004962 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004963 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004964 }
4965
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004966 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004967 return( PSA_SUCCESS );
4968}
4969
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004970psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004971 size_t capacity )
4972{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004973 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004975 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004976 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004977 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004978 return( PSA_SUCCESS );
4979}
4980
John Durkopd0321952020-10-29 21:37:36 -07004981#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004982/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004983 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004984static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004985 psa_algorithm_t hash_alg,
4986 uint8_t *output,
4987 size_t output_length )
4988{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004989 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004990 psa_status_t status;
4991
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004992 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4993 return( PSA_ERROR_BAD_STATE );
4994 hkdf->state = HKDF_STATE_OUTPUT;
4995
Gilles Peskinebef7f142018-07-12 17:22:21 +02004996 while( output_length != 0 )
4997 {
4998 /* Copy what remains of the current block */
4999 uint8_t n = hash_length - hkdf->offset_in_block;
5000 if( n > output_length )
5001 n = (uint8_t) output_length;
5002 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5003 output += n;
5004 output_length -= n;
5005 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005006 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005007 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005008 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005009 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005010 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005011 * object was corrupted or if this function is called directly
5012 * inside the library. */
5013 if( hkdf->block_number == 0xff )
5014 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005015
5016 /* We need a new block */
5017 ++hkdf->block_number;
5018 hkdf->offset_in_block = 0;
5019 status = psa_hmac_setup_internal( &hkdf->hmac,
5020 hkdf->prk, hash_length,
5021 hash_alg );
5022 if( status != PSA_SUCCESS )
5023 return( status );
5024 if( hkdf->block_number != 1 )
5025 {
5026 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5027 hkdf->output_block,
5028 hash_length );
5029 if( status != PSA_SUCCESS )
5030 return( status );
5031 }
5032 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5033 hkdf->info,
5034 hkdf->info_length );
5035 if( status != PSA_SUCCESS )
5036 return( status );
5037 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5038 &hkdf->block_number, 1 );
5039 if( status != PSA_SUCCESS )
5040 return( status );
5041 status = psa_hmac_finish_internal( &hkdf->hmac,
5042 hkdf->output_block,
5043 sizeof( hkdf->output_block ) );
5044 if( status != PSA_SUCCESS )
5045 return( status );
5046 }
5047
5048 return( PSA_SUCCESS );
5049}
John Durkop07cc04a2020-11-16 22:08:34 -08005050#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005051
John Durkop07cc04a2020-11-16 22:08:34 -08005052#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5053 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005054static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5055 psa_tls12_prf_key_derivation_t *tls12_prf,
5056 psa_algorithm_t alg )
5057{
5058 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005059 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005060 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5061 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005062
Janos Follath7742fee2019-06-17 12:58:10 +01005063 /* We can't be wanting more output after block 0xff, otherwise
5064 * the capacity check in psa_key_derivation_output_bytes() would have
5065 * prevented this call. It could happen only if the operation
5066 * object was corrupted or if this function is called directly
5067 * inside the library. */
5068 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005069 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005070
5071 /* We need a new block */
5072 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005073 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005074
5075 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5076 *
5077 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5078 *
5079 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5080 * HMAC_hash(secret, A(2) + seed) +
5081 * HMAC_hash(secret, A(3) + seed) + ...
5082 *
5083 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005084 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005085 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005086 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005087 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005088 * is currently extracted as `output_block` and where i is
5089 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005090 */
5091
Janos Follathea29bfb2019-06-19 12:21:20 +01005092 /* Save the hash context before using it, to preserve the hash state with
5093 * only the inner padding in it. We need this, because inner padding depends
5094 * on the key (secret in the RFC's terminology). */
5095 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5096 if( status != PSA_SUCCESS )
5097 goto cleanup;
5098
5099 /* Calculate A(i) where i = tls12_prf->block_number. */
5100 if( tls12_prf->block_number == 1 )
5101 {
5102 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5103 * the variable seed and in this instance means it in the context of the
5104 * P_hash function, where seed = label + seed.) */
5105 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5106 tls12_prf->label, tls12_prf->label_length );
5107 if( status != PSA_SUCCESS )
5108 goto cleanup;
5109 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5110 tls12_prf->seed, tls12_prf->seed_length );
5111 if( status != PSA_SUCCESS )
5112 goto cleanup;
5113 }
5114 else
5115 {
5116 /* A(i) = HMAC_hash(secret, A(i-1)) */
5117 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5118 tls12_prf->Ai, hash_length );
5119 if( status != PSA_SUCCESS )
5120 goto cleanup;
5121 }
5122
5123 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5124 tls12_prf->Ai, hash_length );
5125 if( status != PSA_SUCCESS )
5126 goto cleanup;
5127 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5128 if( status != PSA_SUCCESS )
5129 goto cleanup;
5130
5131 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5132 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5133 tls12_prf->Ai, hash_length );
5134 if( status != PSA_SUCCESS )
5135 goto cleanup;
5136 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5137 tls12_prf->label, tls12_prf->label_length );
5138 if( status != PSA_SUCCESS )
5139 goto cleanup;
5140 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5141 tls12_prf->seed, tls12_prf->seed_length );
5142 if( status != PSA_SUCCESS )
5143 goto cleanup;
5144 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5145 tls12_prf->output_block, hash_length );
5146 if( status != PSA_SUCCESS )
5147 goto cleanup;
5148 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5149 if( status != PSA_SUCCESS )
5150 goto cleanup;
5151
Janos Follath7742fee2019-06-17 12:58:10 +01005152
5153cleanup:
5154
Janos Follathea29bfb2019-06-19 12:21:20 +01005155 cleanup_status = psa_hash_abort( &backup );
5156 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5157 status = cleanup_status;
5158
5159 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005160}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005161
Janos Follath844eb0e2019-06-19 12:10:49 +01005162static psa_status_t psa_key_derivation_tls12_prf_read(
5163 psa_tls12_prf_key_derivation_t *tls12_prf,
5164 psa_algorithm_t alg,
5165 uint8_t *output,
5166 size_t output_length )
5167{
5168 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005169 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01005170 psa_status_t status;
5171 uint8_t offset, length;
5172
5173 while( output_length != 0 )
5174 {
5175 /* Check if we have fully processed the current block. */
5176 if( tls12_prf->left_in_block == 0 )
5177 {
5178 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5179 alg );
5180 if( status != PSA_SUCCESS )
5181 return( status );
5182
5183 continue;
5184 }
5185
5186 if( tls12_prf->left_in_block > output_length )
5187 length = (uint8_t) output_length;
5188 else
5189 length = tls12_prf->left_in_block;
5190
5191 offset = hash_length - tls12_prf->left_in_block;
5192 memcpy( output, tls12_prf->output_block + offset, length );
5193 output += length;
5194 output_length -= length;
5195 tls12_prf->left_in_block -= length;
5196 }
5197
5198 return( PSA_SUCCESS );
5199}
John Durkop07cc04a2020-11-16 22:08:34 -08005200#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5201 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005202
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005203psa_status_t psa_key_derivation_output_bytes(
5204 psa_key_derivation_operation_t *operation,
5205 uint8_t *output,
5206 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005207{
5208 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005210
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005212 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005213 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005214 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005215 }
5216
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005218 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005219 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005220 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005221 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005222 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005223 goto exit;
5224 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005225 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005226 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005227 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005228 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005229 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5230 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005231 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005232 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005233 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005234 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005235 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005236
John Durkopd0321952020-10-29 21:37:36 -07005237#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005238 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005239 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005240 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005241 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005242 output, output_length );
5243 }
Janos Follathadbec812019-06-14 11:05:39 +01005244 else
John Durkop07cc04a2020-11-16 22:08:34 -08005245#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5246#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5247 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005248 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005249 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005250 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005251 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005252 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005253 output_length );
5254 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005255 else
John Durkop07cc04a2020-11-16 22:08:34 -08005256#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5257 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005258 {
Steven Cooreman74afe472021-02-10 17:19:22 +01005259 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005260 return( PSA_ERROR_BAD_STATE );
5261 }
5262
5263exit:
5264 if( status != PSA_SUCCESS )
5265 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005266 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 * This allows us to differentiate between exhausted operations and
5268 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5269 * operations. */
5270 psa_algorithm_t alg = operation->alg;
5271 psa_key_derivation_abort( operation );
5272 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005273 memset( output, '!', output_length );
5274 }
5275 return( status );
5276}
5277
David Brown7807bf72021-02-09 16:28:23 -07005278#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02005279static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5280{
5281 if( data_size >= 8 )
5282 mbedtls_des_key_set_parity( data );
5283 if( data_size >= 16 )
5284 mbedtls_des_key_set_parity( data + 8 );
5285 if( data_size >= 24 )
5286 mbedtls_des_key_set_parity( data + 16 );
5287}
David Brown7807bf72021-02-09 16:28:23 -07005288#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02005289
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005290static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005291 psa_key_slot_t *slot,
5292 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005293 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005294{
5295 uint8_t *data = NULL;
5296 size_t bytes = PSA_BITS_TO_BYTES( bits );
5297 psa_status_t status;
5298
Gilles Peskine8e338702019-07-30 20:06:31 +02005299 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005300 return( PSA_ERROR_INVALID_ARGUMENT );
5301 if( bits % 8 != 0 )
5302 return( PSA_ERROR_INVALID_ARGUMENT );
5303 data = mbedtls_calloc( 1, bytes );
5304 if( data == NULL )
5305 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5306
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308 if( status != PSA_SUCCESS )
5309 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07005310#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02005311 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005312 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07005313#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01005314
5315 status = psa_allocate_buffer_to_slot( slot, bytes );
5316 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00005317 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01005318
Ronald Cronbf33c932020-11-28 18:06:53 +01005319 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01005320 psa_key_attributes_t attributes = {
5321 .core = slot->attr
5322 };
5323
Ronald Cronbf33c932020-11-28 18:06:53 +01005324 status = psa_driver_wrapper_import_key( &attributes,
5325 data, bytes,
5326 slot->key.data,
5327 slot->key.bytes,
5328 &slot->key.bytes, &bits );
5329 if( bits != slot->attr.bits )
5330 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005331
5332exit:
5333 mbedtls_free( data );
5334 return( status );
5335}
5336
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005337psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005338 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005339 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005340{
5341 psa_status_t status;
5342 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005343 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005344
Ronald Cron81709fc2020-11-14 12:10:32 +01005345 *key = MBEDTLS_SVC_KEY_ID_INIT;
5346
Gilles Peskine0f84d622019-09-12 19:03:13 +02005347 /* Reject any attempt to create a zero-length key so that we don't
5348 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5349 if( psa_get_key_bits( attributes ) == 0 )
5350 return( PSA_ERROR_INVALID_ARGUMENT );
5351
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005352 if( ! operation->can_output_key )
5353 return( PSA_ERROR_NOT_PERMITTED );
5354
Ronald Cron81709fc2020-11-14 12:10:32 +01005355 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5356 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005357#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5358 if( driver != NULL )
5359 {
5360 /* Deriving a key in a secure element is not implemented yet. */
5361 status = PSA_ERROR_NOT_SUPPORTED;
5362 }
5363#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005364 if( status == PSA_SUCCESS )
5365 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005366 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005367 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005369 }
5370 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005371 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005372 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005373 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005374
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005375 return( status );
5376}
5377
Gilles Peskineeab56e42018-07-12 17:12:33 +02005378
5379
5380/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005381/* Key derivation */
5382/****************************************************************/
5383
Steven Cooreman932ffb72021-02-15 12:14:32 +01005384#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005385static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005386 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005387 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005388{
John Durkop07cc04a2020-11-16 22:08:34 -08005389 int is_kdf_alg_supported;
5390
Janos Follath5fe19732019-06-20 15:09:30 +01005391 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5392 * initialisers for this union leave some bytes unspecified.) */
5393 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5394
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005396#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005397 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5398 is_kdf_alg_supported = 1;
5399 else
5400#endif
5401#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5402 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5403 is_kdf_alg_supported = 1;
5404 else
5405#endif
5406#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5407 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5408 is_kdf_alg_supported = 1;
5409 else
5410#endif
5411 is_kdf_alg_supported = 0;
5412
5413 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005414 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005415 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005416 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005417 if( hash_size == 0 )
5418 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005419 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5420 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005421 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005422 {
5423 return( PSA_ERROR_NOT_SUPPORTED );
5424 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005425 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005426 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005427 }
John Durkop07cc04a2020-11-16 22:08:34 -08005428
5429 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005430}
John Durkop07cc04a2020-11-16 22:08:34 -08005431#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005432
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005433psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005434 psa_algorithm_t alg )
5435{
5436 psa_status_t status;
5437
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005438 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005439 return( PSA_ERROR_BAD_STATE );
5440
Gilles Peskine6843c292019-01-18 16:44:49 +01005441 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5442 return( PSA_ERROR_INVALID_ARGUMENT );
5443 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005444 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005445#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005446 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005447 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005448#else
5449 return( PSA_ERROR_NOT_SUPPORTED );
5450#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005451 }
5452 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5453 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01005454#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005455 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01005456#else
5457 return( PSA_ERROR_NOT_SUPPORTED );
5458#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005459 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005460 else
5461 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005462
5463 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005464 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005465 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005466}
5467
John Durkopd0321952020-10-29 21:37:36 -07005468#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005469static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005470 psa_algorithm_t hash_alg,
5471 psa_key_derivation_step_t step,
5472 const uint8_t *data,
5473 size_t data_length )
5474{
5475 psa_status_t status;
5476 switch( step )
5477 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005478 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005479 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005480 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005481 status = psa_hmac_setup_internal( &hkdf->hmac,
5482 data, data_length,
5483 hash_alg );
5484 if( status != PSA_SUCCESS )
5485 return( status );
5486 hkdf->state = HKDF_STATE_STARTED;
5487 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005488 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005489 /* If no salt was provided, use an empty salt. */
5490 if( hkdf->state == HKDF_STATE_INIT )
5491 {
5492 status = psa_hmac_setup_internal( &hkdf->hmac,
5493 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005494 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005495 if( status != PSA_SUCCESS )
5496 return( status );
5497 hkdf->state = HKDF_STATE_STARTED;
5498 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005499 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005500 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005501 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5502 data, data_length );
5503 if( status != PSA_SUCCESS )
5504 return( status );
5505 status = psa_hmac_finish_internal( &hkdf->hmac,
5506 hkdf->prk,
5507 sizeof( hkdf->prk ) );
5508 if( status != PSA_SUCCESS )
5509 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005510 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005511 hkdf->block_number = 0;
5512 hkdf->state = HKDF_STATE_KEYED;
5513 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005514 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005515 if( hkdf->state == HKDF_STATE_OUTPUT )
5516 return( PSA_ERROR_BAD_STATE );
5517 if( hkdf->info_set )
5518 return( PSA_ERROR_BAD_STATE );
5519 hkdf->info_length = data_length;
5520 if( data_length != 0 )
5521 {
5522 hkdf->info = mbedtls_calloc( 1, data_length );
5523 if( hkdf->info == NULL )
5524 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5525 memcpy( hkdf->info, data, data_length );
5526 }
5527 hkdf->info_set = 1;
5528 return( PSA_SUCCESS );
5529 default:
5530 return( PSA_ERROR_INVALID_ARGUMENT );
5531 }
5532}
John Durkop07cc04a2020-11-16 22:08:34 -08005533#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005534
John Durkop07cc04a2020-11-16 22:08:34 -08005535#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5536 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005537static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5538 const uint8_t *data,
5539 size_t data_length )
5540{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005541 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01005542 return( PSA_ERROR_BAD_STATE );
5543
Janos Follathd6dce9f2019-07-04 09:11:38 +01005544 if( data_length != 0 )
5545 {
5546 prf->seed = mbedtls_calloc( 1, data_length );
5547 if( prf->seed == NULL )
5548 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005549
Janos Follathd6dce9f2019-07-04 09:11:38 +01005550 memcpy( prf->seed, data, data_length );
5551 prf->seed_length = data_length;
5552 }
Janos Follathf08e2652019-06-13 09:05:41 +01005553
Gilles Peskine43f958b2020-12-13 14:55:14 +01005554 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01005555
5556 return( PSA_SUCCESS );
5557}
5558
Janos Follath81550542019-06-13 14:26:34 +01005559static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5560 psa_algorithm_t hash_alg,
5561 const uint8_t *data,
5562 size_t data_length )
5563{
5564 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01005565 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01005566 return( PSA_ERROR_BAD_STATE );
5567
5568 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5569 if( status != PSA_SUCCESS )
5570 return( status );
5571
Gilles Peskine43f958b2020-12-13 14:55:14 +01005572 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01005573
5574 return( PSA_SUCCESS );
5575}
5576
Janos Follath63028dd2019-06-13 09:15:47 +01005577static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5578 const uint8_t *data,
5579 size_t data_length )
5580{
Gilles Peskine43f958b2020-12-13 14:55:14 +01005581 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01005582 return( PSA_ERROR_BAD_STATE );
5583
Janos Follathd6dce9f2019-07-04 09:11:38 +01005584 if( data_length != 0 )
5585 {
5586 prf->label = mbedtls_calloc( 1, data_length );
5587 if( prf->label == NULL )
5588 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005589
Janos Follathd6dce9f2019-07-04 09:11:38 +01005590 memcpy( prf->label, data, data_length );
5591 prf->label_length = data_length;
5592 }
Janos Follath63028dd2019-06-13 09:15:47 +01005593
Gilles Peskine43f958b2020-12-13 14:55:14 +01005594 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01005595
5596 return( PSA_SUCCESS );
5597}
5598
Janos Follathb03233e2019-06-11 15:30:30 +01005599static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5600 psa_algorithm_t hash_alg,
5601 psa_key_derivation_step_t step,
5602 const uint8_t *data,
5603 size_t data_length )
5604{
Janos Follathb03233e2019-06-11 15:30:30 +01005605 switch( step )
5606 {
Janos Follathf08e2652019-06-13 09:05:41 +01005607 case PSA_KEY_DERIVATION_INPUT_SEED:
5608 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005609 case PSA_KEY_DERIVATION_INPUT_SECRET:
5610 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005611 case PSA_KEY_DERIVATION_INPUT_LABEL:
5612 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005613 default:
5614 return( PSA_ERROR_INVALID_ARGUMENT );
5615 }
5616}
John Durkop07cc04a2020-11-16 22:08:34 -08005617#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5618 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5619
5620#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5621static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5622 psa_tls12_prf_key_derivation_t *prf,
5623 psa_algorithm_t hash_alg,
5624 const uint8_t *data,
5625 size_t data_length )
5626{
5627 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005628 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08005629 uint8_t *cur = pms;
5630
gabor-mezei-armcbcec212020-12-18 14:23:51 +01005631 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08005632 return( PSA_ERROR_INVALID_ARGUMENT );
5633
5634 /* Quoting RFC 4279, Section 2:
5635 *
5636 * The premaster secret is formed as follows: if the PSK is N octets
5637 * long, concatenate a uint16 with the value N, N zero octets, a second
5638 * uint16 with the value N, and the PSK itself.
5639 */
5640
5641 *cur++ = ( data_length >> 8 ) & 0xff;
5642 *cur++ = ( data_length >> 0 ) & 0xff;
5643 memset( cur, 0, data_length );
5644 cur += data_length;
5645 *cur++ = pms[0];
5646 *cur++ = pms[1];
5647 memcpy( cur, data, data_length );
5648 cur += data_length;
5649
5650 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5651
5652 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5653 return( status );
5654}
Janos Follath6660f0e2019-06-17 08:44:03 +01005655
5656static psa_status_t psa_tls12_prf_psk_to_ms_input(
5657 psa_tls12_prf_key_derivation_t *prf,
5658 psa_algorithm_t hash_alg,
5659 psa_key_derivation_step_t step,
5660 const uint8_t *data,
5661 size_t data_length )
5662{
5663 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005664 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005665 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5666 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005667 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005668
5669 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5670}
John Durkop07cc04a2020-11-16 22:08:34 -08005671#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005672
Gilles Peskineb8965192019-09-24 16:21:10 +02005673/** Check whether the given key type is acceptable for the given
5674 * input step of a key derivation.
5675 *
5676 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5677 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5678 * Both secret and non-secret inputs can alternatively have the type
5679 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5680 * that the input was passed as a buffer rather than via a key object.
5681 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005682static int psa_key_derivation_check_input_type(
5683 psa_key_derivation_step_t step,
5684 psa_key_type_t key_type )
5685{
5686 switch( step )
5687 {
5688 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005689 if( key_type == PSA_KEY_TYPE_DERIVE )
5690 return( PSA_SUCCESS );
5691 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005692 return( PSA_SUCCESS );
5693 break;
5694 case PSA_KEY_DERIVATION_INPUT_LABEL:
5695 case PSA_KEY_DERIVATION_INPUT_SALT:
5696 case PSA_KEY_DERIVATION_INPUT_INFO:
5697 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005698 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5699 return( PSA_SUCCESS );
5700 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005701 return( PSA_SUCCESS );
5702 break;
5703 }
5704 return( PSA_ERROR_INVALID_ARGUMENT );
5705}
5706
Janos Follathb80a94e2019-06-12 15:54:46 +01005707static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005708 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005709 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005710 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005711 const uint8_t *data,
5712 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005713{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005714 psa_status_t status;
5715 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5716
5717 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005718 if( status != PSA_SUCCESS )
5719 goto exit;
5720
John Durkopd0321952020-10-29 21:37:36 -07005721#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005723 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005724 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005726 step, data, data_length );
5727 }
John Durkop07cc04a2020-11-16 22:08:34 -08005728 else
5729#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5730#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5731 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005732 {
Janos Follathb03233e2019-06-11 15:30:30 +01005733 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5734 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5735 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005736 }
John Durkop07cc04a2020-11-16 22:08:34 -08005737 else
5738#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5739#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5740 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005741 {
5742 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5743 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5744 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005745 }
5746 else
John Durkop07cc04a2020-11-16 22:08:34 -08005747#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005749 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005750 (void) data;
5751 (void) data_length;
5752 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005753 return( PSA_ERROR_BAD_STATE );
5754 }
5755
Gilles Peskine224b0d62019-09-23 18:13:17 +02005756exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005758 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005759 return( status );
5760}
5761
Janos Follath51f4a0f2019-06-14 11:35:55 +01005762psa_status_t psa_key_derivation_input_bytes(
5763 psa_key_derivation_operation_t *operation,
5764 psa_key_derivation_step_t step,
5765 const uint8_t *data,
5766 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005767{
Gilles Peskineb8965192019-09-24 16:21:10 +02005768 return( psa_key_derivation_input_internal( operation, step,
5769 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005770 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005771}
5772
Janos Follath51f4a0f2019-06-14 11:35:55 +01005773psa_status_t psa_key_derivation_input_key(
5774 psa_key_derivation_operation_t *operation,
5775 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005776 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005777{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005778 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005779 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005780 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005781
Ronald Cron5c522922020-11-14 16:35:34 +01005782 status = psa_get_and_lock_transparent_key_slot_with_policy(
5783 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005784 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005785 {
5786 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005787 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005788 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005789
5790 /* Passing a key object as a SECRET input unlocks the permission
5791 * to output to a key object. */
5792 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5793 operation->can_output_key = 1;
5794
Ronald Cronf95a2b12020-10-22 15:24:49 +02005795 status = psa_key_derivation_input_internal( operation,
5796 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005797 slot->key.data,
5798 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005799
Ronald Cron5c522922020-11-14 16:35:34 +01005800 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005801
Ronald Cron5c522922020-11-14 16:35:34 +01005802 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005803}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005804
5805
5806
5807/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005808/* Key agreement */
5809/****************************************************************/
5810
John Durkop6ba40d12020-11-10 08:50:04 -08005811#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005812static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5813 size_t peer_key_length,
5814 const mbedtls_ecp_keypair *our_key,
5815 uint8_t *shared_secret,
5816 size_t shared_secret_size,
5817 size_t *shared_secret_length )
5818{
Steven Cooremand4867872020-08-05 16:31:39 +02005819 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005820 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005821 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005822 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005823 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005824 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005825
Ronald Cron90857082020-11-25 14:58:33 +01005826 status = mbedtls_psa_ecp_load_representation(
5827 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005828 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005829 peer_key,
5830 peer_key_length,
5831 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005832 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005833 goto exit;
5834
Jaeden Amero97271b32019-01-10 19:38:51 +00005835 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005836 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005837 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005838 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005839 status = mbedtls_to_psa_error(
5840 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5841 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005842 goto exit;
5843
Jaeden Amero97271b32019-01-10 19:38:51 +00005844 status = mbedtls_to_psa_error(
5845 mbedtls_ecdh_calc_secret( &ecdh,
5846 shared_secret_length,
5847 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005848 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005849 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005850 if( status != PSA_SUCCESS )
5851 goto exit;
5852 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5853 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005854
5855exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005856 if( status != PSA_SUCCESS )
5857 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005858 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005859 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005860 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005861
Jaeden Amero97271b32019-01-10 19:38:51 +00005862 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005863}
John Durkop6ba40d12020-11-10 08:50:04 -08005864#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005865
Gilles Peskine01d718c2018-09-18 12:01:02 +02005866#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5867
Gilles Peskine0216fe12019-04-11 21:23:21 +02005868static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5869 psa_key_slot_t *private_key,
5870 const uint8_t *peer_key,
5871 size_t peer_key_length,
5872 uint8_t *shared_secret,
5873 size_t shared_secret_size,
5874 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005875{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005876 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005877 {
John Durkop6ba40d12020-11-10 08:50:04 -08005878#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005879 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005880 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005881 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005882 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005883 psa_status_t status = mbedtls_psa_ecp_load_representation(
5884 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005885 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005886 private_key->key.data,
5887 private_key->key.bytes,
5888 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005889 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005890 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005891 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005892 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005893 shared_secret, shared_secret_size,
5894 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005895 mbedtls_ecp_keypair_free( ecp );
5896 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005897 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005898#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005899 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005900 (void) private_key;
5901 (void) peer_key;
5902 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005903 (void) shared_secret;
5904 (void) shared_secret_size;
5905 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005906 return( PSA_ERROR_NOT_SUPPORTED );
5907 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005908}
5909
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005910/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005911 * to potentially free embedded data structures and wipe confidential data.
5912 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005913static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005914 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005915 psa_key_slot_t *private_key,
5916 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005917 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005918{
5919 psa_status_t status;
5920 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5921 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005922 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005923
5924 /* Step 1: run the secret agreement algorithm to generate the shared
5925 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005926 status = psa_key_agreement_raw_internal( ka_alg,
5927 private_key,
5928 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005929 shared_secret,
5930 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005931 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005932 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005933 goto exit;
5934
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005935 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005936 * the shared secret. A shared secret is permitted wherever a key
5937 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005938 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005939 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005940 shared_secret,
5941 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005942exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005943 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005944 return( status );
5945}
5946
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005947psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005948 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005949 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005950 const uint8_t *peer_key,
5951 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005952{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005953 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005954 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005955 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005956
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005957 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005958 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005959 status = psa_get_and_lock_transparent_key_slot_with_policy(
5960 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005961 if( status != PSA_SUCCESS )
5962 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005963 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005964 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005965 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005966 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005967 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005968 else
5969 {
5970 /* If a private key has been added as SECRET, we allow the derived
5971 * key material to be used as a key in PSA Crypto. */
5972 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5973 operation->can_output_key = 1;
5974 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005975
Ronald Cron5c522922020-11-14 16:35:34 +01005976 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005977
Ronald Cron5c522922020-11-14 16:35:34 +01005978 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005979}
5980
Gilles Peskinebe697d82019-05-16 18:00:41 +02005981psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005982 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005983 const uint8_t *peer_key,
5984 size_t peer_key_length,
5985 uint8_t *output,
5986 size_t output_size,
5987 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005988{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005989 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005990 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005991 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005992
5993 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5994 {
5995 status = PSA_ERROR_INVALID_ARGUMENT;
5996 goto exit;
5997 }
Ronald Cron5c522922020-11-14 16:35:34 +01005998 status = psa_get_and_lock_transparent_key_slot_with_policy(
5999 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006000 if( status != PSA_SUCCESS )
6001 goto exit;
6002
6003 status = psa_key_agreement_raw_internal( alg, slot,
6004 peer_key, peer_key_length,
6005 output, output_size,
6006 output_length );
6007
6008exit:
6009 if( status != PSA_SUCCESS )
6010 {
6011 /* If an error happens and is not handled properly, the output
6012 * may be used as a key to protect sensitive data. Arrange for such
6013 * a key to be random, which is likely to result in decryption or
6014 * verification errors. This is better than filling the buffer with
6015 * some constant data such as zeros, which would result in the data
6016 * being protected with a reproducible, easily knowable key.
6017 */
6018 psa_generate_random( output, output_size );
6019 *output_length = output_size;
6020 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006021
Ronald Cron5c522922020-11-14 16:35:34 +01006022 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006023
Ronald Cron5c522922020-11-14 16:35:34 +01006024 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006025}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006026
6027
Gilles Peskine30524eb2020-11-13 17:02:26 +01006028
Gilles Peskine86a440b2018-11-12 18:39:40 +01006029/****************************************************************/
6030/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006031/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006032
Gilles Peskine30524eb2020-11-13 17:02:26 +01006033/** Initialize the PSA random generator.
6034 */
6035static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6036{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006037#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6038 memset( rng, 0, sizeof( *rng ) );
6039#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6040
Gilles Peskine30524eb2020-11-13 17:02:26 +01006041 /* Set default configuration if
6042 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6043 if( rng->entropy_init == NULL )
6044 rng->entropy_init = mbedtls_entropy_init;
6045 if( rng->entropy_free == NULL )
6046 rng->entropy_free = mbedtls_entropy_free;
6047
6048 rng->entropy_init( &rng->entropy );
6049#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6050 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6051 /* The PSA entropy injection feature depends on using NV seed as an entropy
6052 * source. Add NV seed as an entropy source for PSA entropy injection. */
6053 mbedtls_entropy_add_source( &rng->entropy,
6054 mbedtls_nv_seed_poll, NULL,
6055 MBEDTLS_ENTROPY_BLOCK_SIZE,
6056 MBEDTLS_ENTROPY_SOURCE_STRONG );
6057#endif
6058
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006059 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006060#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006061}
6062
6063/** Deinitialize the PSA random generator.
6064 */
6065static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6066{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006067#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6068 memset( rng, 0, sizeof( *rng ) );
6069#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006070 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006071 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006072#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006073}
6074
6075/** Seed the PSA random generator.
6076 */
6077static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6078{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006079#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6080 /* Do nothing: the external RNG seeds itself. */
6081 (void) rng;
6082 return( PSA_SUCCESS );
6083#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006084 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006085 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6086 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006087 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006088#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006089}
6090
Gilles Peskinee59236f2018-01-27 23:32:46 +01006091psa_status_t psa_generate_random( uint8_t *output,
6092 size_t output_size )
6093{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006094 GUARD_MODULE_INITIALIZED;
6095
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006096#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6097
6098 size_t output_length = 0;
6099 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6100 output, output_size,
6101 &output_length );
6102 if( status != PSA_SUCCESS )
6103 return( status );
6104 /* Breaking up a request into smaller chunks is currently not supported
6105 * for the extrernal RNG interface. */
6106 if( output_length != output_size )
6107 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6108 return( PSA_SUCCESS );
6109
6110#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6111
Gilles Peskine71ddab92021-01-04 21:01:07 +01006112 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006113 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006114 size_t request_size =
6115 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6116 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6117 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006118 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6119 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006120 if( ret != 0 )
6121 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006122 output_size -= request_size;
6123 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006124 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006125 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006126#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006127}
6128
Gilles Peskine8814fc42020-12-14 15:33:44 +01006129/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006130 *
6131 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6132 * `psa_generate_random(...)`. The state parameter is ignored since the
6133 * PSA API doesn't support passing an explicit state.
6134 *
6135 * In the non-external case, psa_generate_random() calls an
6136 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6137 * and semantics as mbedtls_psa_get_random(). As an optimization,
6138 * instead of doing this back-and-forth between the PSA API and the
6139 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6140 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006141 */
6142#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6143int mbedtls_psa_get_random( void *p_rng,
6144 unsigned char *output,
6145 size_t output_size )
6146{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006147 /* This function takes a pointer to the RNG state because that's what
6148 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6149 * its own state internally and doesn't let the caller access that state.
6150 * So we just ignore the state parameter, and in practice we'll pass
6151 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006152 (void) p_rng;
6153 psa_status_t status = psa_generate_random( output, output_size );
6154 if( status == PSA_SUCCESS )
6155 return( 0 );
6156 else
6157 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6158}
6159#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6160
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006161#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6162#include "mbedtls/entropy_poll.h"
6163
Gilles Peskine7228da22019-07-15 11:06:15 +02006164psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006165 size_t seed_size )
6166{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006167 if( global_data.initialized )
6168 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006169
6170 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6171 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6172 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6173 return( PSA_ERROR_INVALID_ARGUMENT );
6174
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006175 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006176}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006177#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006178
Ronald Cron3772afe2021-02-08 16:10:05 +01006179/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02006180 *
Ronald Cron3772afe2021-02-08 16:10:05 +01006181 * \param type The key type
6182 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02006183 *
6184 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01006185 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02006186 * \retval #PSA_ERROR_INVALID_ARGUMENT
6187 * The size in bits of the key is not valid.
6188 * \retval #PSA_ERROR_NOT_SUPPORTED
6189 * The type and/or the size in bits of the key or the combination of
6190 * the two is not supported.
6191 */
Ronald Cron3772afe2021-02-08 16:10:05 +01006192static psa_status_t psa_validate_key_type_and_size_for_key_generation(
6193 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006194{
Ronald Cronf3bb7612020-10-02 20:11:59 +02006195 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006196
Gilles Peskinee59236f2018-01-27 23:32:46 +01006197 if( key_type_is_raw_bytes( type ) )
6198 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02006199 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006200 if( status != PSA_SUCCESS )
6201 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006202 }
6203 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006204#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006205 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6206 {
6207 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6208 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006209
Ronald Cron01b2aba2020-10-05 09:42:02 +02006210 /* Accept only byte-aligned keys, for the same reasons as
6211 * in psa_import_rsa_key(). */
6212 if( bits % 8 != 0 )
6213 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006214 }
6215 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006216#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006217
Ronald Cron5c4d3862020-12-07 11:07:24 +01006218#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02006219 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
6220 {
Ronald Crond81ab562021-02-16 09:01:16 +01006221 /* To avoid empty block, return successfully here. */
6222 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02006223 }
6224 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01006225#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02006226 {
6227 return( PSA_ERROR_NOT_SUPPORTED );
6228 }
6229
6230 return( PSA_SUCCESS );
6231}
6232
Ronald Cron55ed0592020-10-05 10:30:40 +02006233psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006234 const psa_key_attributes_t *attributes,
6235 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006236{
6237 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006238 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02006239
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006240 if( ( attributes->domain_parameters == NULL ) &&
6241 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02006242 return( PSA_ERROR_INVALID_ARGUMENT );
6243
Ronald Cron01b2aba2020-10-05 09:42:02 +02006244 if( key_type_is_raw_bytes( type ) )
6245 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006246 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006247 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006248 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006249
David Brown12ca5032021-02-11 11:02:00 -07006250#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01006251 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006252 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07006253#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006254 }
6255 else
6256
John Durkop07cc04a2020-11-16 22:08:34 -08006257#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006258 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006259 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01006260 return( mbedtls_psa_rsa_generate_key( attributes,
6261 key_buffer,
6262 key_buffer_size,
6263 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006264 }
6265 else
John Durkop07cc04a2020-11-16 22:08:34 -08006266#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006267
John Durkop9814fa22020-11-04 12:28:15 -08006268#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006269 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006270 {
Ronald Cron7023db52020-11-20 18:17:42 +01006271 return( mbedtls_psa_ecp_generate_key( attributes,
6272 key_buffer,
6273 key_buffer_size,
6274 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006275 }
6276 else
John Durkop9814fa22020-11-04 12:28:15 -08006277#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006278 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02006279 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006280 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006281 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006282
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006283 return( PSA_SUCCESS );
6284}
Darryl Green0c6575a2018-11-07 16:05:30 +00006285
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006286psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006287 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006288{
6289 psa_status_t status;
6290 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006291 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02006292 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02006293
Ronald Cron81709fc2020-11-14 12:10:32 +01006294 *key = MBEDTLS_SVC_KEY_ID_INIT;
6295
Gilles Peskine0f84d622019-09-12 19:03:13 +02006296 /* Reject any attempt to create a zero-length key so that we don't
6297 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6298 if( psa_get_key_bits( attributes ) == 0 )
6299 return( PSA_ERROR_INVALID_ARGUMENT );
6300
Ronald Cron81709fc2020-11-14 12:10:32 +01006301 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6302 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006303 if( status != PSA_SUCCESS )
6304 goto exit;
6305
Ronald Cron977c2472020-10-13 08:32:21 +02006306 /* In the case of a transparent key or an opaque key stored in local
6307 * storage (thus not in the case of generating a key in a secure element
6308 * or cryptoprocessor with storage), we have to allocate a buffer to
6309 * hold the generated key material. */
6310 if( slot->key.data == NULL )
6311 {
6312 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
6313 PSA_KEY_LOCATION_LOCAL_STORAGE )
6314 {
Ronald Cron3772afe2021-02-08 16:10:05 +01006315 status = psa_validate_key_type_and_size_for_key_generation(
6316 attributes->core.type, attributes->core.bits );
6317 if( status != PSA_SUCCESS )
6318 goto exit;
6319
6320 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
6321 attributes->core.type,
6322 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02006323 }
6324 else
6325 {
6326 status = psa_driver_wrapper_get_key_buffer_size(
6327 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01006328 if( status != PSA_SUCCESS )
6329 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02006330 }
Ronald Cron977c2472020-10-13 08:32:21 +02006331
6332 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
6333 if( status != PSA_SUCCESS )
6334 goto exit;
6335 }
6336
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006337 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02006338 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02006339
Ronald Cron2b56bc82020-10-05 10:02:26 +02006340 if( status != PSA_SUCCESS )
6341 psa_remove_key_data_from_memory( slot );
6342
Gilles Peskine11792082019-08-06 18:36:36 +02006343exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006344 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006345 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006346 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006347 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006348
Darryl Green0c6575a2018-11-07 16:05:30 +00006349 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006350}
6351
Gilles Peskinee59236f2018-01-27 23:32:46 +01006352/****************************************************************/
6353/* Module setup */
6354/****************************************************************/
6355
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006356#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006357psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6358 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6359 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6360{
6361 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6362 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006363 global_data.rng.entropy_init = entropy_init;
6364 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006365 return( PSA_SUCCESS );
6366}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006367#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006368
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369void mbedtls_psa_crypto_free( void )
6370{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006371 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006372 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6373 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006374 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006375 }
6376 /* Wipe all remaining data, including configuration.
6377 * In particular, this sets all state indicator to the value
6378 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006379 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006380#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006381 /* Unregister all secure element drivers, so that we restart from
6382 * a pristine state. */
6383 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006384#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006385}
6386
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006387#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6388/** Recover a transaction that was interrupted by a power failure.
6389 *
6390 * This function is called during initialization, before psa_crypto_init()
6391 * returns. If this function returns a failure status, the initialization
6392 * fails.
6393 */
6394static psa_status_t psa_crypto_recover_transaction(
6395 const psa_crypto_transaction_t *transaction )
6396{
6397 switch( transaction->unknown.type )
6398 {
6399 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6400 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006401 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006402 * is implemented.
6403 * https://github.com/ARMmbed/mbed-crypto/issues/218
6404 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006405 default:
6406 /* We found an unsupported transaction in the storage.
6407 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01006408 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006409 }
6410}
6411#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6412
Gilles Peskinee59236f2018-01-27 23:32:46 +01006413psa_status_t psa_crypto_init( void )
6414{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006415 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006416
Gilles Peskinec6b69072018-11-20 21:42:52 +01006417 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006418 if( global_data.initialized != 0 )
6419 return( PSA_SUCCESS );
6420
Gilles Peskine30524eb2020-11-13 17:02:26 +01006421 /* Initialize and seed the random generator. */
6422 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006423 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006424 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006425 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006426 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006427 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428
Gilles Peskine66fb1262018-12-10 16:29:04 +01006429 status = psa_initialize_key_slots( );
6430 if( status != PSA_SUCCESS )
6431 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006432
Gilles Peskined9348f22019-10-01 15:22:29 +02006433#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6434 status = psa_init_all_se_drivers( );
6435 if( status != PSA_SUCCESS )
6436 goto exit;
6437#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6438
Gilles Peskine4b734222019-07-24 15:56:31 +02006439#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006440 status = psa_crypto_load_transaction( );
6441 if( status == PSA_SUCCESS )
6442 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006443 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6444 if( status != PSA_SUCCESS )
6445 goto exit;
6446 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006447 }
6448 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6449 {
6450 /* There's no transaction to complete. It's all good. */
6451 status = PSA_SUCCESS;
6452 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006453#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006454
Gilles Peskinec6b69072018-11-20 21:42:52 +01006455 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456 global_data.initialized = 1;
6457
6458exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006459 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006460 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006461 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006462}
6463
6464#endif /* MBEDTLS_PSA_CRYPTO_C */