blob: a4ca1d0bfa3fd8e2d1585cff43d5ac3d8b1d486c [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"
Steven Cooreman5f88e772021-03-15 11:07:12 +010036#include "psa_crypto_hash.h"
Ronald Cron00b7bfc2020-11-25 15:25:26 +010037#include "psa_crypto_rsa.h"
Ronald Cron7023db52020-11-20 18:17:42 +010038#include "psa_crypto_ecp.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020039#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020040#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020041#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010042#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010043/* Include internal declarations that are useful for implementing persistently
44 * stored keys. */
45#include "psa_crypto_storage.h"
46
Gilles Peskineb2b64d32020-12-14 16:43:58 +010047#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010048
Gilles Peskineb46bef22019-07-30 21:32:04 +020049#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#include <stdlib.h>
51#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010052#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020053#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010054#define mbedtls_calloc calloc
55#define mbedtls_free free
56#endif
57
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010058#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020060#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000061#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020062#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/blowfish.h"
64#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020065#include "mbedtls/chacha20.h"
66#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010067#include "mbedtls/cipher.h"
68#include "mbedtls/ccm.h"
69#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020071#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010072#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010073#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010074#include "mbedtls/error.h"
75#include "mbedtls/gcm.h"
76#include "mbedtls/md2.h"
77#include "mbedtls/md4.h"
78#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010079#include "mbedtls/md.h"
80#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010081#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010083#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000084#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010085#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010086#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010087#include "mbedtls/sha1.h"
88#include "mbedtls/sha256.h"
89#include "mbedtls/sha512.h"
90#include "mbedtls/xtea.h"
91
Gilles Peskine996deb12018-08-01 15:45:45 +020092#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
93
Gilles Peskine9ef733f2018-02-07 21:05:37 +010094/* constant-time buffer comparison */
95static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
96{
97 size_t i;
98 unsigned char diff = 0;
99
100 for( i = 0; i < n; i++ )
101 diff |= a[i] ^ b[i];
102
103 return( diff );
104}
105
106
107
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100108/****************************************************************/
109/* Global data, support functions and library management */
110/****************************************************************/
111
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200112static int key_type_is_raw_bytes( psa_key_type_t type )
113{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200114 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200115}
116
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100117/* Values for psa_global_data_t::rng_state */
118#define RNG_NOT_INITIALIZED 0
119#define RNG_INITIALIZED 1
120#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121
Gilles Peskine2d277862018-06-18 15:41:12 +0200122typedef struct
123{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100124 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100125 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100126 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100127} psa_global_data_t;
128
129static psa_global_data_t global_data;
130
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100131#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
132mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
133 &global_data.rng.drbg;
134#endif
135
itayzafrir0adf0fc2018-09-06 16:24:41 +0300136#define GUARD_MODULE_INITIALIZED \
137 if( global_data.initialized == 0 ) \
138 return( PSA_ERROR_BAD_STATE );
139
Steven Cooreman01164162020-07-20 15:31:37 +0200140psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141{
Gilles Peskinedbf68962021-01-06 20:04:23 +0100142 /* Mbed TLS error codes can combine a high-level error code and a
143 * low-level error code. The low-level error usually reflects the
144 * root cause better, so dispatch on that preferably. */
145 int low_level_ret = - ( -ret & 0x007f );
146 switch( low_level_ret != 0 ? low_level_ret : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100147 {
148 case 0:
149 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100150
151 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
152 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
153 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
154 return( PSA_ERROR_NOT_SUPPORTED );
155 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
156 return( PSA_ERROR_HARDWARE_FAILURE );
157
158 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
159 return( PSA_ERROR_HARDWARE_FAILURE );
160
Gilles Peskine9a944802018-06-21 09:35:35 +0200161 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
162 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
163 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
164 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
165 case MBEDTLS_ERR_ASN1_INVALID_DATA:
166 return( PSA_ERROR_INVALID_ARGUMENT );
167 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
168 return( PSA_ERROR_INSUFFICIENT_MEMORY );
169 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
170 return( PSA_ERROR_BUFFER_TOO_SMALL );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
Jaeden Amero93e21112019-02-20 13:57:28 +0000182#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000183 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000184#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
185 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
186#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100187 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
188 return( PSA_ERROR_NOT_SUPPORTED );
189 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
190 return( PSA_ERROR_HARDWARE_FAILURE );
191
192 case MBEDTLS_ERR_CCM_BAD_INPUT:
193 return( PSA_ERROR_INVALID_ARGUMENT );
194 case MBEDTLS_ERR_CCM_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
197 return( PSA_ERROR_HARDWARE_FAILURE );
198
Gilles Peskine26869f22019-05-06 15:25:00 +0200199 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201
202 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206
Gilles Peskinea5905292018-02-07 20:59:33 +0100207 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
208 return( PSA_ERROR_NOT_SUPPORTED );
209 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
210 return( PSA_ERROR_INVALID_ARGUMENT );
211 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
212 return( PSA_ERROR_INSUFFICIENT_MEMORY );
213 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
214 return( PSA_ERROR_INVALID_PADDING );
215 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200216 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100217 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
218 return( PSA_ERROR_INVALID_SIGNATURE );
219 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200220 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100221 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
222 return( PSA_ERROR_HARDWARE_FAILURE );
223
224 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
228 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100229 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
230 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100231 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
232 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
233 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
234 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
235 return( PSA_ERROR_NOT_SUPPORTED );
236 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
237 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100238#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100239
240 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
241 return( PSA_ERROR_NOT_SUPPORTED );
242 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
243 return( PSA_ERROR_HARDWARE_FAILURE );
244
Gilles Peskinee59236f2018-01-27 23:32:46 +0100245 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
246 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
247 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
248 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100249
250 case MBEDTLS_ERR_GCM_AUTH_FAILED:
251 return( PSA_ERROR_INVALID_SIGNATURE );
252 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200253 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100254 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
255 return( PSA_ERROR_HARDWARE_FAILURE );
256
Gilles Peskine82e57d12020-11-13 21:31:17 +0100257#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
258 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100259 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
260 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100261 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
262 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
263 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
264 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
265 return( PSA_ERROR_NOT_SUPPORTED );
266 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
267 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
268#endif
269
Gilles Peskinea5905292018-02-07 20:59:33 +0100270 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
271 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
272 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
273 return( PSA_ERROR_HARDWARE_FAILURE );
274
275 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
276 return( PSA_ERROR_NOT_SUPPORTED );
277 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MD_ALLOC_FAILED:
280 return( PSA_ERROR_INSUFFICIENT_MEMORY );
281 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
282 return( PSA_ERROR_STORAGE_FAILURE );
283 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
284 return( PSA_ERROR_HARDWARE_FAILURE );
285
Gilles Peskinef76aa772018-10-29 19:24:33 +0100286 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
287 return( PSA_ERROR_STORAGE_FAILURE );
288 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
293 return( PSA_ERROR_BUFFER_TOO_SMALL );
294 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
295 return( PSA_ERROR_INVALID_ARGUMENT );
296 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
299 return( PSA_ERROR_INVALID_ARGUMENT );
300 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
301 return( PSA_ERROR_INSUFFICIENT_MEMORY );
302
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100303 case MBEDTLS_ERR_PK_ALLOC_FAILED:
304 return( PSA_ERROR_INSUFFICIENT_MEMORY );
305 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
306 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100309 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100310 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
311 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
312 return( PSA_ERROR_INVALID_ARGUMENT );
313 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
314 return( PSA_ERROR_NOT_SUPPORTED );
315 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
316 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
317 return( PSA_ERROR_NOT_PERMITTED );
318 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
319 return( PSA_ERROR_INVALID_ARGUMENT );
320 case MBEDTLS_ERR_PK_INVALID_ALG:
321 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
322 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
323 return( PSA_ERROR_NOT_SUPPORTED );
324 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
325 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100326 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328
Gilles Peskineff2d2002019-05-06 15:26:23 +0200329 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
332 return( PSA_ERROR_NOT_SUPPORTED );
333
Gilles Peskinea5905292018-02-07 20:59:33 +0100334 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
337 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
338 return( PSA_ERROR_INVALID_ARGUMENT );
339 case MBEDTLS_ERR_RSA_INVALID_PADDING:
340 return( PSA_ERROR_INVALID_PADDING );
341 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
342 return( PSA_ERROR_HARDWARE_FAILURE );
343 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
344 return( PSA_ERROR_INVALID_ARGUMENT );
345 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
346 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200347 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100348 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
349 return( PSA_ERROR_INVALID_SIGNATURE );
350 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
351 return( PSA_ERROR_BUFFER_TOO_SMALL );
352 case MBEDTLS_ERR_RSA_RNG_FAILED:
Gilles Peskine40d81602020-11-25 00:09:47 +0100353 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100354 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
355 return( PSA_ERROR_NOT_SUPPORTED );
356 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
357 return( PSA_ERROR_HARDWARE_FAILURE );
358
359 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
360 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
361 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
362 return( PSA_ERROR_HARDWARE_FAILURE );
363
364 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
365 return( PSA_ERROR_INVALID_ARGUMENT );
366 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
367 return( PSA_ERROR_HARDWARE_FAILURE );
368
itayzafrir5c753392018-05-08 11:18:38 +0300369 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300370 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300371 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300372 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
373 return( PSA_ERROR_BUFFER_TOO_SMALL );
374 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
375 return( PSA_ERROR_NOT_SUPPORTED );
376 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
377 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
378 return( PSA_ERROR_INVALID_SIGNATURE );
379 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
380 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskine40d81602020-11-25 00:09:47 +0100381 case MBEDTLS_ERR_ECP_RANDOM_FAILED:
382 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300383 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
384 return( PSA_ERROR_HARDWARE_FAILURE );
Gilles Peskine40d81602020-11-25 00:09:47 +0100385
Janos Follatha13b9052019-11-22 12:48:59 +0000386 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
387 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300388
Gilles Peskinee59236f2018-01-27 23:32:46 +0100389 default:
David Saadab4ecc272019-02-14 13:48:10 +0200390 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100391 }
392}
393
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200394
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200395
396
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100397/****************************************************************/
398/* Key management */
399/****************************************************************/
400
Gilles Peskine73167e12019-07-12 23:44:37 +0200401#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
402static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
403{
Gilles Peskine8e338702019-07-30 20:06:31 +0200404 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200405}
406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
407
John Durkop07cc04a2020-11-16 22:08:34 -0800408/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
409 * current test driver in key_management.c is using this function
410 * when accelerators are used for ECC key pair and public key.
411 * Once that dependency is resolved these guards can be removed.
412 */
John Durkop9814fa22020-11-04 12:28:15 -0800413#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800414 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
415 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
416 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100417mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100418 size_t bits,
419 int bits_is_sloppy )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200420{
421 switch( curve )
422 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100423 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100424 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100425 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100426#if defined(PSA_WANT_ECC_SECP_R1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100427 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100428 return( MBEDTLS_ECP_DP_SECP192R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100429#endif
430#if defined(PSA_WANT_ECC_SECP_R1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100431 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 return( MBEDTLS_ECP_DP_SECP224R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100433#endif
434#if defined(PSA_WANT_ECC_SECP_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100435 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100436 return( MBEDTLS_ECP_DP_SECP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100437#endif
438#if defined(PSA_WANT_ECC_SECP_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100439 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100440 return( MBEDTLS_ECP_DP_SECP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100441#endif
442#if defined(PSA_WANT_ECC_SECP_R1_521)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100443 case 521:
Gilles Peskine228abc52019-12-03 17:24:19 +0100444 return( MBEDTLS_ECP_DP_SECP521R1 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100445 case 528:
446 if( bits_is_sloppy )
447 return( MBEDTLS_ECP_DP_SECP521R1 );
448 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100449#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100450 }
451 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100452
Paul Elliott8ff510a2020-06-02 17:19:28 +0100453 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100454 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100455 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100456#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100457 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 return( MBEDTLS_ECP_DP_BP256R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100459#endif
460#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_384)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100461 case 384:
Gilles Peskine228abc52019-12-03 17:24:19 +0100462 return( MBEDTLS_ECP_DP_BP384R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100463#endif
464#if defined(PSA_WANT_ECC_BRAINPOOL_P_R1_512)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100465 case 512:
Gilles Peskine228abc52019-12-03 17:24:19 +0100466 return( MBEDTLS_ECP_DP_BP512R1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100467#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100468 }
469 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100470
Paul Elliott8ff510a2020-06-02 17:19:28 +0100471 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100472 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100473 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100474#if defined(PSA_WANT_ECC_MONTGOMERY_255)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100475 case 255:
Gilles Peskine228abc52019-12-03 17:24:19 +0100476 return( MBEDTLS_ECP_DP_CURVE25519 );
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100477 case 256:
478 if( bits_is_sloppy )
479 return( MBEDTLS_ECP_DP_CURVE25519 );
480 break;
Gilles Peskinea1684f42021-03-23 13:12:34 +0100481#endif
482#if defined(PSA_WANT_ECC_MONTGOMERY_448)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100483 case 448:
Gilles Peskine228abc52019-12-03 17:24:19 +0100484 return( MBEDTLS_ECP_DP_CURVE448 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100485#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100486 }
487 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100488
Paul Elliott8ff510a2020-06-02 17:19:28 +0100489 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100490 switch( bits )
Gilles Peskine228abc52019-12-03 17:24:19 +0100491 {
Gilles Peskinea1684f42021-03-23 13:12:34 +0100492#if defined(PSA_WANT_ECC_SECP_K1_192)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100493 case 192:
Gilles Peskine228abc52019-12-03 17:24:19 +0100494 return( MBEDTLS_ECP_DP_SECP192K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100495#endif
496#if defined(PSA_WANT_ECC_SECP_K1_224)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100497 case 224:
Gilles Peskine228abc52019-12-03 17:24:19 +0100498 return( MBEDTLS_ECP_DP_SECP224K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100499#endif
500#if defined(PSA_WANT_ECC_SECP_K1_256)
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100501 case 256:
Gilles Peskine228abc52019-12-03 17:24:19 +0100502 return( MBEDTLS_ECP_DP_SECP256K1 );
Gilles Peskinea1684f42021-03-23 13:12:34 +0100503#endif
Gilles Peskine228abc52019-12-03 17:24:19 +0100504 }
505 break;
Gilles Peskine12313cd2018-06-20 00:20:32 +0200506 }
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +0100507
508 return( MBEDTLS_ECP_DP_NONE );
Gilles Peskine12313cd2018-06-20 00:20:32 +0200509}
John Durkop9814fa22020-11-04 12:28:15 -0800510#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800511 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
512 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
513 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200514
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200515static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
516 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517{
518 /* Check that the bit size is acceptable for the key type */
519 switch( type )
520 {
521 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200522 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200523 case PSA_KEY_TYPE_DERIVE:
524 break;
David Browne04acc22021-01-26 11:39:16 -0700525#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_AES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 case PSA_KEY_TYPE_AES:
527 if( bits != 128 && bits != 192 && bits != 256 )
528 return( PSA_ERROR_INVALID_ARGUMENT );
529 break;
530#endif
David Browne04acc22021-01-26 11:39:16 -0700531#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CAMELLIA)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200532 case PSA_KEY_TYPE_CAMELLIA:
533 if( bits != 128 && bits != 192 && bits != 256 )
534 return( PSA_ERROR_INVALID_ARGUMENT );
535 break;
536#endif
David Browne04acc22021-01-26 11:39:16 -0700537#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200538 case PSA_KEY_TYPE_DES:
539 if( bits != 64 && bits != 128 && bits != 192 )
540 return( PSA_ERROR_INVALID_ARGUMENT );
541 break;
542#endif
David Browne04acc22021-01-26 11:39:16 -0700543#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ARC4)
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200544 case PSA_KEY_TYPE_ARC4:
545 if( bits < 8 || bits > 2048 )
546 return( PSA_ERROR_INVALID_ARGUMENT );
547 break;
548#endif
David Brown1bfe4d72021-02-16 12:54:35 -0700549#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +0200550 case PSA_KEY_TYPE_CHACHA20:
551 if( bits != 256 )
552 return( PSA_ERROR_INVALID_ARGUMENT );
553 break;
554#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200555 default:
556 return( PSA_ERROR_NOT_SUPPORTED );
557 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200558 if( bits % 8 != 0 )
559 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200560
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200561 return( PSA_SUCCESS );
562}
563
Gilles Peskineb46bef22019-07-30 21:32:04 +0200564/** Return the size of the key in the given slot, in bits.
565 *
566 * \param[in] slot A key slot.
567 *
568 * \return The key size in bits, read from the metadata in the slot.
569 */
570static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
571{
572 return( slot->attr.bits );
573}
574
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100575/** Check whether a given key type is valid for use with a given MAC algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100576 *
Steven Cooremancd640932021-03-08 12:00:27 +0100577 * Upon successful return of this function, the behavior of #PSA_MAC_LENGTH
578 * when called with the validated \p algorithm and \p key_type is well-defined.
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100579 *
580 * \param[in] algorithm The specific MAC algorithm (can be wildcard).
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100581 * \param[in] key_type The key type of the key to be used with the
582 * \p algorithm.
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100583 *
584 * \retval #PSA_SUCCESS
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100585 * The \p key_type is valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100586 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100587 * The \p key_type is not valid for use with the \p algorithm
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100588 */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100589MBEDTLS_STATIC_TESTABLE psa_status_t psa_mac_key_can_do(
Steven Cooreman58c94d32021-03-02 21:31:17 +0100590 psa_algorithm_t algorithm,
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100591 psa_key_type_t key_type )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100592{
Steven Cooremancd640932021-03-08 12:00:27 +0100593 if( PSA_ALG_IS_HMAC( algorithm ) )
594 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100595 if( key_type == PSA_KEY_TYPE_HMAC )
596 return( PSA_SUCCESS );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100597 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100598
599 if( PSA_ALG_IS_BLOCK_CIPHER_MAC( algorithm ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100600 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100601 /* Check that we're calling PSA_BLOCK_CIPHER_BLOCK_LENGTH with a cipher
602 * key. */
603 if( ( key_type & PSA_KEY_TYPE_CATEGORY_MASK ) ==
604 PSA_KEY_TYPE_CATEGORY_SYMMETRIC )
605 {
606 /* PSA_BLOCK_CIPHER_BLOCK_LENGTH returns 1 for stream ciphers and
607 * the block length (larger than 1) for block ciphers. */
608 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( key_type ) > 1 )
609 return( PSA_SUCCESS );
610 }
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100611 }
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100612
613 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100614}
615
Steven Cooreman75b74362020-07-28 14:30:13 +0200616/** Try to allocate a buffer to an empty key slot.
617 *
618 * \param[in,out] slot Key slot to attach buffer to.
619 * \param[in] buffer_length Requested size of the buffer.
620 *
621 * \retval #PSA_SUCCESS
622 * The buffer has been successfully allocated.
623 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
624 * Not enough memory was available for allocation.
625 * \retval #PSA_ERROR_ALREADY_EXISTS
626 * Trying to allocate a buffer to a non-empty key slot.
627 */
628static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
629 size_t buffer_length )
630{
Ronald Cronea0f8a62020-11-25 17:52:23 +0100631 if( slot->key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200632 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200633
Ronald Cronea0f8a62020-11-25 17:52:23 +0100634 slot->key.data = mbedtls_calloc( 1, buffer_length );
635 if( slot->key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200636 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +0200637
Ronald Cronea0f8a62020-11-25 17:52:23 +0100638 slot->key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +0200639 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +0200640}
641
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200642psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
643 const uint8_t* data,
644 size_t data_length )
645{
646 psa_status_t status = psa_allocate_buffer_to_slot( slot,
647 data_length );
648 if( status != PSA_SUCCESS )
649 return( status );
650
Ronald Cronea0f8a62020-11-25 17:52:23 +0100651 memcpy( slot->key.data, data, data_length );
Steven Cooremanf7cebd42020-10-13 20:27:40 +0200652 return( PSA_SUCCESS );
653}
654
Ronald Crona0fe59f2020-11-29 11:14:53 +0100655psa_status_t psa_import_key_into_slot(
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100656 const psa_key_attributes_t *attributes,
657 const uint8_t *data, size_t data_length,
658 uint8_t *key_buffer, size_t key_buffer_size,
659 size_t *key_buffer_length, size_t *bits )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100660{
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
662 psa_key_type_t type = attributes->core.type;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100663
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200664 /* zero-length keys are never supported. */
665 if( data_length == 0 )
666 return( PSA_ERROR_NOT_SUPPORTED );
667
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100668 if( key_type_is_raw_bytes( type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100669 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100670 *bits = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200671
Steven Cooreman75b74362020-07-28 14:30:13 +0200672 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
673 if( data_length > SIZE_MAX / 8 )
674 return( PSA_ERROR_NOT_SUPPORTED );
675
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200676 /* Enforce a size limit, and in particular ensure that the bit
677 * size fits in its representation type. */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100678 if( ( *bits ) > PSA_MAX_KEY_BITS )
Gilles Peskinec744d992019-07-30 17:26:54 +0200679 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200680
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100681 status = validate_unstructured_key_bit_size( type, *bits );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200682 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200683 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200684
Ronald Crondd04d422020-11-28 15:14:42 +0100685 /* Copy the key material. */
686 memcpy( key_buffer, data, data_length );
687 *key_buffer_length = data_length;
688 (void)key_buffer_size;
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200689
Steven Cooreman40120f62020-10-29 11:42:22 +0100690 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100691 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100692 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +0200693 {
John Durkop9814fa22020-11-04 12:28:15 -0800694#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
695 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100696 if( PSA_KEY_TYPE_IS_ECC( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200697 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100698 return( mbedtls_psa_ecp_import_key( attributes,
699 data, data_length,
700 key_buffer, key_buffer_size,
701 key_buffer_length,
702 bits ) );
Steven Cooreman40120f62020-10-29 11:42:22 +0100703 }
John Durkop9814fa22020-11-04 12:28:15 -0800704#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
705 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -0700706#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
707 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100708 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200709 {
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100710 return( mbedtls_psa_rsa_import_key( attributes,
711 data, data_length,
712 key_buffer, key_buffer_size,
713 key_buffer_length,
714 bits ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200715 }
John Durkop9814fa22020-11-04 12:28:15 -0800716#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
717 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100718 }
Steven Cooreman40120f62020-10-29 11:42:22 +0100719
Ronald Cron2ebfdcc2020-11-28 15:54:54 +0100720 return( PSA_ERROR_NOT_SUPPORTED );
Darryl Green06fd18d2018-07-16 11:21:11 +0100721}
722
Gilles Peskinef603c712019-01-19 13:40:11 +0100723/** Calculate the intersection of two algorithm usage policies.
724 *
725 * Return 0 (which allows no operation) on incompatibility.
726 */
727static psa_algorithm_t psa_key_policy_algorithm_intersection(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100728 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100729 psa_algorithm_t alg1,
730 psa_algorithm_t alg2 )
731{
Gilles Peskine549ea862019-05-22 11:45:59 +0200732 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100733 if( alg1 == alg2 )
734 return( alg1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100735 /* If the policies are from the same hash-and-sign family, check
736 * if one is a wildcard. If so the other has the specific algorithm. */
737 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
738 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
739 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
Gilles Peskinef603c712019-01-19 13:40:11 +0100740 {
Steven Cooreman03488022021-02-18 12:07:20 +0100741 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
742 return( alg2 );
743 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
744 return( alg1 );
745 }
746 /* If the policies are from the same AEAD family, check whether
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100747 * one of them is a minimum-tag-length wildcard. Calculate the most
Steven Cooreman03488022021-02-18 12:07:20 +0100748 * restrictive tag length. */
749 if( PSA_ALG_IS_AEAD( alg1 ) && PSA_ALG_IS_AEAD( alg2 ) &&
750 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg1, 0 ) ==
751 PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg2, 0 ) ) )
752 {
753 size_t alg1_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg1 );
754 size_t alg2_len = PSA_ALG_AEAD_GET_TAG_LENGTH( alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100755 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100756
Steven Cooreman7de9e2d2021-02-22 17:05:56 +0100757 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100758 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
759 ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100760 {
Steven Cooremancd640932021-03-08 12:00:27 +0100761 return( PSA_ALG_AEAD_WITH_AT_LEAST_THIS_LENGTH_TAG(
762 alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100763 }
764 /* If only one is a wildcard, return specific algorithm if compatible. */
Steven Cooremand927ed72021-02-22 19:59:35 +0100765 if( ( ( alg1 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100766 ( alg1_len <= alg2_len ) )
767 {
768 return( alg2 );
769 }
Steven Cooremand927ed72021-02-22 19:59:35 +0100770 if( ( ( alg2 & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
Steven Cooreman03488022021-02-18 12:07:20 +0100771 ( alg2_len <= alg1_len ) )
772 {
773 return( alg1 );
774 }
775 }
776 /* If the policies are from the same MAC family, check whether one
777 * of them is a minimum-MAC-length policy. Calculate the most
778 * restrictive tag length. */
779 if( PSA_ALG_IS_MAC( alg1 ) && PSA_ALG_IS_MAC( alg2 ) &&
780 ( PSA_ALG_FULL_LENGTH_MAC( alg1 ) ==
781 PSA_ALG_FULL_LENGTH_MAC( alg2 ) ) )
782 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100783 /* Validate the combination of key type and algorithm. Since the base
784 * algorithm of alg1 and alg2 are the same, we only need this once. */
785 if( PSA_SUCCESS != psa_mac_key_can_do( alg1, key_type ) )
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100786 return( 0 );
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100787
Steven Cooreman31a876d2021-03-03 20:47:40 +0100788 /* Get the (exact or at-least) output lengths for both sides of the
789 * requested intersection. None of the currently supported algorithms
790 * have an output length dependent on the actual key size, so setting it
791 * to a bogus value of 0 is currently OK.
792 *
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100793 * Note that for at-least-this-length wildcard algorithms, the output
794 * length is set to the shortest allowed length, which allows us to
795 * calculate the most restrictive tag length for the intersection. */
796 size_t alg1_len = PSA_MAC_LENGTH( key_type, 0, alg1 );
797 size_t alg2_len = PSA_MAC_LENGTH( key_type, 0, alg2 );
Steven Cooremancd640932021-03-08 12:00:27 +0100798 size_t restricted_len = alg1_len > alg2_len ? alg1_len : alg2_len;
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100799
Steven Cooremana1d83222021-02-25 10:20:29 +0100800 /* If both are wildcards, return most restrictive wildcard */
Steven Cooremand927ed72021-02-22 19:59:35 +0100801 if( ( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) &&
802 ( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100803 {
Steven Cooremancd640932021-03-08 12:00:27 +0100804 return( PSA_ALG_AT_LEAST_THIS_LENGTH_MAC( alg1, restricted_len ) );
Steven Cooreman03488022021-02-18 12:07:20 +0100805 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100806
807 /* If only one is an at-least-this-length policy, the intersection would
808 * be the other (fixed-length) policy as long as said fixed length is
809 * equal to or larger than the shortest allowed length. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100810 if( ( alg1 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100811 {
Steven Cooremancd640932021-03-08 12:00:27 +0100812 return( ( alg1_len <= alg2_len ) ? alg2 : 0 );
Steven Cooreman03488022021-02-18 12:07:20 +0100813 }
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100814 if( ( alg2 & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
Steven Cooreman03488022021-02-18 12:07:20 +0100815 {
Steven Cooremancd640932021-03-08 12:00:27 +0100816 return( ( alg2_len <= alg1_len ) ? alg1 : 0 );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100817 }
Steven Cooreman31a876d2021-03-03 20:47:40 +0100818
Steven Cooremancd640932021-03-08 12:00:27 +0100819 /* If none of them are wildcards, check whether they define the same tag
820 * length. This is still possible here when one is default-length and
821 * the other specific-length. Ensure to always return the
822 * specific-length version for the intersection. */
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100823 if( alg1_len == alg2_len )
824 return( PSA_ALG_TRUNCATED_MAC( alg1, alg1_len ) );
Gilles Peskinef603c712019-01-19 13:40:11 +0100825 }
826 /* If the policies are incompatible, allow nothing. */
827 return( 0 );
828}
829
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100830static int psa_key_algorithm_permits( psa_key_type_t key_type,
831 psa_algorithm_t policy_alg,
Gilles Peskined6f371b2019-05-10 19:33:38 +0200832 psa_algorithm_t requested_alg )
833{
Gilles Peskine549ea862019-05-22 11:45:59 +0200834 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200835 if( requested_alg == policy_alg )
836 return( 1 );
Steven Cooreman03488022021-02-18 12:07:20 +0100837 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
838 * and requested_alg is the same hash-and-sign family with any hash,
839 * then requested_alg is compliant with policy_alg. */
840 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
841 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
Gilles Peskined6f371b2019-05-10 19:33:38 +0200842 {
Steven Cooreman03488022021-02-18 12:07:20 +0100843 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
844 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
845 }
846 /* If policy_alg is a wildcard AEAD algorithm of the same base as
847 * the requested algorithm, check the requested tag length to be
848 * equal-length or longer than the wildcard-specified length. */
849 if( PSA_ALG_IS_AEAD( policy_alg ) &&
850 PSA_ALG_IS_AEAD( requested_alg ) &&
851 ( PSA_ALG_AEAD_WITH_SHORTENED_TAG( policy_alg, 0 ) ==
852 PSA_ALG_AEAD_WITH_SHORTENED_TAG( requested_alg, 0 ) ) &&
Steven Cooremand927ed72021-02-22 19:59:35 +0100853 ( ( policy_alg & PSA_ALG_AEAD_AT_LEAST_THIS_LENGTH_FLAG ) != 0 ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100854 {
855 return( PSA_ALG_AEAD_GET_TAG_LENGTH( policy_alg ) <=
856 PSA_ALG_AEAD_GET_TAG_LENGTH( requested_alg ) );
857 }
Steven Cooremancd640932021-03-08 12:00:27 +0100858 /* If policy_alg is a MAC algorithm of the same base as the requested
859 * algorithm, check whether their MAC lengths are compatible. */
Steven Cooreman03488022021-02-18 12:07:20 +0100860 if( PSA_ALG_IS_MAC( policy_alg ) &&
861 PSA_ALG_IS_MAC( requested_alg ) &&
862 ( PSA_ALG_FULL_LENGTH_MAC( policy_alg ) ==
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100863 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) ) )
Steven Cooreman03488022021-02-18 12:07:20 +0100864 {
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100865 /* Validate the combination of key type and algorithm. Since the policy
866 * and requested algorithms are the same, we only need this once. */
867 if( PSA_SUCCESS != psa_mac_key_can_do( policy_alg, key_type ) )
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100868 return( 0 );
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100869
Steven Cooreman31a876d2021-03-03 20:47:40 +0100870 /* Get both the requested output length for the algorithm which is to be
871 * verified, and the default output length for the base algorithm.
872 * Note that none of the currently supported algorithms have an output
873 * length dependent on actual key size, so setting it to a bogus value
874 * of 0 is currently OK. */
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100875 size_t requested_output_length = PSA_MAC_LENGTH(
876 key_type, 0, requested_alg );
877 size_t default_output_length = PSA_MAC_LENGTH(
878 key_type, 0,
879 PSA_ALG_FULL_LENGTH_MAC( requested_alg ) );
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100880
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100881 /* If the policy is default-length, only allow an algorithm with
882 * a declared exact-length matching the default. */
883 if( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == 0 )
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100884 return( requested_output_length == default_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100885
886 /* If the requested algorithm is default-length, allow it if the policy
Steven Cooremancd640932021-03-08 12:00:27 +0100887 * length exactly matches the default length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100888 if( PSA_MAC_TRUNCATED_LENGTH( requested_alg ) == 0 &&
889 PSA_MAC_TRUNCATED_LENGTH( policy_alg ) == default_output_length )
890 {
891 return( 1 );
892 }
893
Steven Cooremancd640932021-03-08 12:00:27 +0100894 /* If policy_alg is an at-least-this-length wildcard MAC algorithm,
895 * check for the requested MAC length to be equal to or longer than the
896 * minimum allowed length. */
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100897 if( ( policy_alg & PSA_ALG_MAC_AT_LEAST_THIS_LENGTH_FLAG ) != 0 )
898 {
899 return( PSA_MAC_TRUNCATED_LENGTH( policy_alg ) <=
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +0100900 requested_output_length );
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100901 }
Gilles Peskined6f371b2019-05-10 19:33:38 +0200902 }
Steven Cooremance48e852020-10-05 16:02:45 +0200903 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +0200904 * a key derivation with that key agreement should also be allowed. This
905 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +0200906 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
907 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
908 {
909 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
910 policy_alg );
911 }
Steven Cooremanb3ce8152021-02-18 12:03:50 +0100912 /* If it isn't explicitly permitted, it's forbidden. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200913 return( 0 );
914}
915
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100916/** Test whether a policy permits an algorithm.
917 *
918 * The caller must test usage flags separately.
Steven Cooreman7e39f052021-02-23 14:18:32 +0100919 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100920 * \note This function requires providing the key type for which the policy is
921 * being validated, since some algorithm policy definitions (e.g. MAC)
922 * have different properties depending on what kind of cipher it is
923 * combined with.
924 *
Steven Cooreman7e39f052021-02-23 14:18:32 +0100925 * \retval PSA_SUCCESS When \p alg is a specific algorithm
926 * allowed by the \p policy.
927 * \retval PSA_ERROR_INVALID_ARGUMENT When \p alg is not a specific algorithm
928 * \retval PSA_ERROR_NOT_PERMITTED When \p alg is a specific algorithm, but
929 * the \p policy does not allow it.
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100930 */
Steven Cooreman7e39f052021-02-23 14:18:32 +0100931static psa_status_t psa_key_policy_permits( const psa_key_policy_t *policy,
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100932 psa_key_type_t key_type,
Steven Cooreman7e39f052021-02-23 14:18:32 +0100933 psa_algorithm_t alg )
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100934{
Steven Cooremand788fab2021-02-25 11:29:17 +0100935 /* '0' is not a valid algorithm */
936 if( alg == 0 )
937 return( PSA_ERROR_INVALID_ARGUMENT );
938
Steven Cooreman7e39f052021-02-23 14:18:32 +0100939 /* A requested algorithm cannot be a wildcard. */
940 if( PSA_ALG_IS_WILDCARD( alg ) )
941 return( PSA_ERROR_INVALID_ARGUMENT );
942
Steven Cooreman5ad4bf72021-03-02 15:11:57 +0100943 if( psa_key_algorithm_permits( key_type, policy->alg, alg ) ||
944 psa_key_algorithm_permits( key_type, policy->alg2, alg ) )
Steven Cooreman7e39f052021-02-23 14:18:32 +0100945 return( PSA_SUCCESS );
946 else
947 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100948}
949
Gilles Peskinef603c712019-01-19 13:40:11 +0100950/** Restrict a key policy based on a constraint.
951 *
Steven Cooreman5a172672021-03-02 21:27:42 +0100952 * \note This function requires providing the key type for which the policy is
953 * being restricted, since some algorithm policy definitions (e.g. MAC)
954 * have different properties depending on what kind of cipher it is
955 * combined with.
956 *
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100957 * \param[in] key_type The key type for which to restrict the policy
Gilles Peskinef603c712019-01-19 13:40:11 +0100958 * \param[in,out] policy The policy to restrict.
959 * \param[in] constraint The policy constraint to apply.
960 *
961 * \retval #PSA_SUCCESS
962 * \c *policy contains the intersection of the original value of
963 * \c *policy and \c *constraint.
964 * \retval #PSA_ERROR_INVALID_ARGUMENT
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100965 * \c key_type, \c *policy and \c *constraint are incompatible.
Gilles Peskinef603c712019-01-19 13:40:11 +0100966 * \c *policy is unchanged.
967 */
968static psa_status_t psa_restrict_key_policy(
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100969 psa_key_type_t key_type,
Gilles Peskinef603c712019-01-19 13:40:11 +0100970 psa_key_policy_t *policy,
971 const psa_key_policy_t *constraint )
972{
973 psa_algorithm_t intersection_alg =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100974 psa_key_policy_algorithm_intersection( key_type, policy->alg,
975 constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200976 psa_algorithm_t intersection_alg2 =
Steven Cooreman1ac5ce32021-03-02 16:34:49 +0100977 psa_key_policy_algorithm_intersection( key_type, policy->alg2,
978 constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100979 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
980 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200981 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
982 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100983 policy->usage &= constraint->usage;
984 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200985 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100986 return( PSA_SUCCESS );
987}
988
Ronald Cron5c522922020-11-14 16:35:34 +0100989/** Get the description of a key given its identifier and policy constraints
990 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +0200991 *
Ronald Cron5c522922020-11-14 16:35:34 +0100992 * The key must have allow all the usage flags set in \p usage. If \p alg is
Steven Cooremand788fab2021-02-25 11:29:17 +0100993 * nonzero, the key must allow operations with this algorithm. If \p alg is
994 * zero, the algorithm is not checked.
Ronald Cron7587ae42020-11-11 15:04:25 +0100995 *
Ronald Cron5c522922020-11-14 16:35:34 +0100996 * In case of a persistent key, the function loads the description of the key
997 * into a key slot if not already done.
998 *
999 * On success, the returned key slot is locked. It is the responsibility of
1000 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001001 */
Ronald Cron5c522922020-11-14 16:35:34 +01001002static psa_status_t psa_get_and_lock_key_slot_with_policy(
1003 mbedtls_svc_key_id_t key,
1004 psa_key_slot_t **p_slot,
1005 psa_key_usage_t usage,
1006 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001007{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001008 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1009 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001010
Ronald Cron5c522922020-11-14 16:35:34 +01001011 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001012 if( status != PSA_SUCCESS )
1013 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001014 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001015
1016 /* Enforce that usage policy for the key slot contains all the flags
1017 * required by the usage parameter. There is one exception: public
1018 * keys can always be exported, so we treat public key objects as
1019 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001020 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001021 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001022
Gilles Peskine8e338702019-07-30 20:06:31 +02001023 if( ( slot->attr.policy.usage & usage ) != usage )
Steven Cooreman328f11c2021-03-02 11:44:51 +01001024 {
1025 status = PSA_ERROR_NOT_PERMITTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001026 goto error;
Steven Cooreman328f11c2021-03-02 11:44:51 +01001027 }
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001028
1029 /* Enforce that the usage policy permits the requested algortihm. */
Steven Cooreman7e39f052021-02-23 14:18:32 +01001030 if( alg != 0 )
1031 {
Steven Cooreman5ad4bf72021-03-02 15:11:57 +01001032 status = psa_key_policy_permits( &slot->attr.policy,
1033 slot->attr.type,
1034 alg );
Steven Cooreman7e39f052021-02-23 14:18:32 +01001035 if( status != PSA_SUCCESS )
1036 goto error;
1037 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001038
Darryl Green06fd18d2018-07-16 11:21:11 +01001039 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001040
1041error:
1042 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001043 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001044
1045 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001046}
Darryl Green940d72c2018-07-13 13:18:51 +01001047
Ronald Cron5c522922020-11-14 16:35:34 +01001048/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001049 *
1050 * A transparent key is a key for which the key material is directly
1051 * available, as opposed to a key in a secure element.
1052 *
Ronald Cron5c522922020-11-14 16:35:34 +01001053 * This is a temporary function to use instead of
1054 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1055 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001056 *
Ronald Cron5c522922020-11-14 16:35:34 +01001057 * On success, the returned key slot is locked. It is the responsibility of the
1058 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001059 */
1060#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001061static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1062 mbedtls_svc_key_id_t key,
1063 psa_key_slot_t **p_slot,
1064 psa_key_usage_t usage,
1065 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001066{
Ronald Cron5c522922020-11-14 16:35:34 +01001067 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1068 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001069 if( status != PSA_SUCCESS )
1070 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001071
Gilles Peskineadad8132019-07-25 11:31:23 +02001072 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001073 {
Ronald Cron5c522922020-11-14 16:35:34 +01001074 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001075 *p_slot = NULL;
1076 return( PSA_ERROR_NOT_SUPPORTED );
1077 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001078
Gilles Peskine28f8f302019-07-24 13:30:31 +02001079 return( PSA_SUCCESS );
1080}
1081#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1082/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001083#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1084 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001087/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001088static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001089{
Ronald Cronea0f8a62020-11-25 17:52:23 +01001090 /* Data pointer will always be either a valid pointer or NULL in an
1091 * initialized slot, so we can just free it. */
1092 if( slot->key.data != NULL )
1093 mbedtls_platform_zeroize( slot->key.data, slot->key.bytes);
1094
1095 mbedtls_free( slot->key.data );
1096 slot->key.data = NULL;
1097 slot->key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001098
1099 return( PSA_SUCCESS );
1100}
1101
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001102/** Completely wipe a slot in memory, including its policy.
1103 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001104psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001105{
1106 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001107
1108 /*
1109 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001110 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001111 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1112 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001113 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001114 */
Ronald Cron5c522922020-11-14 16:35:34 +01001115 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001116 {
1117#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001118 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001119#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001120 status = PSA_ERROR_CORRUPTION_DETECTED;
1121 }
1122
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001123 /* Multipart operations may still be using the key. This is safe
1124 * because all multipart operation objects are independent from
1125 * the key slot: if they need to access the key after the setup
1126 * phase, they have a copy of the key. Note that this means that
1127 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001128 /* At this point, key material and other type-specific content has
1129 * been wiped. Clear remaining metadata. We can call memset and not
1130 * zeroize because the metadata is not particularly sensitive. */
1131 memset( slot, 0, sizeof( *slot ) );
1132 return( status );
1133}
1134
Ronald Croncf56a0a2020-08-04 09:51:30 +02001135psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001137 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001138 psa_status_t status; /* status of the last operation */
1139 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001140#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1141 psa_se_drv_table_entry_t *driver;
1142#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001143
Ronald Croncf56a0a2020-08-04 09:51:30 +02001144 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001145 return( PSA_SUCCESS );
1146
Ronald Cronf2911112020-10-29 17:51:10 +01001147 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001148 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001149 * key, this will load the key description from persistent memory if not
1150 * done yet. We cannot avoid this loading as without it we don't know if
1151 * the key is operated by an SE or not and this information is needed by
1152 * the current implementation.
1153 */
Ronald Cron5c522922020-11-14 16:35:34 +01001154 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001155 if( status != PSA_SUCCESS )
1156 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001157
Ronald Cronf2911112020-10-29 17:51:10 +01001158 /*
1159 * If the key slot containing the key description is under access by the
1160 * library (apart from the present access), the key cannot be destroyed
1161 * yet. For the time being, just return in error. Eventually (to be
1162 * implemented), the key should be destroyed when all accesses have
1163 * stopped.
1164 */
Ronald Cron5c522922020-11-14 16:35:34 +01001165 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001166 {
Ronald Cron5c522922020-11-14 16:35:34 +01001167 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001168 return( PSA_ERROR_GENERIC_ERROR );
1169 }
1170
Gilles Peskine354f7672019-07-12 23:46:38 +02001171#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001172 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001173 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001174 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001175 /* For a key in a secure element, we need to do three things:
1176 * remove the key file in internal storage, destroy the
1177 * key inside the secure element, and update the driver's
1178 * persistent data. Start a transaction that will encompass these
1179 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001180 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001181 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001182 psa_crypto_transaction.key.slot = psa_key_slot_get_slot_number( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02001183 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001184 status = psa_crypto_save_transaction( );
1185 if( status != PSA_SUCCESS )
1186 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001187 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001188 /* We should still try to destroy the key in the secure
1189 * element and the key metadata in storage. This is especially
1190 * important if the error is that the storage is full.
1191 * But how to do it exactly without risking an inconsistent
1192 * state after a reset?
1193 * https://github.com/ARMmbed/mbed-crypto/issues/215
1194 */
1195 overall_status = status;
1196 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001197 }
1198
Ronald Cronea0f8a62020-11-25 17:52:23 +01001199 status = psa_destroy_se_key( driver,
1200 psa_key_slot_get_slot_number( slot ) );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001201 if( overall_status == PSA_SUCCESS )
1202 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001203 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1205
Darryl Greend49a4992018-06-18 17:27:26 +01001206#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001207 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001208 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001209 status = psa_destroy_persistent_key( slot->attr.id );
1210 if( overall_status == PSA_SUCCESS )
1211 overall_status = status;
1212
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001213 /* TODO: other slots may have a copy of the same key. We should
1214 * invalidate them.
1215 * https://github.com/ARMmbed/mbed-crypto/issues/214
1216 */
Darryl Greend49a4992018-06-18 17:27:26 +01001217 }
1218#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001219
Gilles Peskinefc762652019-07-22 19:30:34 +02001220#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1221 if( driver != NULL )
1222 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001223 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001224 if( overall_status == PSA_SUCCESS )
1225 overall_status = status;
1226 status = psa_crypto_stop_transaction( );
1227 if( overall_status == PSA_SUCCESS )
1228 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001229 }
1230#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1231
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233exit:
1234#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001235 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001236 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1237 if( overall_status == PSA_SUCCESS )
1238 overall_status = status;
1239 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001240}
1241
John Durkop07cc04a2020-11-16 22:08:34 -08001242#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001243 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001244static psa_status_t psa_get_rsa_public_exponent(
1245 const mbedtls_rsa_context *rsa,
1246 psa_key_attributes_t *attributes )
1247{
1248 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001249 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001250 uint8_t *buffer = NULL;
1251 size_t buflen;
1252 mbedtls_mpi_init( &mpi );
1253
1254 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1255 if( ret != 0 )
1256 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001257 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1258 {
1259 /* It's the default value, which is reported as an empty string,
1260 * so there's nothing to do. */
1261 goto exit;
1262 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001263
1264 buflen = mbedtls_mpi_size( &mpi );
1265 buffer = mbedtls_calloc( 1, buflen );
1266 if( buffer == NULL )
1267 {
1268 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1269 goto exit;
1270 }
1271 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1272 if( ret != 0 )
1273 goto exit;
1274 attributes->domain_parameters = buffer;
1275 attributes->domain_parameters_size = buflen;
1276
1277exit:
1278 mbedtls_mpi_free( &mpi );
1279 if( ret != 0 )
1280 mbedtls_free( buffer );
1281 return( mbedtls_to_psa_error( ret ) );
1282}
John Durkop07cc04a2020-11-16 22:08:34 -08001283#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001284 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001285
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001286/** Retrieve all the publicly-accessible attributes of a key.
1287 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001288psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001289 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001290{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001291 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001292 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001293 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001294
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001295 psa_reset_key_attributes( attributes );
1296
Ronald Cron5c522922020-11-14 16:35:34 +01001297 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001298 if( status != PSA_SUCCESS )
1299 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001300
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001301 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001302 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1303 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1304
1305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1306 if( psa_key_slot_is_external( slot ) )
Ronald Cronea0f8a62020-11-25 17:52:23 +01001307 psa_set_key_slot_number( attributes,
1308 psa_key_slot_get_slot_number( slot ) );
Gilles Peskinec8000c02019-08-02 20:15:51 +02001309#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001310
Gilles Peskine8e338702019-07-30 20:06:31 +02001311 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001312 {
John Durkop07cc04a2020-11-16 22:08:34 -08001313#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001314 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001315 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001316 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001317#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001318 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001319 * is not yet implemented.
1320 * https://github.com/ARMmbed/mbed-crypto/issues/216
1321 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001322 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001323 break;
1324#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001325 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001326 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001327
Ronald Cron90857082020-11-25 14:58:33 +01001328 status = mbedtls_psa_rsa_load_representation(
1329 slot->attr.type,
1330 slot->key.data,
1331 slot->key.bytes,
1332 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001333 if( status != PSA_SUCCESS )
1334 break;
1335
Steven Cooremana2371e52020-07-28 14:30:39 +02001336 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001337 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001338 mbedtls_rsa_free( rsa );
1339 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001340 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001341 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001342#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001343 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001344 default:
1345 /* Nothing else to do. */
1346 break;
1347 }
1348
1349 if( status != PSA_SUCCESS )
1350 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001351
Ronald Cron5c522922020-11-14 16:35:34 +01001352 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001353
Ronald Cron5c522922020-11-14 16:35:34 +01001354 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001355}
1356
Gilles Peskinec8000c02019-08-02 20:15:51 +02001357#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1358psa_status_t psa_get_key_slot_number(
1359 const psa_key_attributes_t *attributes,
1360 psa_key_slot_number_t *slot_number )
1361{
1362 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1363 {
1364 *slot_number = attributes->slot_number;
1365 return( PSA_SUCCESS );
1366 }
1367 else
1368 return( PSA_ERROR_INVALID_ARGUMENT );
1369}
1370#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1371
Ronald Crond18b5f82020-11-25 16:46:05 +01001372static psa_status_t psa_export_key_buffer_internal( const uint8_t *key_buffer,
1373 size_t key_buffer_size,
Steven Cooremana01795d2020-07-24 22:48:15 +02001374 uint8_t *data,
1375 size_t data_size,
1376 size_t *data_length )
1377{
Ronald Crond18b5f82020-11-25 16:46:05 +01001378 if( key_buffer_size > data_size )
Steven Cooremana01795d2020-07-24 22:48:15 +02001379 return( PSA_ERROR_BUFFER_TOO_SMALL );
Ronald Crond18b5f82020-11-25 16:46:05 +01001380 memcpy( data, key_buffer, key_buffer_size );
1381 memset( data + key_buffer_size, 0,
1382 data_size - key_buffer_size );
1383 *data_length = key_buffer_size;
Steven Cooremana01795d2020-07-24 22:48:15 +02001384 return( PSA_SUCCESS );
1385}
1386
Ronald Cron7285cda2020-11-26 14:46:37 +01001387psa_status_t psa_export_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001388 const psa_key_attributes_t *attributes,
1389 const uint8_t *key_buffer, size_t key_buffer_size,
1390 uint8_t *data, size_t data_size, size_t *data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001391{
Ronald Crond18b5f82020-11-25 16:46:05 +01001392 psa_key_type_t type = attributes->core.type;
1393
Ronald Crond18b5f82020-11-25 16:46:05 +01001394 if( key_type_is_raw_bytes( type ) ||
1395 PSA_KEY_TYPE_IS_RSA( type ) ||
1396 PSA_KEY_TYPE_IS_ECC( type ) )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001397 {
1398 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001399 key_buffer, key_buffer_size,
1400 data, data_size, data_length ) );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001401 }
1402 else
1403 {
1404 /* This shouldn't happen in the reference implementation, but
1405 it is valid for a special-purpose implementation to omit
1406 support for exporting certain key types. */
1407 return( PSA_ERROR_NOT_SUPPORTED );
1408 }
1409}
1410
1411psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
1412 uint8_t *data,
1413 size_t data_size,
1414 size_t *data_length )
1415{
1416 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1417 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
1418 psa_key_slot_t *slot;
1419
Ronald Crone907e552021-01-18 13:22:38 +01001420 /* Reject a zero-length output buffer now, since this can never be a
1421 * valid key representation. This way we know that data must be a valid
1422 * pointer and we can do things like memset(data, ..., data_size). */
1423 if( data_size == 0 )
1424 return( PSA_ERROR_BUFFER_TOO_SMALL );
1425
Ronald Cron9486f9d2020-11-26 13:36:23 +01001426 /* Set the key to empty now, so that even when there are errors, we always
1427 * set data_length to a value between 0 and data_size. On error, setting
1428 * the key to empty is a good choice because an empty key representation is
1429 * unlikely to be accepted anywhere. */
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001430 *data_length = 0;
1431
Ronald Cron9486f9d2020-11-26 13:36:23 +01001432 /* Export requires the EXPORT flag. There is an exception for public keys,
1433 * which don't require any flag, but
1434 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1435 */
1436 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1437 PSA_KEY_USAGE_EXPORT, 0 );
1438 if( status != PSA_SUCCESS )
1439 return( status );
mohammad160306e79202018-03-28 13:17:44 +03001440
Ronald Crond18b5f82020-11-25 16:46:05 +01001441 psa_key_attributes_t attributes = {
1442 .core = slot->attr
1443 };
Ronald Cron67227982020-11-26 15:16:05 +01001444 status = psa_driver_wrapper_export_key( &attributes,
Ronald Crond18b5f82020-11-25 16:46:05 +01001445 slot->key.data, slot->key.bytes,
1446 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001447
Ronald Cron9486f9d2020-11-26 13:36:23 +01001448 unlock_status = psa_unlock_key_slot( slot );
1449
1450 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
1451}
1452
Ronald Cron7285cda2020-11-26 14:46:37 +01001453psa_status_t psa_export_public_key_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001454 const psa_key_attributes_t *attributes,
1455 const uint8_t *key_buffer,
1456 size_t key_buffer_size,
1457 uint8_t *data,
1458 size_t data_size,
1459 size_t *data_length )
Ronald Cron9486f9d2020-11-26 13:36:23 +01001460{
Ronald Crond18b5f82020-11-25 16:46:05 +01001461 psa_key_type_t type = attributes->core.type;
Ronald Crond18b5f82020-11-25 16:46:05 +01001462
Ronald Crond18b5f82020-11-25 16:46:05 +01001463 if( PSA_KEY_TYPE_IS_RSA( type ) || PSA_KEY_TYPE_IS_ECC( type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001464 {
Ronald Crond18b5f82020-11-25 16:46:05 +01001465 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001466 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001467 /* Exporting public -> public */
Ronald Cron9486f9d2020-11-26 13:36:23 +01001468 return( psa_export_key_buffer_internal(
Ronald Crond18b5f82020-11-25 16:46:05 +01001469 key_buffer, key_buffer_size,
1470 data, data_size, data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001471 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001472
Ronald Crond18b5f82020-11-25 16:46:05 +01001473 if( PSA_KEY_TYPE_IS_RSA( type ) )
Steven Cooreman560c28a2020-07-24 23:20:24 +02001474 {
John Durkop0e005192020-10-31 22:06:54 -07001475#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1476 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001477 return( mbedtls_psa_rsa_export_public_key( attributes,
1478 key_buffer,
1479 key_buffer_size,
1480 data,
1481 data_size,
1482 data_length ) );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001483#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001484 /* We don't know how to convert a private RSA key to public. */
1485 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001486#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1487 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001488 }
1489 else
Moran Pekera998bc62018-04-16 18:16:20 +03001490 {
John Durkop6ba40d12020-11-10 08:50:04 -08001491#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1492 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Ronald Crone5ca3d82020-11-26 16:36:16 +01001493 return( mbedtls_psa_ecp_export_public_key( attributes,
1494 key_buffer,
1495 key_buffer_size,
1496 data,
1497 data_size,
1498 data_length ) );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001499#else
1500 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001501 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001502#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1503 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001504 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001505 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001506 else
1507 {
1508 /* This shouldn't happen in the reference implementation, but
1509 it is valid for a special-purpose implementation to omit
1510 support for exporting certain key types. */
1511 return( PSA_ERROR_NOT_SUPPORTED );
1512 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001513}
Ronald Crond18b5f82020-11-25 16:46:05 +01001514
Ronald Croncf56a0a2020-08-04 09:51:30 +02001515psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001516 uint8_t *data,
1517 size_t data_size,
1518 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001519{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001520 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001521 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001522 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001523
Ronald Crone907e552021-01-18 13:22:38 +01001524 /* Reject a zero-length output buffer now, since this can never be a
1525 * valid key representation. This way we know that data must be a valid
1526 * pointer and we can do things like memset(data, ..., data_size). */
1527 if( data_size == 0 )
1528 return( PSA_ERROR_BUFFER_TOO_SMALL );
1529
Darryl Greendd8fb772018-11-07 16:00:44 +00001530 /* Set the key to empty now, so that even when there are errors, we always
1531 * set data_length to a value between 0 and data_size. On error, setting
1532 * the key to empty is a good choice because an empty key representation is
1533 * unlikely to be accepted anywhere. */
1534 *data_length = 0;
1535
1536 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001537 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001538 if( status != PSA_SUCCESS )
1539 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001540
Ronald Cron9486f9d2020-11-26 13:36:23 +01001541 if( ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
1542 {
1543 status = PSA_ERROR_INVALID_ARGUMENT;
1544 goto exit;
1545 }
1546
Ronald Crond18b5f82020-11-25 16:46:05 +01001547 psa_key_attributes_t attributes = {
1548 .core = slot->attr
1549 };
Ronald Cron67227982020-11-26 15:16:05 +01001550 status = psa_driver_wrapper_export_public_key(
Ronald Crond18b5f82020-11-25 16:46:05 +01001551 &attributes, slot->key.data, slot->key.bytes,
1552 data, data_size, data_length );
Ronald Cron9486f9d2020-11-26 13:36:23 +01001553
1554exit:
Ronald Cron5c522922020-11-14 16:35:34 +01001555 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001556
Ronald Cron5c522922020-11-14 16:35:34 +01001557 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001558}
1559
Gilles Peskine91e8c332019-08-02 19:19:39 +02001560#if defined(static_assert)
1561static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1562 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001563static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001564 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001565static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001566 "One or more key attribute flag is listed as both internal-only and external-only" );
1567#endif
1568
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001569/** Validate that a key policy is internally well-formed.
1570 *
1571 * This function only rejects invalid policies. It does not validate the
1572 * consistency of the policy with respect to other attributes of the key
1573 * such as the key type.
1574 */
1575static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001576{
1577 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001578 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001579 PSA_KEY_USAGE_ENCRYPT |
1580 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001581 PSA_KEY_USAGE_SIGN_HASH |
1582 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001583 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1584 return( PSA_ERROR_INVALID_ARGUMENT );
1585
Gilles Peskine4747d192019-04-17 15:05:45 +02001586 return( PSA_SUCCESS );
1587}
1588
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001589/** Validate the internal consistency of key attributes.
1590 *
1591 * This function only rejects invalid attribute values. If does not
1592 * validate the consistency of the attributes with any key data that may
1593 * be involved in the creation of the key.
1594 *
1595 * Call this function early in the key creation process.
1596 *
1597 * \param[in] attributes Key attributes for the new key.
1598 * \param[out] p_drv On any return, the driver for the key, if any.
1599 * NULL for a transparent key.
1600 *
1601 */
1602static psa_status_t psa_validate_key_attributes(
1603 const psa_key_attributes_t *attributes,
1604 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001605{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001606 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001607 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001608 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001609
Ronald Cron54b90082020-10-29 15:26:43 +01001610 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001611 if( status != PSA_SUCCESS )
1612 return( status );
1613
Ronald Crond2ed4812020-07-17 16:11:30 +02001614 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001615 if( status != PSA_SUCCESS )
1616 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001617
Ronald Cron65f38a32020-10-23 17:11:13 +02001618 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1619 {
1620 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1621 return( PSA_ERROR_INVALID_ARGUMENT );
1622 }
1623 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001624 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001625 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001626 if( status != PSA_SUCCESS )
1627 return( status );
1628 }
1629
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001630 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001631 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001632 return( status );
1633
1634 /* Refuse to create overly large keys.
1635 * Note that this doesn't trigger on import if the attributes don't
1636 * explicitly specify a size (so psa_get_key_bits returns 0), so
1637 * psa_import_key() needs its own checks. */
1638 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1639 return( PSA_ERROR_NOT_SUPPORTED );
1640
Gilles Peskine91e8c332019-08-02 19:19:39 +02001641 /* Reject invalid flags. These should not be reachable through the API. */
1642 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1643 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1644 return( PSA_ERROR_INVALID_ARGUMENT );
1645
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001646 return( PSA_SUCCESS );
1647}
1648
Gilles Peskine4747d192019-04-17 15:05:45 +02001649/** Prepare a key slot to receive key material.
1650 *
1651 * This function allocates a key slot and sets its metadata.
1652 *
1653 * If this function fails, call psa_fail_key_creation().
1654 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001655 * This function is intended to be used as follows:
1656 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001657 * it with the specified attributes, and in case of a volatile key assign it
1658 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001659 * -# Populate the slot with the key material.
1660 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1661 * In case of failure at any step, stop the sequence and call
1662 * psa_fail_key_creation().
1663 *
Ronald Cron5c522922020-11-14 16:35:34 +01001664 * On success, the key slot is locked. It is the responsibility of the caller
1665 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001666 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001667 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001668 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001669 * \param[out] p_slot On success, a pointer to the prepared slot.
1670 * \param[out] p_drv On any return, the driver for the key, if any.
1671 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001672 *
1673 * \retval #PSA_SUCCESS
1674 * The key slot is ready to receive key material.
1675 * \return If this function fails, the key slot is an invalid state.
1676 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001677 */
1678static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001679 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001680 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001681 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001682 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001683{
1684 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001685 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02001686 psa_key_slot_t *slot;
1687
Gilles Peskinedf179142019-07-15 22:02:14 +02001688 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001689 *p_drv = NULL;
1690
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001691 status = psa_validate_key_attributes( attributes, p_drv );
1692 if( status != PSA_SUCCESS )
1693 return( status );
1694
Ronald Cronc4d1b512020-07-31 11:26:37 +02001695 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001696 if( status != PSA_SUCCESS )
1697 return( status );
1698 slot = *p_slot;
1699
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001700 /* We're storing the declared bit-size of the key. It's up to each
1701 * creation mechanism to verify that this information is correct.
1702 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001703 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02001704 * is optional (import, copy). In case of a volatile key, assign it the
1705 * volatile key identifier associated to the slot returned to contain its
1706 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001707
1708 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02001709 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
1710 {
1711#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
1712 slot->attr.id = volatile_key_id;
1713#else
1714 slot->attr.id.key_id = volatile_key_id;
1715#endif
1716 }
Gilles Peskinec744d992019-07-30 17:26:54 +02001717
Gilles Peskine91e8c332019-08-02 19:19:39 +02001718 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001719 * external-only flags, query `attributes`. Thanks to the check
1720 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001721 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001722 * may have set. */
1723 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001724
Gilles Peskinecbaff462019-07-12 23:46:04 +02001725#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001726 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001727 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001728 * create the key file in internal storage, create the
1729 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001730 * persistent data. This is done by starting a transaction that will
1731 * encompass these three actions.
1732 * For registering a volatile key, we just need to find an appropriate
1733 * slot number inside the SE. Since the key is designated volatile, creating
1734 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001735 /* The first thing to do is to find a slot number for the new key.
1736 * We save the slot number in persistent storage as part of the
1737 * transaction data. It will be needed to recover if the power
1738 * fails during the key creation process, to clean up on the secure
1739 * element side after restarting. Obtaining a slot number from the
1740 * secure element driver updates its persistent state, but we do not yet
1741 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001742 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001743 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001744 {
Ronald Cronea0f8a62020-11-25 17:52:23 +01001745 psa_key_slot_number_t slot_number;
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001746 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001747 &slot_number );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001748 if( status != PSA_SUCCESS )
1749 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001750
1751 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001752 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001753 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1754 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001755 psa_crypto_transaction.key.slot = slot_number;
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001756 psa_crypto_transaction.key.id = slot->attr.id;
1757 status = psa_crypto_save_transaction( );
1758 if( status != PSA_SUCCESS )
1759 {
1760 (void) psa_crypto_stop_transaction( );
1761 return( status );
1762 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001763 }
Ronald Cronea0f8a62020-11-25 17:52:23 +01001764
1765 status = psa_copy_key_material_into_slot(
1766 slot, (uint8_t *)( &slot_number ), sizeof( slot_number ) );
Darryl Green0c6575a2018-11-07 16:05:30 +00001767 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001768
1769 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1770 {
1771 /* Key registration only makes sense with a secure element. */
1772 return( PSA_ERROR_INVALID_ARGUMENT );
1773 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001774#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1775
Ronald Cronc4d1b512020-07-31 11:26:37 +02001776 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00001777}
Gilles Peskine4747d192019-04-17 15:05:45 +02001778
1779/** Finalize the creation of a key once its key material has been set.
1780 *
1781 * This entails writing the key to persistent storage.
1782 *
1783 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001784 * See the documentation of psa_start_key_creation() for the intended use
1785 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001786 *
Ronald Cron5c522922020-11-14 16:35:34 +01001787 * If the finalization succeeds, the function unlocks the key slot (it was
1788 * locked by psa_start_key_creation()) and the key slot cannot be accessed
1789 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01001790 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001791 * \param[in,out] slot Pointer to the slot with key material.
1792 * \param[in] driver The secure element driver for the key,
1793 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01001794 * \param[out] key On success, identifier of the key. Note that the
1795 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001796 *
1797 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02001798 * The key was successfully created.
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001799 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1800 * \retval #PSA_ERROR_INSUFFICIENT_STORAGE
1801 * \retval #PSA_ERROR_ALREADY_EXISTS
1802 * \retval #PSA_ERROR_DATA_INVALID
1803 * \retval #PSA_ERROR_DATA_CORRUPT
gabor-mezei-arm86326a92020-11-30 16:50:34 +01001804 * \retval #PSA_ERROR_STORAGE_FAILURE
gabor-mezei-arm452b0a32020-11-09 17:42:55 +01001805 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001806 * \return If this function fails, the key slot is an invalid state.
1807 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001808 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001809static psa_status_t psa_finish_key_creation(
1810 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01001811 psa_se_drv_table_entry_t *driver,
1812 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02001813{
1814 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001815 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001816 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001817
1818#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001819 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001820 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001821#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1822 if( driver != NULL )
1823 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001824 psa_se_key_data_storage_t data;
Ronald Cronea0f8a62020-11-25 17:52:23 +01001825 psa_key_slot_number_t slot_number =
1826 psa_key_slot_get_slot_number( slot ) ;
1827
Gilles Peskineb46bef22019-07-30 21:32:04 +02001828#if defined(static_assert)
Ronald Cronea0f8a62020-11-25 17:52:23 +01001829 static_assert( sizeof( slot_number ) ==
Gilles Peskineb46bef22019-07-30 21:32:04 +02001830 sizeof( data.slot_number ),
1831 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02001832#endif
Ronald Cronea0f8a62020-11-25 17:52:23 +01001833 memcpy( &data.slot_number, &slot_number, sizeof( slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001834 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001835 (uint8_t*) &data,
1836 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001837 }
1838 else
1839#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1840 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001841 /* Key material is saved in export representation in the slot, so
1842 * just pass the slot buffer for storage. */
1843 status = psa_save_persistent_key( &slot->attr,
Ronald Cronea0f8a62020-11-25 17:52:23 +01001844 slot->key.data,
1845 slot->key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001846 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001847 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001848#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1849
Gilles Peskinecbaff462019-07-12 23:46:04 +02001850#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001851 /* Finish the transaction for a key creation. This does not
1852 * happen when registering an existing key. Detect this case
1853 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001854 * creation of a persistent key in a secure element requires a transaction,
1855 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001856 if( driver != NULL &&
1857 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001858 {
1859 status = psa_save_se_persistent_data( driver );
1860 if( status != PSA_SUCCESS )
1861 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001862 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001863 return( status );
1864 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001865 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001866 }
1867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868
Ronald Cron50972942020-11-14 11:28:25 +01001869 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01001870 {
1871 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01001872 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01001873 if( status != PSA_SUCCESS )
1874 *key = MBEDTLS_SVC_KEY_ID_INIT;
1875 }
Ronald Cron50972942020-11-14 11:28:25 +01001876
Gilles Peskine4747d192019-04-17 15:05:45 +02001877 return( status );
1878}
1879
1880/** Abort the creation of a key.
1881 *
1882 * You may call this function after calling psa_start_key_creation(),
1883 * or after psa_finish_key_creation() fails. In other circumstances, this
1884 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001885 * See the documentation of psa_start_key_creation() for the intended use
1886 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001887 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001888 * \param[in,out] slot Pointer to the slot with key material.
1889 * \param[in] driver The secure element driver for the key,
1890 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001891 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001892static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001893 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 (void) driver;
1896
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 if( slot == NULL )
1898 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001899
1900#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001901 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 * element, and the failure happened later (when saving metadata
1903 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001904 * element.
1905 * https://github.com/ARMmbed/mbed-crypto/issues/217
1906 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001907
Gilles Peskined7729582019-08-05 15:55:54 +02001908 /* Abort the ongoing transaction if any (there may not be one if
1909 * the creation process failed before starting one, or if the
1910 * key creation is a registration of a key in a secure element).
1911 * Earlier functions must already have done what it takes to undo any
1912 * partial creation. All that's left is to update the transaction data
1913 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001914 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001915#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1916
Gilles Peskine4747d192019-04-17 15:05:45 +02001917 psa_wipe_key_slot( slot );
1918}
1919
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001920/** Validate optional attributes during key creation.
1921 *
1922 * Some key attributes are optional during key creation. If they are
1923 * specified in the attributes structure, check that they are consistent
1924 * with the data in the slot.
1925 *
1926 * This function should be called near the end of key creation, after
1927 * the slot in memory is fully populated but before saving persistent data.
1928 */
1929static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001930 const psa_key_slot_t *slot,
1931 const psa_key_attributes_t *attributes )
1932{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001933 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001934 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001935 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001936 return( PSA_ERROR_INVALID_ARGUMENT );
1937 }
1938
1939 if( attributes->domain_parameters_size != 0 )
1940 {
John Durkop0e005192020-10-31 22:06:54 -07001941#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1942 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02001943 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001944 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001945 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02001946 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02001947 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02001948
Ronald Cron90857082020-11-25 14:58:33 +01001949 psa_status_t status = mbedtls_psa_rsa_load_representation(
1950 slot->attr.type,
1951 slot->key.data,
1952 slot->key.bytes,
1953 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001954 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001955 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02001956
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001957 mbedtls_mpi_init( &actual );
1958 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02001959 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001960 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02001961 mbedtls_rsa_free( rsa );
1962 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001963 if( ret != 0 )
1964 goto rsa_exit;
1965 ret = mbedtls_mpi_read_binary( &required,
1966 attributes->domain_parameters,
1967 attributes->domain_parameters_size );
1968 if( ret != 0 )
1969 goto rsa_exit;
1970 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1971 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1972 rsa_exit:
1973 mbedtls_mpi_free( &actual );
1974 mbedtls_mpi_free( &required );
1975 if( ret != 0)
1976 return( mbedtls_to_psa_error( ret ) );
1977 }
1978 else
John Durkop9814fa22020-11-04 12:28:15 -08001979#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1980 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001981 {
1982 return( PSA_ERROR_INVALID_ARGUMENT );
1983 }
1984 }
1985
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001986 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001987 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001988 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001989 return( PSA_ERROR_INVALID_ARGUMENT );
1990 }
1991
1992 return( PSA_SUCCESS );
1993}
1994
Gilles Peskine4747d192019-04-17 15:05:45 +02001995psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001996 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001997 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02001998 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02001999{
2000 psa_status_t status;
2001 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002002 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002003 size_t bits;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004
Ronald Cron81709fc2020-11-14 12:10:32 +01002005 *key = MBEDTLS_SVC_KEY_ID_INIT;
2006
Gilles Peskine0f84d622019-09-12 19:03:13 +02002007 /* Reject zero-length symmetric keys (including raw data key objects).
2008 * This also rejects any key which might be encoded as an empty string,
2009 * which is never valid. */
2010 if( data_length == 0 )
2011 return( PSA_ERROR_INVALID_ARGUMENT );
2012
Gilles Peskinedf179142019-07-15 22:02:14 +02002013 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002014 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002015 if( status != PSA_SUCCESS )
2016 goto exit;
2017
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002018 /* In the case of a transparent key or an opaque key stored in local
2019 * storage (thus not in the case of generating a key in a secure element
2020 * or cryptoprocessor with storage), we have to allocate a buffer to
2021 * hold the generated key material. */
2022 if( slot->key.data == NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02002023 {
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002024 status = psa_allocate_buffer_to_slot( slot, data_length );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002025 if( status != PSA_SUCCESS )
2026 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002027 }
Ronald Cronfb2ed5b2020-11-30 12:11:01 +01002028
2029 bits = slot->attr.bits;
2030 status = psa_driver_wrapper_import_key( attributes,
2031 data, data_length,
2032 slot->key.data,
2033 slot->key.bytes,
2034 &slot->key.bytes, &bits );
2035 if( status != PSA_SUCCESS )
2036 goto exit;
2037
2038 if( slot->attr.bits == 0 )
2039 slot->attr.bits = (psa_key_bits_t) bits;
2040 else if( bits != slot->attr.bits )
Steven Cooremanac3434f2021-01-15 17:36:02 +01002041 {
Steven Cooremanac3434f2021-01-15 17:36:02 +01002042 status = PSA_ERROR_INVALID_ARGUMENT;
2043 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002044 }
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01002045
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002046 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002047 if( status != PSA_SUCCESS )
2048 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002049
Ronald Cron81709fc2020-11-14 12:10:32 +01002050 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002051exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002052 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002053 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002054
Gilles Peskine4747d192019-04-17 15:05:45 +02002055 return( status );
2056}
2057
Gilles Peskined7729582019-08-05 15:55:54 +02002058#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2059psa_status_t mbedtls_psa_register_se_key(
2060 const psa_key_attributes_t *attributes )
2061{
2062 psa_status_t status;
2063 psa_key_slot_t *slot = NULL;
2064 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002065 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002066
2067 /* Leaving attributes unspecified is not currently supported.
2068 * It could make sense to query the key type and size from the
2069 * secure element, but not all secure elements support this
2070 * and the driver HAL doesn't currently support it. */
2071 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2072 return( PSA_ERROR_NOT_SUPPORTED );
2073 if( psa_get_key_bits( attributes ) == 0 )
2074 return( PSA_ERROR_NOT_SUPPORTED );
2075
2076 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002077 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002078 if( status != PSA_SUCCESS )
2079 goto exit;
2080
Ronald Cron81709fc2020-11-14 12:10:32 +01002081 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002082
2083exit:
2084 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002085 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002086
Gilles Peskined7729582019-08-05 15:55:54 +02002087 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002088 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002089 return( status );
2090}
2091#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2092
Gilles Peskinef603c712019-01-19 13:40:11 +01002093static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002094 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002095{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002096 psa_status_t status = psa_copy_key_material_into_slot( target,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002097 source->key.data,
2098 source->key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002099 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002100 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002101
Steven Cooreman398aee52020-10-13 14:35:45 +02002102 target->attr.type = source->attr.type;
2103 target->attr.bits = source->attr.bits;
2104
2105 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002106}
2107
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002109 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002110 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002111{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002112 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002113 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002114 psa_key_slot_t *source_slot = NULL;
2115 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002116 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002117 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002118
Ronald Cron81709fc2020-11-14 12:10:32 +01002119 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2120
Ronald Cron5c522922020-11-14 16:35:34 +01002121 status = psa_get_and_lock_transparent_key_slot_with_policy(
2122 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002123 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002124 goto exit;
2125
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002126 status = psa_validate_optional_attributes( source_slot,
2127 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002128 if( status != PSA_SUCCESS )
2129 goto exit;
2130
Steven Cooreman1ac5ce32021-03-02 16:34:49 +01002131 status = psa_restrict_key_policy( source_slot->attr.type,
2132 &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002133 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002134 if( status != PSA_SUCCESS )
2135 goto exit;
2136
Ronald Cron81709fc2020-11-14 12:10:32 +01002137 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2138 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002139 if( status != PSA_SUCCESS )
2140 goto exit;
2141
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002142#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2143 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002144 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002145 /* Copying to a secure element is not implemented yet. */
2146 status = PSA_ERROR_NOT_SUPPORTED;
2147 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002148 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002149#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002150
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002151 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002152 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002153 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002154
Ronald Cron81709fc2020-11-14 12:10:32 +01002155 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002156exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002157 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002158 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002159
Ronald Cron5c522922020-11-14 16:35:34 +01002160 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002161
Ronald Cron5c522922020-11-14 16:35:34 +01002162 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002163}
2164
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002165
2166
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002167/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002168/* Message digests */
2169/****************************************************************/
2170
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002171psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2172{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002173 /* Aborting a non-active operation is allowed */
2174 if( operation->id == 0 )
2175 return( PSA_SUCCESS );
2176
2177 psa_status_t status = psa_driver_wrapper_hash_abort( operation );
2178 operation->id = 0;
2179
2180 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181}
2182
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002183psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184 psa_algorithm_t alg )
2185{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002186 /* A context must be freshly initialized before it can be set up. */
2187 if( operation->id != 0 )
2188 return( PSA_ERROR_BAD_STATE );
2189
2190 if( !PSA_ALG_IS_HASH( alg ) )
Steven Cooreman0e307642021-02-18 16:18:32 +01002191 return( PSA_ERROR_INVALID_ARGUMENT );
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002192
Steven Cooremanb6bf4bb2021-03-15 19:00:14 +01002193 /* Ensure all of the context is zeroized, since PSA_HASH_OPERATION_INIT only
2194 * directly zeroes the int-sized dummy member of the context union. */
Steven Cooreman893232f2021-03-15 12:23:37 +01002195 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
2196
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002197 return( psa_driver_wrapper_hash_setup( operation, alg ) );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198}
2199
2200psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2201 const uint8_t *input,
2202 size_t input_length )
2203{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002204 if( operation->id == 0 )
2205 return( PSA_ERROR_BAD_STATE );
Gilles Peskine94e44542018-07-12 16:58:43 +02002206
Steven Cooremanc8288352021-03-04 14:02:19 +01002207 /* Don't require hash implementations to behave correctly on a
2208 * zero-length input, which may have an invalid pointer. */
2209 if( input_length == 0 )
2210 return( PSA_SUCCESS );
2211
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002212 psa_status_t status = psa_driver_wrapper_hash_update( operation,
2213 input, input_length );
2214 if( status != PSA_SUCCESS )
2215 psa_hash_abort( operation );
2216
2217 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002218}
2219
2220psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2221 uint8_t *hash,
2222 size_t hash_size,
2223 size_t *hash_length )
2224{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002225 *hash_length = 0;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002226 if( operation->id == 0 )
2227 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002228
Steven Cooreman1e582352021-02-18 17:24:37 +01002229 psa_status_t status = psa_driver_wrapper_hash_finish(
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002230 operation, hash, hash_size, hash_length );
Steven Cooreman0e307642021-02-18 16:18:32 +01002231 psa_hash_abort( operation );
2232 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002233}
2234
Gilles Peskine2d277862018-06-18 15:41:12 +02002235psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2236 const uint8_t *hash,
2237 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002238{
2239 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2240 size_t actual_hash_length;
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002241 psa_status_t status = psa_hash_finish(
2242 operation,
Steven Cooreman1e582352021-02-18 17:24:37 +01002243 actual_hash, sizeof( actual_hash ),
2244 &actual_hash_length );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002245 if( status != PSA_SUCCESS )
2246 return( status );
2247 if( actual_hash_length != hash_length )
2248 return( PSA_ERROR_INVALID_SIGNATURE );
2249 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2250 return( PSA_ERROR_INVALID_SIGNATURE );
2251 return( PSA_SUCCESS );
2252}
2253
Gilles Peskine0a749c82019-11-28 19:33:58 +01002254psa_status_t psa_hash_compute( psa_algorithm_t alg,
2255 const uint8_t *input, size_t input_length,
2256 uint8_t *hash, size_t hash_size,
2257 size_t *hash_length )
2258{
Steven Cooremanfbe09282021-03-08 18:41:12 +01002259 *hash_length = 0;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002260 if( !PSA_ALG_IS_HASH( alg ) )
2261 return( PSA_ERROR_INVALID_ARGUMENT );
2262
Steven Cooreman1e582352021-02-18 17:24:37 +01002263 return( psa_driver_wrapper_hash_compute( alg, input, input_length,
2264 hash, hash_size, hash_length ) );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002265}
2266
2267psa_status_t psa_hash_compare( psa_algorithm_t alg,
2268 const uint8_t *input, size_t input_length,
2269 const uint8_t *hash, size_t hash_length )
2270{
Steven Cooreman84d670d2021-02-18 16:22:53 +01002271 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2272 size_t actual_hash_length;
Steven Cooremanf66d5fd2021-03-08 18:40:40 +01002273
2274 if( !PSA_ALG_IS_HASH( alg ) )
2275 return( PSA_ERROR_INVALID_ARGUMENT );
2276
Steven Cooreman1e582352021-02-18 17:24:37 +01002277 psa_status_t status = psa_driver_wrapper_hash_compute(
2278 alg, input, input_length,
2279 actual_hash, sizeof(actual_hash),
2280 &actual_hash_length );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002281 if( status != PSA_SUCCESS )
Steven Cooreman84d670d2021-02-18 16:22:53 +01002282 return( status );
2283 if( actual_hash_length != hash_length )
2284 return( PSA_ERROR_INVALID_SIGNATURE );
2285 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2286 return( PSA_ERROR_INVALID_SIGNATURE );
2287 return( PSA_SUCCESS );
Gilles Peskine0a749c82019-11-28 19:33:58 +01002288}
2289
Gilles Peskineeb35d782019-01-22 17:56:16 +01002290psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2291 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002292{
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002293 if( source_operation->id == 0 ||
2294 target_operation->id != 0 )
2295 {
2296 return( PSA_ERROR_BAD_STATE );
2297 }
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002298
Steven Cooremandbf8ced2021-03-04 13:01:18 +01002299 psa_status_t status = psa_driver_wrapper_hash_clone( source_operation,
2300 target_operation );
2301 if( status != PSA_SUCCESS )
2302 psa_hash_abort( target_operation );
2303
2304 return( status );
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002305}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002306
2307
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002308/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002309/* MAC */
2310/****************************************************************/
2311
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002312static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002313 psa_algorithm_t alg,
2314 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002315 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002316 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002317{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002318 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002319 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002320
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002321 if( PSA_ALG_IS_AEAD( alg ) )
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002322 alg = PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002323
Gilles Peskine8c9def32018-02-08 10:02:12 +01002324 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2325 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002326 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002327 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01002328 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002329 mode = MBEDTLS_MODE_STREAM;
2330 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002331 case PSA_ALG_CTR:
2332 mode = MBEDTLS_MODE_CTR;
2333 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002334 case PSA_ALG_CFB:
2335 mode = MBEDTLS_MODE_CFB;
2336 break;
2337 case PSA_ALG_OFB:
2338 mode = MBEDTLS_MODE_OFB;
2339 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002340 case PSA_ALG_ECB_NO_PADDING:
2341 mode = MBEDTLS_MODE_ECB;
2342 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002343 case PSA_ALG_CBC_NO_PADDING:
2344 mode = MBEDTLS_MODE_CBC;
2345 break;
2346 case PSA_ALG_CBC_PKCS7:
2347 mode = MBEDTLS_MODE_CBC;
2348 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002349 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002350 mode = MBEDTLS_MODE_CCM;
2351 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002352 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002353 mode = MBEDTLS_MODE_GCM;
2354 break;
Bence Szépkútia63b20d2020-12-16 11:36:46 +01002355 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02002356 mode = MBEDTLS_MODE_CHACHAPOLY;
2357 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002358 default:
2359 return( NULL );
2360 }
2361 }
2362 else if( alg == PSA_ALG_CMAC )
2363 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002364 else
2365 return( NULL );
2366
2367 switch( key_type )
2368 {
2369 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002370 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002371 break;
2372 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002373 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2374 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002375 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002376 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002377 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002378 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002379 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2380 * but two-key Triple-DES is functionally three-key Triple-DES
2381 * with K1=K3, so that's how we present it to mbedtls. */
2382 if( key_bits == 128 )
2383 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002384 break;
2385 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002386 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002387 break;
2388 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002389 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002390 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002391 case PSA_KEY_TYPE_CHACHA20:
2392 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2393 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002394 default:
2395 return( NULL );
2396 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002397 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002398 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002399
Jaeden Amero23bbb752018-06-26 14:16:54 +01002400 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2401 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002402}
2403
John Durkop6ba40d12020-11-10 08:50:04 -08002404#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002405static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002406{
Gilles Peskine2d277862018-06-18 15:41:12 +02002407 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002408 {
2409 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002410 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002411 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002412 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002413 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002414 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002415 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002416 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002417 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002418 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002419 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002420 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002421 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002422 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002423 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002424 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002425 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002426 return( 128 );
2427 default:
2428 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002429 }
2430}
John Durkop07cc04a2020-11-16 22:08:34 -08002431#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002432
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002433/* Initialize the MAC operation structure. Once this function has been
2434 * called, psa_mac_abort can run and will do the right thing. */
2435static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2436 psa_algorithm_t alg )
2437{
2438 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2439
Steven Cooreman15472f82021-03-02 16:16:22 +01002440 operation->alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002441 operation->key_set = 0;
2442 operation->iv_set = 0;
2443 operation->iv_required = 0;
2444 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002445 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002446
2447#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002448 if( operation->alg == PSA_ALG_CMAC )
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002449 {
2450 operation->iv_required = 0;
2451 mbedtls_cipher_init( &operation->ctx.cmac );
2452 status = PSA_SUCCESS;
2453 }
2454 else
2455#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002456#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002457 if( PSA_ALG_IS_HMAC( operation->alg ) )
2458 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002459 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
Steven Cooreman0e307642021-02-18 16:18:32 +01002460 operation->ctx.hmac.alg = 0;
Gilles Peskineff94abd2018-07-12 17:07:52 +02002461 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002462 }
2463 else
John Durkopd0321952020-10-29 21:37:36 -07002464#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002465 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002466 if( ! PSA_ALG_IS_MAC( alg ) )
2467 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002468 }
2469
2470 if( status != PSA_SUCCESS )
2471 memset( operation, 0, sizeof( *operation ) );
2472 return( status );
2473}
2474
John Durkop6ba40d12020-11-10 08:50:04 -08002475#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002476static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2477{
Gilles Peskine3f108122018-12-07 18:14:53 +01002478 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002479 return( psa_hash_abort( &hmac->hash_ctx ) );
2480}
John Durkop6ba40d12020-11-10 08:50:04 -08002481#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002482
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2484{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002485 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002487 /* The object has (apparently) been initialized but it is not
2488 * in use. It's ok to call abort on such an object, and there's
2489 * nothing to do. */
2490 return( PSA_SUCCESS );
2491 }
2492 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002494 if( operation->alg == PSA_ALG_CMAC )
2495 {
2496 mbedtls_cipher_free( &operation->ctx.cmac );
2497 }
2498 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002499#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002500#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002501 if( PSA_ALG_IS_HMAC( operation->alg ) )
2502 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002503 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002504 }
2505 else
John Durkopd0321952020-10-29 21:37:36 -07002506#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002507 {
2508 /* Sanity check (shouldn't happen: operation->alg should
2509 * always have been initialized to a valid value). */
2510 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511 }
Moran Peker41deec42018-04-04 15:43:05 +03002512
Gilles Peskine8c9def32018-02-08 10:02:12 +01002513 operation->alg = 0;
2514 operation->key_set = 0;
2515 operation->iv_set = 0;
2516 operation->iv_required = 0;
2517 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002518 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002519
Gilles Peskine8c9def32018-02-08 10:02:12 +01002520 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002521
2522bad_state:
2523 /* If abort is called on an uninitialized object, we can't trust
2524 * anything. Wipe the object in case it contains confidential data.
2525 * This may result in a memory leak if a pointer gets overwritten,
2526 * but it's too late to do anything about this. */
2527 memset( operation, 0, sizeof( *operation ) );
2528 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002529}
2530
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002531#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002532static psa_status_t psa_cmac_setup( psa_mac_operation_t *operation,
2533 psa_key_slot_t *slot )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002534{
Janos Follath24eed8d2019-11-22 13:21:35 +00002535 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman15472f82021-03-02 16:16:22 +01002536 const mbedtls_cipher_info_t *cipher_info =
2537 mbedtls_cipher_info_from_psa( PSA_ALG_CMAC,
2538 slot->attr.type, slot->attr.bits,
2539 NULL );
2540 if( cipher_info == NULL )
2541 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002542
2543 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2544 if( ret != 0 )
Steven Cooreman15472f82021-03-02 16:16:22 +01002545 goto exit;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002546
2547 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002548 slot->key.data,
Steven Cooreman15472f82021-03-02 16:16:22 +01002549 slot->attr.bits );
2550exit:
2551 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002552}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002553#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002554
John Durkop6ba40d12020-11-10 08:50:04 -08002555#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002556static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2557 const uint8_t *key,
2558 size_t key_length,
2559 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002560{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002561 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002562 size_t i;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002563 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002564 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002565 psa_status_t status;
2566
Steven Cooreman0e307642021-02-18 16:18:32 +01002567 hmac->alg = hash_alg;
2568
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002569 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2570 * overflow below. This should never trigger if the hash algorithm
2571 * is implemented correctly. */
2572 /* The size checks against the ipad and opad buffers cannot be written
2573 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2574 * because that triggers -Wlogical-op on GCC 7.3. */
2575 if( block_size > sizeof( ipad ) )
2576 return( PSA_ERROR_NOT_SUPPORTED );
2577 if( block_size > sizeof( hmac->opad ) )
2578 return( PSA_ERROR_NOT_SUPPORTED );
2579 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002580 return( PSA_ERROR_NOT_SUPPORTED );
2581
Gilles Peskined223b522018-06-11 18:12:58 +02002582 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002583 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002584 status = psa_hash_compute( hash_alg, key, key_length,
2585 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002586 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002587 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002588 }
Gilles Peskine96889972018-07-12 17:07:03 +02002589 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2590 * but it is permitted. It is common when HMAC is used in HKDF, for
2591 * example. Don't call `memcpy` in the 0-length because `key` could be
2592 * an invalid pointer which would make the behavior undefined. */
2593 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002594 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002595
Gilles Peskined223b522018-06-11 18:12:58 +02002596 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2597 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002598 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002599 ipad[i] ^= 0x36;
2600 memset( ipad + key_length, 0x36, block_size - key_length );
2601
2602 /* Copy the key material from ipad to opad, flipping the requisite bits,
2603 * and filling the rest of opad with the requisite constant. */
2604 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002605 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2606 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002607
Gilles Peskine01126fa2018-07-12 17:04:55 +02002608 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002609 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002610 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002611
Gilles Peskine01126fa2018-07-12 17:04:55 +02002612 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002613
2614cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02002615 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002616
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002617 return( status );
2618}
John Durkop6ba40d12020-11-10 08:50:04 -08002619#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002620
Gilles Peskine89167cb2018-07-08 20:12:23 +02002621static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002622 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002623 psa_algorithm_t alg,
2624 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002625{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002626 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002627 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002628 psa_key_slot_t *slot;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002629 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002630 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002631
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002632 /* A context must be freshly initialized before it can be set up. */
2633 if( operation->alg != 0 )
2634 {
2635 return( PSA_ERROR_BAD_STATE );
2636 }
2637
Steven Cooreman15472f82021-03-02 16:16:22 +01002638 status = psa_mac_init( operation, alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002639 if( status != PSA_SUCCESS )
2640 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002641 if( is_sign )
2642 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002643
Ronald Cron5c522922020-11-14 16:35:34 +01002644 status = psa_get_and_lock_transparent_key_slot_with_policy(
2645 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002646 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002647 goto exit;
Steven Cooreman15472f82021-03-02 16:16:22 +01002648
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002649 /* Validate the combination of key type and algorithm */
2650 status = psa_mac_key_can_do( alg, slot->attr.type );
Steven Cooreman15472f82021-03-02 16:16:22 +01002651 if( status != PSA_SUCCESS )
2652 goto exit;
2653
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002654 /* Get the output length for the algorithm and key combination. None of the
2655 * currently supported algorithms have an output length dependent on actual
2656 * key size, so setting it to a bogus value is currently OK. */
2657 operation->mac_size = PSA_MAC_LENGTH( slot->attr.type, 0, alg );
Steven Cooreman15472f82021-03-02 16:16:22 +01002658
2659 if( operation->mac_size < 4 )
2660 {
2661 /* A very short MAC is too short for security since it can be
2662 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2663 * so we make this our minimum, even though 32 bits is still
2664 * too small for security. */
2665 status = PSA_ERROR_NOT_SUPPORTED;
2666 goto exit;
2667 }
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002668
Steven Cooremanf9f7fdf2021-03-03 19:04:05 +01002669 if( operation->mac_size >
2670 PSA_MAC_LENGTH( slot->attr.type, 0, PSA_ALG_FULL_LENGTH_MAC( alg ) ) )
2671 {
2672 /* It's impossible to "truncate" to a larger length than the full length
2673 * of the algorithm. */
2674 status = PSA_ERROR_INVALID_ARGUMENT;
2675 goto exit;
2676 }
2677
Gilles Peskine8c9def32018-02-08 10:02:12 +01002678#if defined(MBEDTLS_CMAC_C)
Steven Cooreman15472f82021-03-02 16:16:22 +01002679 if( PSA_ALG_FULL_LENGTH_MAC( alg ) == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002680 {
Steven Cooreman15472f82021-03-02 16:16:22 +01002681 status = psa_cmac_setup( operation, slot );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 }
2683 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002684#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002685#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Steven Cooreman15472f82021-03-02 16:16:22 +01002686 if( PSA_ALG_IS_HMAC( alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002687 {
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002688 /* Sanity check. This shouldn't fail on a valid configuration. */
Steven Cooreman1fb691a2021-03-08 12:01:55 +01002689 if( operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002690 {
2691 status = PSA_ERROR_NOT_SUPPORTED;
2692 goto exit;
2693 }
2694
Gilles Peskine8e338702019-07-30 20:06:31 +02002695 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002696 {
2697 status = PSA_ERROR_INVALID_ARGUMENT;
2698 goto exit;
2699 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002700
Gilles Peskine01126fa2018-07-12 17:04:55 +02002701 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Ronald Cronea0f8a62020-11-25 17:52:23 +01002702 slot->key.data,
2703 slot->key.bytes,
Steven Cooreman15472f82021-03-02 16:16:22 +01002704 PSA_ALG_HMAC_GET_HASH( alg ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002705 }
2706 else
John Durkopd0321952020-10-29 21:37:36 -07002707#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002708 {
2709 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002710 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002711
Gilles Peskinefbfac682018-07-08 20:51:54 +02002712exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002714 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002715 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002716 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717 else
2718 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002719 operation->key_set = 1;
2720 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02002721
Ronald Cron5c522922020-11-14 16:35:34 +01002722 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002723
Ronald Cron5c522922020-11-14 16:35:34 +01002724 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002725}
2726
Gilles Peskine89167cb2018-07-08 20:12:23 +02002727psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002728 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002729 psa_algorithm_t alg )
2730{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002731 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002732}
2733
2734psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002735 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002736 psa_algorithm_t alg )
2737{
Ronald Croncf56a0a2020-08-04 09:51:30 +02002738 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002739}
2740
Gilles Peskine8c9def32018-02-08 10:02:12 +01002741psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2742 const uint8_t *input,
2743 size_t input_length )
2744{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002745 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002746 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002747 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002748 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002749 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002750 operation->has_input = 1;
2751
Gilles Peskine8c9def32018-02-08 10:02:12 +01002752#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002753 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002754 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002755 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2756 input, input_length );
2757 status = mbedtls_to_psa_error( ret );
2758 }
2759 else
2760#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002761#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002762 if( PSA_ALG_IS_HMAC( operation->alg ) )
2763 {
2764 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2765 input_length );
2766 }
2767 else
John Durkopd0321952020-10-29 21:37:36 -07002768#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002769 {
2770 /* This shouldn't happen if `operation` was initialized by
2771 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002772 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002773 }
2774
Gilles Peskinefbfac682018-07-08 20:51:54 +02002775 if( status != PSA_SUCCESS )
2776 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002777 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002778}
2779
John Durkop6ba40d12020-11-10 08:50:04 -08002780#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002781static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2782 uint8_t *mac,
2783 size_t mac_size )
2784{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002785 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Steven Cooreman0e307642021-02-18 16:18:32 +01002786 psa_algorithm_t hash_alg = hmac->alg;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002787 size_t hash_size = 0;
2788 size_t block_size = psa_get_hash_block_size( hash_alg );
2789 psa_status_t status;
2790
Gilles Peskine01126fa2018-07-12 17:04:55 +02002791 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2792 if( status != PSA_SUCCESS )
2793 return( status );
2794 /* From here on, tmp needs to be wiped. */
2795
2796 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2797 if( status != PSA_SUCCESS )
2798 goto exit;
2799
2800 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2801 if( status != PSA_SUCCESS )
2802 goto exit;
2803
2804 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2805 if( status != PSA_SUCCESS )
2806 goto exit;
2807
Gilles Peskined911eb72018-08-14 15:18:45 +02002808 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2809 if( status != PSA_SUCCESS )
2810 goto exit;
2811
2812 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002813
2814exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002815 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002816 return( status );
2817}
John Durkop6ba40d12020-11-10 08:50:04 -08002818#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02002819
mohammad16036df908f2018-04-02 08:34:15 -07002820static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002821 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002822 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002823{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002824 if( ! operation->key_set )
2825 return( PSA_ERROR_BAD_STATE );
2826 if( operation->iv_required && ! operation->iv_set )
2827 return( PSA_ERROR_BAD_STATE );
2828
Gilles Peskine8c9def32018-02-08 10:02:12 +01002829 if( mac_size < operation->mac_size )
2830 return( PSA_ERROR_BUFFER_TOO_SMALL );
2831
Gilles Peskine8c9def32018-02-08 10:02:12 +01002832#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002833 if( operation->alg == PSA_ALG_CMAC )
2834 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01002835 uint8_t tmp[PSA_BLOCK_CIPHER_BLOCK_MAX_SIZE];
Gilles Peskined911eb72018-08-14 15:18:45 +02002836 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
2837 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02002838 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01002839 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002841 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02002842 else
2843#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07002844#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002845 if( PSA_ALG_IS_HMAC( operation->alg ) )
2846 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002847 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02002848 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002849 }
2850 else
John Durkopd0321952020-10-29 21:37:36 -07002851#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002852 {
2853 /* This shouldn't happen if `operation` was initialized by
2854 * a setup function. */
2855 return( PSA_ERROR_BAD_STATE );
2856 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01002857}
2858
Gilles Peskineacd4be32018-07-08 19:56:25 +02002859psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
2860 uint8_t *mac,
2861 size_t mac_size,
2862 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07002863{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002864 psa_status_t status;
2865
Jaeden Amero252ef282019-02-15 14:05:35 +00002866 if( operation->alg == 0 )
2867 {
2868 return( PSA_ERROR_BAD_STATE );
2869 }
2870
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002871 /* Fill the output buffer with something that isn't a valid mac
2872 * (barring an attack on the mac and deliberately-crafted input),
2873 * in case the caller doesn't check the return status properly. */
2874 *mac_length = mac_size;
2875 /* If mac_size is 0 then mac may be NULL and then the
2876 * call to memset would have undefined behavior. */
2877 if( mac_size != 0 )
2878 memset( mac, '!', mac_size );
2879
Gilles Peskine89167cb2018-07-08 20:12:23 +02002880 if( ! operation->is_sign )
2881 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002882 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002883 }
mohammad16036df908f2018-04-02 08:34:15 -07002884
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002885 status = psa_mac_finish_internal( operation, mac, mac_size );
2886
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002887 if( status == PSA_SUCCESS )
2888 {
2889 status = psa_mac_abort( operation );
2890 if( status == PSA_SUCCESS )
2891 *mac_length = operation->mac_size;
2892 else
2893 memset( mac, '!', mac_size );
2894 }
2895 else
2896 psa_mac_abort( operation );
2897 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07002898}
2899
Gilles Peskineacd4be32018-07-08 19:56:25 +02002900psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
2901 const uint8_t *mac,
2902 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002903{
Gilles Peskine828ed142018-06-18 23:25:51 +02002904 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07002905 psa_status_t status;
2906
Jaeden Amero252ef282019-02-15 14:05:35 +00002907 if( operation->alg == 0 )
2908 {
2909 return( PSA_ERROR_BAD_STATE );
2910 }
2911
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912 if( operation->is_sign )
2913 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002914 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002915 }
2916 if( operation->mac_size != mac_length )
2917 {
2918 status = PSA_ERROR_INVALID_SIGNATURE;
2919 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920 }
mohammad16036df908f2018-04-02 08:34:15 -07002921
2922 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002923 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01002924 if( status != PSA_SUCCESS )
2925 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002926
2927 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
2928 status = PSA_ERROR_INVALID_SIGNATURE;
2929
2930cleanup:
2931 if( status == PSA_SUCCESS )
2932 status = psa_mac_abort( operation );
2933 else
2934 psa_mac_abort( operation );
2935
Gilles Peskine3f108122018-12-07 18:14:53 +01002936 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02002937
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002938 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002939}
2940
2941
Gilles Peskine20035e32018-02-03 22:44:14 +01002942
Gilles Peskine20035e32018-02-03 22:44:14 +01002943/****************************************************************/
2944/* Asymmetric cryptography */
2945/****************************************************************/
2946
Ronald Cron67b1eb32020-12-08 17:37:27 +01002947psa_status_t psa_sign_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01002948 const psa_key_attributes_t *attributes,
2949 const uint8_t *key_buffer, size_t key_buffer_size,
2950 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
2951 uint8_t *signature, size_t signature_size, size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01002952{
Gilles Peskine20035e32018-02-03 22:44:14 +01002953#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
2954 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01002955 if( attributes->core.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01002956 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01002957 return( mbedtls_psa_rsa_sign_hash(
2958 attributes,
2959 key_buffer, key_buffer_size,
2960 alg, hash, hash_length,
2961 signature, signature_size, signature_length ) );
Gilles Peskine20035e32018-02-03 22:44:14 +01002962 }
2963 else
Gilles Peskine20035e32018-02-03 22:44:14 +01002964#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
2965 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002966#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
2967 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Ronald Cronbb9cbc72021-03-04 17:09:00 +01002968 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01002969 {
Ronald Cron9103d492021-03-04 11:26:03 +01002970 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02002971 {
Ronald Cron072722c2020-12-09 16:36:19 +01002972 return( mbedtls_psa_ecdsa_sign_hash(
2973 attributes,
2974 key_buffer, key_buffer_size,
2975 alg, hash, hash_length,
2976 signature, signature_size, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02002977 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002978 else
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002979 {
Ronald Cron8a494f32021-02-17 09:49:51 +01002980 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02002981 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01002982 }
2983 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01002984#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
2985 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
itayzafrir5c753392018-05-08 11:18:38 +03002986 {
Ronald Cronbb9cbc72021-03-04 17:09:00 +01002987 (void)attributes;
Ronald Cron8a494f32021-02-17 09:49:51 +01002988 (void)key_buffer;
2989 (void)key_buffer_size;
2990 (void)alg;
2991 (void)hash;
2992 (void)hash_length;
2993 (void)signature;
2994 (void)signature_size;
2995 (void)signature_length;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02002996
Ronald Cron8a494f32021-02-17 09:49:51 +01002997 return( PSA_ERROR_NOT_SUPPORTED );
2998 }
Ronald Cron18659932020-12-08 16:32:23 +01002999}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003000
3001psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
3002 psa_algorithm_t alg,
3003 const uint8_t *hash,
3004 size_t hash_length,
3005 uint8_t *signature,
3006 size_t signature_size,
3007 size_t *signature_length )
3008{
3009 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3010 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
3011 psa_key_slot_t *slot;
3012
3013 *signature_length = signature_size;
3014 /* Immediately reject a zero-length signature buffer. This guarantees
3015 * that signature must be a valid pointer. (On the other hand, the hash
3016 * buffer can in principle be empty since it doesn't actually have
3017 * to be a hash.) */
3018 if( signature_size == 0 )
3019 return( PSA_ERROR_BUFFER_TOO_SMALL );
3020
3021 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3022 PSA_KEY_USAGE_SIGN_HASH,
3023 alg );
3024 if( status != PSA_SUCCESS )
3025 goto exit;
3026 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
3027 {
3028 status = PSA_ERROR_INVALID_ARGUMENT;
3029 goto exit;
3030 }
3031
Ronald Cron9f17aa42020-12-08 17:07:25 +01003032 psa_key_attributes_t attributes = {
3033 .core = slot->attr
3034 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003035
Ronald Cron9f17aa42020-12-08 17:07:25 +01003036 status = psa_driver_wrapper_sign_hash(
3037 &attributes, slot->key.data, slot->key.bytes,
3038 alg, hash, hash_length,
3039 signature, signature_size, signature_length );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003040
3041exit:
3042 /* Fill the unused part of the output buffer (the whole buffer on error,
3043 * the trailing part on success) with something that isn't a valid mac
3044 * (barring an attack on the mac and deliberately-crafted input),
3045 * in case the caller doesn't check the return status properly. */
3046 if( status == PSA_SUCCESS )
3047 memset( signature + *signature_length, '!',
3048 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003049 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003050 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003051 /* If signature_size is 0 then we have nothing to do. We must not call
3052 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003053
Ronald Cron5c522922020-11-14 16:35:34 +01003054 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003055
Ronald Cron5c522922020-11-14 16:35:34 +01003056 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003057}
3058
Ronald Cron67b1eb32020-12-08 17:37:27 +01003059psa_status_t psa_verify_hash_internal(
Ronald Cron18659932020-12-08 16:32:23 +01003060 const psa_key_attributes_t *attributes,
3061 const uint8_t *key_buffer, size_t key_buffer_size,
3062 psa_algorithm_t alg, const uint8_t *hash, size_t hash_length,
3063 const uint8_t *signature, size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003064{
John Durkop6ba40d12020-11-10 08:50:04 -08003065#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3066 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Ronald Cron99b8ed72021-02-17 10:33:32 +01003067 if( PSA_KEY_TYPE_IS_RSA( attributes->core.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003068 {
Ronald Cron7bdbca32020-12-09 13:34:54 +01003069 return( mbedtls_psa_rsa_verify_hash(
3070 attributes,
3071 key_buffer, key_buffer_size,
3072 alg, hash, hash_length,
3073 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003074 }
3075 else
John Durkop6ba40d12020-11-10 08:50:04 -08003076#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3077 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Ronald Cron99b8ed72021-02-17 10:33:32 +01003078 if( PSA_KEY_TYPE_IS_ECC( attributes->core.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003079 {
John Durkop9814fa22020-11-04 12:28:15 -08003080#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3081 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003082 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02003083 {
Ronald Cron072722c2020-12-09 16:36:19 +01003084 return( mbedtls_psa_ecdsa_verify_hash(
3085 attributes,
3086 key_buffer, key_buffer_size,
3087 alg, hash, hash_length,
3088 signature, signature_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003089 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003090 else
John Durkop9814fa22020-11-04 12:28:15 -08003091#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3092 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003093 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003094 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003095 }
itayzafrir5c753392018-05-08 11:18:38 +03003096 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01003097 else
Gilles Peskinee59236f2018-01-27 23:32:46 +01003098 {
Ronald Cron8a494f32021-02-17 09:49:51 +01003099 (void)key_buffer;
3100 (void)key_buffer_size;
3101 (void)alg;
3102 (void)hash;
3103 (void)hash_length;
3104 (void)signature;
3105 (void)signature_length;
Ronald Cronf95a2b12020-10-22 15:24:49 +02003106
Ronald Cron8a494f32021-02-17 09:49:51 +01003107 return( PSA_ERROR_NOT_SUPPORTED );
3108 }
Ronald Cron18659932020-12-08 16:32:23 +01003109}
3110
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003111psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
3112 psa_algorithm_t alg,
3113 const uint8_t *hash,
3114 size_t hash_length,
3115 const uint8_t *signature,
3116 size_t signature_length )
3117{
3118 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3119 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003120 psa_key_slot_t *slot;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003121
3122 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003123 PSA_KEY_USAGE_VERIFY_HASH,
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003124 alg );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003125 if( status != PSA_SUCCESS )
3126 return( status );
3127
Ronald Cron9f17aa42020-12-08 17:07:25 +01003128 psa_key_attributes_t attributes = {
3129 .core = slot->attr
3130 };
Gilles Peskinee59236f2018-01-27 23:32:46 +01003131
Ronald Cron9f17aa42020-12-08 17:07:25 +01003132 status = psa_driver_wrapper_verify_hash(
3133 &attributes, slot->key.data, slot->key.bytes,
3134 alg, hash, hash_length,
3135 signature, signature_length );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003136
Ronald Cron5c522922020-11-14 16:35:34 +01003137 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003138
Ronald Cron5c522922020-11-14 16:35:34 +01003139 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01003140}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003141
John Durkop0e005192020-10-31 22:06:54 -07003142#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02003143static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3144 mbedtls_rsa_context *rsa )
3145{
3146 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3147 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3148 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3149 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3150}
John Durkop0e005192020-10-31 22:06:54 -07003151#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02003152
Ronald Croncf56a0a2020-08-04 09:51:30 +02003153psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003154 psa_algorithm_t alg,
3155 const uint8_t *input,
3156 size_t input_length,
3157 const uint8_t *salt,
3158 size_t salt_length,
3159 uint8_t *output,
3160 size_t output_size,
3161 size_t *output_length )
3162{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003163 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003164 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003165 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003166
Darryl Green5cc689a2018-07-24 15:34:10 +01003167 (void) input;
3168 (void) input_length;
3169 (void) salt;
3170 (void) output;
3171 (void) output_size;
3172
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003173 *output_length = 0;
3174
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003175 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3176 return( PSA_ERROR_INVALID_ARGUMENT );
3177
Ronald Cron5c522922020-11-14 16:35:34 +01003178 status = psa_get_and_lock_transparent_key_slot_with_policy(
3179 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003180 if( status != PSA_SUCCESS )
3181 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003182 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3183 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003184 {
3185 status = PSA_ERROR_INVALID_ARGUMENT;
3186 goto exit;
3187 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003188
John Durkop6ba40d12020-11-10 08:50:04 -08003189#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3190 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003191 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003192 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003193 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003194 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3195 slot->key.data,
3196 slot->key.bytes,
3197 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003198 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02003199 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003200
3201 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003202 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003203 status = PSA_ERROR_BUFFER_TOO_SMALL;
3204 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003205 }
John Durkop0e005192020-10-31 22:06:54 -07003206#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003207 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
3208 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003209 status = mbedtls_to_psa_error(
3210 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003211 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003212 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003213 MBEDTLS_RSA_PUBLIC,
3214 input_length,
3215 input,
3216 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003217 }
3218 else
John Durkop0e005192020-10-31 22:06:54 -07003219#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3220#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003221 if( PSA_ALG_IS_RSA_OAEP( alg ) )
3222 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003223 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003224 status = mbedtls_to_psa_error(
3225 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003226 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003227 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003228 MBEDTLS_RSA_PUBLIC,
3229 salt, salt_length,
3230 input_length,
3231 input,
3232 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003233 }
3234 else
John Durkop0e005192020-10-31 22:06:54 -07003235#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003236 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003237 status = PSA_ERROR_INVALID_ARGUMENT;
3238 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003239 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02003240rsa_exit:
3241 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02003242 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003243
Steven Cooremana2371e52020-07-28 14:30:39 +02003244 mbedtls_rsa_free( rsa );
3245 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003246 }
3247 else
John Durkop9814fa22020-11-04 12:28:15 -08003248#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08003249 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003250 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003251 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003252 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003253
3254exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003255 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003256
Ronald Cron5c522922020-11-14 16:35:34 +01003257 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003258}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003259
Ronald Croncf56a0a2020-08-04 09:51:30 +02003260psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003261 psa_algorithm_t alg,
3262 const uint8_t *input,
3263 size_t input_length,
3264 const uint8_t *salt,
3265 size_t salt_length,
3266 uint8_t *output,
3267 size_t output_size,
3268 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003269{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003270 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003271 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003272 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003273
Darryl Green5cc689a2018-07-24 15:34:10 +01003274 (void) input;
3275 (void) input_length;
3276 (void) salt;
3277 (void) output;
3278 (void) output_size;
3279
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003280 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003281
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003282 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3283 return( PSA_ERROR_INVALID_ARGUMENT );
3284
Ronald Cron5c522922020-11-14 16:35:34 +01003285 status = psa_get_and_lock_transparent_key_slot_with_policy(
3286 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003287 if( status != PSA_SUCCESS )
3288 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003289 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003290 {
3291 status = PSA_ERROR_INVALID_ARGUMENT;
3292 goto exit;
3293 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003294
John Durkop6ba40d12020-11-10 08:50:04 -08003295#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
3296 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02003297 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003298 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003299 mbedtls_rsa_context *rsa = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01003300 status = mbedtls_psa_rsa_load_representation( slot->attr.type,
3301 slot->key.data,
3302 slot->key.bytes,
3303 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02003304 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003305 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003306
Steven Cooremana2371e52020-07-28 14:30:39 +02003307 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02003308 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003309 status = PSA_ERROR_INVALID_ARGUMENT;
3310 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02003311 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003312
John Durkop0e005192020-10-31 22:06:54 -07003313#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003314 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003315 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003316 status = mbedtls_to_psa_error(
3317 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003318 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003319 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003320 MBEDTLS_RSA_PRIVATE,
3321 output_length,
3322 input,
3323 output,
3324 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003325 }
3326 else
John Durkop0e005192020-10-31 22:06:54 -07003327#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
3328#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003329 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003330 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003331 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02003332 status = mbedtls_to_psa_error(
3333 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003334 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003335 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02003336 MBEDTLS_RSA_PRIVATE,
3337 salt, salt_length,
3338 output_length,
3339 input,
3340 output,
3341 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003342 }
3343 else
John Durkop0e005192020-10-31 22:06:54 -07003344#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003345 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02003346 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003347 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003348
Steven Cooreman4fed4552020-08-03 14:46:03 +02003349rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02003350 mbedtls_rsa_free( rsa );
3351 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003352 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003353 else
John Durkop6ba40d12020-11-10 08:50:04 -08003354#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
3355 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003356 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003357 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003358 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003359
3360exit:
Ronald Cron5c522922020-11-14 16:35:34 +01003361 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003362
Ronald Cron5c522922020-11-14 16:35:34 +01003363 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003364}
Gilles Peskinee59236f2018-01-27 23:32:46 +01003365
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003366
3367
mohammad1603503973b2018-03-12 15:59:30 +02003368/****************************************************************/
3369/* Symmetric cryptography */
3370/****************************************************************/
3371
Gilles Peskinee553c652018-06-04 16:22:46 +02003372static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003373 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02003374 psa_algorithm_t alg,
3375 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003376{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003377 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003378 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003379 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003380 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003381 size_t key_bits;
3382 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003383 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3384 PSA_KEY_USAGE_ENCRYPT :
3385 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003386
Steven Cooremand3feccd2020-09-01 15:56:14 +02003387 /* A context must be freshly initialized before it can be set up. */
3388 if( operation->alg != 0 )
3389 return( PSA_ERROR_BAD_STATE );
3390
Steven Cooremana07b9972020-09-10 14:54:14 +02003391 /* The requested algorithm must be one that can be processed by cipher. */
3392 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02003393 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003394
Steven Cooremanef8575e2020-09-11 11:44:50 +02003395 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01003396 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003397 if( status != PSA_SUCCESS )
3398 goto exit;
3399
3400 /* Initialize the operation struct members, except for alg. The alg member
3401 * is used to indicate to psa_cipher_abort that there are resources to free,
3402 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003403 operation->key_set = 0;
3404 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003405 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02003406 operation->iv_size = 0;
3407 operation->block_size = 0;
3408 if( alg == PSA_ALG_ECB_NO_PADDING )
3409 operation->iv_required = 0;
3410 else
3411 operation->iv_required = 1;
3412
Steven Cooremana07b9972020-09-10 14:54:14 +02003413 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02003414 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003415 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003416 slot,
3417 alg );
3418 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003419 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02003420 slot,
3421 alg );
3422
Steven Cooremanef8575e2020-09-11 11:44:50 +02003423 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003424 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02003425 /* Once the driver context is initialised, it needs to be freed using
3426 * psa_cipher_abort. Indicate this through setting alg. */
3427 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02003428 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02003429
Steven Cooremand3feccd2020-09-01 15:56:14 +02003430 if( status != PSA_ERROR_NOT_SUPPORTED ||
3431 psa_key_lifetime_is_external( slot->attr.lifetime ) )
3432 goto exit;
3433
Steven Cooremana07b9972020-09-10 14:54:14 +02003434 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02003435 * available for the given algorithm & key. */
3436 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02003437
Steven Cooremana07b9972020-09-10 14:54:14 +02003438 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02003439 * psa_cipher_abort. Indicate there is something to be freed through setting
3440 * alg, and indicate the operation is being done using mbedtls crypto through
3441 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02003442 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003443 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02003444
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003445 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02003446 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003447 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003448 {
3449 status = PSA_ERROR_NOT_SUPPORTED;
3450 goto exit;
3451 }
mohammad1603503973b2018-03-12 15:59:30 +02003452
mohammad1603503973b2018-03-12 15:59:30 +02003453 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003454 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003455 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003456
David Brown12ca5032021-02-11 11:02:00 -07003457#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02003458 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003459 {
3460 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003461 uint8_t keys[24];
Ronald Cronea0f8a62020-11-25 17:52:23 +01003462 memcpy( keys, slot->key.data, 16 );
3463 memcpy( keys + 16, slot->key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003464 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3465 keys,
3466 192, cipher_operation );
3467 }
3468 else
3469#endif
3470 {
3471 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003472 slot->key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003473 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003474 }
Moran Peker41deec42018-04-04 15:43:05 +03003475 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003476 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003477
David Brown63ca2602021-01-26 11:51:12 -07003478#if defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING) || \
3479 defined(MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003480 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003481 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003482 case PSA_ALG_CBC_NO_PADDING:
3483 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3484 MBEDTLS_PADDING_NONE );
3485 break;
3486 case PSA_ALG_CBC_PKCS7:
3487 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3488 MBEDTLS_PADDING_PKCS7 );
3489 break;
3490 default:
3491 /* The algorithm doesn't involve padding. */
3492 ret = 0;
3493 break;
3494 }
3495 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003496 goto exit;
David Brown288a96e2021-02-09 16:27:55 -07003497#endif /* MBEDTLS_PSA_BUILTIN_ALG_CBC_NO_PADDING || MBEDTLS_PSA_BUILTIN_ALG_CBC_PKCS7 */
mohammad16038481e742018-03-18 13:57:31 +02003498
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003499 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003500 PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02003501 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
3502 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02003503 {
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003504 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_LENGTH( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003505 }
David Brown1bfe4d72021-02-16 12:54:35 -07003506#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_CHACHA20)
Gilles Peskine26869f22019-05-06 15:25:00 +02003507 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01003508 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02003509 operation->iv_size = 12;
3510#endif
mohammad1603503973b2018-03-12 15:59:30 +02003511
Steven Cooreman7df02922020-09-09 15:28:49 +02003512 status = PSA_SUCCESS;
3513
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003514exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003515 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003516 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02003517 if( status == PSA_SUCCESS )
3518 {
3519 /* Update operation flags for both driver and software implementations */
3520 operation->key_set = 1;
3521 }
3522 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003523 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003524
Ronald Cron5c522922020-11-14 16:35:34 +01003525 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003526
Ronald Cron5c522922020-11-14 16:35:34 +01003527 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02003528}
3529
Gilles Peskinefe119512018-07-08 21:39:34 +02003530psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003531 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003532 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003533{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003534 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003535}
3536
Gilles Peskinefe119512018-07-08 21:39:34 +02003537psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003538 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02003539 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003540{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003541 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003542}
3543
Gilles Peskinefe119512018-07-08 21:39:34 +02003544psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003545 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003546 size_t iv_size,
3547 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003548{
itayzafrir534bd7c2018-08-02 13:56:32 +03003549 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003551 if( operation->iv_set || ! operation->iv_required )
3552 {
3553 return( PSA_ERROR_BAD_STATE );
3554 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003555
Steven Cooremanef8575e2020-09-11 11:44:50 +02003556 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003557 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003558 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003559 iv,
3560 iv_size,
3561 iv_length );
3562 goto exit;
3563 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003564
Moran Peker41deec42018-04-04 15:43:05 +03003565 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003566 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003567 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003568 goto exit;
3569 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003570 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003571 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003572 if( ret != 0 )
3573 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003574 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003575 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003576 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003577
mohammad16038481e742018-03-18 13:57:31 +02003578 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003579 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003580
Moran Peker395db872018-05-31 14:07:14 +03003581exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02003582 if( status == PSA_SUCCESS )
3583 operation->iv_set = 1;
3584 else
Moran Peker395db872018-05-31 14:07:14 +03003585 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003586 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003587}
3588
Gilles Peskinefe119512018-07-08 21:39:34 +02003589psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003590 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003591 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003592{
itayzafrir534bd7c2018-08-02 13:56:32 +03003593 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003594 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02003595 if( operation->iv_set || ! operation->iv_required )
3596 {
3597 return( PSA_ERROR_BAD_STATE );
3598 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003599
Steven Cooremanef8575e2020-09-11 11:44:50 +02003600 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003601 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003602 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003603 iv,
3604 iv_length );
3605 goto exit;
3606 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003607
Moran Pekera28258c2018-05-29 16:25:04 +03003608 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003609 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003610 status = PSA_ERROR_INVALID_ARGUMENT;
3611 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003612 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003613 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3614 status = mbedtls_to_psa_error( ret );
3615exit:
3616 if( status == PSA_SUCCESS )
3617 operation->iv_set = 1;
3618 else
mohammad1603503973b2018-03-12 15:59:30 +02003619 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003620 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003621}
3622
Steven Cooreman177deba2020-09-07 17:14:14 +02003623/* Process input for which the algorithm is set to ECB mode. This requires
3624 * manual processing, since the PSA API is defined as being able to process
3625 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
3626 * underlying mbedtls_cipher_update only takes full blocks. */
3627static psa_status_t psa_cipher_update_ecb_internal(
3628 mbedtls_cipher_context_t *ctx,
3629 const uint8_t *input,
3630 size_t input_length,
3631 uint8_t *output,
3632 size_t output_size,
3633 size_t *output_length )
3634{
3635 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
3636 size_t block_size = ctx->cipher_info->block_size;
3637 size_t internal_output_length = 0;
3638 *output_length = 0;
3639
3640 if( input_length == 0 )
3641 {
3642 status = PSA_SUCCESS;
3643 goto exit;
3644 }
3645
3646 if( ctx->unprocessed_len > 0 )
3647 {
3648 /* Fill up to block size, and run the block if there's a full one. */
3649 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
3650
3651 if( input_length < bytes_to_copy )
3652 bytes_to_copy = input_length;
3653
3654 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
3655 input, bytes_to_copy );
3656 input_length -= bytes_to_copy;
3657 input += bytes_to_copy;
3658 ctx->unprocessed_len += bytes_to_copy;
3659
3660 if( ctx->unprocessed_len == block_size )
3661 {
3662 status = mbedtls_to_psa_error(
3663 mbedtls_cipher_update( ctx,
3664 ctx->unprocessed_data,
3665 block_size,
3666 output, &internal_output_length ) );
3667
3668 if( status != PSA_SUCCESS )
3669 goto exit;
3670
3671 output += internal_output_length;
3672 output_size -= internal_output_length;
3673 *output_length += internal_output_length;
3674 ctx->unprocessed_len = 0;
3675 }
3676 }
3677
3678 while( input_length >= block_size )
3679 {
3680 /* Run all full blocks we have, one by one */
3681 status = mbedtls_to_psa_error(
3682 mbedtls_cipher_update( ctx, input,
3683 block_size,
3684 output, &internal_output_length ) );
3685
3686 if( status != PSA_SUCCESS )
3687 goto exit;
3688
3689 input_length -= block_size;
3690 input += block_size;
3691
3692 output += internal_output_length;
3693 output_size -= internal_output_length;
3694 *output_length += internal_output_length;
3695 }
3696
3697 if( input_length > 0 )
3698 {
3699 /* Save unprocessed bytes for later processing */
3700 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
3701 input, input_length );
3702 ctx->unprocessed_len += input_length;
3703 }
3704
3705 status = PSA_SUCCESS;
3706
3707exit:
3708 return( status );
3709}
3710
Gilles Peskinee553c652018-06-04 16:22:46 +02003711psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3712 const uint8_t *input,
3713 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003714 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003715 size_t output_size,
3716 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003717{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003718 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003719 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02003720 if( operation->alg == 0 )
3721 {
3722 return( PSA_ERROR_BAD_STATE );
3723 }
3724 if( operation->iv_required && ! operation->iv_set )
3725 {
3726 return( PSA_ERROR_BAD_STATE );
3727 }
Jaeden Ameroab439972019-02-15 14:12:05 +00003728
Steven Cooremanef8575e2020-09-11 11:44:50 +02003729 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003730 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003731 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003732 input,
3733 input_length,
3734 output,
3735 output_size,
3736 output_length );
3737 goto exit;
3738 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003739
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003740 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003741 {
3742 /* Take the unprocessed partial block left over from previous
3743 * update calls, if any, plus the input to this call. Remove
3744 * the last partial block, if any. You get the data that will be
3745 * output in this call. */
3746 expected_output_size =
3747 ( operation->ctx.cipher.unprocessed_len + input_length )
3748 / operation->block_size * operation->block_size;
3749 }
3750 else
3751 {
3752 expected_output_size = input_length;
3753 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003754
Gilles Peskine89d789c2018-06-04 17:17:16 +02003755 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003756 {
3757 status = PSA_ERROR_BUFFER_TOO_SMALL;
3758 goto exit;
3759 }
mohammad160382759612018-03-12 18:16:40 +02003760
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003761 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
3762 {
3763 /* mbedtls_cipher_update has an API inconsistency: it will only
3764 * process a single block at a time in ECB mode. Abstract away that
3765 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02003766 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
3767 input,
3768 input_length,
3769 output,
3770 output_size,
3771 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02003772 }
3773 else
3774 {
3775 status = mbedtls_to_psa_error(
3776 mbedtls_cipher_update( &operation->ctx.cipher, input,
3777 input_length, output, output_length ) );
3778 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003779exit:
3780 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003781 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003782 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003783}
3784
Gilles Peskinee553c652018-06-04 16:22:46 +02003785psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3786 uint8_t *output,
3787 size_t output_size,
3788 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003789{
David Saadab4ecc272019-02-14 13:48:10 +02003790 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02003791 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02003792 if( operation->alg == 0 )
3793 {
3794 return( PSA_ERROR_BAD_STATE );
3795 }
3796 if( operation->iv_required && ! operation->iv_set )
3797 {
3798 return( PSA_ERROR_BAD_STATE );
3799 }
Moran Pekerbed71a22018-04-22 20:19:20 +03003800
Steven Cooremanef8575e2020-09-11 11:44:50 +02003801 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02003802 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003803 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02003804 output,
3805 output_size,
3806 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02003807 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02003808 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02003809
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003810 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003811 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003812 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02003813 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02003814 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003815 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003816 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003817 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003818 }
3819
Steven Cooremanef8575e2020-09-11 11:44:50 +02003820 status = mbedtls_to_psa_error(
3821 mbedtls_cipher_finish( &operation->ctx.cipher,
3822 temp_output_buffer,
3823 output_length ) );
3824 if( status != PSA_SUCCESS )
3825 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01003826
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003827 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003828 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003829 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003830 memcpy( output, temp_output_buffer, *output_length );
3831 else
Janos Follath315b51c2018-07-09 16:04:51 +01003832 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02003833
Steven Cooremanef8575e2020-09-11 11:44:50 +02003834exit:
3835 if( operation->mbedtls_in_use == 1 )
3836 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01003837
Steven Cooremanef8575e2020-09-11 11:44:50 +02003838 if( status == PSA_SUCCESS )
3839 return( psa_cipher_abort( operation ) );
3840 else
3841 {
3842 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003843 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01003844
Steven Cooremanef8575e2020-09-11 11:44:50 +02003845 return( status );
3846 }
mohammad1603503973b2018-03-12 15:59:30 +02003847}
3848
Gilles Peskinee553c652018-06-04 16:22:46 +02003849psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
3850{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003851 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02003852 {
Steven Cooremana07b9972020-09-10 14:54:14 +02003853 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02003854 * in use. It's ok to call abort on such an object, and there's
3855 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003856 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02003857 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003858
Gilles Peskinef9c2c092018-06-21 16:57:07 +02003859 /* Sanity check (shouldn't happen: operation->alg should
3860 * always have been initialized to a valid value). */
3861 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
3862 return( PSA_ERROR_BAD_STATE );
3863
Steven Cooremanef8575e2020-09-11 11:44:50 +02003864 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02003865 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02003866 else
3867 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02003868
Moran Peker41deec42018-04-04 15:43:05 +03003869 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003870 operation->key_set = 0;
3871 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02003872 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003873 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003874 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03003875 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003876
Moran Peker395db872018-05-31 14:07:14 +03003877 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02003878}
3879
Gilles Peskinea0655c32018-04-30 17:06:50 +02003880
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003881
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003882
mohammad16035955c982018-04-26 00:53:03 +03003883/****************************************************************/
3884/* AEAD */
3885/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003886
Gilles Peskineedf9a652018-08-17 18:11:56 +02003887typedef struct
3888{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003889 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003890 const mbedtls_cipher_info_t *cipher_info;
3891 union
3892 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02003893 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003894#if defined(MBEDTLS_CCM_C)
3895 mbedtls_ccm_context ccm;
3896#endif /* MBEDTLS_CCM_C */
3897#if defined(MBEDTLS_GCM_C)
3898 mbedtls_gcm_context gcm;
3899#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02003900#if defined(MBEDTLS_CHACHAPOLY_C)
3901 mbedtls_chachapoly_context chachapoly;
3902#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02003903 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003904 psa_algorithm_t core_alg;
3905 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003906 uint8_t tag_length;
3907} aead_operation_t;
3908
Ronald Cronf95a2b12020-10-22 15:24:49 +02003909#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
3910
Gilles Peskine30a9e412019-01-14 18:36:12 +01003911static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003912{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02003913 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003914 {
3915#if defined(MBEDTLS_CCM_C)
3916 case PSA_ALG_CCM:
3917 mbedtls_ccm_free( &operation->ctx.ccm );
3918 break;
3919#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01003920#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02003921 case PSA_ALG_GCM:
3922 mbedtls_gcm_free( &operation->ctx.gcm );
3923 break;
3924#endif /* MBEDTLS_GCM_C */
3925 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003926
Ronald Cron5c522922020-11-14 16:35:34 +01003927 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003928}
3929
3930static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003931 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003932 psa_key_usage_t usage,
3933 psa_algorithm_t alg )
3934{
3935 psa_status_t status;
3936 size_t key_bits;
3937 mbedtls_cipher_id_t cipher_id;
3938
Ronald Cron5c522922020-11-14 16:35:34 +01003939 status = psa_get_and_lock_transparent_key_slot_with_policy(
3940 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003941 if( status != PSA_SUCCESS )
3942 return( status );
3943
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003944 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02003945
3946 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02003947 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003948 &cipher_id );
3949 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003950 {
3951 status = PSA_ERROR_NOT_SUPPORTED;
3952 goto cleanup;
3953 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02003954
Bence Szépkútia63b20d2020-12-16 11:36:46 +01003955 switch( PSA_ALG_AEAD_WITH_SHORTENED_TAG( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02003956 {
3957#if defined(MBEDTLS_CCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01003958 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003959 operation->core_alg = PSA_ALG_CCM;
3960 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003961 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
3962 * The call to mbedtls_ccm_encrypt_and_tag or
3963 * mbedtls_ccm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003964 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003965 {
3966 status = PSA_ERROR_INVALID_ARGUMENT;
3967 goto cleanup;
3968 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02003969 mbedtls_ccm_init( &operation->ctx.ccm );
3970 status = mbedtls_to_psa_error(
3971 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003972 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003973 (unsigned int) key_bits ) );
3974 if( status != 0 )
3975 goto cleanup;
3976 break;
3977#endif /* MBEDTLS_CCM_C */
3978
3979#if defined(MBEDTLS_GCM_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01003980 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_GCM, 0 ):
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003981 operation->core_alg = PSA_ALG_GCM;
3982 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003983 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
3984 * The call to mbedtls_gcm_crypt_and_tag or
3985 * mbedtls_gcm_auth_decrypt will validate the tag length. */
gabor-mezei-armcbcec212020-12-18 14:23:51 +01003986 if( PSA_BLOCK_CIPHER_BLOCK_LENGTH( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987 {
3988 status = PSA_ERROR_INVALID_ARGUMENT;
3989 goto cleanup;
3990 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02003991 mbedtls_gcm_init( &operation->ctx.gcm );
3992 status = mbedtls_to_psa_error(
3993 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Ronald Cronea0f8a62020-11-25 17:52:23 +01003994 operation->slot->key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02003995 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02003996 if( status != 0 )
3997 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02003998 break;
3999#endif /* MBEDTLS_GCM_C */
4000
Gilles Peskine26869f22019-05-06 15:25:00 +02004001#if defined(MBEDTLS_CHACHAPOLY_C)
Bence Szépkútia63b20d2020-12-16 11:36:46 +01004002 case PSA_ALG_AEAD_WITH_SHORTENED_TAG( PSA_ALG_CHACHA20_POLY1305, 0 ):
Gilles Peskine26869f22019-05-06 15:25:00 +02004003 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4004 operation->full_tag_length = 16;
4005 /* We only support the default tag length. */
4006 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004007 {
4008 status = PSA_ERROR_NOT_SUPPORTED;
4009 goto cleanup;
4010 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004011 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4012 status = mbedtls_to_psa_error(
4013 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Ronald Cronea0f8a62020-11-25 17:52:23 +01004014 operation->slot->key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004015 if( status != 0 )
4016 goto cleanup;
4017 break;
4018#endif /* MBEDTLS_CHACHAPOLY_C */
4019
Gilles Peskineedf9a652018-08-17 18:11:56 +02004020 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004021 status = PSA_ERROR_NOT_SUPPORTED;
4022 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004023 }
4024
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004025 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4026 {
4027 status = PSA_ERROR_INVALID_ARGUMENT;
4028 goto cleanup;
4029 }
4030 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004031
Gilles Peskineedf9a652018-08-17 18:11:56 +02004032 return( PSA_SUCCESS );
4033
4034cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004035 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004036 return( status );
4037}
4038
Ronald Croncf56a0a2020-08-04 09:51:30 +02004039psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004040 psa_algorithm_t alg,
4041 const uint8_t *nonce,
4042 size_t nonce_length,
4043 const uint8_t *additional_data,
4044 size_t additional_data_length,
4045 const uint8_t *plaintext,
4046 size_t plaintext_length,
4047 uint8_t *ciphertext,
4048 size_t ciphertext_size,
4049 size_t *ciphertext_length )
4050{
mohammad16035955c982018-04-26 00:53:03 +03004051 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004052 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004053 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004054
mohammad1603f08a5502018-06-03 15:05:47 +03004055 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004056
Ronald Croncf56a0a2020-08-04 09:51:30 +02004057 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004058 if( status != PSA_SUCCESS )
4059 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004060
Gilles Peskineedf9a652018-08-17 18:11:56 +02004061 /* For all currently supported modes, the tag is at the end of the
4062 * ciphertext. */
4063 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4064 {
4065 status = PSA_ERROR_BUFFER_TOO_SMALL;
4066 goto exit;
4067 }
4068 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004069
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004070#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004071 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004072 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004073 status = mbedtls_to_psa_error(
4074 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4075 MBEDTLS_GCM_ENCRYPT,
4076 plaintext_length,
4077 nonce, nonce_length,
4078 additional_data, additional_data_length,
4079 plaintext, ciphertext,
4080 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004081 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004082 else
4083#endif /* MBEDTLS_GCM_C */
4084#if defined(MBEDTLS_CCM_C)
4085 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004086 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004087 status = mbedtls_to_psa_error(
4088 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4089 plaintext_length,
4090 nonce, nonce_length,
4091 additional_data,
4092 additional_data_length,
4093 plaintext, ciphertext,
4094 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004095 }
mohammad16035c8845f2018-05-09 05:40:09 -07004096 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004097#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004098#if defined(MBEDTLS_CHACHAPOLY_C)
4099 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4100 {
4101 if( nonce_length != 12 || operation.tag_length != 16 )
4102 {
4103 status = PSA_ERROR_NOT_SUPPORTED;
4104 goto exit;
4105 }
4106 status = mbedtls_to_psa_error(
4107 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4108 plaintext_length,
4109 nonce,
4110 additional_data,
4111 additional_data_length,
4112 plaintext,
4113 ciphertext,
4114 tag ) );
4115 }
4116 else
4117#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004118 {
Steven Cooreman7196fef2021-02-10 17:13:28 +01004119 (void) tag;
mohammad1603554faad2018-06-03 15:07:38 +03004120 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004121 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004122
Gilles Peskineedf9a652018-08-17 18:11:56 +02004123 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4124 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004125
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004127 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004128 if( status == PSA_SUCCESS )
4129 *ciphertext_length = plaintext_length + operation.tag_length;
4130 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004131}
4132
Gilles Peskineee652a32018-06-01 19:23:52 +02004133/* Locate the tag in a ciphertext buffer containing the encrypted data
4134 * followed by the tag. Return the length of the part preceding the tag in
4135 * *plaintext_length. This is the size of the plaintext in modes where
4136 * the encrypted data has the same size as the plaintext, such as
4137 * CCM and GCM. */
4138static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4139 const uint8_t *ciphertext,
4140 size_t ciphertext_length,
4141 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004142 const uint8_t **p_tag )
4143{
4144 size_t payload_length;
4145 if( tag_length > ciphertext_length )
4146 return( PSA_ERROR_INVALID_ARGUMENT );
4147 payload_length = ciphertext_length - tag_length;
4148 if( payload_length > plaintext_size )
4149 return( PSA_ERROR_BUFFER_TOO_SMALL );
4150 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004151 return( PSA_SUCCESS );
4152}
4153
Ronald Croncf56a0a2020-08-04 09:51:30 +02004154psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004155 psa_algorithm_t alg,
4156 const uint8_t *nonce,
4157 size_t nonce_length,
4158 const uint8_t *additional_data,
4159 size_t additional_data_length,
4160 const uint8_t *ciphertext,
4161 size_t ciphertext_length,
4162 uint8_t *plaintext,
4163 size_t plaintext_size,
4164 size_t *plaintext_length )
4165{
mohammad16035955c982018-04-26 00:53:03 +03004166 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004167 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004168 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004169
Gilles Peskineee652a32018-06-01 19:23:52 +02004170 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004171
Ronald Croncf56a0a2020-08-04 09:51:30 +02004172 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004173 if( status != PSA_SUCCESS )
4174 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004175
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004176 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4177 ciphertext, ciphertext_length,
4178 plaintext_size, &tag );
4179 if( status != PSA_SUCCESS )
4180 goto exit;
4181
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004182#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004183 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004184 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004185 status = mbedtls_to_psa_error(
4186 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4187 ciphertext_length - operation.tag_length,
4188 nonce, nonce_length,
4189 additional_data,
4190 additional_data_length,
4191 tag, operation.tag_length,
4192 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004193 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004194 else
4195#endif /* MBEDTLS_GCM_C */
4196#if defined(MBEDTLS_CCM_C)
4197 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004198 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004199 status = mbedtls_to_psa_error(
4200 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4201 ciphertext_length - operation.tag_length,
4202 nonce, nonce_length,
4203 additional_data,
4204 additional_data_length,
4205 ciphertext, plaintext,
4206 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004207 }
mohammad160339574652018-06-01 04:39:53 -07004208 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004209#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004210#if defined(MBEDTLS_CHACHAPOLY_C)
4211 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4212 {
4213 if( nonce_length != 12 || operation.tag_length != 16 )
4214 {
4215 status = PSA_ERROR_NOT_SUPPORTED;
4216 goto exit;
4217 }
4218 status = mbedtls_to_psa_error(
4219 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4220 ciphertext_length - operation.tag_length,
4221 nonce,
4222 additional_data,
4223 additional_data_length,
4224 tag,
4225 ciphertext,
4226 plaintext ) );
4227 }
4228 else
4229#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004230 {
mohammad1603554faad2018-06-03 15:07:38 +03004231 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004232 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004233
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234 if( status != PSA_SUCCESS && plaintext_size != 0 )
4235 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004236
Gilles Peskineedf9a652018-08-17 18:11:56 +02004237exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004238 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004239 if( status == PSA_SUCCESS )
4240 *plaintext_length = ciphertext_length - operation.tag_length;
4241 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004242}
4243
Gilles Peskinea0655c32018-04-30 17:06:50 +02004244
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004245
Gilles Peskinee59236f2018-01-27 23:32:46 +01004246/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004247/* Generators */
4248/****************************************************************/
4249
John Durkop07cc04a2020-11-16 22:08:34 -08004250#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
4251 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4252 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4253#define AT_LEAST_ONE_BUILTIN_KDF
4254#endif
4255
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004256#define HKDF_STATE_INIT 0 /* no input yet */
4257#define HKDF_STATE_STARTED 1 /* got salt */
4258#define HKDF_STATE_KEYED 2 /* got key */
4259#define HKDF_STATE_OUTPUT 3 /* output started */
4260
Gilles Peskinecbe66502019-05-16 16:59:18 +02004261static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004262 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004263{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004264 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4265 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004266 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004267 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004268}
4269
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004270psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004271{
4272 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004273 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004274 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004275 {
4276 /* The object has (apparently) been initialized but it is not
4277 * in use. It's ok to call abort on such an object, and there's
4278 * nothing to do. */
4279 }
4280 else
John Durkopd0321952020-10-29 21:37:36 -07004281#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004282 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004283 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004284 mbedtls_free( operation->ctx.hkdf.info );
4285 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004286 }
John Durkop07cc04a2020-11-16 22:08:34 -08004287 else
4288#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4289#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4290 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4291 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004292 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004293 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004294 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004295 if( operation->ctx.tls12_prf.seed != NULL )
4296 {
4297 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4298 operation->ctx.tls12_prf.seed_length );
4299 mbedtls_free( operation->ctx.tls12_prf.seed );
4300 }
4301
4302 if( operation->ctx.tls12_prf.label != NULL )
4303 {
4304 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4305 operation->ctx.tls12_prf.label_length );
4306 mbedtls_free( operation->ctx.tls12_prf.label );
4307 }
4308
4309 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4310
4311 /* We leave the fields Ai and output_block to be erased safely by the
4312 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004313 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004314 else
John Durkop07cc04a2020-11-16 22:08:34 -08004315#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4316 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004317 {
4318 status = PSA_ERROR_BAD_STATE;
4319 }
Janos Follath083036a2019-06-11 10:22:26 +01004320 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004321 return( status );
4322}
4323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004324psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004325 size_t *capacity)
4326{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004327 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004328 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004329 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004330 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004331 }
4332
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004333 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004334 return( PSA_SUCCESS );
4335}
4336
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004337psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004338 size_t capacity )
4339{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004340 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004341 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004342 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004343 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004344 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004345 return( PSA_SUCCESS );
4346}
4347
John Durkopd0321952020-10-29 21:37:36 -07004348#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004349/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004350 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004351static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004352 psa_algorithm_t hash_alg,
4353 uint8_t *output,
4354 size_t output_length )
4355{
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004356 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004357 psa_status_t status;
4358
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004359 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4360 return( PSA_ERROR_BAD_STATE );
4361 hkdf->state = HKDF_STATE_OUTPUT;
4362
Gilles Peskinebef7f142018-07-12 17:22:21 +02004363 while( output_length != 0 )
4364 {
4365 /* Copy what remains of the current block */
4366 uint8_t n = hash_length - hkdf->offset_in_block;
4367 if( n > output_length )
4368 n = (uint8_t) output_length;
4369 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4370 output += n;
4371 output_length -= n;
4372 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004373 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004374 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004375 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004376 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004377 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004378 * object was corrupted or if this function is called directly
4379 * inside the library. */
4380 if( hkdf->block_number == 0xff )
4381 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004382
4383 /* We need a new block */
4384 ++hkdf->block_number;
4385 hkdf->offset_in_block = 0;
4386 status = psa_hmac_setup_internal( &hkdf->hmac,
4387 hkdf->prk, hash_length,
4388 hash_alg );
4389 if( status != PSA_SUCCESS )
4390 return( status );
4391 if( hkdf->block_number != 1 )
4392 {
4393 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4394 hkdf->output_block,
4395 hash_length );
4396 if( status != PSA_SUCCESS )
4397 return( status );
4398 }
4399 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4400 hkdf->info,
4401 hkdf->info_length );
4402 if( status != PSA_SUCCESS )
4403 return( status );
4404 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4405 &hkdf->block_number, 1 );
4406 if( status != PSA_SUCCESS )
4407 return( status );
4408 status = psa_hmac_finish_internal( &hkdf->hmac,
4409 hkdf->output_block,
4410 sizeof( hkdf->output_block ) );
4411 if( status != PSA_SUCCESS )
4412 return( status );
4413 }
4414
4415 return( PSA_SUCCESS );
4416}
John Durkop07cc04a2020-11-16 22:08:34 -08004417#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004418
John Durkop07cc04a2020-11-16 22:08:34 -08004419#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4420 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01004421static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4422 psa_tls12_prf_key_derivation_t *tls12_prf,
4423 psa_algorithm_t alg )
4424{
4425 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004426 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004427 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4428 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004429
Janos Follath7742fee2019-06-17 12:58:10 +01004430 /* We can't be wanting more output after block 0xff, otherwise
4431 * the capacity check in psa_key_derivation_output_bytes() would have
4432 * prevented this call. It could happen only if the operation
4433 * object was corrupted or if this function is called directly
4434 * inside the library. */
4435 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004436 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004437
4438 /* We need a new block */
4439 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004440 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004441
4442 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4443 *
4444 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4445 *
4446 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4447 * HMAC_hash(secret, A(2) + seed) +
4448 * HMAC_hash(secret, A(3) + seed) + ...
4449 *
4450 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004451 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004452 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004453 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004454 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004455 * is currently extracted as `output_block` and where i is
4456 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004457 */
4458
Janos Follathea29bfb2019-06-19 12:21:20 +01004459 /* Save the hash context before using it, to preserve the hash state with
4460 * only the inner padding in it. We need this, because inner padding depends
4461 * on the key (secret in the RFC's terminology). */
4462 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4463 if( status != PSA_SUCCESS )
4464 goto cleanup;
4465
4466 /* Calculate A(i) where i = tls12_prf->block_number. */
4467 if( tls12_prf->block_number == 1 )
4468 {
4469 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4470 * the variable seed and in this instance means it in the context of the
4471 * P_hash function, where seed = label + seed.) */
4472 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4473 tls12_prf->label, tls12_prf->label_length );
4474 if( status != PSA_SUCCESS )
4475 goto cleanup;
4476 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4477 tls12_prf->seed, tls12_prf->seed_length );
4478 if( status != PSA_SUCCESS )
4479 goto cleanup;
4480 }
4481 else
4482 {
4483 /* A(i) = HMAC_hash(secret, A(i-1)) */
4484 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4485 tls12_prf->Ai, hash_length );
4486 if( status != PSA_SUCCESS )
4487 goto cleanup;
4488 }
4489
4490 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4491 tls12_prf->Ai, hash_length );
4492 if( status != PSA_SUCCESS )
4493 goto cleanup;
4494 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4495 if( status != PSA_SUCCESS )
4496 goto cleanup;
4497
4498 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4499 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4500 tls12_prf->Ai, hash_length );
4501 if( status != PSA_SUCCESS )
4502 goto cleanup;
4503 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4504 tls12_prf->label, tls12_prf->label_length );
4505 if( status != PSA_SUCCESS )
4506 goto cleanup;
4507 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4508 tls12_prf->seed, tls12_prf->seed_length );
4509 if( status != PSA_SUCCESS )
4510 goto cleanup;
4511 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4512 tls12_prf->output_block, hash_length );
4513 if( status != PSA_SUCCESS )
4514 goto cleanup;
4515 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4516 if( status != PSA_SUCCESS )
4517 goto cleanup;
4518
Janos Follath7742fee2019-06-17 12:58:10 +01004519
4520cleanup:
4521
Janos Follathea29bfb2019-06-19 12:21:20 +01004522 cleanup_status = psa_hash_abort( &backup );
4523 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4524 status = cleanup_status;
4525
4526 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004527}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004528
Janos Follath844eb0e2019-06-19 12:10:49 +01004529static psa_status_t psa_key_derivation_tls12_prf_read(
4530 psa_tls12_prf_key_derivation_t *tls12_prf,
4531 psa_algorithm_t alg,
4532 uint8_t *output,
4533 size_t output_length )
4534{
4535 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004536 uint8_t hash_length = PSA_HASH_LENGTH( hash_alg );
Janos Follath844eb0e2019-06-19 12:10:49 +01004537 psa_status_t status;
4538 uint8_t offset, length;
4539
4540 while( output_length != 0 )
4541 {
4542 /* Check if we have fully processed the current block. */
4543 if( tls12_prf->left_in_block == 0 )
4544 {
4545 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4546 alg );
4547 if( status != PSA_SUCCESS )
4548 return( status );
4549
4550 continue;
4551 }
4552
4553 if( tls12_prf->left_in_block > output_length )
4554 length = (uint8_t) output_length;
4555 else
4556 length = tls12_prf->left_in_block;
4557
4558 offset = hash_length - tls12_prf->left_in_block;
4559 memcpy( output, tls12_prf->output_block + offset, length );
4560 output += length;
4561 output_length -= length;
4562 tls12_prf->left_in_block -= length;
4563 }
4564
4565 return( PSA_SUCCESS );
4566}
John Durkop07cc04a2020-11-16 22:08:34 -08004567#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4568 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02004569
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004570psa_status_t psa_key_derivation_output_bytes(
4571 psa_key_derivation_operation_t *operation,
4572 uint8_t *output,
4573 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004574{
4575 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004576 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004577
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004578 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004579 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004580 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02004581 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004582 }
4583
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004584 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004585 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004586 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004587 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004588 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004589 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004590 goto exit;
4591 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004592 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004593 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004594 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004595 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004596 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4597 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004598 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004599 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004600 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004601 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004602 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004603
John Durkopd0321952020-10-29 21:37:36 -07004604#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004605 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004606 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004607 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004608 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004609 output, output_length );
4610 }
Janos Follathadbec812019-06-14 11:05:39 +01004611 else
John Durkop07cc04a2020-11-16 22:08:34 -08004612#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
4613#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4614 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01004615 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08004616 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004617 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004618 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004619 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004620 output_length );
4621 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004622 else
John Durkop07cc04a2020-11-16 22:08:34 -08004623#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
4624 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004625 {
Steven Cooreman74afe472021-02-10 17:19:22 +01004626 (void) kdf_alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004627 return( PSA_ERROR_BAD_STATE );
4628 }
4629
4630exit:
4631 if( status != PSA_SUCCESS )
4632 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004633 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004634 * This allows us to differentiate between exhausted operations and
4635 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4636 * operations. */
4637 psa_algorithm_t alg = operation->alg;
4638 psa_key_derivation_abort( operation );
4639 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004640 memset( output, '!', output_length );
4641 }
4642 return( status );
4643}
4644
David Brown7807bf72021-02-09 16:28:23 -07004645#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine08542d82018-07-19 17:05:42 +02004646static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4647{
4648 if( data_size >= 8 )
4649 mbedtls_des_key_set_parity( data );
4650 if( data_size >= 16 )
4651 mbedtls_des_key_set_parity( data + 8 );
4652 if( data_size >= 24 )
4653 mbedtls_des_key_set_parity( data + 16 );
4654}
David Brown7807bf72021-02-09 16:28:23 -07004655#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskine08542d82018-07-19 17:05:42 +02004656
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004657static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004658 psa_key_slot_t *slot,
4659 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004660 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004661{
4662 uint8_t *data = NULL;
4663 size_t bytes = PSA_BITS_TO_BYTES( bits );
4664 psa_status_t status;
4665
Gilles Peskine8e338702019-07-30 20:06:31 +02004666 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004667 return( PSA_ERROR_INVALID_ARGUMENT );
4668 if( bits % 8 != 0 )
4669 return( PSA_ERROR_INVALID_ARGUMENT );
4670 data = mbedtls_calloc( 1, bytes );
4671 if( data == NULL )
4672 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4673
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004674 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004675 if( status != PSA_SUCCESS )
4676 goto exit;
David Brown12ca5032021-02-11 11:02:00 -07004677#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskine8e338702019-07-30 20:06:31 +02004678 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004679 psa_des_set_key_parity( data, bytes );
David Brown8107e312021-02-12 08:08:46 -07004680#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Ronald Crondd04d422020-11-28 15:14:42 +01004681
4682 status = psa_allocate_buffer_to_slot( slot, bytes );
4683 if( status != PSA_SUCCESS )
Paul Elliottda3e7db2021-02-09 18:58:20 +00004684 goto exit;
Ronald Crondd04d422020-11-28 15:14:42 +01004685
Ronald Cronbf33c932020-11-28 18:06:53 +01004686 slot->attr.bits = (psa_key_bits_t) bits;
Ronald Cron2ebfdcc2020-11-28 15:54:54 +01004687 psa_key_attributes_t attributes = {
4688 .core = slot->attr
4689 };
4690
Ronald Cronbf33c932020-11-28 18:06:53 +01004691 status = psa_driver_wrapper_import_key( &attributes,
4692 data, bytes,
4693 slot->key.data,
4694 slot->key.bytes,
4695 &slot->key.bytes, &bits );
4696 if( bits != slot->attr.bits )
4697 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004698
4699exit:
4700 mbedtls_free( data );
4701 return( status );
4702}
4703
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004704psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004705 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004706 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004707{
4708 psa_status_t status;
4709 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004710 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004711
Ronald Cron81709fc2020-11-14 12:10:32 +01004712 *key = MBEDTLS_SVC_KEY_ID_INIT;
4713
Gilles Peskine0f84d622019-09-12 19:03:13 +02004714 /* Reject any attempt to create a zero-length key so that we don't
4715 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4716 if( psa_get_key_bits( attributes ) == 0 )
4717 return( PSA_ERROR_INVALID_ARGUMENT );
4718
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004719 if( ! operation->can_output_key )
4720 return( PSA_ERROR_NOT_PERMITTED );
4721
Ronald Cron81709fc2020-11-14 12:10:32 +01004722 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
4723 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004724#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4725 if( driver != NULL )
4726 {
4727 /* Deriving a key in a secure element is not implemented yet. */
4728 status = PSA_ERROR_NOT_SUPPORTED;
4729 }
4730#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004731 if( status == PSA_SUCCESS )
4732 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004733 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004734 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004735 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004736 }
4737 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01004738 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004739 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004740 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004741
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004742 return( status );
4743}
4744
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745
4746
4747/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004748/* Key derivation */
4749/****************************************************************/
4750
Steven Cooreman932ffb72021-02-15 12:14:32 +01004751#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004754 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004755{
John Durkop07cc04a2020-11-16 22:08:34 -08004756 int is_kdf_alg_supported;
4757
Janos Follath5fe19732019-06-20 15:09:30 +01004758 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4759 * initialisers for this union leave some bytes unspecified.) */
4760 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4761
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07004763#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08004764 if( PSA_ALG_IS_HKDF( kdf_alg ) )
4765 is_kdf_alg_supported = 1;
4766 else
4767#endif
4768#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
4769 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
4770 is_kdf_alg_supported = 1;
4771 else
4772#endif
4773#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4774 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
4775 is_kdf_alg_supported = 1;
4776 else
4777#endif
4778 is_kdf_alg_supported = 0;
4779
4780 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004781 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004782 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004783 size_t hash_size = PSA_HASH_LENGTH( hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004784 if( hash_size == 0 )
4785 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004786 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4787 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004788 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004789 {
4790 return( PSA_ERROR_NOT_SUPPORTED );
4791 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004792 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004793 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004794 }
John Durkop07cc04a2020-11-16 22:08:34 -08004795
4796 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004797}
John Durkop07cc04a2020-11-16 22:08:34 -08004798#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004799
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004801 psa_algorithm_t alg )
4802{
4803 psa_status_t status;
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004806 return( PSA_ERROR_BAD_STATE );
4807
Gilles Peskine6843c292019-01-18 16:44:49 +01004808 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4809 return( PSA_ERROR_INVALID_ARGUMENT );
4810 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004811 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004812#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004813 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004815#else
4816 return( PSA_ERROR_NOT_SUPPORTED );
4817#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004818 }
4819 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4820 {
Steven Cooreman932ffb72021-02-15 12:14:32 +01004821#if defined(AT_LEAST_ONE_BUILTIN_KDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 status = psa_key_derivation_setup_kdf( operation, alg );
Steven Cooreman932ffb72021-02-15 12:14:32 +01004823#else
4824 return( PSA_ERROR_NOT_SUPPORTED );
4825#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004826 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004827 else
4828 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004829
4830 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004831 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004832 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004833}
4834
John Durkopd0321952020-10-29 21:37:36 -07004835#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004836static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004837 psa_algorithm_t hash_alg,
4838 psa_key_derivation_step_t step,
4839 const uint8_t *data,
4840 size_t data_length )
4841{
4842 psa_status_t status;
4843 switch( step )
4844 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004845 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004846 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004847 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004848 status = psa_hmac_setup_internal( &hkdf->hmac,
4849 data, data_length,
4850 hash_alg );
4851 if( status != PSA_SUCCESS )
4852 return( status );
4853 hkdf->state = HKDF_STATE_STARTED;
4854 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004855 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004856 /* If no salt was provided, use an empty salt. */
4857 if( hkdf->state == HKDF_STATE_INIT )
4858 {
4859 status = psa_hmac_setup_internal( &hkdf->hmac,
4860 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004861 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004862 if( status != PSA_SUCCESS )
4863 return( status );
4864 hkdf->state = HKDF_STATE_STARTED;
4865 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004866 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004867 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004868 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4869 data, data_length );
4870 if( status != PSA_SUCCESS )
4871 return( status );
4872 status = psa_hmac_finish_internal( &hkdf->hmac,
4873 hkdf->prk,
4874 sizeof( hkdf->prk ) );
4875 if( status != PSA_SUCCESS )
4876 return( status );
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004877 hkdf->offset_in_block = PSA_HASH_LENGTH( hash_alg );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004878 hkdf->block_number = 0;
4879 hkdf->state = HKDF_STATE_KEYED;
4880 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004881 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004882 if( hkdf->state == HKDF_STATE_OUTPUT )
4883 return( PSA_ERROR_BAD_STATE );
4884 if( hkdf->info_set )
4885 return( PSA_ERROR_BAD_STATE );
4886 hkdf->info_length = data_length;
4887 if( data_length != 0 )
4888 {
4889 hkdf->info = mbedtls_calloc( 1, data_length );
4890 if( hkdf->info == NULL )
4891 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4892 memcpy( hkdf->info, data, data_length );
4893 }
4894 hkdf->info_set = 1;
4895 return( PSA_SUCCESS );
4896 default:
4897 return( PSA_ERROR_INVALID_ARGUMENT );
4898 }
4899}
John Durkop07cc04a2020-11-16 22:08:34 -08004900#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01004901
John Durkop07cc04a2020-11-16 22:08:34 -08004902#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
4903 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01004904static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4905 const uint8_t *data,
4906 size_t data_length )
4907{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004908 if( prf->state != PSA_TLS12_PRF_STATE_INIT )
Janos Follathf08e2652019-06-13 09:05:41 +01004909 return( PSA_ERROR_BAD_STATE );
4910
Janos Follathd6dce9f2019-07-04 09:11:38 +01004911 if( data_length != 0 )
4912 {
4913 prf->seed = mbedtls_calloc( 1, data_length );
4914 if( prf->seed == NULL )
4915 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01004916
Janos Follathd6dce9f2019-07-04 09:11:38 +01004917 memcpy( prf->seed, data, data_length );
4918 prf->seed_length = data_length;
4919 }
Janos Follathf08e2652019-06-13 09:05:41 +01004920
Gilles Peskine43f958b2020-12-13 14:55:14 +01004921 prf->state = PSA_TLS12_PRF_STATE_SEED_SET;
Janos Follathf08e2652019-06-13 09:05:41 +01004922
4923 return( PSA_SUCCESS );
4924}
4925
Janos Follath81550542019-06-13 14:26:34 +01004926static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
4927 psa_algorithm_t hash_alg,
4928 const uint8_t *data,
4929 size_t data_length )
4930{
4931 psa_status_t status;
Gilles Peskine43f958b2020-12-13 14:55:14 +01004932 if( prf->state != PSA_TLS12_PRF_STATE_SEED_SET )
Janos Follath81550542019-06-13 14:26:34 +01004933 return( PSA_ERROR_BAD_STATE );
4934
4935 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
4936 if( status != PSA_SUCCESS )
4937 return( status );
4938
Gilles Peskine43f958b2020-12-13 14:55:14 +01004939 prf->state = PSA_TLS12_PRF_STATE_KEY_SET;
Janos Follath81550542019-06-13 14:26:34 +01004940
4941 return( PSA_SUCCESS );
4942}
4943
Janos Follath63028dd2019-06-13 09:15:47 +01004944static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
4945 const uint8_t *data,
4946 size_t data_length )
4947{
Gilles Peskine43f958b2020-12-13 14:55:14 +01004948 if( prf->state != PSA_TLS12_PRF_STATE_KEY_SET )
Janos Follath63028dd2019-06-13 09:15:47 +01004949 return( PSA_ERROR_BAD_STATE );
4950
Janos Follathd6dce9f2019-07-04 09:11:38 +01004951 if( data_length != 0 )
4952 {
4953 prf->label = mbedtls_calloc( 1, data_length );
4954 if( prf->label == NULL )
4955 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01004956
Janos Follathd6dce9f2019-07-04 09:11:38 +01004957 memcpy( prf->label, data, data_length );
4958 prf->label_length = data_length;
4959 }
Janos Follath63028dd2019-06-13 09:15:47 +01004960
Gilles Peskine43f958b2020-12-13 14:55:14 +01004961 prf->state = PSA_TLS12_PRF_STATE_LABEL_SET;
Janos Follath63028dd2019-06-13 09:15:47 +01004962
4963 return( PSA_SUCCESS );
4964}
4965
Janos Follathb03233e2019-06-11 15:30:30 +01004966static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
4967 psa_algorithm_t hash_alg,
4968 psa_key_derivation_step_t step,
4969 const uint8_t *data,
4970 size_t data_length )
4971{
Janos Follathb03233e2019-06-11 15:30:30 +01004972 switch( step )
4973 {
Janos Follathf08e2652019-06-13 09:05:41 +01004974 case PSA_KEY_DERIVATION_INPUT_SEED:
4975 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01004976 case PSA_KEY_DERIVATION_INPUT_SECRET:
4977 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01004978 case PSA_KEY_DERIVATION_INPUT_LABEL:
4979 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01004980 default:
4981 return( PSA_ERROR_INVALID_ARGUMENT );
4982 }
4983}
John Durkop07cc04a2020-11-16 22:08:34 -08004984#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
4985 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
4986
4987#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
4988static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
4989 psa_tls12_prf_key_derivation_t *prf,
4990 psa_algorithm_t hash_alg,
4991 const uint8_t *data,
4992 size_t data_length )
4993{
4994 psa_status_t status;
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004995 uint8_t pms[ 4 + 2 * PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE ];
John Durkop07cc04a2020-11-16 22:08:34 -08004996 uint8_t *cur = pms;
4997
gabor-mezei-armcbcec212020-12-18 14:23:51 +01004998 if( data_length > PSA_TLS12_PSK_TO_MS_PSK_MAX_SIZE )
John Durkop07cc04a2020-11-16 22:08:34 -08004999 return( PSA_ERROR_INVALID_ARGUMENT );
5000
5001 /* Quoting RFC 4279, Section 2:
5002 *
5003 * The premaster secret is formed as follows: if the PSK is N octets
5004 * long, concatenate a uint16 with the value N, N zero octets, a second
5005 * uint16 with the value N, and the PSK itself.
5006 */
5007
5008 *cur++ = ( data_length >> 8 ) & 0xff;
5009 *cur++ = ( data_length >> 0 ) & 0xff;
5010 memset( cur, 0, data_length );
5011 cur += data_length;
5012 *cur++ = pms[0];
5013 *cur++ = pms[1];
5014 memcpy( cur, data, data_length );
5015 cur += data_length;
5016
5017 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5018
5019 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5020 return( status );
5021}
Janos Follath6660f0e2019-06-17 08:44:03 +01005022
5023static psa_status_t psa_tls12_prf_psk_to_ms_input(
5024 psa_tls12_prf_key_derivation_t *prf,
5025 psa_algorithm_t hash_alg,
5026 psa_key_derivation_step_t step,
5027 const uint8_t *data,
5028 size_t data_length )
5029{
5030 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005031 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005032 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5033 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005034 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005035
5036 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5037}
John Durkop07cc04a2020-11-16 22:08:34 -08005038#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005039
Gilles Peskineb8965192019-09-24 16:21:10 +02005040/** Check whether the given key type is acceptable for the given
5041 * input step of a key derivation.
5042 *
5043 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5044 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5045 * Both secret and non-secret inputs can alternatively have the type
5046 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5047 * that the input was passed as a buffer rather than via a key object.
5048 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005049static int psa_key_derivation_check_input_type(
5050 psa_key_derivation_step_t step,
5051 psa_key_type_t key_type )
5052{
5053 switch( step )
5054 {
5055 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005056 if( key_type == PSA_KEY_TYPE_DERIVE )
5057 return( PSA_SUCCESS );
5058 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005059 return( PSA_SUCCESS );
5060 break;
5061 case PSA_KEY_DERIVATION_INPUT_LABEL:
5062 case PSA_KEY_DERIVATION_INPUT_SALT:
5063 case PSA_KEY_DERIVATION_INPUT_INFO:
5064 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005065 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5066 return( PSA_SUCCESS );
5067 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005068 return( PSA_SUCCESS );
5069 break;
5070 }
5071 return( PSA_ERROR_INVALID_ARGUMENT );
5072}
5073
Janos Follathb80a94e2019-06-12 15:54:46 +01005074static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005075 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005076 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005077 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005078 const uint8_t *data,
5079 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005080{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005081 psa_status_t status;
5082 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5083
5084 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005085 if( status != PSA_SUCCESS )
5086 goto exit;
5087
John Durkopd0321952020-10-29 21:37:36 -07005088#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005089 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005090 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005091 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005092 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005093 step, data, data_length );
5094 }
John Durkop07cc04a2020-11-16 22:08:34 -08005095 else
5096#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5097#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5098 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005099 {
Janos Follathb03233e2019-06-11 15:30:30 +01005100 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5101 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5102 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005103 }
John Durkop07cc04a2020-11-16 22:08:34 -08005104 else
5105#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
5106#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5107 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01005108 {
5109 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5110 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5111 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005112 }
5113 else
John Durkop07cc04a2020-11-16 22:08:34 -08005114#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005115 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005116 /* This can't happen unless the operation object was not initialized */
Steven Cooreman74afe472021-02-10 17:19:22 +01005117 (void) data;
5118 (void) data_length;
5119 (void) kdf_alg;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005120 return( PSA_ERROR_BAD_STATE );
5121 }
5122
Gilles Peskine224b0d62019-09-23 18:13:17 +02005123exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005124 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005125 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005126 return( status );
5127}
5128
Janos Follath51f4a0f2019-06-14 11:35:55 +01005129psa_status_t psa_key_derivation_input_bytes(
5130 psa_key_derivation_operation_t *operation,
5131 psa_key_derivation_step_t step,
5132 const uint8_t *data,
5133 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005134{
Gilles Peskineb8965192019-09-24 16:21:10 +02005135 return( psa_key_derivation_input_internal( operation, step,
5136 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005137 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005138}
5139
Janos Follath51f4a0f2019-06-14 11:35:55 +01005140psa_status_t psa_key_derivation_input_key(
5141 psa_key_derivation_operation_t *operation,
5142 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005143 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005144{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005146 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005147 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005148
Ronald Cron5c522922020-11-14 16:35:34 +01005149 status = psa_get_and_lock_transparent_key_slot_with_policy(
5150 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005151 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005152 {
5153 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005154 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005155 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005156
5157 /* Passing a key object as a SECRET input unlocks the permission
5158 * to output to a key object. */
5159 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5160 operation->can_output_key = 1;
5161
Ronald Cronf95a2b12020-10-22 15:24:49 +02005162 status = psa_key_derivation_input_internal( operation,
5163 step, slot->attr.type,
Ronald Cronea0f8a62020-11-25 17:52:23 +01005164 slot->key.data,
5165 slot->key.bytes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005166
Ronald Cron5c522922020-11-14 16:35:34 +01005167 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005168
Ronald Cron5c522922020-11-14 16:35:34 +01005169 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005170}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005171
5172
5173
5174/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005175/* Key agreement */
5176/****************************************************************/
5177
John Durkop6ba40d12020-11-10 08:50:04 -08005178#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005179static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5180 size_t peer_key_length,
5181 const mbedtls_ecp_keypair *our_key,
5182 uint8_t *shared_secret,
5183 size_t shared_secret_size,
5184 size_t *shared_secret_length )
5185{
Steven Cooremand4867872020-08-05 16:31:39 +02005186 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005187 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005188 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005189 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005190 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005191 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005192
Ronald Cron90857082020-11-25 14:58:33 +01005193 status = mbedtls_psa_ecp_load_representation(
5194 PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005195 bits,
Ronald Cron90857082020-11-25 14:58:33 +01005196 peer_key,
5197 peer_key_length,
5198 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005199 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005200 goto exit;
5201
Jaeden Amero97271b32019-01-10 19:38:51 +00005202 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02005203 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00005204 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005205 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005206 status = mbedtls_to_psa_error(
5207 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5208 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005209 goto exit;
5210
Jaeden Amero97271b32019-01-10 19:38:51 +00005211 status = mbedtls_to_psa_error(
5212 mbedtls_ecdh_calc_secret( &ecdh,
5213 shared_secret_length,
5214 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01005215 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005216 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005217 if( status != PSA_SUCCESS )
5218 goto exit;
5219 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5220 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005221
5222exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005223 if( status != PSA_SUCCESS )
5224 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005225 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02005226 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02005227 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02005228
Jaeden Amero97271b32019-01-10 19:38:51 +00005229 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005230}
John Durkop6ba40d12020-11-10 08:50:04 -08005231#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005232
Gilles Peskine01d718c2018-09-18 12:01:02 +02005233#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5234
Gilles Peskine0216fe12019-04-11 21:23:21 +02005235static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5236 psa_key_slot_t *private_key,
5237 const uint8_t *peer_key,
5238 size_t peer_key_length,
5239 uint8_t *shared_secret,
5240 size_t shared_secret_size,
5241 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005242{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005243 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005244 {
John Durkop6ba40d12020-11-10 08:50:04 -08005245#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005246 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005247 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005248 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02005249 mbedtls_ecp_keypair *ecp = NULL;
Ronald Cron90857082020-11-25 14:58:33 +01005250 psa_status_t status = mbedtls_psa_ecp_load_representation(
5251 private_key->attr.type,
Gilles Peskine2fa6b5f2021-01-27 15:44:45 +01005252 private_key->attr.bits,
Ronald Cron90857082020-11-25 14:58:33 +01005253 private_key->key.data,
5254 private_key->key.bytes,
5255 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02005256 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02005257 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02005258 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02005259 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02005260 shared_secret, shared_secret_size,
5261 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02005262 mbedtls_ecp_keypair_free( ecp );
5263 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02005264 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08005265#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005266 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005267 (void) private_key;
5268 (void) peer_key;
5269 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005270 (void) shared_secret;
5271 (void) shared_secret_size;
5272 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005273 return( PSA_ERROR_NOT_SUPPORTED );
5274 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005275}
5276
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005277/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005278 * to potentially free embedded data structures and wipe confidential data.
5279 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005281 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005282 psa_key_slot_t *private_key,
5283 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005284 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005285{
5286 psa_status_t status;
5287 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5288 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005289 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005290
5291 /* Step 1: run the secret agreement algorithm to generate the shared
5292 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005293 status = psa_key_agreement_raw_internal( ka_alg,
5294 private_key,
5295 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005296 shared_secret,
5297 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005298 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005299 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005300 goto exit;
5301
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005302 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005303 * the shared secret. A shared secret is permitted wherever a key
5304 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005305 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005306 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005307 shared_secret,
5308 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005309exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005310 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005311 return( status );
5312}
5313
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005314psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005315 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005316 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005317 const uint8_t *peer_key,
5318 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005319{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005320 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005321 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01005322 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005325 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01005326 status = psa_get_and_lock_transparent_key_slot_with_policy(
5327 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005328 if( status != PSA_SUCCESS )
5329 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005330 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005331 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005332 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005333 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005334 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02005335 else
5336 {
5337 /* If a private key has been added as SECRET, we allow the derived
5338 * key material to be used as a key in PSA Crypto. */
5339 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5340 operation->can_output_key = 1;
5341 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005342
Ronald Cron5c522922020-11-14 16:35:34 +01005343 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005344
Ronald Cron5c522922020-11-14 16:35:34 +01005345 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005346}
5347
Gilles Peskinebe697d82019-05-16 18:00:41 +02005348psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005349 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02005350 const uint8_t *peer_key,
5351 size_t peer_key_length,
5352 uint8_t *output,
5353 size_t output_size,
5354 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005355{
Ronald Cronf95a2b12020-10-22 15:24:49 +02005356 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01005357 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005358 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005359
5360 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5361 {
5362 status = PSA_ERROR_INVALID_ARGUMENT;
5363 goto exit;
5364 }
Ronald Cron5c522922020-11-14 16:35:34 +01005365 status = psa_get_and_lock_transparent_key_slot_with_policy(
5366 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005367 if( status != PSA_SUCCESS )
5368 goto exit;
5369
5370 status = psa_key_agreement_raw_internal( alg, slot,
5371 peer_key, peer_key_length,
5372 output, output_size,
5373 output_length );
5374
5375exit:
5376 if( status != PSA_SUCCESS )
5377 {
5378 /* If an error happens and is not handled properly, the output
5379 * may be used as a key to protect sensitive data. Arrange for such
5380 * a key to be random, which is likely to result in decryption or
5381 * verification errors. This is better than filling the buffer with
5382 * some constant data such as zeros, which would result in the data
5383 * being protected with a reproducible, easily knowable key.
5384 */
5385 psa_generate_random( output, output_size );
5386 *output_length = output_size;
5387 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02005388
Ronald Cron5c522922020-11-14 16:35:34 +01005389 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005390
Ronald Cron5c522922020-11-14 16:35:34 +01005391 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005392}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005393
5394
Gilles Peskine30524eb2020-11-13 17:02:26 +01005395
Gilles Peskine86a440b2018-11-12 18:39:40 +01005396/****************************************************************/
5397/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005398/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005399
Gilles Peskine30524eb2020-11-13 17:02:26 +01005400/** Initialize the PSA random generator.
5401 */
5402static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
5403{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005404#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5405 memset( rng, 0, sizeof( *rng ) );
5406#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5407
Gilles Peskine30524eb2020-11-13 17:02:26 +01005408 /* Set default configuration if
5409 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5410 if( rng->entropy_init == NULL )
5411 rng->entropy_init = mbedtls_entropy_init;
5412 if( rng->entropy_free == NULL )
5413 rng->entropy_free = mbedtls_entropy_free;
5414
5415 rng->entropy_init( &rng->entropy );
5416#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5417 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5418 /* The PSA entropy injection feature depends on using NV seed as an entropy
5419 * source. Add NV seed as an entropy source for PSA entropy injection. */
5420 mbedtls_entropy_add_source( &rng->entropy,
5421 mbedtls_nv_seed_poll, NULL,
5422 MBEDTLS_ENTROPY_BLOCK_SIZE,
5423 MBEDTLS_ENTROPY_SOURCE_STRONG );
5424#endif
5425
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005426 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005427#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005428}
5429
5430/** Deinitialize the PSA random generator.
5431 */
5432static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
5433{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005434#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5435 memset( rng, 0, sizeof( *rng ) );
5436#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005437 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005438 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005439#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005440}
5441
5442/** Seed the PSA random generator.
5443 */
5444static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
5445{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005446#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5447 /* Do nothing: the external RNG seeds itself. */
5448 (void) rng;
5449 return( PSA_SUCCESS );
5450#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005451 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01005452 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
5453 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005454 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005455#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01005456}
5457
Gilles Peskinee59236f2018-01-27 23:32:46 +01005458psa_status_t psa_generate_random( uint8_t *output,
5459 size_t output_size )
5460{
Gilles Peskinee59236f2018-01-27 23:32:46 +01005461 GUARD_MODULE_INITIALIZED;
5462
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005463#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5464
5465 size_t output_length = 0;
5466 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
5467 output, output_size,
5468 &output_length );
5469 if( status != PSA_SUCCESS )
5470 return( status );
5471 /* Breaking up a request into smaller chunks is currently not supported
5472 * for the extrernal RNG interface. */
5473 if( output_length != output_size )
5474 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
5475 return( PSA_SUCCESS );
5476
5477#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5478
Gilles Peskine71ddab92021-01-04 21:01:07 +01005479 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02005480 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01005481 size_t request_size =
5482 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
5483 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
5484 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005485 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
5486 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02005487 if( ret != 0 )
5488 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01005489 output_size -= request_size;
5490 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02005491 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01005492 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005493#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005494}
5495
Gilles Peskine8814fc42020-12-14 15:33:44 +01005496/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005497 *
5498 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
5499 * `psa_generate_random(...)`. The state parameter is ignored since the
5500 * PSA API doesn't support passing an explicit state.
5501 *
5502 * In the non-external case, psa_generate_random() calls an
5503 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
5504 * and semantics as mbedtls_psa_get_random(). As an optimization,
5505 * instead of doing this back-and-forth between the PSA API and the
5506 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
5507 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01005508 */
5509#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
5510int mbedtls_psa_get_random( void *p_rng,
5511 unsigned char *output,
5512 size_t output_size )
5513{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01005514 /* This function takes a pointer to the RNG state because that's what
5515 * classic mbedtls functions using an RNG expect. The PSA RNG manages
5516 * its own state internally and doesn't let the caller access that state.
5517 * So we just ignore the state parameter, and in practice we'll pass
5518 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01005519 (void) p_rng;
5520 psa_status_t status = psa_generate_random( output, output_size );
5521 if( status == PSA_SUCCESS )
5522 return( 0 );
5523 else
5524 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
5525}
5526#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
5527
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005528#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5529#include "mbedtls/entropy_poll.h"
5530
Gilles Peskine7228da22019-07-15 11:06:15 +02005531psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005532 size_t seed_size )
5533{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005534 if( global_data.initialized )
5535 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005536
5537 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5538 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5539 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5540 return( PSA_ERROR_INVALID_ARGUMENT );
5541
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005542 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005543}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005544#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005545
Ronald Cron3772afe2021-02-08 16:10:05 +01005546/** Validate the key type and size for key generation
Ronald Cron01b2aba2020-10-05 09:42:02 +02005547 *
Ronald Cron3772afe2021-02-08 16:10:05 +01005548 * \param type The key type
5549 * \param bits The number of bits of the key
Ronald Cron01b2aba2020-10-05 09:42:02 +02005550 *
5551 * \retval #PSA_SUCCESS
Ronald Cron3772afe2021-02-08 16:10:05 +01005552 * The key type and size are valid.
Ronald Cron01b2aba2020-10-05 09:42:02 +02005553 * \retval #PSA_ERROR_INVALID_ARGUMENT
5554 * The size in bits of the key is not valid.
5555 * \retval #PSA_ERROR_NOT_SUPPORTED
5556 * The type and/or the size in bits of the key or the combination of
5557 * the two is not supported.
5558 */
Ronald Cron3772afe2021-02-08 16:10:05 +01005559static psa_status_t psa_validate_key_type_and_size_for_key_generation(
5560 psa_key_type_t type, size_t bits )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561{
Ronald Cronf3bb7612020-10-02 20:11:59 +02005562 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005563
Gilles Peskinee59236f2018-01-27 23:32:46 +01005564 if( key_type_is_raw_bytes( type ) )
5565 {
Ronald Cronf3bb7612020-10-02 20:11:59 +02005566 status = validate_unstructured_key_bit_size( type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005567 if( status != PSA_SUCCESS )
5568 return( status );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005569 }
5570 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005571#if defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005572 if( PSA_KEY_TYPE_IS_RSA( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5573 {
5574 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5575 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005576
Ronald Cron01b2aba2020-10-05 09:42:02 +02005577 /* Accept only byte-aligned keys, for the same reasons as
5578 * in psa_import_rsa_key(). */
5579 if( bits % 8 != 0 )
5580 return( PSA_ERROR_NOT_SUPPORTED );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005581 }
5582 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005583#endif /* defined(PSA_WANT_KEY_TYPE_RSA_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005584
Ronald Cron5c4d3862020-12-07 11:07:24 +01005585#if defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR)
Ronald Cron01b2aba2020-10-05 09:42:02 +02005586 if( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
5587 {
Ronald Crond81ab562021-02-16 09:01:16 +01005588 /* To avoid empty block, return successfully here. */
5589 return( PSA_SUCCESS );
Ronald Cron01b2aba2020-10-05 09:42:02 +02005590 }
5591 else
Ronald Cron5c4d3862020-12-07 11:07:24 +01005592#endif /* defined(PSA_WANT_KEY_TYPE_ECC_KEY_PAIR) */
Ronald Cron01b2aba2020-10-05 09:42:02 +02005593 {
5594 return( PSA_ERROR_NOT_SUPPORTED );
5595 }
5596
5597 return( PSA_SUCCESS );
5598}
5599
Ronald Cron55ed0592020-10-05 10:30:40 +02005600psa_status_t psa_generate_key_internal(
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005601 const psa_key_attributes_t *attributes,
5602 uint8_t *key_buffer, size_t key_buffer_size, size_t *key_buffer_length )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005603{
5604 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005605 psa_key_type_t type = attributes->core.type;
Ronald Cron01b2aba2020-10-05 09:42:02 +02005606
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005607 if( ( attributes->domain_parameters == NULL ) &&
5608 ( attributes->domain_parameters_size != 0 ) )
Ronald Cron01b2aba2020-10-05 09:42:02 +02005609 return( PSA_ERROR_INVALID_ARGUMENT );
5610
Ronald Cron01b2aba2020-10-05 09:42:02 +02005611 if( key_type_is_raw_bytes( type ) )
5612 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005613 status = psa_generate_random( key_buffer, key_buffer_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005614 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005615 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02005616
David Brown12ca5032021-02-11 11:02:00 -07005617#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES)
Gilles Peskinee59236f2018-01-27 23:32:46 +01005618 if( type == PSA_KEY_TYPE_DES )
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005619 psa_des_set_key_parity( key_buffer, key_buffer_size );
David Brown8107e312021-02-12 08:08:46 -07005620#endif /* MBEDTLS_PSA_BUILTIN_KEY_TYPE_DES */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005621 }
5622 else
5623
John Durkop07cc04a2020-11-16 22:08:34 -08005624#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005625 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005626 {
Ronald Cron9e18fc12020-11-05 17:36:40 +01005627 return( mbedtls_psa_rsa_generate_key( attributes,
5628 key_buffer,
5629 key_buffer_size,
5630 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005631 }
5632 else
John Durkop07cc04a2020-11-16 22:08:34 -08005633#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005634
John Durkop9814fa22020-11-04 12:28:15 -08005635#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005636 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005637 {
Ronald Cron7023db52020-11-20 18:17:42 +01005638 return( mbedtls_psa_ecp_generate_key( attributes,
5639 key_buffer,
5640 key_buffer_size,
5641 key_buffer_length ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005642 }
5643 else
John Durkop9814fa22020-11-04 12:28:15 -08005644#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02005645 {
Ronald Cron2a38a6b2020-10-02 20:02:04 +02005646 (void)key_buffer_length;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005647 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02005648 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01005649
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005650 return( PSA_SUCCESS );
5651}
Darryl Green0c6575a2018-11-07 16:05:30 +00005652
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005653psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005654 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005655{
5656 psa_status_t status;
5657 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005658 psa_se_drv_table_entry_t *driver = NULL;
Ronald Cron2b56bc82020-10-05 10:02:26 +02005659 size_t key_buffer_size;
Gilles Peskine11792082019-08-06 18:36:36 +02005660
Ronald Cron81709fc2020-11-14 12:10:32 +01005661 *key = MBEDTLS_SVC_KEY_ID_INIT;
5662
Gilles Peskine0f84d622019-09-12 19:03:13 +02005663 /* Reject any attempt to create a zero-length key so that we don't
5664 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5665 if( psa_get_key_bits( attributes ) == 0 )
5666 return( PSA_ERROR_INVALID_ARGUMENT );
5667
Ronald Cron81709fc2020-11-14 12:10:32 +01005668 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
5669 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005670 if( status != PSA_SUCCESS )
5671 goto exit;
5672
Ronald Cron977c2472020-10-13 08:32:21 +02005673 /* In the case of a transparent key or an opaque key stored in local
5674 * storage (thus not in the case of generating a key in a secure element
5675 * or cryptoprocessor with storage), we have to allocate a buffer to
5676 * hold the generated key material. */
5677 if( slot->key.data == NULL )
5678 {
5679 if ( PSA_KEY_LIFETIME_GET_LOCATION( attributes->core.lifetime ) ==
5680 PSA_KEY_LOCATION_LOCAL_STORAGE )
5681 {
Ronald Cron3772afe2021-02-08 16:10:05 +01005682 status = psa_validate_key_type_and_size_for_key_generation(
5683 attributes->core.type, attributes->core.bits );
5684 if( status != PSA_SUCCESS )
5685 goto exit;
5686
5687 key_buffer_size = PSA_EXPORT_KEY_OUTPUT_SIZE(
5688 attributes->core.type,
5689 attributes->core.bits );
Ronald Cron977c2472020-10-13 08:32:21 +02005690 }
5691 else
5692 {
5693 status = psa_driver_wrapper_get_key_buffer_size(
5694 attributes, &key_buffer_size );
Ronald Cron3772afe2021-02-08 16:10:05 +01005695 if( status != PSA_SUCCESS )
5696 goto exit;
Ronald Cron977c2472020-10-13 08:32:21 +02005697 }
Ronald Cron977c2472020-10-13 08:32:21 +02005698
5699 status = psa_allocate_buffer_to_slot( slot, key_buffer_size );
5700 if( status != PSA_SUCCESS )
5701 goto exit;
5702 }
5703
Steven Cooreman2a1664c2020-07-20 15:33:08 +02005704 status = psa_driver_wrapper_generate_key( attributes,
Ronald Cron977c2472020-10-13 08:32:21 +02005705 slot->key.data, slot->key.bytes, &slot->key.bytes );
Gilles Peskine11792082019-08-06 18:36:36 +02005706
Ronald Cron2b56bc82020-10-05 10:02:26 +02005707 if( status != PSA_SUCCESS )
5708 psa_remove_key_data_from_memory( slot );
5709
Gilles Peskine11792082019-08-06 18:36:36 +02005710exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005711 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005712 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005713 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005714 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005715
Darryl Green0c6575a2018-11-07 16:05:30 +00005716 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005717}
5718
Gilles Peskinee59236f2018-01-27 23:32:46 +01005719/****************************************************************/
5720/* Module setup */
5721/****************************************************************/
5722
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005723#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01005724psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5725 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5726 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5727{
5728 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5729 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01005730 global_data.rng.entropy_init = entropy_init;
5731 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01005732 return( PSA_SUCCESS );
5733}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01005734#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01005735
Gilles Peskinee59236f2018-01-27 23:32:46 +01005736void mbedtls_psa_crypto_free( void )
5737{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005738 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005739 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5740 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01005741 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005742 }
5743 /* Wipe all remaining data, including configuration.
5744 * In particular, this sets all state indicator to the value
5745 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005746 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005747#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005748 /* Unregister all secure element drivers, so that we restart from
5749 * a pristine state. */
5750 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005751#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005752}
5753
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005754#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5755/** Recover a transaction that was interrupted by a power failure.
5756 *
5757 * This function is called during initialization, before psa_crypto_init()
5758 * returns. If this function returns a failure status, the initialization
5759 * fails.
5760 */
5761static psa_status_t psa_crypto_recover_transaction(
5762 const psa_crypto_transaction_t *transaction )
5763{
5764 switch( transaction->unknown.type )
5765 {
5766 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5767 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005768 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005769 * is implemented.
5770 * https://github.com/ARMmbed/mbed-crypto/issues/218
5771 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005772 default:
5773 /* We found an unsupported transaction in the storage.
5774 * We don't know what state the storage is in. Give up. */
gabor-mezei-armfe309242020-11-09 17:39:56 +01005775 return( PSA_ERROR_DATA_INVALID );
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005776 }
5777}
5778#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5779
Gilles Peskinee59236f2018-01-27 23:32:46 +01005780psa_status_t psa_crypto_init( void )
5781{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005782 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005783
Gilles Peskinec6b69072018-11-20 21:42:52 +01005784 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 if( global_data.initialized != 0 )
5786 return( PSA_SUCCESS );
5787
Gilles Peskine30524eb2020-11-13 17:02:26 +01005788 /* Initialize and seed the random generator. */
5789 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005790 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01005791 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005792 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005793 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005794 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005795
Gilles Peskine66fb1262018-12-10 16:29:04 +01005796 status = psa_initialize_key_slots( );
5797 if( status != PSA_SUCCESS )
5798 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005799
Gilles Peskined9348f22019-10-01 15:22:29 +02005800#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5801 status = psa_init_all_se_drivers( );
5802 if( status != PSA_SUCCESS )
5803 goto exit;
5804#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5805
Gilles Peskine4b734222019-07-24 15:56:31 +02005806#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005807 status = psa_crypto_load_transaction( );
5808 if( status == PSA_SUCCESS )
5809 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005810 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5811 if( status != PSA_SUCCESS )
5812 goto exit;
5813 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005814 }
5815 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5816 {
5817 /* There's no transaction to complete. It's all good. */
5818 status = PSA_SUCCESS;
5819 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005820#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005821
Gilles Peskinec6b69072018-11-20 21:42:52 +01005822 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005823 global_data.initialized = 1;
5824
5825exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005826 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005827 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005828 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005829}
5830
5831#endif /* MBEDTLS_PSA_CRYPTO_C */