blob: e0ed35f0a3f2d9d00859d894d9e49006020186c3 [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"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb2b64d32020-12-14 16:43:58 +010043#include "psa_crypto_random_impl.h"
Gilles Peskine90edc992020-11-13 15:39:19 +010044
Gilles Peskineb46bef22019-07-30 21:32:04 +020045#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include <stdlib.h>
47#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020049#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010050#define mbedtls_calloc calloc
51#define mbedtls_free free
52#endif
53
Gilles Peskine1c49f1a2020-11-13 18:46:25 +010054#include "mbedtls/aes.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010055#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020056#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000057#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020058#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010059#include "mbedtls/blowfish.h"
60#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020061#include "mbedtls/chacha20.h"
62#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010063#include "mbedtls/cipher.h"
64#include "mbedtls/ccm.h"
65#include "mbedtls/cmac.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010066#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020067#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010068#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010069#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010070#include "mbedtls/error.h"
71#include "mbedtls/gcm.h"
72#include "mbedtls/md2.h"
73#include "mbedtls/md4.h"
74#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010075#include "mbedtls/md.h"
76#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010077#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010078#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010079#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000080#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010082#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010083#include "mbedtls/sha1.h"
84#include "mbedtls/sha256.h"
85#include "mbedtls/sha512.h"
86#include "mbedtls/xtea.h"
87
Gilles Peskine996deb12018-08-01 15:45:45 +020088#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
89
Gilles Peskine9ef733f2018-02-07 21:05:37 +010090/* constant-time buffer comparison */
91static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
92{
93 size_t i;
94 unsigned char diff = 0;
95
96 for( i = 0; i < n; i++ )
97 diff |= a[i] ^ b[i];
98
99 return( diff );
100}
101
102
103
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100104/****************************************************************/
105/* Global data, support functions and library management */
106/****************************************************************/
107
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200108static int key_type_is_raw_bytes( psa_key_type_t type )
109{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200110 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200111}
112
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100113/* Values for psa_global_data_t::rng_state */
114#define RNG_NOT_INITIALIZED 0
115#define RNG_INITIALIZED 1
116#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100117
Gilles Peskine2d277862018-06-18 15:41:12 +0200118typedef struct
119{
Gilles Peskine30524eb2020-11-13 17:02:26 +0100120 mbedtls_psa_random_context_t rng;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100121 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100122 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100123} psa_global_data_t;
124
125static psa_global_data_t global_data;
126
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100127#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
128mbedtls_psa_drbg_context_t *const mbedtls_psa_random_state =
129 &global_data.rng.drbg;
130#endif
131
itayzafrir0adf0fc2018-09-06 16:24:41 +0300132#define GUARD_MODULE_INITIALIZED \
133 if( global_data.initialized == 0 ) \
134 return( PSA_ERROR_BAD_STATE );
135
Steven Cooreman01164162020-07-20 15:31:37 +0200136psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137{
Gilles Peskinea5905292018-02-07 20:59:33 +0100138 /* If there's both a high-level code and low-level code, dispatch on
139 * the high-level code. */
140 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100141 {
142 case 0:
143 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100144
145 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
146 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
147 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
148 return( PSA_ERROR_NOT_SUPPORTED );
149 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
150 return( PSA_ERROR_HARDWARE_FAILURE );
151
152 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
153 return( PSA_ERROR_HARDWARE_FAILURE );
154
Gilles Peskine9a944802018-06-21 09:35:35 +0200155 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
156 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
157 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
158 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
159 case MBEDTLS_ERR_ASN1_INVALID_DATA:
160 return( PSA_ERROR_INVALID_ARGUMENT );
161 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
162 return( PSA_ERROR_INSUFFICIENT_MEMORY );
163 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
164 return( PSA_ERROR_BUFFER_TOO_SMALL );
165
Jaeden Amero93e21112019-02-20 13:57:28 +0000166#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000167 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000168#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
169 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
170#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100171 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
172 return( PSA_ERROR_NOT_SUPPORTED );
173 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
174 return( PSA_ERROR_HARDWARE_FAILURE );
175
Jaeden Amero93e21112019-02-20 13:57:28 +0000176#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000177 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000178#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
179 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
180#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100181 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
182 return( PSA_ERROR_NOT_SUPPORTED );
183 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
186 case MBEDTLS_ERR_CCM_BAD_INPUT:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188 case MBEDTLS_ERR_CCM_AUTH_FAILED:
189 return( PSA_ERROR_INVALID_SIGNATURE );
190 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
191 return( PSA_ERROR_HARDWARE_FAILURE );
192
Gilles Peskine26869f22019-05-06 15:25:00 +0200193 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
194 return( PSA_ERROR_INVALID_ARGUMENT );
195
196 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
197 return( PSA_ERROR_BAD_STATE );
198 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
199 return( PSA_ERROR_INVALID_SIGNATURE );
200
Gilles Peskinea5905292018-02-07 20:59:33 +0100201 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
202 return( PSA_ERROR_NOT_SUPPORTED );
203 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
204 return( PSA_ERROR_INVALID_ARGUMENT );
205 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
206 return( PSA_ERROR_INSUFFICIENT_MEMORY );
207 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
208 return( PSA_ERROR_INVALID_PADDING );
209 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200210 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
212 return( PSA_ERROR_INVALID_SIGNATURE );
213 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200214 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100215 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
216 return( PSA_ERROR_HARDWARE_FAILURE );
217
218 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
219 return( PSA_ERROR_HARDWARE_FAILURE );
220
Gilles Peskine82e57d12020-11-13 21:31:17 +0100221#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
222 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100223 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
224 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100225 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
227 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
228 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
229 return( PSA_ERROR_NOT_SUPPORTED );
230 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
231 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100232#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100233
234 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
235 return( PSA_ERROR_NOT_SUPPORTED );
236 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
Gilles Peskinee59236f2018-01-27 23:32:46 +0100239 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
240 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
241 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
242 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100243
244 case MBEDTLS_ERR_GCM_AUTH_FAILED:
245 return( PSA_ERROR_INVALID_SIGNATURE );
246 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200247 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100248 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
249 return( PSA_ERROR_HARDWARE_FAILURE );
250
Gilles Peskine82e57d12020-11-13 21:31:17 +0100251#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
252 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100253 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
254 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100255 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
256 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
257 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
258 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
259 return( PSA_ERROR_NOT_SUPPORTED );
260 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
261 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
262#endif
263
Gilles Peskinea5905292018-02-07 20:59:33 +0100264 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
265 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
266 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
267 return( PSA_ERROR_HARDWARE_FAILURE );
268
269 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
270 return( PSA_ERROR_NOT_SUPPORTED );
271 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_MD_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
276 return( PSA_ERROR_STORAGE_FAILURE );
277 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
278 return( PSA_ERROR_HARDWARE_FAILURE );
279
Gilles Peskinef76aa772018-10-29 19:24:33 +0100280 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
281 return( PSA_ERROR_STORAGE_FAILURE );
282 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
283 return( PSA_ERROR_INVALID_ARGUMENT );
284 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
287 return( PSA_ERROR_BUFFER_TOO_SMALL );
288 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
289 return( PSA_ERROR_INVALID_ARGUMENT );
290 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
293 return( PSA_ERROR_INVALID_ARGUMENT );
294 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
295 return( PSA_ERROR_INSUFFICIENT_MEMORY );
296
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100297 case MBEDTLS_ERR_PK_ALLOC_FAILED:
298 return( PSA_ERROR_INSUFFICIENT_MEMORY );
299 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
300 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100304 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
305 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
306 return( PSA_ERROR_INVALID_ARGUMENT );
307 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
310 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
311 return( PSA_ERROR_NOT_PERMITTED );
312 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_PK_INVALID_ALG:
315 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
316 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
317 return( PSA_ERROR_NOT_SUPPORTED );
318 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
319 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
321 return( PSA_ERROR_HARDWARE_FAILURE );
322
Gilles Peskineff2d2002019-05-06 15:26:23 +0200323 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
326 return( PSA_ERROR_NOT_SUPPORTED );
327
Gilles Peskinea5905292018-02-07 20:59:33 +0100328 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
332 return( PSA_ERROR_INVALID_ARGUMENT );
333 case MBEDTLS_ERR_RSA_INVALID_PADDING:
334 return( PSA_ERROR_INVALID_PADDING );
335 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
338 return( PSA_ERROR_INVALID_ARGUMENT );
339 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
340 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200341 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100342 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
343 return( PSA_ERROR_INVALID_SIGNATURE );
344 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_RSA_RNG_FAILED:
347 return( PSA_ERROR_INSUFFICIENT_MEMORY );
348 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
349 return( PSA_ERROR_NOT_SUPPORTED );
350 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
352
353 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
354 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
355 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
358 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
359 return( PSA_ERROR_INVALID_ARGUMENT );
360 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
361 return( PSA_ERROR_HARDWARE_FAILURE );
362
itayzafrir5c753392018-05-08 11:18:38 +0300363 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300364 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300365 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300366 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
367 return( PSA_ERROR_BUFFER_TOO_SMALL );
368 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
369 return( PSA_ERROR_NOT_SUPPORTED );
370 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
371 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
372 return( PSA_ERROR_INVALID_SIGNATURE );
373 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
374 return( PSA_ERROR_INSUFFICIENT_MEMORY );
375 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
376 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000377 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
378 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300379
Gilles Peskinee59236f2018-01-27 23:32:46 +0100380 default:
David Saadab4ecc272019-02-14 13:48:10 +0200381 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100382 }
383}
384
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200385
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200386
387
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100388/****************************************************************/
389/* Key management */
390/****************************************************************/
391
Gilles Peskine73167e12019-07-12 23:44:37 +0200392#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
393static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
394{
Gilles Peskine8e338702019-07-30 20:06:31 +0200395 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200396}
397#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
398
John Durkop07cc04a2020-11-16 22:08:34 -0800399/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
400 * current test driver in key_management.c is using this function
401 * when accelerators are used for ECC key pair and public key.
402 * Once that dependency is resolved these guards can be removed.
403 */
John Durkop9814fa22020-11-04 12:28:15 -0800404#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800405 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
406 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
407 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100408mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100409 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200410{
411 switch( curve )
412 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100413 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100414 switch( byte_length )
415 {
416 case PSA_BITS_TO_BYTES( 192 ):
417 return( MBEDTLS_ECP_DP_SECP192R1 );
418 case PSA_BITS_TO_BYTES( 224 ):
419 return( MBEDTLS_ECP_DP_SECP224R1 );
420 case PSA_BITS_TO_BYTES( 256 ):
421 return( MBEDTLS_ECP_DP_SECP256R1 );
422 case PSA_BITS_TO_BYTES( 384 ):
423 return( MBEDTLS_ECP_DP_SECP384R1 );
424 case PSA_BITS_TO_BYTES( 521 ):
425 return( MBEDTLS_ECP_DP_SECP521R1 );
426 default:
427 return( MBEDTLS_ECP_DP_NONE );
428 }
429 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100430
Paul Elliott8ff510a2020-06-02 17:19:28 +0100431 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100432 switch( byte_length )
433 {
434 case PSA_BITS_TO_BYTES( 256 ):
435 return( MBEDTLS_ECP_DP_BP256R1 );
436 case PSA_BITS_TO_BYTES( 384 ):
437 return( MBEDTLS_ECP_DP_BP384R1 );
438 case PSA_BITS_TO_BYTES( 512 ):
439 return( MBEDTLS_ECP_DP_BP512R1 );
440 default:
441 return( MBEDTLS_ECP_DP_NONE );
442 }
443 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100444
Paul Elliott8ff510a2020-06-02 17:19:28 +0100445 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100446 switch( byte_length )
447 {
448 case PSA_BITS_TO_BYTES( 255 ):
449 return( MBEDTLS_ECP_DP_CURVE25519 );
450 case PSA_BITS_TO_BYTES( 448 ):
451 return( MBEDTLS_ECP_DP_CURVE448 );
452 default:
453 return( MBEDTLS_ECP_DP_NONE );
454 }
455 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100456
Paul Elliott8ff510a2020-06-02 17:19:28 +0100457 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100458 switch( byte_length )
459 {
460 case PSA_BITS_TO_BYTES( 192 ):
461 return( MBEDTLS_ECP_DP_SECP192K1 );
462 case PSA_BITS_TO_BYTES( 224 ):
463 return( MBEDTLS_ECP_DP_SECP224K1 );
464 case PSA_BITS_TO_BYTES( 256 ):
465 return( MBEDTLS_ECP_DP_SECP256K1 );
466 default:
467 return( MBEDTLS_ECP_DP_NONE );
468 }
469 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100470
Gilles Peskine12313cd2018-06-20 00:20:32 +0200471 default:
472 return( MBEDTLS_ECP_DP_NONE );
473 }
474}
John Durkop9814fa22020-11-04 12:28:15 -0800475#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800476 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
477 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
478 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200479
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200480static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
481 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482{
483 /* Check that the bit size is acceptable for the key type */
484 switch( type )
485 {
486 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200488 case PSA_KEY_TYPE_DERIVE:
489 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200490#if defined(MBEDTLS_AES_C)
491 case PSA_KEY_TYPE_AES:
492 if( bits != 128 && bits != 192 && bits != 256 )
493 return( PSA_ERROR_INVALID_ARGUMENT );
494 break;
495#endif
496#if defined(MBEDTLS_CAMELLIA_C)
497 case PSA_KEY_TYPE_CAMELLIA:
498 if( bits != 128 && bits != 192 && bits != 256 )
499 return( PSA_ERROR_INVALID_ARGUMENT );
500 break;
501#endif
502#if defined(MBEDTLS_DES_C)
503 case PSA_KEY_TYPE_DES:
504 if( bits != 64 && bits != 128 && bits != 192 )
505 return( PSA_ERROR_INVALID_ARGUMENT );
506 break;
507#endif
508#if defined(MBEDTLS_ARC4_C)
509 case PSA_KEY_TYPE_ARC4:
510 if( bits < 8 || bits > 2048 )
511 return( PSA_ERROR_INVALID_ARGUMENT );
512 break;
513#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200514#if defined(MBEDTLS_CHACHA20_C)
515 case PSA_KEY_TYPE_CHACHA20:
516 if( bits != 256 )
517 return( PSA_ERROR_INVALID_ARGUMENT );
518 break;
519#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520 default:
521 return( PSA_ERROR_NOT_SUPPORTED );
522 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200523 if( bits % 8 != 0 )
524 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200525
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200526 return( PSA_SUCCESS );
527}
528
John Durkop0e005192020-10-31 22:06:54 -0700529#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
530 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
531 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800532 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
533 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
534 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200535
Gilles Peskine86a440b2018-11-12 18:39:40 +0100536/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
537 * that are not a multiple of 8) well. For example, there is only
538 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
539 * way to return the exact bit size of a key.
540 * To keep things simple, reject non-byte-aligned key sizes. */
541static psa_status_t psa_check_rsa_key_byte_aligned(
542 const mbedtls_rsa_context *rsa )
543{
544 mbedtls_mpi n;
545 psa_status_t status;
546 mbedtls_mpi_init( &n );
547 status = mbedtls_to_psa_error(
548 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
549 if( status == PSA_SUCCESS )
550 {
551 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
552 status = PSA_ERROR_NOT_SUPPORTED;
553 }
554 mbedtls_mpi_free( &n );
555 return( status );
556}
557
Steven Cooreman7f391872020-07-30 14:57:44 +0200558/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200559 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200560 * \param[in] type The type of key contained in \p data.
561 * \param[in] data The buffer from which to load the representation.
562 * \param[in] data_length The size in bytes of \p data.
563 * \param[out] p_rsa Returns a pointer to an RSA context on success.
564 * The caller is responsible for freeing both the
565 * contents of the context and the context itself
566 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200567 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200568static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
569 const uint8_t *data,
570 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200571 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200572{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000573 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200574 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200576 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000577
578 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200579 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200581 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000583 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200584 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000585 if( status != PSA_SUCCESS )
586 goto exit;
587
588 /* We have something that the pkparse module recognizes. If it is a
589 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200590 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200591 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000592 status = PSA_ERROR_INVALID_ARGUMENT;
593 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200594 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000595
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000596 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
597 * supports non-byte-aligned key sizes, but not well. For example,
598 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200599 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000600 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
601 {
602 status = PSA_ERROR_NOT_SUPPORTED;
603 goto exit;
604 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200605 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200606 if( status != PSA_SUCCESS )
607 goto exit;
608
Steven Cooremana2371e52020-07-28 14:30:39 +0200609 /* Copy out the pointer to the RSA context, and reset the PK context
610 * such that pk_free doesn't free the RSA context we just grabbed. */
611 *p_rsa = mbedtls_pk_rsa( ctx );
612 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000613
614exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200615 mbedtls_pk_free( &ctx );
616 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200617}
618
John Durkop6ba40d12020-11-10 08:50:04 -0800619#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
620 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
621 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
622 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
623 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
624 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
625
626#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
627 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
628
Steven Cooremana01795d2020-07-24 22:48:15 +0200629/** Export an RSA key to export representation
630 *
631 * \param[in] type The type of key (public/private) to export
632 * \param[in] rsa The internal RSA representation from which to export
633 * \param[out] data The buffer to export to
634 * \param[in] data_size The length of the buffer to export to
635 * \param[out] data_length The amount of bytes written to \p data
636 */
637static psa_status_t psa_export_rsa_key( psa_key_type_t type,
638 mbedtls_rsa_context *rsa,
639 uint8_t *data,
640 size_t data_size,
641 size_t *data_length )
642{
643#if defined(MBEDTLS_PK_WRITE_C)
644 int ret;
645 mbedtls_pk_context pk;
646 uint8_t *pos = data + data_size;
647
648 mbedtls_pk_init( &pk );
649 pk.pk_info = &mbedtls_rsa_info;
650 pk.pk_ctx = rsa;
651
652 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200653 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
654 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200655 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
656 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
657 else
658 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
659
660 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200661 {
662 /* Clean up in case pk_write failed halfway through. */
663 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200664 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200665 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200666
667 /* The mbedtls_pk_xxx functions write to the end of the buffer.
668 * Move the data to the beginning and erase remaining data
669 * at the original location. */
670 if( 2 * (size_t) ret <= data_size )
671 {
672 memcpy( data, data + data_size - ret, ret );
673 memset( data + data_size - ret, 0, ret );
674 }
675 else if( (size_t) ret < data_size )
676 {
677 memmove( data, data + data_size - ret, ret );
678 memset( data + ret, 0, data_size - ret );
679 }
680
681 *data_length = ret;
682 return( PSA_SUCCESS );
683#else
684 (void) type;
685 (void) rsa;
686 (void) data;
687 (void) data_size;
688 (void) data_length;
689 return( PSA_ERROR_NOT_SUPPORTED );
690#endif /* MBEDTLS_PK_WRITE_C */
691}
692
693/** Import an RSA key from import representation to a slot
694 *
695 * \param[in,out] slot The slot where to store the export representation to
696 * \param[in] data The buffer containing the import representation
697 * \param[in] data_length The amount of bytes in \p data
698 */
699static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
700 const uint8_t *data,
701 size_t data_length )
702{
703 psa_status_t status;
704 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200705 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200706
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200708 status = psa_load_rsa_representation( slot->attr.type,
709 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200710 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200711 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200712 if( status != PSA_SUCCESS )
713 goto exit;
714
715 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200716 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200717
Steven Cooreman75b74362020-07-28 14:30:13 +0200718 /* Re-export the data to PSA export format, such that we can store export
719 * representation in the key slot. Export representation in case of RSA is
720 * the smallest representation that's allowed as input, so a straight-up
721 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200722 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200723 if( output == NULL )
724 {
725 status = PSA_ERROR_INSUFFICIENT_MEMORY;
726 goto exit;
727 }
728
Steven Cooremana01795d2020-07-24 22:48:15 +0200729 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200730 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200731 output,
732 data_length,
733 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200734exit:
735 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200736 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200737 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200738
739 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000740 if( status != PSA_SUCCESS )
741 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200742 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000743 return( status );
744 }
745
Steven Cooremana01795d2020-07-24 22:48:15 +0200746 /* On success, store the allocated export-formatted key. */
747 slot->data.key.data = output;
748 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000749
750 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200751}
John Durkop6ba40d12020-11-10 08:50:04 -0800752
753#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800754 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200755
John Durkop9814fa22020-11-04 12:28:15 -0800756#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800757 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
758 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
759 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
760 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200761/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200762 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200763 * \param[in] type The type of key contained in \p data.
764 * \param[in] data The buffer from which to load the representation.
765 * \param[in] data_length The size in bytes of \p data.
766 * \param[out] p_ecp Returns a pointer to an ECP context on success.
767 * The caller is responsible for freeing both the
768 * contents of the context and the context itself
769 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200770 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200771static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
772 const uint8_t *data,
773 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200774 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100775{
776 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200777 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200778 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200779 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100780
Steven Cooreman4fed4552020-08-03 14:46:03 +0200781 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
782 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100783 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 /* A Weierstrass public key is represented as:
785 * - The byte 0x04;
786 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
787 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200788 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200789 */
790 if( ( data_length & 1 ) == 0 )
791 return( PSA_ERROR_INVALID_ARGUMENT );
792 curve_size = data_length / 2;
793
794 /* Montgomery public keys are represented in compressed format, meaning
795 * their curve_size is equal to the amount of input. */
796
797 /* Private keys are represented in uncompressed private random integer
798 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100799 }
800
Steven Cooreman6d839f02020-07-30 11:36:45 +0200801 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200802 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200803 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200804 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200805 mbedtls_ecp_keypair_init( ecp );
806
Gilles Peskine4cd32772019-12-02 20:49:42 +0100807 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200808 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
809 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100810 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200811 {
812 status = PSA_ERROR_INVALID_ARGUMENT;
813 goto exit;
814 }
815
Steven Cooremanacda8342020-07-24 23:09:52 +0200816 status = mbedtls_to_psa_error(
817 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
818 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200819 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200820
Steven Cooreman6d839f02020-07-30 11:36:45 +0200821 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200822 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200823 {
824 /* Load the public value. */
825 status = mbedtls_to_psa_error(
826 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200827 data,
828 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200829 if( status != PSA_SUCCESS )
830 goto exit;
831
832 /* Check that the point is on the curve. */
833 status = mbedtls_to_psa_error(
834 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
835 if( status != PSA_SUCCESS )
836 goto exit;
837 }
838 else
839 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200840 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200841 status = mbedtls_to_psa_error(
842 mbedtls_ecp_read_key( ecp->grp.id,
843 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200844 data,
845 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200846 if( status != PSA_SUCCESS )
847 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200848 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200849
850 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200851exit:
852 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200853 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200854 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200855 mbedtls_free( ecp );
856 }
857
Steven Cooreman29149862020-08-05 15:43:42 +0200858 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100859}
John Durkop6ba40d12020-11-10 08:50:04 -0800860#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
861 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
862 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
863 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
864 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000865
John Durkop6ba40d12020-11-10 08:50:04 -0800866#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
867 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200868/** Export an ECP key to export representation
869 *
870 * \param[in] type The type of key (public/private) to export
871 * \param[in] ecp The internal ECP representation from which to export
872 * \param[out] data The buffer to export to
873 * \param[in] data_size The length of the buffer to export to
874 * \param[out] data_length The amount of bytes written to \p data
875 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200876static psa_status_t psa_export_ecp_key( psa_key_type_t type,
877 mbedtls_ecp_keypair *ecp,
878 uint8_t *data,
879 size_t data_size,
880 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200881{
Steven Cooremanacda8342020-07-24 23:09:52 +0200882 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000883
Steven Cooremanacda8342020-07-24 23:09:52 +0200884 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200885 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 /* Check whether the public part is loaded */
887 if( mbedtls_ecp_is_zero( &ecp->Q ) )
888 {
889 /* Calculate the public key */
890 status = mbedtls_to_psa_error(
891 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +0100892 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200894 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200895 }
896
Steven Cooreman4fed4552020-08-03 14:46:03 +0200897 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200898 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
899 MBEDTLS_ECP_PF_UNCOMPRESSED,
900 data_length,
901 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200902 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200903 if( status != PSA_SUCCESS )
904 memset( data, 0, data_size );
905
Steven Cooreman29149862020-08-05 15:43:42 +0200906 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200907 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200908 else
909 {
Steven Cooreman29149862020-08-05 15:43:42 +0200910 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200911 return( PSA_ERROR_BUFFER_TOO_SMALL );
912
913 status = mbedtls_to_psa_error(
914 mbedtls_ecp_write_key( ecp,
915 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200916 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200917 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200918 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200919 else
920 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200921
922 return( status );
923 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200924}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200925
Steven Cooreman4fed4552020-08-03 14:46:03 +0200926/** Import an ECP key from import representation to a slot
927 *
928 * \param[in,out] slot The slot where to store the export representation to
929 * \param[in] data The buffer containing the import representation
930 * \param[in] data_length The amount of bytes in \p data
931 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200932static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
933 const uint8_t *data,
934 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100935{
Steven Cooremanacda8342020-07-24 23:09:52 +0200936 psa_status_t status;
937 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200938 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100939
Steven Cooremanacda8342020-07-24 23:09:52 +0200940 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200941 status = psa_load_ecp_representation( slot->attr.type,
942 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200943 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200944 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100945 if( status != PSA_SUCCESS )
946 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100947
Steven Cooremanacda8342020-07-24 23:09:52 +0200948 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200949 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200950 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200951 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200952
Steven Cooremanacda8342020-07-24 23:09:52 +0200953 /* Re-export the data to PSA export format. There is currently no support
954 * for other input formats then the export format, so this is a 1-1
955 * copy operation. */
956 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200957 if( output == NULL )
958 {
959 status = PSA_ERROR_INSUFFICIENT_MEMORY;
960 goto exit;
961 }
962
963 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200964 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200965 output,
966 data_length,
967 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100968exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200969 /* Always free the PK object (will also free contained ECP context) */
970 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200971 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200972
973 /* Free the allocated buffer only on error. */
974 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100975 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200976 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200977 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100978 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200979
980 /* On success, store the allocated export-formatted key. */
981 slot->data.key.data = output;
982 slot->data.key.bytes = data_length;
983
984 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100985}
John Durkop9814fa22020-11-04 12:28:15 -0800986#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
987 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100988
Gilles Peskineb46bef22019-07-30 21:32:04 +0200989/** Return the size of the key in the given slot, in bits.
990 *
991 * \param[in] slot A key slot.
992 *
993 * \return The key size in bits, read from the metadata in the slot.
994 */
995static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
996{
997 return( slot->attr.bits );
998}
999
Steven Cooreman75b74362020-07-28 14:30:13 +02001000/** Try to allocate a buffer to an empty key slot.
1001 *
1002 * \param[in,out] slot Key slot to attach buffer to.
1003 * \param[in] buffer_length Requested size of the buffer.
1004 *
1005 * \retval #PSA_SUCCESS
1006 * The buffer has been successfully allocated.
1007 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1008 * Not enough memory was available for allocation.
1009 * \retval #PSA_ERROR_ALREADY_EXISTS
1010 * Trying to allocate a buffer to a non-empty key slot.
1011 */
1012static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1013 size_t buffer_length )
1014{
1015 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001016 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001017
1018 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1019 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001020 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001021
1022 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001023 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001024}
1025
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001026psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1027 const uint8_t* data,
1028 size_t data_length )
1029{
1030 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1031 data_length );
1032 if( status != PSA_SUCCESS )
1033 return( status );
1034
1035 memcpy( slot->data.key.data, data, data_length );
1036 return( PSA_SUCCESS );
1037}
1038
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001039/** Import key data into a slot.
1040 *
1041 * `slot->type` must have been set previously.
1042 * This function assumes that the slot does not contain any key material yet.
1043 * On failure, the slot content is unchanged.
1044 *
1045 * Persistent storage is not affected.
1046 *
1047 * \param[in,out] slot The key slot to import data into.
1048 * Its `type` field must have previously been set to
1049 * the desired key type.
1050 * It must not contain any key material yet.
1051 * \param[in] data Buffer containing the key material to parse and import.
1052 * \param data_length Size of \p data in bytes.
1053 *
1054 * \retval #PSA_SUCCESS
1055 * \retval #PSA_ERROR_INVALID_ARGUMENT
1056 * \retval #PSA_ERROR_NOT_SUPPORTED
1057 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1058 */
1059static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1060 const uint8_t *data,
1061 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001063 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001064 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001065
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001066 /* zero-length keys are never supported. */
1067 if( data_length == 0 )
1068 return( PSA_ERROR_NOT_SUPPORTED );
1069
Gilles Peskine8e338702019-07-30 20:06:31 +02001070 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001071 {
Steven Cooreman04524762020-10-13 17:43:44 +02001072 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001073
Steven Cooreman75b74362020-07-28 14:30:13 +02001074 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1075 if( data_length > SIZE_MAX / 8 )
1076 return( PSA_ERROR_NOT_SUPPORTED );
1077
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001078 /* Enforce a size limit, and in particular ensure that the bit
1079 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001080 if( bit_size > PSA_MAX_KEY_BITS )
1081 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001082
1083 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001084 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001085 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001086
1087 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001088 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001089 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001090 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001091
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001092 /* Write the actual key size to the slot.
1093 * psa_start_key_creation() wrote the size declared by the
1094 * caller, which may be 0 (meaning unspecified) or wrong. */
1095 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001096
1097 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001098 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001099 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001100 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001101 /* Try validation through accelerators first. */
1102 bit_size = slot->attr.bits;
1103 psa_key_attributes_t attributes = {
1104 .core = slot->attr
1105 };
1106 status = psa_driver_wrapper_validate_key( &attributes,
1107 data,
1108 data_length,
1109 &bit_size );
1110 if( status == PSA_SUCCESS )
1111 {
1112 /* Key has been validated successfully by an accelerator.
1113 * Copy key material into slot. */
1114 status = psa_copy_key_material_into_slot( slot, data, data_length );
1115 if( status != PSA_SUCCESS )
1116 return( status );
1117
1118 slot->attr.bits = (psa_key_bits_t) bit_size;
1119 return( PSA_SUCCESS );
1120 }
1121 else if( status != PSA_ERROR_NOT_SUPPORTED )
1122 return( status );
1123
1124 /* Key format is not supported by any accelerator, try software fallback
1125 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001126#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1127 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001128 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1129 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001130 return( psa_import_ecp_key( slot, data, data_length ) );
1131 }
John Durkop9814fa22020-11-04 12:28:15 -08001132#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1133 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001134#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1135 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001136 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001137 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001138 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001139 }
John Durkop9814fa22020-11-04 12:28:15 -08001140#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1141 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001142
1143 /* Fell through the fallback as well, so have nothing else to try. */
1144 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001145 }
1146 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001147 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001148 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001149 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001150 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001151}
1152
Gilles Peskinef603c712019-01-19 13:40:11 +01001153/** Calculate the intersection of two algorithm usage policies.
1154 *
1155 * Return 0 (which allows no operation) on incompatibility.
1156 */
1157static psa_algorithm_t psa_key_policy_algorithm_intersection(
1158 psa_algorithm_t alg1,
1159 psa_algorithm_t alg2 )
1160{
Gilles Peskine549ea862019-05-22 11:45:59 +02001161 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001162 if( alg1 == alg2 )
1163 return( alg1 );
1164 /* If the policies are from the same hash-and-sign family, check
1165 * if one is a wildcard. If so the other has the specific algorithm. */
1166 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1167 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1168 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1169 {
1170 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1171 return( alg2 );
1172 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1173 return( alg1 );
1174 }
1175 /* If the policies are incompatible, allow nothing. */
1176 return( 0 );
1177}
1178
Gilles Peskined6f371b2019-05-10 19:33:38 +02001179static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1180 psa_algorithm_t requested_alg )
1181{
Gilles Peskine549ea862019-05-22 11:45:59 +02001182 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001183 if( requested_alg == policy_alg )
1184 return( 1 );
1185 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001186 * and requested_alg is the same hash-and-sign family with any hash,
1187 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001188 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1189 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1190 {
1191 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1192 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1193 }
Steven Cooremance48e852020-10-05 16:02:45 +02001194 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001195 * a key derivation with that key agreement should also be allowed. This
1196 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001197 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1198 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1199 {
1200 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1201 policy_alg );
1202 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001203 /* If it isn't permitted, it's forbidden. */
1204 return( 0 );
1205}
1206
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001207/** Test whether a policy permits an algorithm.
1208 *
1209 * The caller must test usage flags separately.
1210 */
1211static int psa_key_policy_permits( const psa_key_policy_t *policy,
1212 psa_algorithm_t alg )
1213{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001214 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1215 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001216}
1217
Gilles Peskinef603c712019-01-19 13:40:11 +01001218/** Restrict a key policy based on a constraint.
1219 *
1220 * \param[in,out] policy The policy to restrict.
1221 * \param[in] constraint The policy constraint to apply.
1222 *
1223 * \retval #PSA_SUCCESS
1224 * \c *policy contains the intersection of the original value of
1225 * \c *policy and \c *constraint.
1226 * \retval #PSA_ERROR_INVALID_ARGUMENT
1227 * \c *policy and \c *constraint are incompatible.
1228 * \c *policy is unchanged.
1229 */
1230static psa_status_t psa_restrict_key_policy(
1231 psa_key_policy_t *policy,
1232 const psa_key_policy_t *constraint )
1233{
1234 psa_algorithm_t intersection_alg =
1235 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001236 psa_algorithm_t intersection_alg2 =
1237 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001238 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1239 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001240 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1241 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001242 policy->usage &= constraint->usage;
1243 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001244 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001245 return( PSA_SUCCESS );
1246}
1247
Ronald Cron5c522922020-11-14 16:35:34 +01001248/** Get the description of a key given its identifier and policy constraints
1249 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001250 *
Ronald Cron5c522922020-11-14 16:35:34 +01001251 * The key must have allow all the usage flags set in \p usage. If \p alg is
1252 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001253 *
Ronald Cron5c522922020-11-14 16:35:34 +01001254 * In case of a persistent key, the function loads the description of the key
1255 * into a key slot if not already done.
1256 *
1257 * On success, the returned key slot is locked. It is the responsibility of
1258 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001259 */
Ronald Cron5c522922020-11-14 16:35:34 +01001260static psa_status_t psa_get_and_lock_key_slot_with_policy(
1261 mbedtls_svc_key_id_t key,
1262 psa_key_slot_t **p_slot,
1263 psa_key_usage_t usage,
1264 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001265{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001266 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1267 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001268
Ronald Cron5c522922020-11-14 16:35:34 +01001269 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001270 if( status != PSA_SUCCESS )
1271 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001272 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001273
1274 /* Enforce that usage policy for the key slot contains all the flags
1275 * required by the usage parameter. There is one exception: public
1276 * keys can always be exported, so we treat public key objects as
1277 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001278 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001279 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001280
1281 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001282 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001283 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001284
1285 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001287 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001288
Darryl Green06fd18d2018-07-16 11:21:11 +01001289 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001290
1291error:
1292 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001293 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001294
1295 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001296}
Darryl Green940d72c2018-07-13 13:18:51 +01001297
Ronald Cron5c522922020-11-14 16:35:34 +01001298/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001299 *
1300 * A transparent key is a key for which the key material is directly
1301 * available, as opposed to a key in a secure element.
1302 *
Ronald Cron5c522922020-11-14 16:35:34 +01001303 * This is a temporary function to use instead of
1304 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1305 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001306 *
Ronald Cron5c522922020-11-14 16:35:34 +01001307 * On success, the returned key slot is locked. It is the responsibility of the
1308 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001309 */
1310#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001311static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1312 mbedtls_svc_key_id_t key,
1313 psa_key_slot_t **p_slot,
1314 psa_key_usage_t usage,
1315 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001316{
Ronald Cron5c522922020-11-14 16:35:34 +01001317 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1318 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001319 if( status != PSA_SUCCESS )
1320 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001321
Gilles Peskineadad8132019-07-25 11:31:23 +02001322 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001323 {
Ronald Cron5c522922020-11-14 16:35:34 +01001324 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001325 *p_slot = NULL;
1326 return( PSA_ERROR_NOT_SUPPORTED );
1327 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001328
Gilles Peskine28f8f302019-07-24 13:30:31 +02001329 return( PSA_SUCCESS );
1330}
1331#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1332/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001333#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1334 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001335#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1336
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001337/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001338static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001339{
Gilles Peskine73167e12019-07-12 23:44:37 +02001340#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Fredrik Strupe462aa572020-12-17 10:44:38 +01001341 if( psa_get_se_driver( slot->attr.lifetime, NULL, NULL ) &&
1342 psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001343 {
1344 /* No key material to clean. */
1345 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001346 else
1347#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001348 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001349 /* Data pointer will always be either a valid pointer or NULL in an
1350 * initialized slot, so we can just free it. */
1351 if( slot->data.key.data != NULL )
1352 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001353 mbedtls_free( slot->data.key.data );
1354 slot->data.key.data = NULL;
1355 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001356 }
Darryl Green40225ba2018-11-15 14:48:15 +00001357
1358 return( PSA_SUCCESS );
1359}
1360
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001361/** Completely wipe a slot in memory, including its policy.
1362 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001363psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001364{
1365 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001366
1367 /*
1368 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001369 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001370 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1371 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001372 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001373 */
Ronald Cron5c522922020-11-14 16:35:34 +01001374 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001375 {
1376#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001377 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001378#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001379 status = PSA_ERROR_CORRUPTION_DETECTED;
1380 }
1381
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001382 /* Multipart operations may still be using the key. This is safe
1383 * because all multipart operation objects are independent from
1384 * the key slot: if they need to access the key after the setup
1385 * phase, they have a copy of the key. Note that this means that
1386 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001387 /* At this point, key material and other type-specific content has
1388 * been wiped. Clear remaining metadata. We can call memset and not
1389 * zeroize because the metadata is not particularly sensitive. */
1390 memset( slot, 0, sizeof( *slot ) );
1391 return( status );
1392}
1393
Ronald Croncf56a0a2020-08-04 09:51:30 +02001394psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001395{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001396 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001397 psa_status_t status; /* status of the last operation */
1398 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001399#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1400 psa_se_drv_table_entry_t *driver;
1401#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001402
Ronald Croncf56a0a2020-08-04 09:51:30 +02001403 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001404 return( PSA_SUCCESS );
1405
Ronald Cronf2911112020-10-29 17:51:10 +01001406 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001407 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001408 * key, this will load the key description from persistent memory if not
1409 * done yet. We cannot avoid this loading as without it we don't know if
1410 * the key is operated by an SE or not and this information is needed by
1411 * the current implementation.
1412 */
Ronald Cron5c522922020-11-14 16:35:34 +01001413 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001414 if( status != PSA_SUCCESS )
1415 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001416
Ronald Cronf2911112020-10-29 17:51:10 +01001417 /*
1418 * If the key slot containing the key description is under access by the
1419 * library (apart from the present access), the key cannot be destroyed
1420 * yet. For the time being, just return in error. Eventually (to be
1421 * implemented), the key should be destroyed when all accesses have
1422 * stopped.
1423 */
Ronald Cron5c522922020-11-14 16:35:34 +01001424 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001425 {
Ronald Cron5c522922020-11-14 16:35:34 +01001426 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001427 return( PSA_ERROR_GENERIC_ERROR );
1428 }
1429
Gilles Peskine354f7672019-07-12 23:46:38 +02001430#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001431 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001432 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001433 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001434 /* For a key in a secure element, we need to do three things:
1435 * remove the key file in internal storage, destroy the
1436 * key inside the secure element, and update the driver's
1437 * persistent data. Start a transaction that will encompass these
1438 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001439 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001440 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001441 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001442 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001443 status = psa_crypto_save_transaction( );
1444 if( status != PSA_SUCCESS )
1445 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001446 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001447 /* We should still try to destroy the key in the secure
1448 * element and the key metadata in storage. This is especially
1449 * important if the error is that the storage is full.
1450 * But how to do it exactly without risking an inconsistent
1451 * state after a reset?
1452 * https://github.com/ARMmbed/mbed-crypto/issues/215
1453 */
1454 overall_status = status;
1455 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001456 }
1457
Gilles Peskine354f7672019-07-12 23:46:38 +02001458 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001459 if( overall_status == PSA_SUCCESS )
1460 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001461 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001462#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1463
Darryl Greend49a4992018-06-18 17:27:26 +01001464#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001465 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001466 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001467 status = psa_destroy_persistent_key( slot->attr.id );
1468 if( overall_status == PSA_SUCCESS )
1469 overall_status = status;
1470
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001471 /* TODO: other slots may have a copy of the same key. We should
1472 * invalidate them.
1473 * https://github.com/ARMmbed/mbed-crypto/issues/214
1474 */
Darryl Greend49a4992018-06-18 17:27:26 +01001475 }
1476#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001477
Gilles Peskinefc762652019-07-22 19:30:34 +02001478#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1479 if( driver != NULL )
1480 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001481 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001482 if( overall_status == PSA_SUCCESS )
1483 overall_status = status;
1484 status = psa_crypto_stop_transaction( );
1485 if( overall_status == PSA_SUCCESS )
1486 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001487 }
1488#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1489
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001490#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1491exit:
1492#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001493 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001494 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1495 if( overall_status == PSA_SUCCESS )
1496 overall_status = status;
1497 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001498}
1499
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001500void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001501{
Gilles Peskineb699f072019-04-26 16:06:02 +02001502 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001503 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001504}
1505
Gilles Peskineb699f072019-04-26 16:06:02 +02001506psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1507 psa_key_type_t type,
1508 const uint8_t *data,
1509 size_t data_length )
1510{
1511 uint8_t *copy = NULL;
1512
1513 if( data_length != 0 )
1514 {
1515 copy = mbedtls_calloc( 1, data_length );
1516 if( copy == NULL )
1517 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1518 memcpy( copy, data, data_length );
1519 }
1520 /* After this point, this function is guaranteed to succeed, so it
1521 * can start modifying `*attributes`. */
1522
1523 if( attributes->domain_parameters != NULL )
1524 {
1525 mbedtls_free( attributes->domain_parameters );
1526 attributes->domain_parameters = NULL;
1527 attributes->domain_parameters_size = 0;
1528 }
1529
1530 attributes->domain_parameters = copy;
1531 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001532 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001533 return( PSA_SUCCESS );
1534}
1535
1536psa_status_t psa_get_key_domain_parameters(
1537 const psa_key_attributes_t *attributes,
1538 uint8_t *data, size_t data_size, size_t *data_length )
1539{
1540 if( attributes->domain_parameters_size > data_size )
1541 return( PSA_ERROR_BUFFER_TOO_SMALL );
1542 *data_length = attributes->domain_parameters_size;
1543 if( attributes->domain_parameters_size != 0 )
1544 memcpy( data, attributes->domain_parameters,
1545 attributes->domain_parameters_size );
1546 return( PSA_SUCCESS );
1547}
1548
John Durkop07cc04a2020-11-16 22:08:34 -08001549#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001550 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001551static psa_status_t psa_get_rsa_public_exponent(
1552 const mbedtls_rsa_context *rsa,
1553 psa_key_attributes_t *attributes )
1554{
1555 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001556 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001557 uint8_t *buffer = NULL;
1558 size_t buflen;
1559 mbedtls_mpi_init( &mpi );
1560
1561 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1562 if( ret != 0 )
1563 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001564 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1565 {
1566 /* It's the default value, which is reported as an empty string,
1567 * so there's nothing to do. */
1568 goto exit;
1569 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001570
1571 buflen = mbedtls_mpi_size( &mpi );
1572 buffer = mbedtls_calloc( 1, buflen );
1573 if( buffer == NULL )
1574 {
1575 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1576 goto exit;
1577 }
1578 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1579 if( ret != 0 )
1580 goto exit;
1581 attributes->domain_parameters = buffer;
1582 attributes->domain_parameters_size = buflen;
1583
1584exit:
1585 mbedtls_mpi_free( &mpi );
1586 if( ret != 0 )
1587 mbedtls_free( buffer );
1588 return( mbedtls_to_psa_error( ret ) );
1589}
John Durkop07cc04a2020-11-16 22:08:34 -08001590#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001591 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001592
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001593/** Retrieve all the publicly-accessible attributes of a key.
1594 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001595psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001596 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001597{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001598 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001599 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001600 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001601
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001602 psa_reset_key_attributes( attributes );
1603
Ronald Cron5c522922020-11-14 16:35:34 +01001604 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001605 if( status != PSA_SUCCESS )
1606 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001607
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001608 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001609 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1610 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1611
1612#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1613 if( psa_key_slot_is_external( slot ) )
1614 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1615#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001616
Gilles Peskine8e338702019-07-30 20:06:31 +02001617 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001618 {
John Durkop07cc04a2020-11-16 22:08:34 -08001619#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001620 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001621 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001622 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001623#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001624 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001625 * is not yet implemented.
1626 * https://github.com/ARMmbed/mbed-crypto/issues/216
1627 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001628 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001629 break;
1630#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001631 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001632 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001633
Steven Cooreman4fed4552020-08-03 14:46:03 +02001634 status = psa_load_rsa_representation( slot->attr.type,
1635 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001636 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001637 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001638 if( status != PSA_SUCCESS )
1639 break;
1640
Steven Cooremana2371e52020-07-28 14:30:39 +02001641 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001642 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001643 mbedtls_rsa_free( rsa );
1644 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001645 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001646 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001647#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001648 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001649 default:
1650 /* Nothing else to do. */
1651 break;
1652 }
1653
1654 if( status != PSA_SUCCESS )
1655 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001656
Ronald Cron5c522922020-11-14 16:35:34 +01001657 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001658
Ronald Cron5c522922020-11-14 16:35:34 +01001659 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001660}
1661
Gilles Peskinec8000c02019-08-02 20:15:51 +02001662#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1663psa_status_t psa_get_key_slot_number(
1664 const psa_key_attributes_t *attributes,
1665 psa_key_slot_number_t *slot_number )
1666{
1667 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1668 {
1669 *slot_number = attributes->slot_number;
1670 return( PSA_SUCCESS );
1671 }
1672 else
1673 return( PSA_ERROR_INVALID_ARGUMENT );
1674}
1675#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1676
Steven Cooremana01795d2020-07-24 22:48:15 +02001677static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1678 uint8_t *data,
1679 size_t data_size,
1680 size_t *data_length )
1681{
1682 if( slot->data.key.bytes > data_size )
1683 return( PSA_ERROR_BUFFER_TOO_SMALL );
1684 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1685 memset( data + slot->data.key.bytes, 0,
1686 data_size - slot->data.key.bytes );
1687 *data_length = slot->data.key.bytes;
1688 return( PSA_SUCCESS );
1689}
1690
Gilles Peskinef603c712019-01-19 13:40:11 +01001691static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1692 uint8_t *data,
1693 size_t data_size,
1694 size_t *data_length,
1695 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001696{
Gilles Peskine5d309672019-07-12 23:47:28 +02001697#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1698 const psa_drv_se_t *drv;
1699 psa_drv_se_context_t *drv_context;
1700#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1701
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001702 *data_length = 0;
1703
Gilles Peskine8e338702019-07-30 20:06:31 +02001704 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001705 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001706
Gilles Peskinef9168942019-09-12 19:20:29 +02001707 /* Reject a zero-length output buffer now, since this can never be a
1708 * valid key representation. This way we know that data must be a valid
1709 * pointer and we can do things like memset(data, ..., data_size). */
1710 if( data_size == 0 )
1711 return( PSA_ERROR_BUFFER_TOO_SMALL );
1712
Gilles Peskine5d309672019-07-12 23:47:28 +02001713#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001714 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001715 {
1716 psa_drv_se_export_key_t method;
1717 if( drv->key_management == NULL )
1718 return( PSA_ERROR_NOT_SUPPORTED );
1719 method = ( export_public_key ?
1720 drv->key_management->p_export_public :
1721 drv->key_management->p_export );
1722 if( method == NULL )
1723 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001724 return( method( drv_context,
1725 slot->data.se.slot_number,
1726 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001727 }
1728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1729
Gilles Peskine8e338702019-07-30 20:06:31 +02001730 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001731 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001732 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001733 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001734 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1735 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001736 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001737 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001738 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001739 /* Exporting public -> public */
1740 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1741 }
1742 else if( !export_public_key )
1743 {
1744 /* Exporting private -> private */
1745 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1746 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001747
Steven Cooreman560c28a2020-07-24 23:20:24 +02001748 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001749 * so conversion is needed. Try the accelerators first. */
1750 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1751 data,
1752 data_size,
1753 data_length );
1754
1755 if( status != PSA_ERROR_NOT_SUPPORTED ||
1756 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1757 return( status );
1758
Steven Cooreman560c28a2020-07-24 23:20:24 +02001759 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1760 {
John Durkop0e005192020-10-31 22:06:54 -07001761#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1762 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001763 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001764 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001765 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001766 slot->data.key.data,
1767 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001768 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001769 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001770 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001771
Steven Cooreman560c28a2020-07-24 23:20:24 +02001772 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001773 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001774 data,
1775 data_size,
1776 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001777
Steven Cooremana2371e52020-07-28 14:30:39 +02001778 mbedtls_rsa_free( rsa );
1779 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001780
Steven Cooreman560c28a2020-07-24 23:20:24 +02001781 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001782#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001783 /* We don't know how to convert a private RSA key to public. */
1784 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001785#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1786 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001787 }
1788 else
Moran Pekera998bc62018-04-16 18:16:20 +03001789 {
John Durkop6ba40d12020-11-10 08:50:04 -08001790#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1791 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001792 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001793 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001794 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001795 slot->data.key.data,
1796 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001797 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001798 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001799 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001800
1801 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1802 PSA_KEY_TYPE_ECC_GET_FAMILY(
1803 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001804 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001805 data,
1806 data_size,
1807 data_length );
1808
Steven Cooremana2371e52020-07-28 14:30:39 +02001809 mbedtls_ecp_keypair_free( ecp );
1810 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001811 return( status );
1812#else
1813 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001814 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001815#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1816 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001817 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001818 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001819 else
1820 {
1821 /* This shouldn't happen in the reference implementation, but
1822 it is valid for a special-purpose implementation to omit
1823 support for exporting certain key types. */
1824 return( PSA_ERROR_NOT_SUPPORTED );
1825 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001826}
1827
Ronald Croncf56a0a2020-08-04 09:51:30 +02001828psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001829 uint8_t *data,
1830 size_t data_size,
1831 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001832{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001834 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001835 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001836
1837 /* Set the key to empty now, so that even when there are errors, we always
1838 * set data_length to a value between 0 and data_size. On error, setting
1839 * the key to empty is a good choice because an empty key representation is
1840 * unlikely to be accepted anywhere. */
1841 *data_length = 0;
1842
1843 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001844 * which don't require any flag, but
1845 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1846 */
1847 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1848 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001849 if( status != PSA_SUCCESS )
1850 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001851
1852 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001853 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001854
Ronald Cron5c522922020-11-14 16:35:34 +01001855 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001856}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001857
Ronald Croncf56a0a2020-08-04 09:51:30 +02001858psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001859 uint8_t *data,
1860 size_t data_size,
1861 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001862{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001864 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001865 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001866
1867 /* Set the key to empty now, so that even when there are errors, we always
1868 * set data_length to a value between 0 and data_size. On error, setting
1869 * the key to empty is a good choice because an empty key representation is
1870 * unlikely to be accepted anywhere. */
1871 *data_length = 0;
1872
1873 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001874 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001875 if( status != PSA_SUCCESS )
1876 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001877
1878 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001879 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001880
Ronald Cron5c522922020-11-14 16:35:34 +01001881 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001882}
1883
Gilles Peskine91e8c332019-08-02 19:19:39 +02001884#if defined(static_assert)
1885static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1886 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001887static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001888 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001889static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001890 "One or more key attribute flag is listed as both internal-only and external-only" );
1891#endif
1892
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001893/** Validate that a key policy is internally well-formed.
1894 *
1895 * This function only rejects invalid policies. It does not validate the
1896 * consistency of the policy with respect to other attributes of the key
1897 * such as the key type.
1898 */
1899static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001900{
1901 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001902 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 PSA_KEY_USAGE_ENCRYPT |
1904 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001905 PSA_KEY_USAGE_SIGN_HASH |
1906 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001907 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1908 return( PSA_ERROR_INVALID_ARGUMENT );
1909
Gilles Peskine4747d192019-04-17 15:05:45 +02001910 return( PSA_SUCCESS );
1911}
1912
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001913/** Validate the internal consistency of key attributes.
1914 *
1915 * This function only rejects invalid attribute values. If does not
1916 * validate the consistency of the attributes with any key data that may
1917 * be involved in the creation of the key.
1918 *
1919 * Call this function early in the key creation process.
1920 *
1921 * \param[in] attributes Key attributes for the new key.
1922 * \param[out] p_drv On any return, the driver for the key, if any.
1923 * NULL for a transparent key.
1924 *
1925 */
1926static psa_status_t psa_validate_key_attributes(
1927 const psa_key_attributes_t *attributes,
1928 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001929{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001930 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001931 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001932 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001933
Ronald Cron54b90082020-10-29 15:26:43 +01001934 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001935 if( status != PSA_SUCCESS )
1936 return( status );
1937
Ronald Crond2ed4812020-07-17 16:11:30 +02001938 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001939 if( status != PSA_SUCCESS )
1940 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001941
Ronald Cron65f38a32020-10-23 17:11:13 +02001942 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1943 {
1944 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1945 return( PSA_ERROR_INVALID_ARGUMENT );
1946 }
1947 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001948 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001949 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001950 if( status != PSA_SUCCESS )
1951 return( status );
1952 }
1953
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001954 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001955 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001956 return( status );
1957
1958 /* Refuse to create overly large keys.
1959 * Note that this doesn't trigger on import if the attributes don't
1960 * explicitly specify a size (so psa_get_key_bits returns 0), so
1961 * psa_import_key() needs its own checks. */
1962 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1963 return( PSA_ERROR_NOT_SUPPORTED );
1964
Gilles Peskine91e8c332019-08-02 19:19:39 +02001965 /* Reject invalid flags. These should not be reachable through the API. */
1966 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1967 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1968 return( PSA_ERROR_INVALID_ARGUMENT );
1969
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001970 return( PSA_SUCCESS );
1971}
1972
Gilles Peskine4747d192019-04-17 15:05:45 +02001973/** Prepare a key slot to receive key material.
1974 *
1975 * This function allocates a key slot and sets its metadata.
1976 *
1977 * If this function fails, call psa_fail_key_creation().
1978 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001979 * This function is intended to be used as follows:
1980 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001981 * it with the specified attributes, and in case of a volatile key assign it
1982 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001983 * -# Populate the slot with the key material.
1984 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1985 * In case of failure at any step, stop the sequence and call
1986 * psa_fail_key_creation().
1987 *
Ronald Cron5c522922020-11-14 16:35:34 +01001988 * On success, the key slot is locked. It is the responsibility of the caller
1989 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001990 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001991 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001993 * \param[out] p_slot On success, a pointer to the prepared slot.
1994 * \param[out] p_drv On any return, the driver for the key, if any.
1995 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001996 *
1997 * \retval #PSA_SUCCESS
1998 * The key slot is ready to receive key material.
1999 * \return If this function fails, the key slot is an invalid state.
2000 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002001 */
2002static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02002003 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02002004 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02002005 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002006 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002007{
2008 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002009 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 psa_key_slot_t *slot;
2011
Gilles Peskinedf179142019-07-15 22:02:14 +02002012 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 *p_drv = NULL;
2014
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002015 status = psa_validate_key_attributes( attributes, p_drv );
2016 if( status != PSA_SUCCESS )
2017 return( status );
2018
Ronald Cronc4d1b512020-07-31 11:26:37 +02002019 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002020 if( status != PSA_SUCCESS )
2021 return( status );
2022 slot = *p_slot;
2023
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002024 /* We're storing the declared bit-size of the key. It's up to each
2025 * creation mechanism to verify that this information is correct.
2026 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002027 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002028 * is optional (import, copy). In case of a volatile key, assign it the
2029 * volatile key identifier associated to the slot returned to contain its
2030 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002031
2032 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002033 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2034 {
2035#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2036 slot->attr.id = volatile_key_id;
2037#else
2038 slot->attr.id.key_id = volatile_key_id;
2039#endif
2040 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002041
Gilles Peskine91e8c332019-08-02 19:19:39 +02002042 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002043 * external-only flags, query `attributes`. Thanks to the check
2044 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002045 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002046 * may have set. */
2047 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002048
Gilles Peskinecbaff462019-07-12 23:46:04 +02002049#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002050 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002051 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002052 * create the key file in internal storage, create the
2053 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002054 * persistent data. This is done by starting a transaction that will
2055 * encompass these three actions.
2056 * For registering a volatile key, we just need to find an appropriate
2057 * slot number inside the SE. Since the key is designated volatile, creating
2058 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002059 /* The first thing to do is to find a slot number for the new key.
2060 * We save the slot number in persistent storage as part of the
2061 * transaction data. It will be needed to recover if the power
2062 * fails during the key creation process, to clean up on the secure
2063 * element side after restarting. Obtaining a slot number from the
2064 * secure element driver updates its persistent state, but we do not yet
2065 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002066 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002067 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002068 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002069 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002070 &slot->data.se.slot_number );
2071 if( status != PSA_SUCCESS )
2072 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002073
2074 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002075 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002076 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2077 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2078 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2079 psa_crypto_transaction.key.id = slot->attr.id;
2080 status = psa_crypto_save_transaction( );
2081 if( status != PSA_SUCCESS )
2082 {
2083 (void) psa_crypto_stop_transaction( );
2084 return( status );
2085 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002086 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002087 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002088
2089 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2090 {
2091 /* Key registration only makes sense with a secure element. */
2092 return( PSA_ERROR_INVALID_ARGUMENT );
2093 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002094#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2095
Ronald Cronc4d1b512020-07-31 11:26:37 +02002096 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002097}
Gilles Peskine4747d192019-04-17 15:05:45 +02002098
2099/** Finalize the creation of a key once its key material has been set.
2100 *
2101 * This entails writing the key to persistent storage.
2102 *
2103 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002104 * See the documentation of psa_start_key_creation() for the intended use
2105 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002106 *
Ronald Cron5c522922020-11-14 16:35:34 +01002107 * If the finalization succeeds, the function unlocks the key slot (it was
2108 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2109 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002110 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002111 * \param[in,out] slot Pointer to the slot with key material.
2112 * \param[in] driver The secure element driver for the key,
2113 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002114 * \param[out] key On success, identifier of the key. Note that the
2115 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002116 *
2117 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002118 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002119 * \return If this function fails, the key slot is an invalid state.
2120 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002121 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002122static psa_status_t psa_finish_key_creation(
2123 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002124 psa_se_drv_table_entry_t *driver,
2125 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002126{
2127 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002128 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002129 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002130
2131#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002132 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002133 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002134#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2135 if( driver != NULL )
2136 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002137 psa_se_key_data_storage_t data;
2138#if defined(static_assert)
2139 static_assert( sizeof( slot->data.se.slot_number ) ==
2140 sizeof( data.slot_number ),
2141 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002142#endif
2143 memcpy( &data.slot_number, &slot->data.se.slot_number,
2144 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002145 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002146 (uint8_t*) &data,
2147 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002148 }
2149 else
2150#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2151 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002152 /* Key material is saved in export representation in the slot, so
2153 * just pass the slot buffer for storage. */
2154 status = psa_save_persistent_key( &slot->attr,
2155 slot->data.key.data,
2156 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002157 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002158 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002159#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2160
Gilles Peskinecbaff462019-07-12 23:46:04 +02002161#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002162 /* Finish the transaction for a key creation. This does not
2163 * happen when registering an existing key. Detect this case
2164 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002165 * creation of a persistent key in a secure element requires a transaction,
2166 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002167 if( driver != NULL &&
2168 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002169 {
2170 status = psa_save_se_persistent_data( driver );
2171 if( status != PSA_SUCCESS )
2172 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002173 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002174 return( status );
2175 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002176 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002177 }
2178#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2179
Ronald Cron50972942020-11-14 11:28:25 +01002180 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002181 {
2182 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002183 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002184 if( status != PSA_SUCCESS )
2185 *key = MBEDTLS_SVC_KEY_ID_INIT;
2186 }
Ronald Cron50972942020-11-14 11:28:25 +01002187
Gilles Peskine4747d192019-04-17 15:05:45 +02002188 return( status );
2189}
2190
2191/** Abort the creation of a key.
2192 *
2193 * You may call this function after calling psa_start_key_creation(),
2194 * or after psa_finish_key_creation() fails. In other circumstances, this
2195 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002196 * See the documentation of psa_start_key_creation() for the intended use
2197 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002198 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002199 * \param[in,out] slot Pointer to the slot with key material.
2200 * \param[in] driver The secure element driver for the key,
2201 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002202 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002203static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002204 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002205{
Gilles Peskine011e4282019-06-26 18:34:38 +02002206 (void) driver;
2207
Gilles Peskine4747d192019-04-17 15:05:45 +02002208 if( slot == NULL )
2209 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002210
2211#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002212 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002213 * element, and the failure happened later (when saving metadata
2214 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002215 * element.
2216 * https://github.com/ARMmbed/mbed-crypto/issues/217
2217 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002218
Gilles Peskined7729582019-08-05 15:55:54 +02002219 /* Abort the ongoing transaction if any (there may not be one if
2220 * the creation process failed before starting one, or if the
2221 * key creation is a registration of a key in a secure element).
2222 * Earlier functions must already have done what it takes to undo any
2223 * partial creation. All that's left is to update the transaction data
2224 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002225 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002226#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2227
Gilles Peskine4747d192019-04-17 15:05:45 +02002228 psa_wipe_key_slot( slot );
2229}
2230
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002231/** Validate optional attributes during key creation.
2232 *
2233 * Some key attributes are optional during key creation. If they are
2234 * specified in the attributes structure, check that they are consistent
2235 * with the data in the slot.
2236 *
2237 * This function should be called near the end of key creation, after
2238 * the slot in memory is fully populated but before saving persistent data.
2239 */
2240static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002241 const psa_key_slot_t *slot,
2242 const psa_key_attributes_t *attributes )
2243{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002244 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002246 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002247 return( PSA_ERROR_INVALID_ARGUMENT );
2248 }
2249
2250 if( attributes->domain_parameters_size != 0 )
2251 {
John Durkop0e005192020-10-31 22:06:54 -07002252#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2253 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002254 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002255 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002256 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002257 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002258 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002259
Steven Cooreman7f391872020-07-30 14:57:44 +02002260 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002261 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002262 slot->data.key.data,
2263 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002264 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002265 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002266 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002267
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002268 mbedtls_mpi_init( &actual );
2269 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002270 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002271 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002272 mbedtls_rsa_free( rsa );
2273 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002274 if( ret != 0 )
2275 goto rsa_exit;
2276 ret = mbedtls_mpi_read_binary( &required,
2277 attributes->domain_parameters,
2278 attributes->domain_parameters_size );
2279 if( ret != 0 )
2280 goto rsa_exit;
2281 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2282 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2283 rsa_exit:
2284 mbedtls_mpi_free( &actual );
2285 mbedtls_mpi_free( &required );
2286 if( ret != 0)
2287 return( mbedtls_to_psa_error( ret ) );
2288 }
2289 else
John Durkop9814fa22020-11-04 12:28:15 -08002290#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2291 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002292 {
2293 return( PSA_ERROR_INVALID_ARGUMENT );
2294 }
2295 }
2296
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002297 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002298 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002299 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002300 return( PSA_ERROR_INVALID_ARGUMENT );
2301 }
2302
2303 return( PSA_SUCCESS );
2304}
2305
Gilles Peskine4747d192019-04-17 15:05:45 +02002306psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002307 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002308 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002309 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002310{
2311 psa_status_t status;
2312 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002313 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002314
Ronald Cron81709fc2020-11-14 12:10:32 +01002315 *key = MBEDTLS_SVC_KEY_ID_INIT;
2316
Gilles Peskine0f84d622019-09-12 19:03:13 +02002317 /* Reject zero-length symmetric keys (including raw data key objects).
2318 * This also rejects any key which might be encoded as an empty string,
2319 * which is never valid. */
2320 if( data_length == 0 )
2321 return( PSA_ERROR_INVALID_ARGUMENT );
2322
Gilles Peskinedf179142019-07-15 22:02:14 +02002323 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002324 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002325 if( status != PSA_SUCCESS )
2326 goto exit;
2327
Steven Cooreman98435dd2021-01-08 19:19:40 +01002328 if( psa_key_lifetime_is_external( psa_get_key_lifetime( attributes ) ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02002329 {
Steven Cooreman98435dd2021-01-08 19:19:40 +01002330#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2331 if( driver != NULL )
Gilles Peskine5d309672019-07-12 23:47:28 +02002332 {
Steven Cooreman98435dd2021-01-08 19:19:40 +01002333 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
2334 /* The driver should set the number of key bits, however in
2335 * case it doesn't, we initialize bits to an invalid value. */
2336 size_t bits = PSA_MAX_KEY_BITS + 1;
2337 if( drv->key_management == NULL ||
2338 drv->key_management->p_import == NULL )
2339 {
2340 status = PSA_ERROR_NOT_SUPPORTED;
2341 goto exit;
2342 }
2343 status = drv->key_management->p_import(
2344 psa_get_se_driver_context( driver ),
2345 slot->data.se.slot_number, attributes, data, data_length,
2346 &bits );
2347 if( status != PSA_SUCCESS )
2348 goto exit;
2349 if( bits > PSA_MAX_KEY_BITS )
2350 {
2351 status = PSA_ERROR_NOT_SUPPORTED;
2352 goto exit;
2353 }
2354 slot->attr.bits = (psa_key_bits_t) bits;
2355 }
2356 else
2357#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2358 {
2359 /* Importing a key with external lifetime through the driver wrapper
2360 * interface is not yet supported. Return as if this was an invalid
2361 * lifetime. */
2362 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine5d309672019-07-12 23:47:28 +02002363 goto exit;
2364 }
Gilles Peskine5d309672019-07-12 23:47:28 +02002365 }
2366 else
Gilles Peskine5d309672019-07-12 23:47:28 +02002367 {
2368 status = psa_import_key_into_slot( slot, data, data_length );
2369 if( status != PSA_SUCCESS )
2370 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002371 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002372 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002373 if( status != PSA_SUCCESS )
2374 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002375
Ronald Cron81709fc2020-11-14 12:10:32 +01002376 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002377exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002378 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002379 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002380
Gilles Peskine4747d192019-04-17 15:05:45 +02002381 return( status );
2382}
2383
Gilles Peskined7729582019-08-05 15:55:54 +02002384#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2385psa_status_t mbedtls_psa_register_se_key(
2386 const psa_key_attributes_t *attributes )
2387{
2388 psa_status_t status;
2389 psa_key_slot_t *slot = NULL;
2390 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002391 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002392
2393 /* Leaving attributes unspecified is not currently supported.
2394 * It could make sense to query the key type and size from the
2395 * secure element, but not all secure elements support this
2396 * and the driver HAL doesn't currently support it. */
2397 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2398 return( PSA_ERROR_NOT_SUPPORTED );
2399 if( psa_get_key_bits( attributes ) == 0 )
2400 return( PSA_ERROR_NOT_SUPPORTED );
2401
2402 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002403 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002404 if( status != PSA_SUCCESS )
2405 goto exit;
2406
Ronald Cron81709fc2020-11-14 12:10:32 +01002407 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002408
2409exit:
2410 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002411 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002412
Gilles Peskined7729582019-08-05 15:55:54 +02002413 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002414 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002415 return( status );
2416}
2417#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2418
Gilles Peskinef603c712019-01-19 13:40:11 +01002419static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002420 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002421{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002422 psa_status_t status = psa_copy_key_material_into_slot( target,
2423 source->data.key.data,
2424 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002425 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002426 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002427
Steven Cooreman398aee52020-10-13 14:35:45 +02002428 target->attr.type = source->attr.type;
2429 target->attr.bits = source->attr.bits;
2430
2431 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002432}
2433
Ronald Croncf56a0a2020-08-04 09:51:30 +02002434psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002435 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002436 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002437{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002438 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002439 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002440 psa_key_slot_t *source_slot = NULL;
2441 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002442 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002443 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002444
Ronald Cron81709fc2020-11-14 12:10:32 +01002445 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2446
Ronald Cron5c522922020-11-14 16:35:34 +01002447 status = psa_get_and_lock_transparent_key_slot_with_policy(
2448 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002449 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002450 goto exit;
2451
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002452 status = psa_validate_optional_attributes( source_slot,
2453 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002454 if( status != PSA_SUCCESS )
2455 goto exit;
2456
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002457 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002458 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002459 if( status != PSA_SUCCESS )
2460 goto exit;
2461
Ronald Cron81709fc2020-11-14 12:10:32 +01002462 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2463 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002464 if( status != PSA_SUCCESS )
2465 goto exit;
2466
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002467#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2468 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002469 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002470 /* Copying to a secure element is not implemented yet. */
2471 status = PSA_ERROR_NOT_SUPPORTED;
2472 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002473 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002474#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002475
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002476 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002477 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002478 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002479
Ronald Cron81709fc2020-11-14 12:10:32 +01002480 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002481exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002482 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002483 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002484
Ronald Cron5c522922020-11-14 16:35:34 +01002485 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002486
Ronald Cron5c522922020-11-14 16:35:34 +01002487 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002488}
2489
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002490
2491
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002492/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002493/* Message digests */
2494/****************************************************************/
2495
John Durkop07cc04a2020-11-16 22:08:34 -08002496#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002497 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2498 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002499 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002500static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002501{
2502 switch( alg )
2503 {
John Durkopee4e6602020-11-27 08:48:46 -08002504#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002505 case PSA_ALG_MD2:
2506 return( &mbedtls_md2_info );
2507#endif
John Durkopee4e6602020-11-27 08:48:46 -08002508#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002509 case PSA_ALG_MD4:
2510 return( &mbedtls_md4_info );
2511#endif
John Durkopee4e6602020-11-27 08:48:46 -08002512#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002513 case PSA_ALG_MD5:
2514 return( &mbedtls_md5_info );
2515#endif
John Durkopee4e6602020-11-27 08:48:46 -08002516#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002517 case PSA_ALG_RIPEMD160:
2518 return( &mbedtls_ripemd160_info );
2519#endif
John Durkopee4e6602020-11-27 08:48:46 -08002520#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002521 case PSA_ALG_SHA_1:
2522 return( &mbedtls_sha1_info );
2523#endif
John Durkopee4e6602020-11-27 08:48:46 -08002524#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002525 case PSA_ALG_SHA_224:
2526 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002527#endif
2528#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002529 case PSA_ALG_SHA_256:
2530 return( &mbedtls_sha256_info );
2531#endif
John Durkopee4e6602020-11-27 08:48:46 -08002532#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002533 case PSA_ALG_SHA_384:
2534 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002535#endif
John Durkopee4e6602020-11-27 08:48:46 -08002536#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002537 case PSA_ALG_SHA_512:
2538 return( &mbedtls_sha512_info );
2539#endif
2540 default:
2541 return( NULL );
2542 }
2543}
John Durkop07cc04a2020-11-16 22:08:34 -08002544#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002545 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2546 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2547 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002548
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002549psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2550{
2551 switch( operation->alg )
2552 {
Gilles Peskine81736312018-06-26 15:04:31 +02002553 case 0:
2554 /* The object has (apparently) been initialized but it is not
2555 * in use. It's ok to call abort on such an object, and there's
2556 * nothing to do. */
2557 break;
John Durkopee4e6602020-11-27 08:48:46 -08002558#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002559 case PSA_ALG_MD2:
2560 mbedtls_md2_free( &operation->ctx.md2 );
2561 break;
2562#endif
John Durkopee4e6602020-11-27 08:48:46 -08002563#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002564 case PSA_ALG_MD4:
2565 mbedtls_md4_free( &operation->ctx.md4 );
2566 break;
2567#endif
John Durkopee4e6602020-11-27 08:48:46 -08002568#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002569 case PSA_ALG_MD5:
2570 mbedtls_md5_free( &operation->ctx.md5 );
2571 break;
2572#endif
John Durkopee4e6602020-11-27 08:48:46 -08002573#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002574 case PSA_ALG_RIPEMD160:
2575 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2576 break;
2577#endif
John Durkopee4e6602020-11-27 08:48:46 -08002578#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002579 case PSA_ALG_SHA_1:
2580 mbedtls_sha1_free( &operation->ctx.sha1 );
2581 break;
2582#endif
John Durkop6ca23272020-12-03 06:01:32 -08002583#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002584 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002585 mbedtls_sha256_free( &operation->ctx.sha256 );
2586 break;
2587#endif
2588#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002589 case PSA_ALG_SHA_256:
2590 mbedtls_sha256_free( &operation->ctx.sha256 );
2591 break;
2592#endif
John Durkop6ca23272020-12-03 06:01:32 -08002593#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002595 mbedtls_sha512_free( &operation->ctx.sha512 );
2596 break;
2597#endif
2598#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002599 case PSA_ALG_SHA_512:
2600 mbedtls_sha512_free( &operation->ctx.sha512 );
2601 break;
2602#endif
2603 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002604 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002605 }
2606 operation->alg = 0;
2607 return( PSA_SUCCESS );
2608}
2609
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002610psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002611 psa_algorithm_t alg )
2612{
Janos Follath24eed8d2019-11-22 13:21:35 +00002613 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002614
2615 /* A context must be freshly initialized before it can be set up. */
2616 if( operation->alg != 0 )
2617 {
2618 return( PSA_ERROR_BAD_STATE );
2619 }
2620
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002621 switch( alg )
2622 {
John Durkopee4e6602020-11-27 08:48:46 -08002623#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002624 case PSA_ALG_MD2:
2625 mbedtls_md2_init( &operation->ctx.md2 );
2626 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2627 break;
2628#endif
John Durkopee4e6602020-11-27 08:48:46 -08002629#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002630 case PSA_ALG_MD4:
2631 mbedtls_md4_init( &operation->ctx.md4 );
2632 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2633 break;
2634#endif
John Durkopee4e6602020-11-27 08:48:46 -08002635#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636 case PSA_ALG_MD5:
2637 mbedtls_md5_init( &operation->ctx.md5 );
2638 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2639 break;
2640#endif
John Durkopee4e6602020-11-27 08:48:46 -08002641#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002642 case PSA_ALG_RIPEMD160:
2643 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2644 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2645 break;
2646#endif
John Durkopee4e6602020-11-27 08:48:46 -08002647#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002648 case PSA_ALG_SHA_1:
2649 mbedtls_sha1_init( &operation->ctx.sha1 );
2650 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2651 break;
2652#endif
John Durkopee4e6602020-11-27 08:48:46 -08002653#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654 case PSA_ALG_SHA_224:
2655 mbedtls_sha256_init( &operation->ctx.sha256 );
2656 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2657 break;
John Durkopee4e6602020-11-27 08:48:46 -08002658#endif
2659#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002660 case PSA_ALG_SHA_256:
2661 mbedtls_sha256_init( &operation->ctx.sha256 );
2662 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2663 break;
2664#endif
John Durkopee4e6602020-11-27 08:48:46 -08002665#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002666 case PSA_ALG_SHA_384:
2667 mbedtls_sha512_init( &operation->ctx.sha512 );
2668 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2669 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002670#endif
John Durkopee4e6602020-11-27 08:48:46 -08002671#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002672 case PSA_ALG_SHA_512:
2673 mbedtls_sha512_init( &operation->ctx.sha512 );
2674 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2675 break;
2676#endif
2677 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002678 return( PSA_ALG_IS_HASH( alg ) ?
2679 PSA_ERROR_NOT_SUPPORTED :
2680 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002681 }
2682 if( ret == 0 )
2683 operation->alg = alg;
2684 else
2685 psa_hash_abort( operation );
2686 return( mbedtls_to_psa_error( ret ) );
2687}
2688
2689psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2690 const uint8_t *input,
2691 size_t input_length )
2692{
Janos Follath24eed8d2019-11-22 13:21:35 +00002693 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002694
2695 /* Don't require hash implementations to behave correctly on a
2696 * zero-length input, which may have an invalid pointer. */
2697 if( input_length == 0 )
2698 return( PSA_SUCCESS );
2699
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002700 switch( operation->alg )
2701 {
John Durkopee4e6602020-11-27 08:48:46 -08002702#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002703 case PSA_ALG_MD2:
2704 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2705 input, input_length );
2706 break;
2707#endif
John Durkopee4e6602020-11-27 08:48:46 -08002708#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002709 case PSA_ALG_MD4:
2710 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2711 input, input_length );
2712 break;
2713#endif
John Durkopee4e6602020-11-27 08:48:46 -08002714#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002715 case PSA_ALG_MD5:
2716 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2717 input, input_length );
2718 break;
2719#endif
John Durkopee4e6602020-11-27 08:48:46 -08002720#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 case PSA_ALG_RIPEMD160:
2722 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2723 input, input_length );
2724 break;
2725#endif
John Durkopee4e6602020-11-27 08:48:46 -08002726#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002727 case PSA_ALG_SHA_1:
2728 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2729 input, input_length );
2730 break;
2731#endif
John Durkop6ca23272020-12-03 06:01:32 -08002732#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002733 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002734 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2735 input, input_length );
2736 break;
2737#endif
2738#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002739 case PSA_ALG_SHA_256:
2740 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2741 input, input_length );
2742 break;
2743#endif
John Durkop6ca23272020-12-03 06:01:32 -08002744#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002745 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002746 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2747 input, input_length );
2748 break;
2749#endif
2750#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002751 case PSA_ALG_SHA_512:
2752 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2753 input, input_length );
2754 break;
2755#endif
2756 default:
John Durkopee4e6602020-11-27 08:48:46 -08002757 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002758 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002759 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002760
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002761 if( ret != 0 )
2762 psa_hash_abort( operation );
2763 return( mbedtls_to_psa_error( ret ) );
2764}
2765
2766psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2767 uint8_t *hash,
2768 size_t hash_size,
2769 size_t *hash_length )
2770{
itayzafrir40835d42018-08-02 13:14:17 +03002771 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002772 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002773 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002774
2775 /* Fill the output buffer with something that isn't a valid hash
2776 * (barring an attack on the hash and deliberately-crafted input),
2777 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002778 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002779 /* If hash_size is 0 then hash may be NULL and then the
2780 * call to memset would have undefined behavior. */
2781 if( hash_size != 0 )
2782 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002783
2784 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002785 {
2786 status = PSA_ERROR_BUFFER_TOO_SMALL;
2787 goto exit;
2788 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789
2790 switch( operation->alg )
2791 {
John Durkopee4e6602020-11-27 08:48:46 -08002792#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002793 case PSA_ALG_MD2:
2794 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2795 break;
2796#endif
John Durkopee4e6602020-11-27 08:48:46 -08002797#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002798 case PSA_ALG_MD4:
2799 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2800 break;
2801#endif
John Durkopee4e6602020-11-27 08:48:46 -08002802#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002803 case PSA_ALG_MD5:
2804 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2805 break;
2806#endif
John Durkopee4e6602020-11-27 08:48:46 -08002807#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002808 case PSA_ALG_RIPEMD160:
2809 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2810 break;
2811#endif
John Durkopee4e6602020-11-27 08:48:46 -08002812#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002813 case PSA_ALG_SHA_1:
2814 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2815 break;
2816#endif
John Durkop6ca23272020-12-03 06:01:32 -08002817#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002818 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002819 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2820 break;
2821#endif
2822#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002823 case PSA_ALG_SHA_256:
2824 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2825 break;
2826#endif
John Durkop6ca23272020-12-03 06:01:32 -08002827#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002828 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002829 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2830 break;
2831#endif
2832#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002833 case PSA_ALG_SHA_512:
2834 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2835 break;
2836#endif
2837 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002838 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002839 }
itayzafrir40835d42018-08-02 13:14:17 +03002840 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002841
itayzafrir40835d42018-08-02 13:14:17 +03002842exit:
2843 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002844 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002845 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002846 return( psa_hash_abort( operation ) );
2847 }
2848 else
2849 {
2850 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002851 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002852 }
2853}
2854
Gilles Peskine2d277862018-06-18 15:41:12 +02002855psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2856 const uint8_t *hash,
2857 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002858{
2859 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2860 size_t actual_hash_length;
2861 psa_status_t status = psa_hash_finish( operation,
2862 actual_hash, sizeof( actual_hash ),
2863 &actual_hash_length );
2864 if( status != PSA_SUCCESS )
2865 return( status );
2866 if( actual_hash_length != hash_length )
2867 return( PSA_ERROR_INVALID_SIGNATURE );
2868 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2869 return( PSA_ERROR_INVALID_SIGNATURE );
2870 return( PSA_SUCCESS );
2871}
2872
Gilles Peskine0a749c82019-11-28 19:33:58 +01002873psa_status_t psa_hash_compute( psa_algorithm_t alg,
2874 const uint8_t *input, size_t input_length,
2875 uint8_t *hash, size_t hash_size,
2876 size_t *hash_length )
2877{
2878 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2879 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2880
2881 *hash_length = hash_size;
2882 status = psa_hash_setup( &operation, alg );
2883 if( status != PSA_SUCCESS )
2884 goto exit;
2885 status = psa_hash_update( &operation, input, input_length );
2886 if( status != PSA_SUCCESS )
2887 goto exit;
2888 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2889 if( status != PSA_SUCCESS )
2890 goto exit;
2891
2892exit:
2893 if( status == PSA_SUCCESS )
2894 status = psa_hash_abort( &operation );
2895 else
2896 psa_hash_abort( &operation );
2897 return( status );
2898}
2899
2900psa_status_t psa_hash_compare( psa_algorithm_t alg,
2901 const uint8_t *input, size_t input_length,
2902 const uint8_t *hash, size_t hash_length )
2903{
2904 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2905 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2906
2907 status = psa_hash_setup( &operation, alg );
2908 if( status != PSA_SUCCESS )
2909 goto exit;
2910 status = psa_hash_update( &operation, input, input_length );
2911 if( status != PSA_SUCCESS )
2912 goto exit;
2913 status = psa_hash_verify( &operation, hash, hash_length );
2914 if( status != PSA_SUCCESS )
2915 goto exit;
2916
2917exit:
2918 if( status == PSA_SUCCESS )
2919 status = psa_hash_abort( &operation );
2920 else
2921 psa_hash_abort( &operation );
2922 return( status );
2923}
2924
Gilles Peskineeb35d782019-01-22 17:56:16 +01002925psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2926 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002927{
2928 if( target_operation->alg != 0 )
2929 return( PSA_ERROR_BAD_STATE );
2930
2931 switch( source_operation->alg )
2932 {
2933 case 0:
2934 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002935#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002936 case PSA_ALG_MD2:
2937 mbedtls_md2_clone( &target_operation->ctx.md2,
2938 &source_operation->ctx.md2 );
2939 break;
2940#endif
John Durkopee4e6602020-11-27 08:48:46 -08002941#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002942 case PSA_ALG_MD4:
2943 mbedtls_md4_clone( &target_operation->ctx.md4,
2944 &source_operation->ctx.md4 );
2945 break;
2946#endif
John Durkopee4e6602020-11-27 08:48:46 -08002947#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002948 case PSA_ALG_MD5:
2949 mbedtls_md5_clone( &target_operation->ctx.md5,
2950 &source_operation->ctx.md5 );
2951 break;
2952#endif
John Durkopee4e6602020-11-27 08:48:46 -08002953#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002954 case PSA_ALG_RIPEMD160:
2955 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2956 &source_operation->ctx.ripemd160 );
2957 break;
2958#endif
John Durkopee4e6602020-11-27 08:48:46 -08002959#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002960 case PSA_ALG_SHA_1:
2961 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2962 &source_operation->ctx.sha1 );
2963 break;
2964#endif
John Durkop6ca23272020-12-03 06:01:32 -08002965#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002966 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002967 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2968 &source_operation->ctx.sha256 );
2969 break;
2970#endif
2971#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002972 case PSA_ALG_SHA_256:
2973 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2974 &source_operation->ctx.sha256 );
2975 break;
2976#endif
John Durkop6ca23272020-12-03 06:01:32 -08002977#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002978 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002979 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2980 &source_operation->ctx.sha512 );
2981 break;
2982#endif
2983#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002984 case PSA_ALG_SHA_512:
2985 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2986 &source_operation->ctx.sha512 );
2987 break;
2988#endif
2989 default:
2990 return( PSA_ERROR_NOT_SUPPORTED );
2991 }
2992
2993 target_operation->alg = source_operation->alg;
2994 return( PSA_SUCCESS );
2995}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002996
2997
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002998/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999/* MAC */
3000/****************************************************************/
3001
Gilles Peskinedc2fc842018-03-07 16:42:59 +01003002static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003 psa_algorithm_t alg,
3004 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02003005 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03003006 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003007{
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03003009 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003011 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003012 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003013
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3015 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003016 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003018 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003019 mode = MBEDTLS_MODE_STREAM;
3020 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 case PSA_ALG_CTR:
3022 mode = MBEDTLS_MODE_CTR;
3023 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003024 case PSA_ALG_CFB:
3025 mode = MBEDTLS_MODE_CFB;
3026 break;
3027 case PSA_ALG_OFB:
3028 mode = MBEDTLS_MODE_OFB;
3029 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003030 case PSA_ALG_ECB_NO_PADDING:
3031 mode = MBEDTLS_MODE_ECB;
3032 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003033 case PSA_ALG_CBC_NO_PADDING:
3034 mode = MBEDTLS_MODE_CBC;
3035 break;
3036 case PSA_ALG_CBC_PKCS7:
3037 mode = MBEDTLS_MODE_CBC;
3038 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003039 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040 mode = MBEDTLS_MODE_CCM;
3041 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003042 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 mode = MBEDTLS_MODE_GCM;
3044 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003045 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3046 mode = MBEDTLS_MODE_CHACHAPOLY;
3047 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003048 default:
3049 return( NULL );
3050 }
3051 }
3052 else if( alg == PSA_ALG_CMAC )
3053 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054 else
3055 return( NULL );
3056
3057 switch( key_type )
3058 {
3059 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003060 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003061 break;
3062 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003063 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3064 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003065 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003066 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003067 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003068 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003069 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3070 * but two-key Triple-DES is functionally three-key Triple-DES
3071 * with K1=K3, so that's how we present it to mbedtls. */
3072 if( key_bits == 128 )
3073 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003074 break;
3075 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003076 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003077 break;
3078 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003079 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003080 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003081 case PSA_KEY_TYPE_CHACHA20:
3082 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3083 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084 default:
3085 return( NULL );
3086 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003087 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003088 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003089
Jaeden Amero23bbb752018-06-26 14:16:54 +01003090 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3091 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003092}
3093
John Durkop6ba40d12020-11-10 08:50:04 -08003094#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003095static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003096{
Gilles Peskine2d277862018-06-18 15:41:12 +02003097 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003098 {
3099 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003100 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003101 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003102 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003103 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003104 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003105 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003106 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003107 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003108 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003109 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003110 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003111 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003112 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003113 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003114 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003115 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003116 return( 128 );
3117 default:
3118 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003119 }
3120}
John Durkop07cc04a2020-11-16 22:08:34 -08003121#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003122
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003123/* Initialize the MAC operation structure. Once this function has been
3124 * called, psa_mac_abort can run and will do the right thing. */
3125static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3126 psa_algorithm_t alg )
3127{
3128 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3129
3130 operation->alg = alg;
3131 operation->key_set = 0;
3132 operation->iv_set = 0;
3133 operation->iv_required = 0;
3134 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003135 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003136
3137#if defined(MBEDTLS_CMAC_C)
3138 if( alg == PSA_ALG_CMAC )
3139 {
3140 operation->iv_required = 0;
3141 mbedtls_cipher_init( &operation->ctx.cmac );
3142 status = PSA_SUCCESS;
3143 }
3144 else
3145#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003146#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147 if( PSA_ALG_IS_HMAC( operation->alg ) )
3148 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003149 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3150 operation->ctx.hmac.hash_ctx.alg = 0;
3151 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003152 }
3153 else
John Durkopd0321952020-10-29 21:37:36 -07003154#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003155 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003156 if( ! PSA_ALG_IS_MAC( alg ) )
3157 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003158 }
3159
3160 if( status != PSA_SUCCESS )
3161 memset( operation, 0, sizeof( *operation ) );
3162 return( status );
3163}
3164
John Durkop6ba40d12020-11-10 08:50:04 -08003165#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003166static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3167{
Gilles Peskine3f108122018-12-07 18:14:53 +01003168 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003169 return( psa_hash_abort( &hmac->hash_ctx ) );
3170}
John Durkop6ba40d12020-11-10 08:50:04 -08003171#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003172
Gilles Peskine8c9def32018-02-08 10:02:12 +01003173psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3174{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003175 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003176 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003177 /* The object has (apparently) been initialized but it is not
3178 * in use. It's ok to call abort on such an object, and there's
3179 * nothing to do. */
3180 return( PSA_SUCCESS );
3181 }
3182 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003183#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003184 if( operation->alg == PSA_ALG_CMAC )
3185 {
3186 mbedtls_cipher_free( &operation->ctx.cmac );
3187 }
3188 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003189#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003190#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003191 if( PSA_ALG_IS_HMAC( operation->alg ) )
3192 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003193 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003194 }
3195 else
John Durkopd0321952020-10-29 21:37:36 -07003196#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003197 {
3198 /* Sanity check (shouldn't happen: operation->alg should
3199 * always have been initialized to a valid value). */
3200 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003201 }
Moran Peker41deec42018-04-04 15:43:05 +03003202
Gilles Peskine8c9def32018-02-08 10:02:12 +01003203 operation->alg = 0;
3204 operation->key_set = 0;
3205 operation->iv_set = 0;
3206 operation->iv_required = 0;
3207 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003208 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003209
Gilles Peskine8c9def32018-02-08 10:02:12 +01003210 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003211
3212bad_state:
3213 /* If abort is called on an uninitialized object, we can't trust
3214 * anything. Wipe the object in case it contains confidential data.
3215 * This may result in a memory leak if a pointer gets overwritten,
3216 * but it's too late to do anything about this. */
3217 memset( operation, 0, sizeof( *operation ) );
3218 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003219}
3220
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003221#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003222static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003223 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003224 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003225 const mbedtls_cipher_info_t *cipher_info )
3226{
Janos Follath24eed8d2019-11-22 13:21:35 +00003227 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003228
3229 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003230
3231 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3232 if( ret != 0 )
3233 return( ret );
3234
3235 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003236 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003237 key_bits );
3238 return( ret );
3239}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003240#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241
John Durkop6ba40d12020-11-10 08:50:04 -08003242#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003243static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3244 const uint8_t *key,
3245 size_t key_length,
3246 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003247{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003248 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003250 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003251 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003252 psa_status_t status;
3253
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003254 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3255 * overflow below. This should never trigger if the hash algorithm
3256 * is implemented correctly. */
3257 /* The size checks against the ipad and opad buffers cannot be written
3258 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3259 * because that triggers -Wlogical-op on GCC 7.3. */
3260 if( block_size > sizeof( ipad ) )
3261 return( PSA_ERROR_NOT_SUPPORTED );
3262 if( block_size > sizeof( hmac->opad ) )
3263 return( PSA_ERROR_NOT_SUPPORTED );
3264 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003265 return( PSA_ERROR_NOT_SUPPORTED );
3266
Gilles Peskined223b522018-06-11 18:12:58 +02003267 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003268 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003269 status = psa_hash_compute( hash_alg, key, key_length,
3270 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003271 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003272 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003273 }
Gilles Peskine96889972018-07-12 17:07:03 +02003274 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3275 * but it is permitted. It is common when HMAC is used in HKDF, for
3276 * example. Don't call `memcpy` in the 0-length because `key` could be
3277 * an invalid pointer which would make the behavior undefined. */
3278 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003279 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003280
Gilles Peskined223b522018-06-11 18:12:58 +02003281 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3282 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003283 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003284 ipad[i] ^= 0x36;
3285 memset( ipad + key_length, 0x36, block_size - key_length );
3286
3287 /* Copy the key material from ipad to opad, flipping the requisite bits,
3288 * and filling the rest of opad with the requisite constant. */
3289 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003290 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3291 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003292
Gilles Peskine01126fa2018-07-12 17:04:55 +02003293 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003294 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003295 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003296
Gilles Peskine01126fa2018-07-12 17:04:55 +02003297 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003298
3299cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003300 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003301
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003302 return( status );
3303}
John Durkop6ba40d12020-11-10 08:50:04 -08003304#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003305
Gilles Peskine89167cb2018-07-08 20:12:23 +02003306static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003307 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003308 psa_algorithm_t alg,
3309 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003310{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003311 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003312 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003313 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003314 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003315 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003316 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003317 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003318 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003319
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003320 /* A context must be freshly initialized before it can be set up. */
3321 if( operation->alg != 0 )
3322 {
3323 return( PSA_ERROR_BAD_STATE );
3324 }
3325
Gilles Peskined911eb72018-08-14 15:18:45 +02003326 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003327 if( status != PSA_SUCCESS )
3328 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003329 if( is_sign )
3330 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003331
Ronald Cron5c522922020-11-14 16:35:34 +01003332 status = psa_get_and_lock_transparent_key_slot_with_policy(
3333 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003334 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003335 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003336 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003337
Gilles Peskine8c9def32018-02-08 10:02:12 +01003338#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003339 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003340 {
3341 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003342 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003343 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003344 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003345 if( cipher_info == NULL )
3346 {
3347 status = PSA_ERROR_NOT_SUPPORTED;
3348 goto exit;
3349 }
3350 operation->mac_size = cipher_info->block_size;
3351 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3352 status = mbedtls_to_psa_error( ret );
3353 }
3354 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003355#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003356#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003357 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003358 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003359 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003360 if( hash_alg == 0 )
3361 {
3362 status = PSA_ERROR_NOT_SUPPORTED;
3363 goto exit;
3364 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003365
3366 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3367 /* Sanity check. This shouldn't fail on a valid configuration. */
3368 if( operation->mac_size == 0 ||
3369 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3370 {
3371 status = PSA_ERROR_NOT_SUPPORTED;
3372 goto exit;
3373 }
3374
Gilles Peskine8e338702019-07-30 20:06:31 +02003375 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003376 {
3377 status = PSA_ERROR_INVALID_ARGUMENT;
3378 goto exit;
3379 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003380
Gilles Peskine01126fa2018-07-12 17:04:55 +02003381 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003382 slot->data.key.data,
3383 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003384 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003385 }
3386 else
John Durkopd0321952020-10-29 21:37:36 -07003387#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003388 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003389 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003390 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003391 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003392
Gilles Peskined911eb72018-08-14 15:18:45 +02003393 if( truncated == 0 )
3394 {
3395 /* The "normal" case: untruncated algorithm. Nothing to do. */
3396 }
3397 else if( truncated < 4 )
3398 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003399 /* A very short MAC is too short for security since it can be
3400 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3401 * so we make this our minimum, even though 32 bits is still
3402 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003403 status = PSA_ERROR_NOT_SUPPORTED;
3404 }
3405 else if( truncated > operation->mac_size )
3406 {
3407 /* It's impossible to "truncate" to a larger length. */
3408 status = PSA_ERROR_INVALID_ARGUMENT;
3409 }
3410 else
3411 operation->mac_size = truncated;
3412
Gilles Peskinefbfac682018-07-08 20:51:54 +02003413exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003414 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003416 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003417 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003418 else
3419 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003420 operation->key_set = 1;
3421 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003422
Ronald Cron5c522922020-11-14 16:35:34 +01003423 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003424
Ronald Cron5c522922020-11-14 16:35:34 +01003425 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003426}
3427
Gilles Peskine89167cb2018-07-08 20:12:23 +02003428psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003429 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003430 psa_algorithm_t alg )
3431{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003432 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003433}
3434
3435psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003436 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003437 psa_algorithm_t alg )
3438{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003439 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003440}
3441
Gilles Peskine8c9def32018-02-08 10:02:12 +01003442psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3443 const uint8_t *input,
3444 size_t input_length )
3445{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003446 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003447 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003448 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003449 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003450 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003451 operation->has_input = 1;
3452
Gilles Peskine8c9def32018-02-08 10:02:12 +01003453#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003454 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003455 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003456 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3457 input, input_length );
3458 status = mbedtls_to_psa_error( ret );
3459 }
3460 else
3461#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003462#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003463 if( PSA_ALG_IS_HMAC( operation->alg ) )
3464 {
3465 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3466 input_length );
3467 }
3468 else
John Durkopd0321952020-10-29 21:37:36 -07003469#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003470 {
3471 /* This shouldn't happen if `operation` was initialized by
3472 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003473 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003474 }
3475
Gilles Peskinefbfac682018-07-08 20:51:54 +02003476 if( status != PSA_SUCCESS )
3477 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003478 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003479}
3480
John Durkop6ba40d12020-11-10 08:50:04 -08003481#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003482static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3483 uint8_t *mac,
3484 size_t mac_size )
3485{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003486 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003487 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3488 size_t hash_size = 0;
3489 size_t block_size = psa_get_hash_block_size( hash_alg );
3490 psa_status_t status;
3491
Gilles Peskine01126fa2018-07-12 17:04:55 +02003492 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3493 if( status != PSA_SUCCESS )
3494 return( status );
3495 /* From here on, tmp needs to be wiped. */
3496
3497 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3498 if( status != PSA_SUCCESS )
3499 goto exit;
3500
3501 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3502 if( status != PSA_SUCCESS )
3503 goto exit;
3504
3505 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3506 if( status != PSA_SUCCESS )
3507 goto exit;
3508
Gilles Peskined911eb72018-08-14 15:18:45 +02003509 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3510 if( status != PSA_SUCCESS )
3511 goto exit;
3512
3513 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003514
3515exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003516 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003517 return( status );
3518}
John Durkop6ba40d12020-11-10 08:50:04 -08003519#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003520
mohammad16036df908f2018-04-02 08:34:15 -07003521static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003522 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003523 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003524{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003525 if( ! operation->key_set )
3526 return( PSA_ERROR_BAD_STATE );
3527 if( operation->iv_required && ! operation->iv_set )
3528 return( PSA_ERROR_BAD_STATE );
3529
Gilles Peskine8c9def32018-02-08 10:02:12 +01003530 if( mac_size < operation->mac_size )
3531 return( PSA_ERROR_BUFFER_TOO_SMALL );
3532
Gilles Peskine8c9def32018-02-08 10:02:12 +01003533#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003534 if( operation->alg == PSA_ALG_CMAC )
3535 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003536 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3537 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3538 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003539 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003540 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003541 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003542 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003543 else
3544#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003545#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003546 if( PSA_ALG_IS_HMAC( operation->alg ) )
3547 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003548 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003549 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003550 }
3551 else
John Durkopd0321952020-10-29 21:37:36 -07003552#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003553 {
3554 /* This shouldn't happen if `operation` was initialized by
3555 * a setup function. */
3556 return( PSA_ERROR_BAD_STATE );
3557 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003558}
3559
Gilles Peskineacd4be32018-07-08 19:56:25 +02003560psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3561 uint8_t *mac,
3562 size_t mac_size,
3563 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003564{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003565 psa_status_t status;
3566
Jaeden Amero252ef282019-02-15 14:05:35 +00003567 if( operation->alg == 0 )
3568 {
3569 return( PSA_ERROR_BAD_STATE );
3570 }
3571
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003572 /* Fill the output buffer with something that isn't a valid mac
3573 * (barring an attack on the mac and deliberately-crafted input),
3574 * in case the caller doesn't check the return status properly. */
3575 *mac_length = mac_size;
3576 /* If mac_size is 0 then mac may be NULL and then the
3577 * call to memset would have undefined behavior. */
3578 if( mac_size != 0 )
3579 memset( mac, '!', mac_size );
3580
Gilles Peskine89167cb2018-07-08 20:12:23 +02003581 if( ! operation->is_sign )
3582 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003583 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003584 }
mohammad16036df908f2018-04-02 08:34:15 -07003585
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003586 status = psa_mac_finish_internal( operation, mac, mac_size );
3587
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003588 if( status == PSA_SUCCESS )
3589 {
3590 status = psa_mac_abort( operation );
3591 if( status == PSA_SUCCESS )
3592 *mac_length = operation->mac_size;
3593 else
3594 memset( mac, '!', mac_size );
3595 }
3596 else
3597 psa_mac_abort( operation );
3598 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003599}
3600
Gilles Peskineacd4be32018-07-08 19:56:25 +02003601psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3602 const uint8_t *mac,
3603 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003604{
Gilles Peskine828ed142018-06-18 23:25:51 +02003605 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003606 psa_status_t status;
3607
Jaeden Amero252ef282019-02-15 14:05:35 +00003608 if( operation->alg == 0 )
3609 {
3610 return( PSA_ERROR_BAD_STATE );
3611 }
3612
Gilles Peskine89167cb2018-07-08 20:12:23 +02003613 if( operation->is_sign )
3614 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003615 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003616 }
3617 if( operation->mac_size != mac_length )
3618 {
3619 status = PSA_ERROR_INVALID_SIGNATURE;
3620 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003621 }
mohammad16036df908f2018-04-02 08:34:15 -07003622
3623 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003624 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003625 if( status != PSA_SUCCESS )
3626 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003627
3628 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3629 status = PSA_ERROR_INVALID_SIGNATURE;
3630
3631cleanup:
3632 if( status == PSA_SUCCESS )
3633 status = psa_mac_abort( operation );
3634 else
3635 psa_mac_abort( operation );
3636
Gilles Peskine3f108122018-12-07 18:14:53 +01003637 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003638
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003639 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003640}
3641
3642
Gilles Peskine20035e32018-02-03 22:44:14 +01003643
Gilles Peskine20035e32018-02-03 22:44:14 +01003644/****************************************************************/
3645/* Asymmetric cryptography */
3646/****************************************************************/
3647
John Durkop6ba40d12020-11-10 08:50:04 -08003648#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3649 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003650/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003651 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003652static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3653 size_t hash_length,
3654 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003655{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003656 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003657 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003658 *md_alg = mbedtls_md_get_type( md_info );
3659
3660 /* The Mbed TLS RSA module uses an unsigned int for hash length
3661 * parameters. Validate that it fits so that we don't risk an
3662 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003663#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003664 if( hash_length > UINT_MAX )
3665 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003666#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003667
John Durkop0e005192020-10-31 22:06:54 -07003668#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003669 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3670 * must be correct. */
3671 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3672 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003673 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003674 if( md_info == NULL )
3675 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003676 if( mbedtls_md_get_size( md_info ) != hash_length )
3677 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003678 }
John Durkop0e005192020-10-31 22:06:54 -07003679#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003680
John Durkop0e005192020-10-31 22:06:54 -07003681#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003682 /* PSS requires a hash internally. */
3683 if( PSA_ALG_IS_RSA_PSS( alg ) )
3684 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003685 if( md_info == NULL )
3686 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003687 }
John Durkop0e005192020-10-31 22:06:54 -07003688#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003689
Gilles Peskine61b91d42018-06-08 16:09:36 +02003690 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003691}
3692
Gilles Peskine2b450e32018-06-27 15:42:46 +02003693static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3694 psa_algorithm_t alg,
3695 const uint8_t *hash,
3696 size_t hash_length,
3697 uint8_t *signature,
3698 size_t signature_size,
3699 size_t *signature_length )
3700{
3701 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003702 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003703 mbedtls_md_type_t md_alg;
3704
3705 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3706 if( status != PSA_SUCCESS )
3707 return( status );
3708
Gilles Peskine630a18a2018-06-29 17:49:35 +02003709 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003710 return( PSA_ERROR_BUFFER_TOO_SMALL );
3711
John Durkop0e005192020-10-31 22:06:54 -07003712#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003713 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3714 {
3715 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3716 MBEDTLS_MD_NONE );
3717 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003718 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003719 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003720 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003721 md_alg,
3722 (unsigned int) hash_length,
3723 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003724 signature );
3725 }
3726 else
John Durkop0e005192020-10-31 22:06:54 -07003727#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3728#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003729 if( PSA_ALG_IS_RSA_PSS( alg ) )
3730 {
3731 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3732 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003733 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003734 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003735 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003736 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003737 (unsigned int) hash_length,
3738 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003739 signature );
3740 }
3741 else
John Durkop0e005192020-10-31 22:06:54 -07003742#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003743 {
3744 return( PSA_ERROR_INVALID_ARGUMENT );
3745 }
3746
3747 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003748 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003749 return( mbedtls_to_psa_error( ret ) );
3750}
3751
3752static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3753 psa_algorithm_t alg,
3754 const uint8_t *hash,
3755 size_t hash_length,
3756 const uint8_t *signature,
3757 size_t signature_length )
3758{
3759 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003760 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003761 mbedtls_md_type_t md_alg;
3762
3763 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3764 if( status != PSA_SUCCESS )
3765 return( status );
3766
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003767 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3768 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003769
John Durkop0e005192020-10-31 22:06:54 -07003770#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003771 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3772 {
3773 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3774 MBEDTLS_MD_NONE );
3775 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003776 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003777 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003778 MBEDTLS_RSA_PUBLIC,
3779 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003780 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003781 hash,
3782 signature );
3783 }
3784 else
John Durkop0e005192020-10-31 22:06:54 -07003785#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3786#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003787 if( PSA_ALG_IS_RSA_PSS( alg ) )
3788 {
3789 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3790 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003791 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003792 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003793 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003794 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003795 (unsigned int) hash_length,
3796 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003797 signature );
3798 }
3799 else
John Durkop0e005192020-10-31 22:06:54 -07003800#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003801 {
3802 return( PSA_ERROR_INVALID_ARGUMENT );
3803 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003804
3805 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3806 * the rest of the signature is invalid". This has little use in
3807 * practice and PSA doesn't report this distinction. */
3808 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3809 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003810 return( mbedtls_to_psa_error( ret ) );
3811}
John Durkop6ba40d12020-11-10 08:50:04 -08003812#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3813 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003814
John Durkop6ba40d12020-11-10 08:50:04 -08003815#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3816 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003817/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3818 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3819 * (even though these functions don't modify it). */
3820static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3821 psa_algorithm_t alg,
3822 const uint8_t *hash,
3823 size_t hash_length,
3824 uint8_t *signature,
3825 size_t signature_size,
3826 size_t *signature_length )
3827{
Janos Follath24eed8d2019-11-22 13:21:35 +00003828 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003829 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003830 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003831 mbedtls_mpi_init( &r );
3832 mbedtls_mpi_init( &s );
3833
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003834 if( signature_size < 2 * curve_bytes )
3835 {
3836 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3837 goto cleanup;
3838 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003839
John Durkop0ea39e02020-10-13 19:58:20 -07003840#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003841 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3842 {
3843 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3844 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3845 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003846 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3847 &ecp->d, hash,
3848 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003849 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003850 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003851 }
3852 else
John Durkop0ea39e02020-10-13 19:58:20 -07003853#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003854 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003855 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003856 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3857 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003858 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003859 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003860 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003861
3862 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3863 signature,
3864 curve_bytes ) );
3865 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3866 signature + curve_bytes,
3867 curve_bytes ) );
3868
3869cleanup:
3870 mbedtls_mpi_free( &r );
3871 mbedtls_mpi_free( &s );
3872 if( ret == 0 )
3873 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003874 return( mbedtls_to_psa_error( ret ) );
3875}
3876
3877static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3878 const uint8_t *hash,
3879 size_t hash_length,
3880 const uint8_t *signature,
3881 size_t signature_length )
3882{
Janos Follath24eed8d2019-11-22 13:21:35 +00003883 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003884 mbedtls_mpi r, s;
3885 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3886 mbedtls_mpi_init( &r );
3887 mbedtls_mpi_init( &s );
3888
3889 if( signature_length != 2 * curve_bytes )
3890 return( PSA_ERROR_INVALID_SIGNATURE );
3891
3892 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3893 signature,
3894 curve_bytes ) );
3895 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3896 signature + curve_bytes,
3897 curve_bytes ) );
3898
Steven Cooremanacda8342020-07-24 23:09:52 +02003899 /* Check whether the public part is loaded. If not, load it. */
3900 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3901 {
3902 MBEDTLS_MPI_CHK(
3903 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003904 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003905 }
3906
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003907 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3908 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003909
3910cleanup:
3911 mbedtls_mpi_free( &r );
3912 mbedtls_mpi_free( &s );
3913 return( mbedtls_to_psa_error( ret ) );
3914}
John Durkop6ba40d12020-11-10 08:50:04 -08003915#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3916 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003917
Ronald Croncf56a0a2020-08-04 09:51:30 +02003918psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003919 psa_algorithm_t alg,
3920 const uint8_t *hash,
3921 size_t hash_length,
3922 uint8_t *signature,
3923 size_t signature_size,
3924 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003925{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003926 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003927 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003928 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003929
3930 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003931 /* Immediately reject a zero-length signature buffer. This guarantees
3932 * that signature must be a valid pointer. (On the other hand, the hash
3933 * buffer can in principle be empty since it doesn't actually have
3934 * to be a hash.) */
3935 if( signature_size == 0 )
3936 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003937
Ronald Cron5c522922020-11-14 16:35:34 +01003938 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3939 PSA_KEY_USAGE_SIGN_HASH,
3940 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003941 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003942 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003943 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003944 {
3945 status = PSA_ERROR_INVALID_ARGUMENT;
3946 goto exit;
3947 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003948
Steven Cooremancd84cb42020-07-16 20:28:36 +02003949 /* Try any of the available accelerators first */
3950 status = psa_driver_wrapper_sign_hash( slot,
3951 alg,
3952 hash,
3953 hash_length,
3954 signature,
3955 signature_size,
3956 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003957 if( status != PSA_ERROR_NOT_SUPPORTED ||
3958 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003959 goto exit;
3960
Steven Cooreman7a250572020-07-17 16:43:05 +02003961 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003962#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3963 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003964 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003965 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003966 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003967
Steven Cooreman4fed4552020-08-03 14:46:03 +02003968 status = psa_load_rsa_representation( slot->attr.type,
3969 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003970 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003971 &rsa );
3972 if( status != PSA_SUCCESS )
3973 goto exit;
3974
Steven Cooremana2371e52020-07-28 14:30:39 +02003975 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003976 alg,
3977 hash, hash_length,
3978 signature, signature_size,
3979 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003980
Steven Cooremana2371e52020-07-28 14:30:39 +02003981 mbedtls_rsa_free( rsa );
3982 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003983 }
3984 else
John Durkop6ba40d12020-11-10 08:50:04 -08003985#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3986 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003987 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003988 {
John Durkop9814fa22020-11-04 12:28:15 -08003989#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3990 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003991 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003992#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003993 PSA_ALG_IS_ECDSA( alg )
3994#else
3995 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3996#endif
3997 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003998 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003999 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004000 status = psa_load_ecp_representation( slot->attr.type,
4001 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004002 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004003 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004004 if( status != PSA_SUCCESS )
4005 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004006 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004007 alg,
4008 hash, hash_length,
4009 signature, signature_size,
4010 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004011 mbedtls_ecp_keypair_free( ecp );
4012 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004013 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004014 else
John Durkop9814fa22020-11-04 12:28:15 -08004015#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4016 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004017 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004018 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004019 }
itayzafrir5c753392018-05-08 11:18:38 +03004020 }
4021 else
itayzafrir5c753392018-05-08 11:18:38 +03004022 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004023 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004024 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004025
4026exit:
4027 /* Fill the unused part of the output buffer (the whole buffer on error,
4028 * the trailing part on success) with something that isn't a valid mac
4029 * (barring an attack on the mac and deliberately-crafted input),
4030 * in case the caller doesn't check the return status properly. */
4031 if( status == PSA_SUCCESS )
4032 memset( signature + *signature_length, '!',
4033 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004034 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004035 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004036 /* If signature_size is 0 then we have nothing to do. We must not call
4037 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004038
Ronald Cron5c522922020-11-14 16:35:34 +01004039 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004040
Ronald Cron5c522922020-11-14 16:35:34 +01004041 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004042}
4043
Ronald Croncf56a0a2020-08-04 09:51:30 +02004044psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004045 psa_algorithm_t alg,
4046 const uint8_t *hash,
4047 size_t hash_length,
4048 const uint8_t *signature,
4049 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004050{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004051 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004052 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004053 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004054
Ronald Cron5c522922020-11-14 16:35:34 +01004055 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4056 PSA_KEY_USAGE_VERIFY_HASH,
4057 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004058 if( status != PSA_SUCCESS )
4059 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004060
Steven Cooreman55ae2172020-07-17 19:46:15 +02004061 /* Try any of the available accelerators first */
4062 status = psa_driver_wrapper_verify_hash( slot,
4063 alg,
4064 hash,
4065 hash_length,
4066 signature,
4067 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004068 if( status != PSA_ERROR_NOT_SUPPORTED ||
4069 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004070 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004071
John Durkop6ba40d12020-11-10 08:50:04 -08004072#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4073 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004074 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004075 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004076 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004077
Steven Cooreman4fed4552020-08-03 14:46:03 +02004078 status = psa_load_rsa_representation( slot->attr.type,
4079 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004080 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004081 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004082 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004083 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004084
Steven Cooremana2371e52020-07-28 14:30:39 +02004085 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004086 alg,
4087 hash, hash_length,
4088 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004089 mbedtls_rsa_free( rsa );
4090 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004091 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004092 }
4093 else
John Durkop6ba40d12020-11-10 08:50:04 -08004094#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4095 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004096 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004097 {
John Durkop9814fa22020-11-04 12:28:15 -08004098#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4099 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004100 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004101 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004102 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004103 status = psa_load_ecp_representation( slot->attr.type,
4104 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004105 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004106 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004107 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004108 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004109 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004110 hash, hash_length,
4111 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004112 mbedtls_ecp_keypair_free( ecp );
4113 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004114 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004115 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004116 else
John Durkop9814fa22020-11-04 12:28:15 -08004117#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4118 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004119 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004120 status = PSA_ERROR_INVALID_ARGUMENT;
4121 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004122 }
itayzafrir5c753392018-05-08 11:18:38 +03004123 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004124 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004125 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004126 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004127 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004128
4129exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004130 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004131
Ronald Cron5c522922020-11-14 16:35:34 +01004132 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004133}
4134
John Durkop0e005192020-10-31 22:06:54 -07004135#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004136static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4137 mbedtls_rsa_context *rsa )
4138{
4139 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4140 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4141 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4142 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4143}
John Durkop0e005192020-10-31 22:06:54 -07004144#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004145
Ronald Croncf56a0a2020-08-04 09:51:30 +02004146psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004147 psa_algorithm_t alg,
4148 const uint8_t *input,
4149 size_t input_length,
4150 const uint8_t *salt,
4151 size_t salt_length,
4152 uint8_t *output,
4153 size_t output_size,
4154 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004155{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004156 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004157 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004158 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004159
Darryl Green5cc689a2018-07-24 15:34:10 +01004160 (void) input;
4161 (void) input_length;
4162 (void) salt;
4163 (void) output;
4164 (void) output_size;
4165
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004166 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004167
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004168 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4169 return( PSA_ERROR_INVALID_ARGUMENT );
4170
Ronald Cron5c522922020-11-14 16:35:34 +01004171 status = psa_get_and_lock_transparent_key_slot_with_policy(
4172 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004173 if( status != PSA_SUCCESS )
4174 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004175 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4176 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004177 {
4178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto exit;
4180 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004181
John Durkop6ba40d12020-11-10 08:50:04 -08004182#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4183 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004184 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004185 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004186 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004187 status = psa_load_rsa_representation( slot->attr.type,
4188 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004189 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004190 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004191 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004192 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004193
4194 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004195 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004196 status = PSA_ERROR_BUFFER_TOO_SMALL;
4197 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004198 }
John Durkop0e005192020-10-31 22:06:54 -07004199#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004200 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004201 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004202 status = mbedtls_to_psa_error(
4203 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004204 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004205 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004206 MBEDTLS_RSA_PUBLIC,
4207 input_length,
4208 input,
4209 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004210 }
4211 else
John Durkop0e005192020-10-31 22:06:54 -07004212#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4213#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004214 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004216 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004217 status = mbedtls_to_psa_error(
4218 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004219 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004220 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004221 MBEDTLS_RSA_PUBLIC,
4222 salt, salt_length,
4223 input_length,
4224 input,
4225 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004226 }
4227 else
John Durkop0e005192020-10-31 22:06:54 -07004228#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004230 status = PSA_ERROR_INVALID_ARGUMENT;
4231 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004233rsa_exit:
4234 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004235 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004236
Steven Cooremana2371e52020-07-28 14:30:39 +02004237 mbedtls_rsa_free( rsa );
4238 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004239 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004240 else
John Durkop9814fa22020-11-04 12:28:15 -08004241#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004242 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004243 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004244 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004245 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004246
4247exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004248 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004249
Ronald Cron5c522922020-11-14 16:35:34 +01004250 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004251}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252
Ronald Croncf56a0a2020-08-04 09:51:30 +02004253psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004254 psa_algorithm_t alg,
4255 const uint8_t *input,
4256 size_t input_length,
4257 const uint8_t *salt,
4258 size_t salt_length,
4259 uint8_t *output,
4260 size_t output_size,
4261 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004262{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004263 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004264 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004265 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004266
Darryl Green5cc689a2018-07-24 15:34:10 +01004267 (void) input;
4268 (void) input_length;
4269 (void) salt;
4270 (void) output;
4271 (void) output_size;
4272
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004273 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004275 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4276 return( PSA_ERROR_INVALID_ARGUMENT );
4277
Ronald Cron5c522922020-11-14 16:35:34 +01004278 status = psa_get_and_lock_transparent_key_slot_with_policy(
4279 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004280 if( status != PSA_SUCCESS )
4281 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004282 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004283 {
4284 status = PSA_ERROR_INVALID_ARGUMENT;
4285 goto exit;
4286 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287
John Durkop6ba40d12020-11-10 08:50:04 -08004288#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4289 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004290 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004292 mbedtls_rsa_context *rsa = NULL;
4293 status = psa_load_rsa_representation( slot->attr.type,
4294 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004295 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004296 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004297 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004298 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004299
Steven Cooremana2371e52020-07-28 14:30:39 +02004300 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004301 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004302 status = PSA_ERROR_INVALID_ARGUMENT;
4303 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004304 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004305
John Durkop0e005192020-10-31 22:06:54 -07004306#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004307 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004308 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004309 status = mbedtls_to_psa_error(
4310 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004311 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004312 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004313 MBEDTLS_RSA_PRIVATE,
4314 output_length,
4315 input,
4316 output,
4317 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004318 }
4319 else
John Durkop0e005192020-10-31 22:06:54 -07004320#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4321#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004322 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004323 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004324 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004325 status = mbedtls_to_psa_error(
4326 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004327 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004328 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004329 MBEDTLS_RSA_PRIVATE,
4330 salt, salt_length,
4331 output_length,
4332 input,
4333 output,
4334 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335 }
4336 else
John Durkop0e005192020-10-31 22:06:54 -07004337#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004339 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004341
Steven Cooreman4fed4552020-08-03 14:46:03 +02004342rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004343 mbedtls_rsa_free( rsa );
4344 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004345 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004346 else
John Durkop6ba40d12020-11-10 08:50:04 -08004347#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4348 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004349 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004350 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004351 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004352
4353exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004354 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004355
Ronald Cron5c522922020-11-14 16:35:34 +01004356 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004357}
Gilles Peskine20035e32018-02-03 22:44:14 +01004358
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004359
4360
mohammad1603503973b2018-03-12 15:59:30 +02004361/****************************************************************/
4362/* Symmetric cryptography */
4363/****************************************************************/
4364
Gilles Peskinee553c652018-06-04 16:22:46 +02004365static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004366 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004367 psa_algorithm_t alg,
4368 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004369{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004370 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004371 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004372 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004373 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004374 size_t key_bits;
4375 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004376 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4377 PSA_KEY_USAGE_ENCRYPT :
4378 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004379
Steven Cooremand3feccd2020-09-01 15:56:14 +02004380 /* A context must be freshly initialized before it can be set up. */
4381 if( operation->alg != 0 )
4382 return( PSA_ERROR_BAD_STATE );
4383
Steven Cooremana07b9972020-09-10 14:54:14 +02004384 /* The requested algorithm must be one that can be processed by cipher. */
4385 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004386 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004387
Steven Cooremanef8575e2020-09-11 11:44:50 +02004388 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004389 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004390 if( status != PSA_SUCCESS )
4391 goto exit;
4392
4393 /* Initialize the operation struct members, except for alg. The alg member
4394 * is used to indicate to psa_cipher_abort that there are resources to free,
4395 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004396 operation->key_set = 0;
4397 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004398 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004399 operation->iv_size = 0;
4400 operation->block_size = 0;
4401 if( alg == PSA_ALG_ECB_NO_PADDING )
4402 operation->iv_required = 0;
4403 else
4404 operation->iv_required = 1;
4405
Steven Cooremana07b9972020-09-10 14:54:14 +02004406 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004407 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004408 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004409 slot,
4410 alg );
4411 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004412 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004413 slot,
4414 alg );
4415
Steven Cooremanef8575e2020-09-11 11:44:50 +02004416 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004417 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004418 /* Once the driver context is initialised, it needs to be freed using
4419 * psa_cipher_abort. Indicate this through setting alg. */
4420 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004421 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004422
Steven Cooremand3feccd2020-09-01 15:56:14 +02004423 if( status != PSA_ERROR_NOT_SUPPORTED ||
4424 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4425 goto exit;
4426
Steven Cooremana07b9972020-09-10 14:54:14 +02004427 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004428 * available for the given algorithm & key. */
4429 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004430
Steven Cooremana07b9972020-09-10 14:54:14 +02004431 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004432 * psa_cipher_abort. Indicate there is something to be freed through setting
4433 * alg, and indicate the operation is being done using mbedtls crypto through
4434 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004435 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004436 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004437
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004438 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004439 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004440 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004441 {
4442 status = PSA_ERROR_NOT_SUPPORTED;
4443 goto exit;
4444 }
mohammad1603503973b2018-03-12 15:59:30 +02004445
mohammad1603503973b2018-03-12 15:59:30 +02004446 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004447 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004448 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004449
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004450#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004451 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004452 {
4453 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004454 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004455 memcpy( keys, slot->data.key.data, 16 );
4456 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004457 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4458 keys,
4459 192, cipher_operation );
4460 }
4461 else
4462#endif
4463 {
4464 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004465 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004466 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004467 }
Moran Peker41deec42018-04-04 15:43:05 +03004468 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004469 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004470
mohammad16038481e742018-03-18 13:57:31 +02004471#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004472 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004473 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004474 case PSA_ALG_CBC_NO_PADDING:
4475 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4476 MBEDTLS_PADDING_NONE );
4477 break;
4478 case PSA_ALG_CBC_PKCS7:
4479 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4480 MBEDTLS_PADDING_PKCS7 );
4481 break;
4482 default:
4483 /* The algorithm doesn't involve padding. */
4484 ret = 0;
4485 break;
4486 }
4487 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004488 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004489#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4490
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004491 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004492 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004493 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4494 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004495 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004496 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004497 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004498#if defined(MBEDTLS_CHACHA20_C)
4499 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004500 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004501 operation->iv_size = 12;
4502#endif
mohammad1603503973b2018-03-12 15:59:30 +02004503
Steven Cooreman7df02922020-09-09 15:28:49 +02004504 status = PSA_SUCCESS;
4505
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004506exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004507 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004508 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004509 if( status == PSA_SUCCESS )
4510 {
4511 /* Update operation flags for both driver and software implementations */
4512 operation->key_set = 1;
4513 }
4514 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004515 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004516
Ronald Cron5c522922020-11-14 16:35:34 +01004517 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004518
Ronald Cron5c522922020-11-14 16:35:34 +01004519 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004520}
4521
Gilles Peskinefe119512018-07-08 21:39:34 +02004522psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004523 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004524 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004525{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004526 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004527}
4528
Gilles Peskinefe119512018-07-08 21:39:34 +02004529psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004530 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004531 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004532{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004533 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004534}
4535
Gilles Peskinefe119512018-07-08 21:39:34 +02004536psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004537 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004538 size_t iv_size,
4539 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004540{
itayzafrir534bd7c2018-08-02 13:56:32 +03004541 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004542 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004543 if( operation->iv_set || ! operation->iv_required )
4544 {
4545 return( PSA_ERROR_BAD_STATE );
4546 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004547
Steven Cooremanef8575e2020-09-11 11:44:50 +02004548 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004549 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004550 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004551 iv,
4552 iv_size,
4553 iv_length );
4554 goto exit;
4555 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004556
Moran Peker41deec42018-04-04 15:43:05 +03004557 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004558 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004559 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004560 goto exit;
4561 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004562 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004563 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004564 if( ret != 0 )
4565 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004566 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004567 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004568 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004569
mohammad16038481e742018-03-18 13:57:31 +02004570 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004571 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004572
Moran Peker395db872018-05-31 14:07:14 +03004573exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004574 if( status == PSA_SUCCESS )
4575 operation->iv_set = 1;
4576 else
Moran Peker395db872018-05-31 14:07:14 +03004577 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004578 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004579}
4580
Gilles Peskinefe119512018-07-08 21:39:34 +02004581psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004582 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004583 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004584{
itayzafrir534bd7c2018-08-02 13:56:32 +03004585 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004586 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004587 if( operation->iv_set || ! operation->iv_required )
4588 {
4589 return( PSA_ERROR_BAD_STATE );
4590 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004591
Steven Cooremanef8575e2020-09-11 11:44:50 +02004592 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004593 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004594 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004595 iv,
4596 iv_length );
4597 goto exit;
4598 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004599
Moran Pekera28258c2018-05-29 16:25:04 +03004600 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004601 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004602 status = PSA_ERROR_INVALID_ARGUMENT;
4603 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004604 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004605 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4606 status = mbedtls_to_psa_error( ret );
4607exit:
4608 if( status == PSA_SUCCESS )
4609 operation->iv_set = 1;
4610 else
mohammad1603503973b2018-03-12 15:59:30 +02004611 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004612 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004613}
4614
Steven Cooreman177deba2020-09-07 17:14:14 +02004615/* Process input for which the algorithm is set to ECB mode. This requires
4616 * manual processing, since the PSA API is defined as being able to process
4617 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4618 * underlying mbedtls_cipher_update only takes full blocks. */
4619static psa_status_t psa_cipher_update_ecb_internal(
4620 mbedtls_cipher_context_t *ctx,
4621 const uint8_t *input,
4622 size_t input_length,
4623 uint8_t *output,
4624 size_t output_size,
4625 size_t *output_length )
4626{
4627 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4628 size_t block_size = ctx->cipher_info->block_size;
4629 size_t internal_output_length = 0;
4630 *output_length = 0;
4631
4632 if( input_length == 0 )
4633 {
4634 status = PSA_SUCCESS;
4635 goto exit;
4636 }
4637
4638 if( ctx->unprocessed_len > 0 )
4639 {
4640 /* Fill up to block size, and run the block if there's a full one. */
4641 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4642
4643 if( input_length < bytes_to_copy )
4644 bytes_to_copy = input_length;
4645
4646 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4647 input, bytes_to_copy );
4648 input_length -= bytes_to_copy;
4649 input += bytes_to_copy;
4650 ctx->unprocessed_len += bytes_to_copy;
4651
4652 if( ctx->unprocessed_len == block_size )
4653 {
4654 status = mbedtls_to_psa_error(
4655 mbedtls_cipher_update( ctx,
4656 ctx->unprocessed_data,
4657 block_size,
4658 output, &internal_output_length ) );
4659
4660 if( status != PSA_SUCCESS )
4661 goto exit;
4662
4663 output += internal_output_length;
4664 output_size -= internal_output_length;
4665 *output_length += internal_output_length;
4666 ctx->unprocessed_len = 0;
4667 }
4668 }
4669
4670 while( input_length >= block_size )
4671 {
4672 /* Run all full blocks we have, one by one */
4673 status = mbedtls_to_psa_error(
4674 mbedtls_cipher_update( ctx, input,
4675 block_size,
4676 output, &internal_output_length ) );
4677
4678 if( status != PSA_SUCCESS )
4679 goto exit;
4680
4681 input_length -= block_size;
4682 input += block_size;
4683
4684 output += internal_output_length;
4685 output_size -= internal_output_length;
4686 *output_length += internal_output_length;
4687 }
4688
4689 if( input_length > 0 )
4690 {
4691 /* Save unprocessed bytes for later processing */
4692 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4693 input, input_length );
4694 ctx->unprocessed_len += input_length;
4695 }
4696
4697 status = PSA_SUCCESS;
4698
4699exit:
4700 return( status );
4701}
4702
Gilles Peskinee553c652018-06-04 16:22:46 +02004703psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4704 const uint8_t *input,
4705 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004706 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004707 size_t output_size,
4708 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004709{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004710 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004711 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004712 if( operation->alg == 0 )
4713 {
4714 return( PSA_ERROR_BAD_STATE );
4715 }
4716 if( operation->iv_required && ! operation->iv_set )
4717 {
4718 return( PSA_ERROR_BAD_STATE );
4719 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004720
Steven Cooremanef8575e2020-09-11 11:44:50 +02004721 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004722 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004723 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004724 input,
4725 input_length,
4726 output,
4727 output_size,
4728 output_length );
4729 goto exit;
4730 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004731
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004732 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004733 {
4734 /* Take the unprocessed partial block left over from previous
4735 * update calls, if any, plus the input to this call. Remove
4736 * the last partial block, if any. You get the data that will be
4737 * output in this call. */
4738 expected_output_size =
4739 ( operation->ctx.cipher.unprocessed_len + input_length )
4740 / operation->block_size * operation->block_size;
4741 }
4742 else
4743 {
4744 expected_output_size = input_length;
4745 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004746
Gilles Peskine89d789c2018-06-04 17:17:16 +02004747 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004748 {
4749 status = PSA_ERROR_BUFFER_TOO_SMALL;
4750 goto exit;
4751 }
mohammad160382759612018-03-12 18:16:40 +02004752
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004753 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4754 {
4755 /* mbedtls_cipher_update has an API inconsistency: it will only
4756 * process a single block at a time in ECB mode. Abstract away that
4757 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004758 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4759 input,
4760 input_length,
4761 output,
4762 output_size,
4763 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004764 }
4765 else
4766 {
4767 status = mbedtls_to_psa_error(
4768 mbedtls_cipher_update( &operation->ctx.cipher, input,
4769 input_length, output, output_length ) );
4770 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004771exit:
4772 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004773 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004774 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004775}
4776
Gilles Peskinee553c652018-06-04 16:22:46 +02004777psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4778 uint8_t *output,
4779 size_t output_size,
4780 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004781{
David Saadab4ecc272019-02-14 13:48:10 +02004782 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004783 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004784 if( operation->alg == 0 )
4785 {
4786 return( PSA_ERROR_BAD_STATE );
4787 }
4788 if( operation->iv_required && ! operation->iv_set )
4789 {
4790 return( PSA_ERROR_BAD_STATE );
4791 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004792
Steven Cooremanef8575e2020-09-11 11:44:50 +02004793 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004794 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004795 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004796 output,
4797 output_size,
4798 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004799 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004800 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004801
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004802 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004803 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004804 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004805 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004806 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004807 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004808 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004809 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004810 }
4811
Steven Cooremanef8575e2020-09-11 11:44:50 +02004812 status = mbedtls_to_psa_error(
4813 mbedtls_cipher_finish( &operation->ctx.cipher,
4814 temp_output_buffer,
4815 output_length ) );
4816 if( status != PSA_SUCCESS )
4817 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004818
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004819 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004820 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004821 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004822 memcpy( output, temp_output_buffer, *output_length );
4823 else
Janos Follath315b51c2018-07-09 16:04:51 +01004824 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004825
Steven Cooremanef8575e2020-09-11 11:44:50 +02004826exit:
4827 if( operation->mbedtls_in_use == 1 )
4828 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004829
Steven Cooremanef8575e2020-09-11 11:44:50 +02004830 if( status == PSA_SUCCESS )
4831 return( psa_cipher_abort( operation ) );
4832 else
4833 {
4834 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004835 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004836
Steven Cooremanef8575e2020-09-11 11:44:50 +02004837 return( status );
4838 }
mohammad1603503973b2018-03-12 15:59:30 +02004839}
4840
Gilles Peskinee553c652018-06-04 16:22:46 +02004841psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4842{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004843 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004844 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004845 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004846 * in use. It's ok to call abort on such an object, and there's
4847 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004848 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004849 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004850
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004851 /* Sanity check (shouldn't happen: operation->alg should
4852 * always have been initialized to a valid value). */
4853 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4854 return( PSA_ERROR_BAD_STATE );
4855
Steven Cooremanef8575e2020-09-11 11:44:50 +02004856 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004857 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004858 else
4859 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004860
Moran Peker41deec42018-04-04 15:43:05 +03004861 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004862 operation->key_set = 0;
4863 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004864 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004865 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004866 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004867 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004868
Moran Peker395db872018-05-31 14:07:14 +03004869 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004870}
4871
Gilles Peskinea0655c32018-04-30 17:06:50 +02004872
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004873
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004874
mohammad16035955c982018-04-26 00:53:03 +03004875/****************************************************************/
4876/* AEAD */
4877/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004878
Gilles Peskineedf9a652018-08-17 18:11:56 +02004879typedef struct
4880{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004881 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004882 const mbedtls_cipher_info_t *cipher_info;
4883 union
4884 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004885 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004886#if defined(MBEDTLS_CCM_C)
4887 mbedtls_ccm_context ccm;
4888#endif /* MBEDTLS_CCM_C */
4889#if defined(MBEDTLS_GCM_C)
4890 mbedtls_gcm_context gcm;
4891#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004892#if defined(MBEDTLS_CHACHAPOLY_C)
4893 mbedtls_chachapoly_context chachapoly;
4894#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004895 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004896 psa_algorithm_t core_alg;
4897 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004898 uint8_t tag_length;
4899} aead_operation_t;
4900
Ronald Cronf95a2b12020-10-22 15:24:49 +02004901#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4902
Gilles Peskine30a9e412019-01-14 18:36:12 +01004903static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004904{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004905 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004906 {
4907#if defined(MBEDTLS_CCM_C)
4908 case PSA_ALG_CCM:
4909 mbedtls_ccm_free( &operation->ctx.ccm );
4910 break;
4911#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004912#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004913 case PSA_ALG_GCM:
4914 mbedtls_gcm_free( &operation->ctx.gcm );
4915 break;
4916#endif /* MBEDTLS_GCM_C */
4917 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004918
Ronald Cron5c522922020-11-14 16:35:34 +01004919 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004920}
4921
4922static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004923 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004924 psa_key_usage_t usage,
4925 psa_algorithm_t alg )
4926{
4927 psa_status_t status;
4928 size_t key_bits;
4929 mbedtls_cipher_id_t cipher_id;
4930
Ronald Cron5c522922020-11-14 16:35:34 +01004931 status = psa_get_and_lock_transparent_key_slot_with_policy(
4932 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004933 if( status != PSA_SUCCESS )
4934 return( status );
4935
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004936 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004937
4938 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004939 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004940 &cipher_id );
4941 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004942 {
4943 status = PSA_ERROR_NOT_SUPPORTED;
4944 goto cleanup;
4945 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004946
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004947 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004948 {
4949#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004950 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4951 operation->core_alg = PSA_ALG_CCM;
4952 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004953 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4954 * The call to mbedtls_ccm_encrypt_and_tag or
4955 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004956 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004957 {
4958 status = PSA_ERROR_INVALID_ARGUMENT;
4959 goto cleanup;
4960 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004961 mbedtls_ccm_init( &operation->ctx.ccm );
4962 status = mbedtls_to_psa_error(
4963 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004964 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004965 (unsigned int) key_bits ) );
4966 if( status != 0 )
4967 goto cleanup;
4968 break;
4969#endif /* MBEDTLS_CCM_C */
4970
4971#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004972 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4973 operation->core_alg = PSA_ALG_GCM;
4974 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004975 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4976 * The call to mbedtls_gcm_crypt_and_tag or
4977 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004978 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004979 {
4980 status = PSA_ERROR_INVALID_ARGUMENT;
4981 goto cleanup;
4982 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004983 mbedtls_gcm_init( &operation->ctx.gcm );
4984 status = mbedtls_to_psa_error(
4985 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004986 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004987 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004988 if( status != 0 )
4989 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004990 break;
4991#endif /* MBEDTLS_GCM_C */
4992
Gilles Peskine26869f22019-05-06 15:25:00 +02004993#if defined(MBEDTLS_CHACHAPOLY_C)
4994 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4995 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4996 operation->full_tag_length = 16;
4997 /* We only support the default tag length. */
4998 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004999 {
5000 status = PSA_ERROR_NOT_SUPPORTED;
5001 goto cleanup;
5002 }
Gilles Peskine26869f22019-05-06 15:25:00 +02005003 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
5004 status = mbedtls_to_psa_error(
5005 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005006 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02005007 if( status != 0 )
5008 goto cleanup;
5009 break;
5010#endif /* MBEDTLS_CHACHAPOLY_C */
5011
Gilles Peskineedf9a652018-08-17 18:11:56 +02005012 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005013 status = PSA_ERROR_NOT_SUPPORTED;
5014 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005015 }
5016
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005017 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5018 {
5019 status = PSA_ERROR_INVALID_ARGUMENT;
5020 goto cleanup;
5021 }
5022 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005023
Gilles Peskineedf9a652018-08-17 18:11:56 +02005024 return( PSA_SUCCESS );
5025
5026cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005027 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005028 return( status );
5029}
5030
Ronald Croncf56a0a2020-08-04 09:51:30 +02005031psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005032 psa_algorithm_t alg,
5033 const uint8_t *nonce,
5034 size_t nonce_length,
5035 const uint8_t *additional_data,
5036 size_t additional_data_length,
5037 const uint8_t *plaintext,
5038 size_t plaintext_length,
5039 uint8_t *ciphertext,
5040 size_t ciphertext_size,
5041 size_t *ciphertext_length )
5042{
mohammad16035955c982018-04-26 00:53:03 +03005043 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005044 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005045 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005046
mohammad1603f08a5502018-06-03 15:05:47 +03005047 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005048
Ronald Croncf56a0a2020-08-04 09:51:30 +02005049 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005050 if( status != PSA_SUCCESS )
5051 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005052
Gilles Peskineedf9a652018-08-17 18:11:56 +02005053 /* For all currently supported modes, the tag is at the end of the
5054 * ciphertext. */
5055 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5056 {
5057 status = PSA_ERROR_BUFFER_TOO_SMALL;
5058 goto exit;
5059 }
5060 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005061
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005062#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005063 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005064 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005065 status = mbedtls_to_psa_error(
5066 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5067 MBEDTLS_GCM_ENCRYPT,
5068 plaintext_length,
5069 nonce, nonce_length,
5070 additional_data, additional_data_length,
5071 plaintext, ciphertext,
5072 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005073 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005074 else
5075#endif /* MBEDTLS_GCM_C */
5076#if defined(MBEDTLS_CCM_C)
5077 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005078 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005079 status = mbedtls_to_psa_error(
5080 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5081 plaintext_length,
5082 nonce, nonce_length,
5083 additional_data,
5084 additional_data_length,
5085 plaintext, ciphertext,
5086 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005087 }
mohammad16035c8845f2018-05-09 05:40:09 -07005088 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005089#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005090#if defined(MBEDTLS_CHACHAPOLY_C)
5091 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5092 {
5093 if( nonce_length != 12 || operation.tag_length != 16 )
5094 {
5095 status = PSA_ERROR_NOT_SUPPORTED;
5096 goto exit;
5097 }
5098 status = mbedtls_to_psa_error(
5099 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5100 plaintext_length,
5101 nonce,
5102 additional_data,
5103 additional_data_length,
5104 plaintext,
5105 ciphertext,
5106 tag ) );
5107 }
5108 else
5109#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005110 {
mohammad1603554faad2018-06-03 15:07:38 +03005111 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005112 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005113
Gilles Peskineedf9a652018-08-17 18:11:56 +02005114 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5115 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005116
Gilles Peskineedf9a652018-08-17 18:11:56 +02005117exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005118 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005119 if( status == PSA_SUCCESS )
5120 *ciphertext_length = plaintext_length + operation.tag_length;
5121 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005122}
5123
Gilles Peskineee652a32018-06-01 19:23:52 +02005124/* Locate the tag in a ciphertext buffer containing the encrypted data
5125 * followed by the tag. Return the length of the part preceding the tag in
5126 * *plaintext_length. This is the size of the plaintext in modes where
5127 * the encrypted data has the same size as the plaintext, such as
5128 * CCM and GCM. */
5129static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5130 const uint8_t *ciphertext,
5131 size_t ciphertext_length,
5132 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005133 const uint8_t **p_tag )
5134{
5135 size_t payload_length;
5136 if( tag_length > ciphertext_length )
5137 return( PSA_ERROR_INVALID_ARGUMENT );
5138 payload_length = ciphertext_length - tag_length;
5139 if( payload_length > plaintext_size )
5140 return( PSA_ERROR_BUFFER_TOO_SMALL );
5141 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005142 return( PSA_SUCCESS );
5143}
5144
Ronald Croncf56a0a2020-08-04 09:51:30 +02005145psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005146 psa_algorithm_t alg,
5147 const uint8_t *nonce,
5148 size_t nonce_length,
5149 const uint8_t *additional_data,
5150 size_t additional_data_length,
5151 const uint8_t *ciphertext,
5152 size_t ciphertext_length,
5153 uint8_t *plaintext,
5154 size_t plaintext_size,
5155 size_t *plaintext_length )
5156{
mohammad16035955c982018-04-26 00:53:03 +03005157 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005158 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005159 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005160
Gilles Peskineee652a32018-06-01 19:23:52 +02005161 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005162
Ronald Croncf56a0a2020-08-04 09:51:30 +02005163 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005164 if( status != PSA_SUCCESS )
5165 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005166
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005167 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5168 ciphertext, ciphertext_length,
5169 plaintext_size, &tag );
5170 if( status != PSA_SUCCESS )
5171 goto exit;
5172
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005173#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005174 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005175 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005176 status = mbedtls_to_psa_error(
5177 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5178 ciphertext_length - operation.tag_length,
5179 nonce, nonce_length,
5180 additional_data,
5181 additional_data_length,
5182 tag, operation.tag_length,
5183 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005184 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005185 else
5186#endif /* MBEDTLS_GCM_C */
5187#if defined(MBEDTLS_CCM_C)
5188 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005189 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005190 status = mbedtls_to_psa_error(
5191 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5192 ciphertext_length - operation.tag_length,
5193 nonce, nonce_length,
5194 additional_data,
5195 additional_data_length,
5196 ciphertext, plaintext,
5197 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005198 }
mohammad160339574652018-06-01 04:39:53 -07005199 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005200#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005201#if defined(MBEDTLS_CHACHAPOLY_C)
5202 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5203 {
5204 if( nonce_length != 12 || operation.tag_length != 16 )
5205 {
5206 status = PSA_ERROR_NOT_SUPPORTED;
5207 goto exit;
5208 }
5209 status = mbedtls_to_psa_error(
5210 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5211 ciphertext_length - operation.tag_length,
5212 nonce,
5213 additional_data,
5214 additional_data_length,
5215 tag,
5216 ciphertext,
5217 plaintext ) );
5218 }
5219 else
5220#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005221 {
mohammad1603554faad2018-06-03 15:07:38 +03005222 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005223 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005224
Gilles Peskineedf9a652018-08-17 18:11:56 +02005225 if( status != PSA_SUCCESS && plaintext_size != 0 )
5226 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005227
Gilles Peskineedf9a652018-08-17 18:11:56 +02005228exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005229 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005230 if( status == PSA_SUCCESS )
5231 *plaintext_length = ciphertext_length - operation.tag_length;
5232 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005233}
5234
Gilles Peskinea0655c32018-04-30 17:06:50 +02005235
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005236
Gilles Peskine20035e32018-02-03 22:44:14 +01005237/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005238/* Generators */
5239/****************************************************************/
5240
John Durkop07cc04a2020-11-16 22:08:34 -08005241#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5242 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5243 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5244#define AT_LEAST_ONE_BUILTIN_KDF
5245#endif
5246
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005247#define HKDF_STATE_INIT 0 /* no input yet */
5248#define HKDF_STATE_STARTED 1 /* got salt */
5249#define HKDF_STATE_KEYED 2 /* got key */
5250#define HKDF_STATE_OUTPUT 3 /* output started */
5251
Gilles Peskinecbe66502019-05-16 16:59:18 +02005252static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005254{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005255 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5256 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005257 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005258 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005259}
5260
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005261psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005262{
5263 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005265 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005266 {
5267 /* The object has (apparently) been initialized but it is not
5268 * in use. It's ok to call abort on such an object, and there's
5269 * nothing to do. */
5270 }
5271 else
John Durkopd0321952020-10-29 21:37:36 -07005272#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005273 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005274 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 mbedtls_free( operation->ctx.hkdf.info );
5276 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005277 }
John Durkop07cc04a2020-11-16 22:08:34 -08005278 else
5279#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5280#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5281 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5282 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005283 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005284 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005285 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005286 if( operation->ctx.tls12_prf.seed != NULL )
5287 {
5288 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5289 operation->ctx.tls12_prf.seed_length );
5290 mbedtls_free( operation->ctx.tls12_prf.seed );
5291 }
5292
5293 if( operation->ctx.tls12_prf.label != NULL )
5294 {
5295 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5296 operation->ctx.tls12_prf.label_length );
5297 mbedtls_free( operation->ctx.tls12_prf.label );
5298 }
5299
5300 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5301
5302 /* We leave the fields Ai and output_block to be erased safely by the
5303 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005304 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305 else
John Durkop07cc04a2020-11-16 22:08:34 -08005306#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5307 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005308 {
5309 status = PSA_ERROR_BAD_STATE;
5310 }
Janos Follath083036a2019-06-11 10:22:26 +01005311 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005312 return( status );
5313}
5314
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005316 size_t *capacity)
5317{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005318 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005319 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005321 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005322 }
5323
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005325 return( PSA_SUCCESS );
5326}
5327
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005329 size_t capacity )
5330{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005331 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005333 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005334 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005335 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005336 return( PSA_SUCCESS );
5337}
5338
John Durkopd0321952020-10-29 21:37:36 -07005339#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005340/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005341 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005342static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005343 psa_algorithm_t hash_alg,
5344 uint8_t *output,
5345 size_t output_length )
5346{
5347 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5348 psa_status_t status;
5349
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005350 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5351 return( PSA_ERROR_BAD_STATE );
5352 hkdf->state = HKDF_STATE_OUTPUT;
5353
Gilles Peskinebef7f142018-07-12 17:22:21 +02005354 while( output_length != 0 )
5355 {
5356 /* Copy what remains of the current block */
5357 uint8_t n = hash_length - hkdf->offset_in_block;
5358 if( n > output_length )
5359 n = (uint8_t) output_length;
5360 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5361 output += n;
5362 output_length -= n;
5363 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005364 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005365 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005366 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005367 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005368 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005369 * object was corrupted or if this function is called directly
5370 * inside the library. */
5371 if( hkdf->block_number == 0xff )
5372 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005373
5374 /* We need a new block */
5375 ++hkdf->block_number;
5376 hkdf->offset_in_block = 0;
5377 status = psa_hmac_setup_internal( &hkdf->hmac,
5378 hkdf->prk, hash_length,
5379 hash_alg );
5380 if( status != PSA_SUCCESS )
5381 return( status );
5382 if( hkdf->block_number != 1 )
5383 {
5384 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5385 hkdf->output_block,
5386 hash_length );
5387 if( status != PSA_SUCCESS )
5388 return( status );
5389 }
5390 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5391 hkdf->info,
5392 hkdf->info_length );
5393 if( status != PSA_SUCCESS )
5394 return( status );
5395 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5396 &hkdf->block_number, 1 );
5397 if( status != PSA_SUCCESS )
5398 return( status );
5399 status = psa_hmac_finish_internal( &hkdf->hmac,
5400 hkdf->output_block,
5401 sizeof( hkdf->output_block ) );
5402 if( status != PSA_SUCCESS )
5403 return( status );
5404 }
5405
5406 return( PSA_SUCCESS );
5407}
John Durkop07cc04a2020-11-16 22:08:34 -08005408#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005409
John Durkop07cc04a2020-11-16 22:08:34 -08005410#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5411 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005412static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5413 psa_tls12_prf_key_derivation_t *tls12_prf,
5414 psa_algorithm_t alg )
5415{
5416 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5417 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005418 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5419 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005420
Janos Follath7742fee2019-06-17 12:58:10 +01005421 /* We can't be wanting more output after block 0xff, otherwise
5422 * the capacity check in psa_key_derivation_output_bytes() would have
5423 * prevented this call. It could happen only if the operation
5424 * object was corrupted or if this function is called directly
5425 * inside the library. */
5426 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005427 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005428
5429 /* We need a new block */
5430 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005431 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005432
5433 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5434 *
5435 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5436 *
5437 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5438 * HMAC_hash(secret, A(2) + seed) +
5439 * HMAC_hash(secret, A(3) + seed) + ...
5440 *
5441 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005442 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005443 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005444 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005445 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005446 * is currently extracted as `output_block` and where i is
5447 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005448 */
5449
Janos Follathea29bfb2019-06-19 12:21:20 +01005450 /* Save the hash context before using it, to preserve the hash state with
5451 * only the inner padding in it. We need this, because inner padding depends
5452 * on the key (secret in the RFC's terminology). */
5453 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5454 if( status != PSA_SUCCESS )
5455 goto cleanup;
5456
5457 /* Calculate A(i) where i = tls12_prf->block_number. */
5458 if( tls12_prf->block_number == 1 )
5459 {
5460 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5461 * the variable seed and in this instance means it in the context of the
5462 * P_hash function, where seed = label + seed.) */
5463 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5464 tls12_prf->label, tls12_prf->label_length );
5465 if( status != PSA_SUCCESS )
5466 goto cleanup;
5467 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5468 tls12_prf->seed, tls12_prf->seed_length );
5469 if( status != PSA_SUCCESS )
5470 goto cleanup;
5471 }
5472 else
5473 {
5474 /* A(i) = HMAC_hash(secret, A(i-1)) */
5475 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5476 tls12_prf->Ai, hash_length );
5477 if( status != PSA_SUCCESS )
5478 goto cleanup;
5479 }
5480
5481 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5482 tls12_prf->Ai, hash_length );
5483 if( status != PSA_SUCCESS )
5484 goto cleanup;
5485 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5486 if( status != PSA_SUCCESS )
5487 goto cleanup;
5488
5489 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5490 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5491 tls12_prf->Ai, hash_length );
5492 if( status != PSA_SUCCESS )
5493 goto cleanup;
5494 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5495 tls12_prf->label, tls12_prf->label_length );
5496 if( status != PSA_SUCCESS )
5497 goto cleanup;
5498 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5499 tls12_prf->seed, tls12_prf->seed_length );
5500 if( status != PSA_SUCCESS )
5501 goto cleanup;
5502 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5503 tls12_prf->output_block, hash_length );
5504 if( status != PSA_SUCCESS )
5505 goto cleanup;
5506 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5507 if( status != PSA_SUCCESS )
5508 goto cleanup;
5509
Janos Follath7742fee2019-06-17 12:58:10 +01005510
5511cleanup:
5512
Janos Follathea29bfb2019-06-19 12:21:20 +01005513 cleanup_status = psa_hash_abort( &backup );
5514 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5515 status = cleanup_status;
5516
5517 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005518}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005519
Janos Follath844eb0e2019-06-19 12:10:49 +01005520static psa_status_t psa_key_derivation_tls12_prf_read(
5521 psa_tls12_prf_key_derivation_t *tls12_prf,
5522 psa_algorithm_t alg,
5523 uint8_t *output,
5524 size_t output_length )
5525{
5526 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5527 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5528 psa_status_t status;
5529 uint8_t offset, length;
5530
5531 while( output_length != 0 )
5532 {
5533 /* Check if we have fully processed the current block. */
5534 if( tls12_prf->left_in_block == 0 )
5535 {
5536 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5537 alg );
5538 if( status != PSA_SUCCESS )
5539 return( status );
5540
5541 continue;
5542 }
5543
5544 if( tls12_prf->left_in_block > output_length )
5545 length = (uint8_t) output_length;
5546 else
5547 length = tls12_prf->left_in_block;
5548
5549 offset = hash_length - tls12_prf->left_in_block;
5550 memcpy( output, tls12_prf->output_block + offset, length );
5551 output += length;
5552 output_length -= length;
5553 tls12_prf->left_in_block -= length;
5554 }
5555
5556 return( PSA_SUCCESS );
5557}
John Durkop07cc04a2020-11-16 22:08:34 -08005558#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5559 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005560
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005561psa_status_t psa_key_derivation_output_bytes(
5562 psa_key_derivation_operation_t *operation,
5563 uint8_t *output,
5564 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005565{
5566 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005567 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005568
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005569 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005570 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005571 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005572 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005573 }
5574
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005577 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005578 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005579 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005580 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 goto exit;
5582 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005583 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005584 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005585 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005586 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005587 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5588 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005589 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005590 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005591 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005592 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005593 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005594
John Durkopd0321952020-10-29 21:37:36 -07005595#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005596 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005597 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005598 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005599 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005600 output, output_length );
5601 }
Janos Follathadbec812019-06-14 11:05:39 +01005602 else
John Durkop07cc04a2020-11-16 22:08:34 -08005603#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5604#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5605 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005606 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005607 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005608 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005609 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005610 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005611 output_length );
5612 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005613 else
John Durkop07cc04a2020-11-16 22:08:34 -08005614#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5615 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005616 {
5617 return( PSA_ERROR_BAD_STATE );
5618 }
5619
5620exit:
5621 if( status != PSA_SUCCESS )
5622 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005623 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005624 * This allows us to differentiate between exhausted operations and
5625 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5626 * operations. */
5627 psa_algorithm_t alg = operation->alg;
5628 psa_key_derivation_abort( operation );
5629 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005630 memset( output, '!', output_length );
5631 }
5632 return( status );
5633}
5634
Gilles Peskine08542d82018-07-19 17:05:42 +02005635#if defined(MBEDTLS_DES_C)
5636static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5637{
5638 if( data_size >= 8 )
5639 mbedtls_des_key_set_parity( data );
5640 if( data_size >= 16 )
5641 mbedtls_des_key_set_parity( data + 8 );
5642 if( data_size >= 24 )
5643 mbedtls_des_key_set_parity( data + 16 );
5644}
5645#endif /* MBEDTLS_DES_C */
5646
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005647static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005648 psa_key_slot_t *slot,
5649 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005650 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005651{
5652 uint8_t *data = NULL;
5653 size_t bytes = PSA_BITS_TO_BYTES( bits );
5654 psa_status_t status;
5655
Gilles Peskine8e338702019-07-30 20:06:31 +02005656 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005657 return( PSA_ERROR_INVALID_ARGUMENT );
5658 if( bits % 8 != 0 )
5659 return( PSA_ERROR_INVALID_ARGUMENT );
5660 data = mbedtls_calloc( 1, bytes );
5661 if( data == NULL )
5662 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5663
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005664 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005665 if( status != PSA_SUCCESS )
5666 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005667#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005668 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005669 psa_des_set_key_parity( data, bytes );
5670#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005672
5673exit:
5674 mbedtls_free( data );
5675 return( status );
5676}
5677
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005678psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005679 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005680 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005681{
5682 psa_status_t status;
5683 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005684 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005685
Ronald Cron81709fc2020-11-14 12:10:32 +01005686 *key = MBEDTLS_SVC_KEY_ID_INIT;
5687
Gilles Peskine0f84d622019-09-12 19:03:13 +02005688 /* Reject any attempt to create a zero-length key so that we don't
5689 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5690 if( psa_get_key_bits( attributes ) == 0 )
5691 return( PSA_ERROR_INVALID_ARGUMENT );
5692
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005693 if( ! operation->can_output_key )
5694 return( PSA_ERROR_NOT_PERMITTED );
5695
Ronald Cron81709fc2020-11-14 12:10:32 +01005696 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5697 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005698#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5699 if( driver != NULL )
5700 {
5701 /* Deriving a key in a secure element is not implemented yet. */
5702 status = PSA_ERROR_NOT_SUPPORTED;
5703 }
5704#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005705 if( status == PSA_SUCCESS )
5706 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005707 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005708 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005709 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005710 }
5711 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
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005716 return( status );
5717}
5718
Gilles Peskineeab56e42018-07-12 17:12:33 +02005719
5720
5721/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005722/* Key derivation */
5723/****************************************************************/
5724
John Durkop07cc04a2020-11-16 22:08:34 -08005725#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005726static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005727 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005728 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005729{
John Durkop07cc04a2020-11-16 22:08:34 -08005730 int is_kdf_alg_supported;
5731
Janos Follath5fe19732019-06-20 15:09:30 +01005732 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5733 * initialisers for this union leave some bytes unspecified.) */
5734 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5735
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005737#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005738 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5739 is_kdf_alg_supported = 1;
5740 else
5741#endif
5742#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5743 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5744 is_kdf_alg_supported = 1;
5745 else
5746#endif
5747#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5748 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5749 is_kdf_alg_supported = 1;
5750 else
5751#endif
5752 is_kdf_alg_supported = 0;
5753
5754 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005755 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005756 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5758 if( hash_size == 0 )
5759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005760 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5761 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005762 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005763 {
5764 return( PSA_ERROR_NOT_SUPPORTED );
5765 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005766 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005767 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005768 }
John Durkop07cc04a2020-11-16 22:08:34 -08005769
5770 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005771}
John Durkop07cc04a2020-11-16 22:08:34 -08005772#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005773
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005774psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005775 psa_algorithm_t alg )
5776{
5777 psa_status_t status;
5778
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005779 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005780 return( PSA_ERROR_BAD_STATE );
5781
Gilles Peskine6843c292019-01-18 16:44:49 +01005782 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5783 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005784#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005785 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005786 {
5787 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005788 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005789 }
5790 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5791 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005792 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005793 }
John Durkop07cc04a2020-11-16 22:08:34 -08005794#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005795 else
5796 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005797
5798 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005799 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005800 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801}
5802
John Durkopd0321952020-10-29 21:37:36 -07005803#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005804static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005805 psa_algorithm_t hash_alg,
5806 psa_key_derivation_step_t step,
5807 const uint8_t *data,
5808 size_t data_length )
5809{
5810 psa_status_t status;
5811 switch( step )
5812 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005813 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005814 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005815 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005816 status = psa_hmac_setup_internal( &hkdf->hmac,
5817 data, data_length,
5818 hash_alg );
5819 if( status != PSA_SUCCESS )
5820 return( status );
5821 hkdf->state = HKDF_STATE_STARTED;
5822 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005823 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005824 /* If no salt was provided, use an empty salt. */
5825 if( hkdf->state == HKDF_STATE_INIT )
5826 {
5827 status = psa_hmac_setup_internal( &hkdf->hmac,
5828 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005829 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005830 if( status != PSA_SUCCESS )
5831 return( status );
5832 hkdf->state = HKDF_STATE_STARTED;
5833 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005834 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005835 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005836 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5837 data, data_length );
5838 if( status != PSA_SUCCESS )
5839 return( status );
5840 status = psa_hmac_finish_internal( &hkdf->hmac,
5841 hkdf->prk,
5842 sizeof( hkdf->prk ) );
5843 if( status != PSA_SUCCESS )
5844 return( status );
5845 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5846 hkdf->block_number = 0;
5847 hkdf->state = HKDF_STATE_KEYED;
5848 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005849 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005850 if( hkdf->state == HKDF_STATE_OUTPUT )
5851 return( PSA_ERROR_BAD_STATE );
5852 if( hkdf->info_set )
5853 return( PSA_ERROR_BAD_STATE );
5854 hkdf->info_length = data_length;
5855 if( data_length != 0 )
5856 {
5857 hkdf->info = mbedtls_calloc( 1, data_length );
5858 if( hkdf->info == NULL )
5859 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5860 memcpy( hkdf->info, data, data_length );
5861 }
5862 hkdf->info_set = 1;
5863 return( PSA_SUCCESS );
5864 default:
5865 return( PSA_ERROR_INVALID_ARGUMENT );
5866 }
5867}
John Durkop07cc04a2020-11-16 22:08:34 -08005868#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005869
John Durkop07cc04a2020-11-16 22:08:34 -08005870#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5871 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005872static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5873 const uint8_t *data,
5874 size_t data_length )
5875{
5876 if( prf->state != TLS12_PRF_STATE_INIT )
5877 return( PSA_ERROR_BAD_STATE );
5878
Janos Follathd6dce9f2019-07-04 09:11:38 +01005879 if( data_length != 0 )
5880 {
5881 prf->seed = mbedtls_calloc( 1, data_length );
5882 if( prf->seed == NULL )
5883 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005884
Janos Follathd6dce9f2019-07-04 09:11:38 +01005885 memcpy( prf->seed, data, data_length );
5886 prf->seed_length = data_length;
5887 }
Janos Follathf08e2652019-06-13 09:05:41 +01005888
5889 prf->state = TLS12_PRF_STATE_SEED_SET;
5890
5891 return( PSA_SUCCESS );
5892}
5893
Janos Follath81550542019-06-13 14:26:34 +01005894static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5895 psa_algorithm_t hash_alg,
5896 const uint8_t *data,
5897 size_t data_length )
5898{
5899 psa_status_t status;
5900 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5901 return( PSA_ERROR_BAD_STATE );
5902
5903 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5904 if( status != PSA_SUCCESS )
5905 return( status );
5906
5907 prf->state = TLS12_PRF_STATE_KEY_SET;
5908
5909 return( PSA_SUCCESS );
5910}
5911
Janos Follath63028dd2019-06-13 09:15:47 +01005912static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5913 const uint8_t *data,
5914 size_t data_length )
5915{
5916 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5917 return( PSA_ERROR_BAD_STATE );
5918
Janos Follathd6dce9f2019-07-04 09:11:38 +01005919 if( data_length != 0 )
5920 {
5921 prf->label = mbedtls_calloc( 1, data_length );
5922 if( prf->label == NULL )
5923 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005924
Janos Follathd6dce9f2019-07-04 09:11:38 +01005925 memcpy( prf->label, data, data_length );
5926 prf->label_length = data_length;
5927 }
Janos Follath63028dd2019-06-13 09:15:47 +01005928
5929 prf->state = TLS12_PRF_STATE_LABEL_SET;
5930
5931 return( PSA_SUCCESS );
5932}
5933
Janos Follathb03233e2019-06-11 15:30:30 +01005934static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5935 psa_algorithm_t hash_alg,
5936 psa_key_derivation_step_t step,
5937 const uint8_t *data,
5938 size_t data_length )
5939{
Janos Follathb03233e2019-06-11 15:30:30 +01005940 switch( step )
5941 {
Janos Follathf08e2652019-06-13 09:05:41 +01005942 case PSA_KEY_DERIVATION_INPUT_SEED:
5943 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005944 case PSA_KEY_DERIVATION_INPUT_SECRET:
5945 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005946 case PSA_KEY_DERIVATION_INPUT_LABEL:
5947 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005948 default:
5949 return( PSA_ERROR_INVALID_ARGUMENT );
5950 }
5951}
John Durkop07cc04a2020-11-16 22:08:34 -08005952#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5953 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5954
5955#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5956static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5957 psa_tls12_prf_key_derivation_t *prf,
5958 psa_algorithm_t hash_alg,
5959 const uint8_t *data,
5960 size_t data_length )
5961{
5962 psa_status_t status;
5963 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5964 uint8_t *cur = pms;
5965
5966 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5967 return( PSA_ERROR_INVALID_ARGUMENT );
5968
5969 /* Quoting RFC 4279, Section 2:
5970 *
5971 * The premaster secret is formed as follows: if the PSK is N octets
5972 * long, concatenate a uint16 with the value N, N zero octets, a second
5973 * uint16 with the value N, and the PSK itself.
5974 */
5975
5976 *cur++ = ( data_length >> 8 ) & 0xff;
5977 *cur++ = ( data_length >> 0 ) & 0xff;
5978 memset( cur, 0, data_length );
5979 cur += data_length;
5980 *cur++ = pms[0];
5981 *cur++ = pms[1];
5982 memcpy( cur, data, data_length );
5983 cur += data_length;
5984
5985 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5986
5987 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5988 return( status );
5989}
Janos Follath6660f0e2019-06-17 08:44:03 +01005990
5991static psa_status_t psa_tls12_prf_psk_to_ms_input(
5992 psa_tls12_prf_key_derivation_t *prf,
5993 psa_algorithm_t hash_alg,
5994 psa_key_derivation_step_t step,
5995 const uint8_t *data,
5996 size_t data_length )
5997{
5998 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005999 {
Janos Follath6660f0e2019-06-17 08:44:03 +01006000 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
6001 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01006002 }
Janos Follath6660f0e2019-06-17 08:44:03 +01006003
6004 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
6005}
John Durkop07cc04a2020-11-16 22:08:34 -08006006#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006007
Gilles Peskineb8965192019-09-24 16:21:10 +02006008/** Check whether the given key type is acceptable for the given
6009 * input step of a key derivation.
6010 *
6011 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6012 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6013 * Both secret and non-secret inputs can alternatively have the type
6014 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6015 * that the input was passed as a buffer rather than via a key object.
6016 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006017static int psa_key_derivation_check_input_type(
6018 psa_key_derivation_step_t step,
6019 psa_key_type_t key_type )
6020{
6021 switch( step )
6022 {
6023 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006024 if( key_type == PSA_KEY_TYPE_DERIVE )
6025 return( PSA_SUCCESS );
6026 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006027 return( PSA_SUCCESS );
6028 break;
6029 case PSA_KEY_DERIVATION_INPUT_LABEL:
6030 case PSA_KEY_DERIVATION_INPUT_SALT:
6031 case PSA_KEY_DERIVATION_INPUT_INFO:
6032 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006033 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6034 return( PSA_SUCCESS );
6035 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006036 return( PSA_SUCCESS );
6037 break;
6038 }
6039 return( PSA_ERROR_INVALID_ARGUMENT );
6040}
6041
Janos Follathb80a94e2019-06-12 15:54:46 +01006042static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006043 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006044 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006045 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006046 const uint8_t *data,
6047 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006048{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006049 psa_status_t status;
6050 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6051
6052 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006053 if( status != PSA_SUCCESS )
6054 goto exit;
6055
John Durkopd0321952020-10-29 21:37:36 -07006056#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006057 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006058 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006059 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006060 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006061 step, data, data_length );
6062 }
John Durkop07cc04a2020-11-16 22:08:34 -08006063 else
6064#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6065#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6066 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006067 {
Janos Follathb03233e2019-06-11 15:30:30 +01006068 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6069 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6070 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006071 }
John Durkop07cc04a2020-11-16 22:08:34 -08006072 else
6073#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6074#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6075 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006076 {
6077 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6078 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6079 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006080 }
6081 else
John Durkop07cc04a2020-11-16 22:08:34 -08006082#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006083 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006084 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006085 return( PSA_ERROR_BAD_STATE );
6086 }
6087
Gilles Peskine224b0d62019-09-23 18:13:17 +02006088exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006089 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006090 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006091 return( status );
6092}
6093
Janos Follath51f4a0f2019-06-14 11:35:55 +01006094psa_status_t psa_key_derivation_input_bytes(
6095 psa_key_derivation_operation_t *operation,
6096 psa_key_derivation_step_t step,
6097 const uint8_t *data,
6098 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006099{
Gilles Peskineb8965192019-09-24 16:21:10 +02006100 return( psa_key_derivation_input_internal( operation, step,
6101 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006102 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006103}
6104
Janos Follath51f4a0f2019-06-14 11:35:55 +01006105psa_status_t psa_key_derivation_input_key(
6106 psa_key_derivation_operation_t *operation,
6107 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006108 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006109{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006110 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006111 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006112 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006113
Ronald Cron5c522922020-11-14 16:35:34 +01006114 status = psa_get_and_lock_transparent_key_slot_with_policy(
6115 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006116 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006117 {
6118 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006119 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006120 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006121
6122 /* Passing a key object as a SECRET input unlocks the permission
6123 * to output to a key object. */
6124 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6125 operation->can_output_key = 1;
6126
Ronald Cronf95a2b12020-10-22 15:24:49 +02006127 status = psa_key_derivation_input_internal( operation,
6128 step, slot->attr.type,
6129 slot->data.key.data,
6130 slot->data.key.bytes );
6131
Ronald Cron5c522922020-11-14 16:35:34 +01006132 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006133
Ronald Cron5c522922020-11-14 16:35:34 +01006134 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006135}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006136
6137
6138
6139/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006140/* Key agreement */
6141/****************************************************************/
6142
John Durkop6ba40d12020-11-10 08:50:04 -08006143#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006144static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6145 size_t peer_key_length,
6146 const mbedtls_ecp_keypair *our_key,
6147 uint8_t *shared_secret,
6148 size_t shared_secret_size,
6149 size_t *shared_secret_length )
6150{
Steven Cooremand4867872020-08-05 16:31:39 +02006151 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006152 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006153 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006154 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006155 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006156 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006157
Steven Cooreman4fed4552020-08-03 14:46:03 +02006158 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6159 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006160 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006161 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006162 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006163 goto exit;
6164
Jaeden Amero97271b32019-01-10 19:38:51 +00006165 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006166 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006167 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006168 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006169 status = mbedtls_to_psa_error(
6170 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6171 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006172 goto exit;
6173
Jaeden Amero97271b32019-01-10 19:38:51 +00006174 status = mbedtls_to_psa_error(
6175 mbedtls_ecdh_calc_secret( &ecdh,
6176 shared_secret_length,
6177 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006178 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006179 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006180 if( status != PSA_SUCCESS )
6181 goto exit;
6182 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6183 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006184
6185exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006186 if( status != PSA_SUCCESS )
6187 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006188 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006189 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006190 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006191
Jaeden Amero97271b32019-01-10 19:38:51 +00006192 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006193}
John Durkop6ba40d12020-11-10 08:50:04 -08006194#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006195
Gilles Peskine01d718c2018-09-18 12:01:02 +02006196#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6197
Gilles Peskine0216fe12019-04-11 21:23:21 +02006198static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6199 psa_key_slot_t *private_key,
6200 const uint8_t *peer_key,
6201 size_t peer_key_length,
6202 uint8_t *shared_secret,
6203 size_t shared_secret_size,
6204 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006205{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006206 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006207 {
John Durkop6ba40d12020-11-10 08:50:04 -08006208#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006209 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006210 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006211 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006212 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006213 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006214 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006215 private_key->data.key.data,
6216 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006217 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006218 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006219 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006220 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006221 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006222 shared_secret, shared_secret_size,
6223 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006224 mbedtls_ecp_keypair_free( ecp );
6225 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006226 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006227#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006228 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006229 (void) private_key;
6230 (void) peer_key;
6231 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006232 (void) shared_secret;
6233 (void) shared_secret_size;
6234 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006235 return( PSA_ERROR_NOT_SUPPORTED );
6236 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006237}
6238
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006239/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006240 * to potentially free embedded data structures and wipe confidential data.
6241 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006242static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006243 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006244 psa_key_slot_t *private_key,
6245 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006246 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006247{
6248 psa_status_t status;
6249 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6250 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006251 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006252
6253 /* Step 1: run the secret agreement algorithm to generate the shared
6254 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006255 status = psa_key_agreement_raw_internal( ka_alg,
6256 private_key,
6257 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006258 shared_secret,
6259 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006260 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006261 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006262 goto exit;
6263
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006264 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006265 * the shared secret. A shared secret is permitted wherever a key
6266 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006267 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006268 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006269 shared_secret,
6270 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006271exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006272 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006273 return( status );
6274}
6275
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006276psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006277 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006278 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006279 const uint8_t *peer_key,
6280 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006281{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006282 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006283 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006284 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006285
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006286 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006287 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006288 status = psa_get_and_lock_transparent_key_slot_with_policy(
6289 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006290 if( status != PSA_SUCCESS )
6291 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006292 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006293 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006294 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006295 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006296 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006297 else
6298 {
6299 /* If a private key has been added as SECRET, we allow the derived
6300 * key material to be used as a key in PSA Crypto. */
6301 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6302 operation->can_output_key = 1;
6303 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006304
Ronald Cron5c522922020-11-14 16:35:34 +01006305 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006306
Ronald Cron5c522922020-11-14 16:35:34 +01006307 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006308}
6309
Gilles Peskinebe697d82019-05-16 18:00:41 +02006310psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006311 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006312 const uint8_t *peer_key,
6313 size_t peer_key_length,
6314 uint8_t *output,
6315 size_t output_size,
6316 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006317{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006318 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006319 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006320 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006321
6322 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6323 {
6324 status = PSA_ERROR_INVALID_ARGUMENT;
6325 goto exit;
6326 }
Ronald Cron5c522922020-11-14 16:35:34 +01006327 status = psa_get_and_lock_transparent_key_slot_with_policy(
6328 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006329 if( status != PSA_SUCCESS )
6330 goto exit;
6331
6332 status = psa_key_agreement_raw_internal( alg, slot,
6333 peer_key, peer_key_length,
6334 output, output_size,
6335 output_length );
6336
6337exit:
6338 if( status != PSA_SUCCESS )
6339 {
6340 /* If an error happens and is not handled properly, the output
6341 * may be used as a key to protect sensitive data. Arrange for such
6342 * a key to be random, which is likely to result in decryption or
6343 * verification errors. This is better than filling the buffer with
6344 * some constant data such as zeros, which would result in the data
6345 * being protected with a reproducible, easily knowable key.
6346 */
6347 psa_generate_random( output, output_size );
6348 *output_length = output_size;
6349 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006350
Ronald Cron5c522922020-11-14 16:35:34 +01006351 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006352
Ronald Cron5c522922020-11-14 16:35:34 +01006353 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006354}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006355
6356
Gilles Peskine30524eb2020-11-13 17:02:26 +01006357
Gilles Peskine86a440b2018-11-12 18:39:40 +01006358/****************************************************************/
6359/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006360/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006361
Gilles Peskine30524eb2020-11-13 17:02:26 +01006362/** Initialize the PSA random generator.
6363 */
6364static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6365{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006366#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6367 memset( rng, 0, sizeof( *rng ) );
6368#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6369
Gilles Peskine30524eb2020-11-13 17:02:26 +01006370 /* Set default configuration if
6371 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6372 if( rng->entropy_init == NULL )
6373 rng->entropy_init = mbedtls_entropy_init;
6374 if( rng->entropy_free == NULL )
6375 rng->entropy_free = mbedtls_entropy_free;
6376
6377 rng->entropy_init( &rng->entropy );
6378#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6379 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6380 /* The PSA entropy injection feature depends on using NV seed as an entropy
6381 * source. Add NV seed as an entropy source for PSA entropy injection. */
6382 mbedtls_entropy_add_source( &rng->entropy,
6383 mbedtls_nv_seed_poll, NULL,
6384 MBEDTLS_ENTROPY_BLOCK_SIZE,
6385 MBEDTLS_ENTROPY_SOURCE_STRONG );
6386#endif
6387
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006388 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006389#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006390}
6391
6392/** Deinitialize the PSA random generator.
6393 */
6394static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6395{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006396#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6397 memset( rng, 0, sizeof( *rng ) );
6398#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006399 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006400 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006401#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006402}
6403
6404/** Seed the PSA random generator.
6405 */
6406static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6407{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006408#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6409 /* Do nothing: the external RNG seeds itself. */
6410 (void) rng;
6411 return( PSA_SUCCESS );
6412#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006413 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006414 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6415 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006416 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006417#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006418}
6419
Gilles Peskinee59236f2018-01-27 23:32:46 +01006420psa_status_t psa_generate_random( uint8_t *output,
6421 size_t output_size )
6422{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006423 GUARD_MODULE_INITIALIZED;
6424
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006425#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6426
6427 size_t output_length = 0;
6428 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6429 output, output_size,
6430 &output_length );
6431 if( status != PSA_SUCCESS )
6432 return( status );
6433 /* Breaking up a request into smaller chunks is currently not supported
6434 * for the extrernal RNG interface. */
6435 if( output_length != output_size )
6436 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6437 return( PSA_SUCCESS );
6438
6439#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6440
Gilles Peskine71ddab92021-01-04 21:01:07 +01006441 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006442 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006443 size_t request_size =
6444 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6445 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6446 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006447 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6448 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006449 if( ret != 0 )
6450 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006451 output_size -= request_size;
6452 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006453 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006454 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006455#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006456}
6457
Gilles Peskine8814fc42020-12-14 15:33:44 +01006458/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006459 *
6460 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6461 * `psa_generate_random(...)`. The state parameter is ignored since the
6462 * PSA API doesn't support passing an explicit state.
6463 *
6464 * In the non-external case, psa_generate_random() calls an
6465 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6466 * and semantics as mbedtls_psa_get_random(). As an optimization,
6467 * instead of doing this back-and-forth between the PSA API and the
6468 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6469 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006470 */
6471#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6472int mbedtls_psa_get_random( void *p_rng,
6473 unsigned char *output,
6474 size_t output_size )
6475{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006476 /* This function takes a pointer to the RNG state because that's what
6477 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6478 * its own state internally and doesn't let the caller access that state.
6479 * So we just ignore the state parameter, and in practice we'll pass
6480 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006481 (void) p_rng;
6482 psa_status_t status = psa_generate_random( output, output_size );
6483 if( status == PSA_SUCCESS )
6484 return( 0 );
6485 else
6486 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6487}
6488#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6489
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006490#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6491#include "mbedtls/entropy_poll.h"
6492
Gilles Peskine7228da22019-07-15 11:06:15 +02006493psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006494 size_t seed_size )
6495{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006496 if( global_data.initialized )
6497 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006498
6499 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6500 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6501 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6502 return( PSA_ERROR_INVALID_ARGUMENT );
6503
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006504 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006505}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006506#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006507
John Durkop07cc04a2020-11-16 22:08:34 -08006508#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006509static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6510 size_t domain_parameters_size,
6511 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006512{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006513 size_t i;
6514 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006515
Gilles Peskinee56e8782019-04-26 17:34:02 +02006516 if( domain_parameters_size == 0 )
6517 {
6518 *exponent = 65537;
6519 return( PSA_SUCCESS );
6520 }
6521
6522 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6523 * support values that fit in a 32-bit integer, which is larger than
6524 * int on just about every platform anyway. */
6525 if( domain_parameters_size > sizeof( acc ) )
6526 return( PSA_ERROR_NOT_SUPPORTED );
6527 for( i = 0; i < domain_parameters_size; i++ )
6528 acc = ( acc << 8 ) | domain_parameters[i];
6529 if( acc > INT_MAX )
6530 return( PSA_ERROR_NOT_SUPPORTED );
6531 *exponent = acc;
6532 return( PSA_SUCCESS );
6533}
John Durkop07cc04a2020-11-16 22:08:34 -08006534#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006535
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006536static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006537 psa_key_slot_t *slot, size_t bits,
6538 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539{
Gilles Peskine8e338702019-07-30 20:06:31 +02006540 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006541
Gilles Peskinee56e8782019-04-26 17:34:02 +02006542 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006543 return( PSA_ERROR_INVALID_ARGUMENT );
6544
Gilles Peskinee59236f2018-01-27 23:32:46 +01006545 if( key_type_is_raw_bytes( type ) )
6546 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006547 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006548
6549 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006550 if( status != PSA_SUCCESS )
6551 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006552
6553 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006554 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6555 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006556 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006557
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006558 status = psa_generate_random( slot->data.key.data,
6559 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006561 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006562
6563 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006564#if defined(MBEDTLS_DES_C)
6565 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006566 psa_des_set_key_parity( slot->data.key.data,
6567 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568#endif /* MBEDTLS_DES_C */
6569 }
6570 else
6571
John Durkop07cc04a2020-11-16 22:08:34 -08006572#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006573 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006574 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006575 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006576 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006577 int exponent;
6578 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006579 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6580 return( PSA_ERROR_NOT_SUPPORTED );
6581 /* Accept only byte-aligned keys, for the same reasons as
6582 * in psa_import_rsa_key(). */
6583 if( bits % 8 != 0 )
6584 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006585 status = psa_read_rsa_exponent( domain_parameters,
6586 domain_parameters_size,
6587 &exponent );
6588 if( status != PSA_SUCCESS )
6589 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006590 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6591 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006592 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006593 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006594 (unsigned int) bits,
6595 exponent );
6596 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006597 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006598
6599 /* Make sure to always have an export representation available */
6600 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6601
Steven Cooreman75b74362020-07-28 14:30:13 +02006602 status = psa_allocate_buffer_to_slot( slot, bytes );
6603 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006604 {
6605 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006606 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006607 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006608
6609 status = psa_export_rsa_key( type,
6610 &rsa,
6611 slot->data.key.data,
6612 bytes,
6613 &slot->data.key.bytes );
6614 mbedtls_rsa_free( &rsa );
6615 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006616 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006617 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006618 }
6619 else
John Durkop07cc04a2020-11-16 22:08:34 -08006620#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006621
John Durkop9814fa22020-11-04 12:28:15 -08006622#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006623 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006624 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006625 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006626 mbedtls_ecp_group_id grp_id =
6627 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006628 const mbedtls_ecp_curve_info *curve_info =
6629 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006630 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006631 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006632 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006633 return( PSA_ERROR_NOT_SUPPORTED );
6634 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6635 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006636 mbedtls_ecp_keypair_init( &ecp );
6637 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006638 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006639 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006640 if( ret != 0 )
6641 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006642 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006643 return( mbedtls_to_psa_error( ret ) );
6644 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006645
6646
6647 /* Make sure to always have an export representation available */
6648 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006649 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6650 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006651 {
6652 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006653 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006654 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006655
6656 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006657 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6658
6659 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006660 if( status != PSA_SUCCESS ) {
6661 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006662 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006663 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006664 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006665 }
6666 else
John Durkop9814fa22020-11-04 12:28:15 -08006667#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006668 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006669 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006670 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006671
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006672 return( PSA_SUCCESS );
6673}
Darryl Green0c6575a2018-11-07 16:05:30 +00006674
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006675psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006676 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006677{
6678 psa_status_t status;
6679 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006680 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006681
Ronald Cron81709fc2020-11-14 12:10:32 +01006682 *key = MBEDTLS_SVC_KEY_ID_INIT;
6683
Gilles Peskine0f84d622019-09-12 19:03:13 +02006684 /* Reject any attempt to create a zero-length key so that we don't
6685 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6686 if( psa_get_key_bits( attributes ) == 0 )
6687 return( PSA_ERROR_INVALID_ARGUMENT );
6688
Ronald Cron81709fc2020-11-14 12:10:32 +01006689 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6690 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006691 if( status != PSA_SUCCESS )
6692 goto exit;
6693
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006694 status = psa_driver_wrapper_generate_key( attributes,
6695 slot );
6696 if( status != PSA_ERROR_NOT_SUPPORTED ||
6697 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6698 goto exit;
6699
6700 status = psa_generate_key_internal(
6701 slot, attributes->core.bits,
6702 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006703
6704exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006705 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006706 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006707 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006708 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006709
Darryl Green0c6575a2018-11-07 16:05:30 +00006710 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006711}
6712
6713
Gilles Peskinee59236f2018-01-27 23:32:46 +01006714
6715/****************************************************************/
6716/* Module setup */
6717/****************************************************************/
6718
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006719#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006720psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6721 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6722 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6723{
6724 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6725 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006726 global_data.rng.entropy_init = entropy_init;
6727 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006728 return( PSA_SUCCESS );
6729}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006730#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006731
Gilles Peskinee59236f2018-01-27 23:32:46 +01006732void mbedtls_psa_crypto_free( void )
6733{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006734 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006735 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6736 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006737 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006738 }
6739 /* Wipe all remaining data, including configuration.
6740 * In particular, this sets all state indicator to the value
6741 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006742 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006743#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006744 /* Unregister all secure element drivers, so that we restart from
6745 * a pristine state. */
6746 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006747#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006748}
6749
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006750#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6751/** Recover a transaction that was interrupted by a power failure.
6752 *
6753 * This function is called during initialization, before psa_crypto_init()
6754 * returns. If this function returns a failure status, the initialization
6755 * fails.
6756 */
6757static psa_status_t psa_crypto_recover_transaction(
6758 const psa_crypto_transaction_t *transaction )
6759{
6760 switch( transaction->unknown.type )
6761 {
6762 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6763 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006764 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006765 * is implemented.
6766 * https://github.com/ARMmbed/mbed-crypto/issues/218
6767 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006768 default:
6769 /* We found an unsupported transaction in the storage.
6770 * We don't know what state the storage is in. Give up. */
6771 return( PSA_ERROR_STORAGE_FAILURE );
6772 }
6773}
6774#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6775
Gilles Peskinee59236f2018-01-27 23:32:46 +01006776psa_status_t psa_crypto_init( void )
6777{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006778 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006779
Gilles Peskinec6b69072018-11-20 21:42:52 +01006780 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006781 if( global_data.initialized != 0 )
6782 return( PSA_SUCCESS );
6783
Gilles Peskine30524eb2020-11-13 17:02:26 +01006784 /* Initialize and seed the random generator. */
6785 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006786 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006787 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006788 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006789 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006790 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006791
Gilles Peskine66fb1262018-12-10 16:29:04 +01006792 status = psa_initialize_key_slots( );
6793 if( status != PSA_SUCCESS )
6794 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006795
Gilles Peskined9348f22019-10-01 15:22:29 +02006796#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6797 status = psa_init_all_se_drivers( );
6798 if( status != PSA_SUCCESS )
6799 goto exit;
6800#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6801
Gilles Peskine4b734222019-07-24 15:56:31 +02006802#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006803 status = psa_crypto_load_transaction( );
6804 if( status == PSA_SUCCESS )
6805 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006806 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6807 if( status != PSA_SUCCESS )
6808 goto exit;
6809 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006810 }
6811 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6812 {
6813 /* There's no transaction to complete. It's all good. */
6814 status = PSA_SUCCESS;
6815 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006816#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006817
Gilles Peskinec6b69072018-11-20 21:42:52 +01006818 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006819 global_data.initialized = 1;
6820
6821exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006822 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006823 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006824 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006825}
6826
6827#endif /* MBEDTLS_PSA_CRYPTO_C */