blob: 7ea2a1a9ad0cbf5427ee4cdb0a47306f97b42e49 [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
Gilles Peskine5d309672019-07-12 23:47:28 +02002328#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2329 if( driver != NULL )
2330 {
2331 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002332 /* The driver should set the number of key bits, however in
2333 * case it doesn't, we initialize bits to an invalid value. */
2334 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002335 if( drv->key_management == NULL ||
2336 drv->key_management->p_import == NULL )
2337 {
2338 status = PSA_ERROR_NOT_SUPPORTED;
2339 goto exit;
2340 }
2341 status = drv->key_management->p_import(
2342 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002343 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002344 &bits );
2345 if( status != PSA_SUCCESS )
2346 goto exit;
2347 if( bits > PSA_MAX_KEY_BITS )
2348 {
2349 status = PSA_ERROR_NOT_SUPPORTED;
2350 goto exit;
2351 }
2352 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002353 }
2354 else
2355#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2356 {
2357 status = psa_import_key_into_slot( slot, data, data_length );
2358 if( status != PSA_SUCCESS )
2359 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002360 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002361 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002362 if( status != PSA_SUCCESS )
2363 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002364
Ronald Cron81709fc2020-11-14 12:10:32 +01002365 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002366exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002367 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002368 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002369
Gilles Peskine4747d192019-04-17 15:05:45 +02002370 return( status );
2371}
2372
Gilles Peskined7729582019-08-05 15:55:54 +02002373#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2374psa_status_t mbedtls_psa_register_se_key(
2375 const psa_key_attributes_t *attributes )
2376{
2377 psa_status_t status;
2378 psa_key_slot_t *slot = NULL;
2379 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002380 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002381
2382 /* Leaving attributes unspecified is not currently supported.
2383 * It could make sense to query the key type and size from the
2384 * secure element, but not all secure elements support this
2385 * and the driver HAL doesn't currently support it. */
2386 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2387 return( PSA_ERROR_NOT_SUPPORTED );
2388 if( psa_get_key_bits( attributes ) == 0 )
2389 return( PSA_ERROR_NOT_SUPPORTED );
2390
2391 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002392 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002393 if( status != PSA_SUCCESS )
2394 goto exit;
2395
Ronald Cron81709fc2020-11-14 12:10:32 +01002396 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002397
2398exit:
2399 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002400 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002401
Gilles Peskined7729582019-08-05 15:55:54 +02002402 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002403 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002404 return( status );
2405}
2406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2407
Gilles Peskinef603c712019-01-19 13:40:11 +01002408static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002409 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002410{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002411 psa_status_t status = psa_copy_key_material_into_slot( target,
2412 source->data.key.data,
2413 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002414 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002415 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002416
Steven Cooreman398aee52020-10-13 14:35:45 +02002417 target->attr.type = source->attr.type;
2418 target->attr.bits = source->attr.bits;
2419
2420 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002421}
2422
Ronald Croncf56a0a2020-08-04 09:51:30 +02002423psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002424 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002425 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002426{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002427 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002428 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002429 psa_key_slot_t *source_slot = NULL;
2430 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002431 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002432 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002433
Ronald Cron81709fc2020-11-14 12:10:32 +01002434 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2435
Ronald Cron5c522922020-11-14 16:35:34 +01002436 status = psa_get_and_lock_transparent_key_slot_with_policy(
2437 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002438 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002439 goto exit;
2440
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002441 status = psa_validate_optional_attributes( source_slot,
2442 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002443 if( status != PSA_SUCCESS )
2444 goto exit;
2445
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002446 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002447 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002448 if( status != PSA_SUCCESS )
2449 goto exit;
2450
Ronald Cron81709fc2020-11-14 12:10:32 +01002451 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2452 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002453 if( status != PSA_SUCCESS )
2454 goto exit;
2455
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002456#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2457 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002458 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002459 /* Copying to a secure element is not implemented yet. */
2460 status = PSA_ERROR_NOT_SUPPORTED;
2461 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002462 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002463#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002464
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002465 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002466 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002467 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002468
Ronald Cron81709fc2020-11-14 12:10:32 +01002469 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002470exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002471 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002472 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002473
Ronald Cron5c522922020-11-14 16:35:34 +01002474 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002475
Ronald Cron5c522922020-11-14 16:35:34 +01002476 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002477}
2478
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002479
2480
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002481/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002482/* Message digests */
2483/****************************************************************/
2484
John Durkop07cc04a2020-11-16 22:08:34 -08002485#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002486 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2487 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002488 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002489static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002490{
2491 switch( alg )
2492 {
John Durkopee4e6602020-11-27 08:48:46 -08002493#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine20035e32018-02-03 22:44:14 +01002494 case PSA_ALG_MD2:
2495 return( &mbedtls_md2_info );
2496#endif
John Durkopee4e6602020-11-27 08:48:46 -08002497#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine20035e32018-02-03 22:44:14 +01002498 case PSA_ALG_MD4:
2499 return( &mbedtls_md4_info );
2500#endif
John Durkopee4e6602020-11-27 08:48:46 -08002501#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine20035e32018-02-03 22:44:14 +01002502 case PSA_ALG_MD5:
2503 return( &mbedtls_md5_info );
2504#endif
John Durkopee4e6602020-11-27 08:48:46 -08002505#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine20035e32018-02-03 22:44:14 +01002506 case PSA_ALG_RIPEMD160:
2507 return( &mbedtls_ripemd160_info );
2508#endif
John Durkopee4e6602020-11-27 08:48:46 -08002509#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine20035e32018-02-03 22:44:14 +01002510 case PSA_ALG_SHA_1:
2511 return( &mbedtls_sha1_info );
2512#endif
John Durkopee4e6602020-11-27 08:48:46 -08002513#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine20035e32018-02-03 22:44:14 +01002514 case PSA_ALG_SHA_224:
2515 return( &mbedtls_sha224_info );
John Durkopee4e6602020-11-27 08:48:46 -08002516#endif
2517#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 case PSA_ALG_SHA_256:
2519 return( &mbedtls_sha256_info );
2520#endif
John Durkopee4e6602020-11-27 08:48:46 -08002521#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002522 case PSA_ALG_SHA_384:
2523 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002524#endif
John Durkopee4e6602020-11-27 08:48:46 -08002525#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine20035e32018-02-03 22:44:14 +01002526 case PSA_ALG_SHA_512:
2527 return( &mbedtls_sha512_info );
2528#endif
2529 default:
2530 return( NULL );
2531 }
2532}
John Durkop07cc04a2020-11-16 22:08:34 -08002533#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002534 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2535 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2536 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002537
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002538psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2539{
2540 switch( operation->alg )
2541 {
Gilles Peskine81736312018-06-26 15:04:31 +02002542 case 0:
2543 /* The object has (apparently) been initialized but it is not
2544 * in use. It's ok to call abort on such an object, and there's
2545 * nothing to do. */
2546 break;
John Durkopee4e6602020-11-27 08:48:46 -08002547#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002548 case PSA_ALG_MD2:
2549 mbedtls_md2_free( &operation->ctx.md2 );
2550 break;
2551#endif
John Durkopee4e6602020-11-27 08:48:46 -08002552#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002553 case PSA_ALG_MD4:
2554 mbedtls_md4_free( &operation->ctx.md4 );
2555 break;
2556#endif
John Durkopee4e6602020-11-27 08:48:46 -08002557#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002558 case PSA_ALG_MD5:
2559 mbedtls_md5_free( &operation->ctx.md5 );
2560 break;
2561#endif
John Durkopee4e6602020-11-27 08:48:46 -08002562#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002563 case PSA_ALG_RIPEMD160:
2564 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2565 break;
2566#endif
John Durkopee4e6602020-11-27 08:48:46 -08002567#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568 case PSA_ALG_SHA_1:
2569 mbedtls_sha1_free( &operation->ctx.sha1 );
2570 break;
2571#endif
John Durkop6ca23272020-12-03 06:01:32 -08002572#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002573 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002574 mbedtls_sha256_free( &operation->ctx.sha256 );
2575 break;
2576#endif
2577#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002578 case PSA_ALG_SHA_256:
2579 mbedtls_sha256_free( &operation->ctx.sha256 );
2580 break;
2581#endif
John Durkop6ca23272020-12-03 06:01:32 -08002582#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002583 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002584 mbedtls_sha512_free( &operation->ctx.sha512 );
2585 break;
2586#endif
2587#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002588 case PSA_ALG_SHA_512:
2589 mbedtls_sha512_free( &operation->ctx.sha512 );
2590 break;
2591#endif
2592 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002593 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002594 }
2595 operation->alg = 0;
2596 return( PSA_SUCCESS );
2597}
2598
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002599psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002600 psa_algorithm_t alg )
2601{
Janos Follath24eed8d2019-11-22 13:21:35 +00002602 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002603
2604 /* A context must be freshly initialized before it can be set up. */
2605 if( operation->alg != 0 )
2606 {
2607 return( PSA_ERROR_BAD_STATE );
2608 }
2609
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002610 switch( alg )
2611 {
John Durkopee4e6602020-11-27 08:48:46 -08002612#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002613 case PSA_ALG_MD2:
2614 mbedtls_md2_init( &operation->ctx.md2 );
2615 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2616 break;
2617#endif
John Durkopee4e6602020-11-27 08:48:46 -08002618#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002619 case PSA_ALG_MD4:
2620 mbedtls_md4_init( &operation->ctx.md4 );
2621 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2622 break;
2623#endif
John Durkopee4e6602020-11-27 08:48:46 -08002624#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002625 case PSA_ALG_MD5:
2626 mbedtls_md5_init( &operation->ctx.md5 );
2627 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2628 break;
2629#endif
John Durkopee4e6602020-11-27 08:48:46 -08002630#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002631 case PSA_ALG_RIPEMD160:
2632 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2633 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2634 break;
2635#endif
John Durkopee4e6602020-11-27 08:48:46 -08002636#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002637 case PSA_ALG_SHA_1:
2638 mbedtls_sha1_init( &operation->ctx.sha1 );
2639 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2640 break;
2641#endif
John Durkopee4e6602020-11-27 08:48:46 -08002642#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002643 case PSA_ALG_SHA_224:
2644 mbedtls_sha256_init( &operation->ctx.sha256 );
2645 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2646 break;
John Durkopee4e6602020-11-27 08:48:46 -08002647#endif
2648#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002649 case PSA_ALG_SHA_256:
2650 mbedtls_sha256_init( &operation->ctx.sha256 );
2651 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2652 break;
2653#endif
John Durkopee4e6602020-11-27 08:48:46 -08002654#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002655 case PSA_ALG_SHA_384:
2656 mbedtls_sha512_init( &operation->ctx.sha512 );
2657 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2658 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002659#endif
John Durkopee4e6602020-11-27 08:48:46 -08002660#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002661 case PSA_ALG_SHA_512:
2662 mbedtls_sha512_init( &operation->ctx.sha512 );
2663 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2664 break;
2665#endif
2666 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002667 return( PSA_ALG_IS_HASH( alg ) ?
2668 PSA_ERROR_NOT_SUPPORTED :
2669 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002670 }
2671 if( ret == 0 )
2672 operation->alg = alg;
2673 else
2674 psa_hash_abort( operation );
2675 return( mbedtls_to_psa_error( ret ) );
2676}
2677
2678psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2679 const uint8_t *input,
2680 size_t input_length )
2681{
Janos Follath24eed8d2019-11-22 13:21:35 +00002682 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002683
2684 /* Don't require hash implementations to behave correctly on a
2685 * zero-length input, which may have an invalid pointer. */
2686 if( input_length == 0 )
2687 return( PSA_SUCCESS );
2688
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002689 switch( operation->alg )
2690 {
John Durkopee4e6602020-11-27 08:48:46 -08002691#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002692 case PSA_ALG_MD2:
2693 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2694 input, input_length );
2695 break;
2696#endif
John Durkopee4e6602020-11-27 08:48:46 -08002697#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002698 case PSA_ALG_MD4:
2699 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2700 input, input_length );
2701 break;
2702#endif
John Durkopee4e6602020-11-27 08:48:46 -08002703#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002704 case PSA_ALG_MD5:
2705 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2706 input, input_length );
2707 break;
2708#endif
John Durkopee4e6602020-11-27 08:48:46 -08002709#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002710 case PSA_ALG_RIPEMD160:
2711 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2712 input, input_length );
2713 break;
2714#endif
John Durkopee4e6602020-11-27 08:48:46 -08002715#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002716 case PSA_ALG_SHA_1:
2717 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2718 input, input_length );
2719 break;
2720#endif
John Durkop6ca23272020-12-03 06:01:32 -08002721#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002722 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002723 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2724 input, input_length );
2725 break;
2726#endif
2727#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002728 case PSA_ALG_SHA_256:
2729 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2730 input, input_length );
2731 break;
2732#endif
John Durkop6ca23272020-12-03 06:01:32 -08002733#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002734 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002735 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2736 input, input_length );
2737 break;
2738#endif
2739#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002740 case PSA_ALG_SHA_512:
2741 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2742 input, input_length );
2743 break;
2744#endif
2745 default:
John Durkopee4e6602020-11-27 08:48:46 -08002746 (void)input;
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002747 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002748 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002749
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002750 if( ret != 0 )
2751 psa_hash_abort( operation );
2752 return( mbedtls_to_psa_error( ret ) );
2753}
2754
2755psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2756 uint8_t *hash,
2757 size_t hash_size,
2758 size_t *hash_length )
2759{
itayzafrir40835d42018-08-02 13:14:17 +03002760 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002761 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002762 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002763
2764 /* Fill the output buffer with something that isn't a valid hash
2765 * (barring an attack on the hash and deliberately-crafted input),
2766 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002767 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002768 /* If hash_size is 0 then hash may be NULL and then the
2769 * call to memset would have undefined behavior. */
2770 if( hash_size != 0 )
2771 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002772
2773 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002774 {
2775 status = PSA_ERROR_BUFFER_TOO_SMALL;
2776 goto exit;
2777 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002778
2779 switch( operation->alg )
2780 {
John Durkopee4e6602020-11-27 08:48:46 -08002781#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002782 case PSA_ALG_MD2:
2783 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2784 break;
2785#endif
John Durkopee4e6602020-11-27 08:48:46 -08002786#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 case PSA_ALG_MD4:
2788 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2789 break;
2790#endif
John Durkopee4e6602020-11-27 08:48:46 -08002791#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002792 case PSA_ALG_MD5:
2793 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2794 break;
2795#endif
John Durkopee4e6602020-11-27 08:48:46 -08002796#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797 case PSA_ALG_RIPEMD160:
2798 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2799 break;
2800#endif
John Durkopee4e6602020-11-27 08:48:46 -08002801#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002802 case PSA_ALG_SHA_1:
2803 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2804 break;
2805#endif
John Durkop6ca23272020-12-03 06:01:32 -08002806#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002807 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002808 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2809 break;
2810#endif
2811#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812 case PSA_ALG_SHA_256:
2813 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2814 break;
2815#endif
John Durkop6ca23272020-12-03 06:01:32 -08002816#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002817 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002818 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2819 break;
2820#endif
2821#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002822 case PSA_ALG_SHA_512:
2823 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2824 break;
2825#endif
2826 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002827 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002828 }
itayzafrir40835d42018-08-02 13:14:17 +03002829 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002830
itayzafrir40835d42018-08-02 13:14:17 +03002831exit:
2832 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002833 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002834 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002835 return( psa_hash_abort( operation ) );
2836 }
2837 else
2838 {
2839 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002840 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002841 }
2842}
2843
Gilles Peskine2d277862018-06-18 15:41:12 +02002844psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2845 const uint8_t *hash,
2846 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002847{
2848 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2849 size_t actual_hash_length;
2850 psa_status_t status = psa_hash_finish( operation,
2851 actual_hash, sizeof( actual_hash ),
2852 &actual_hash_length );
2853 if( status != PSA_SUCCESS )
2854 return( status );
2855 if( actual_hash_length != hash_length )
2856 return( PSA_ERROR_INVALID_SIGNATURE );
2857 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2858 return( PSA_ERROR_INVALID_SIGNATURE );
2859 return( PSA_SUCCESS );
2860}
2861
Gilles Peskine0a749c82019-11-28 19:33:58 +01002862psa_status_t psa_hash_compute( psa_algorithm_t alg,
2863 const uint8_t *input, size_t input_length,
2864 uint8_t *hash, size_t hash_size,
2865 size_t *hash_length )
2866{
2867 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2868 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2869
2870 *hash_length = hash_size;
2871 status = psa_hash_setup( &operation, alg );
2872 if( status != PSA_SUCCESS )
2873 goto exit;
2874 status = psa_hash_update( &operation, input, input_length );
2875 if( status != PSA_SUCCESS )
2876 goto exit;
2877 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2878 if( status != PSA_SUCCESS )
2879 goto exit;
2880
2881exit:
2882 if( status == PSA_SUCCESS )
2883 status = psa_hash_abort( &operation );
2884 else
2885 psa_hash_abort( &operation );
2886 return( status );
2887}
2888
2889psa_status_t psa_hash_compare( psa_algorithm_t alg,
2890 const uint8_t *input, size_t input_length,
2891 const uint8_t *hash, size_t hash_length )
2892{
2893 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2894 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2895
2896 status = psa_hash_setup( &operation, alg );
2897 if( status != PSA_SUCCESS )
2898 goto exit;
2899 status = psa_hash_update( &operation, input, input_length );
2900 if( status != PSA_SUCCESS )
2901 goto exit;
2902 status = psa_hash_verify( &operation, hash, hash_length );
2903 if( status != PSA_SUCCESS )
2904 goto exit;
2905
2906exit:
2907 if( status == PSA_SUCCESS )
2908 status = psa_hash_abort( &operation );
2909 else
2910 psa_hash_abort( &operation );
2911 return( status );
2912}
2913
Gilles Peskineeb35d782019-01-22 17:56:16 +01002914psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2915 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002916{
2917 if( target_operation->alg != 0 )
2918 return( PSA_ERROR_BAD_STATE );
2919
2920 switch( source_operation->alg )
2921 {
2922 case 0:
2923 return( PSA_ERROR_BAD_STATE );
John Durkopee4e6602020-11-27 08:48:46 -08002924#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD2)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002925 case PSA_ALG_MD2:
2926 mbedtls_md2_clone( &target_operation->ctx.md2,
2927 &source_operation->ctx.md2 );
2928 break;
2929#endif
John Durkopee4e6602020-11-27 08:48:46 -08002930#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD4)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002931 case PSA_ALG_MD4:
2932 mbedtls_md4_clone( &target_operation->ctx.md4,
2933 &source_operation->ctx.md4 );
2934 break;
2935#endif
John Durkopee4e6602020-11-27 08:48:46 -08002936#if defined(MBEDTLS_PSA_BUILTIN_ALG_MD5)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002937 case PSA_ALG_MD5:
2938 mbedtls_md5_clone( &target_operation->ctx.md5,
2939 &source_operation->ctx.md5 );
2940 break;
2941#endif
John Durkopee4e6602020-11-27 08:48:46 -08002942#if defined(MBEDTLS_PSA_BUILTIN_ALG_RIPEMD160)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002943 case PSA_ALG_RIPEMD160:
2944 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2945 &source_operation->ctx.ripemd160 );
2946 break;
2947#endif
John Durkopee4e6602020-11-27 08:48:46 -08002948#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_1)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002949 case PSA_ALG_SHA_1:
2950 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2951 &source_operation->ctx.sha1 );
2952 break;
2953#endif
John Durkop6ca23272020-12-03 06:01:32 -08002954#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_224)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002955 case PSA_ALG_SHA_224:
John Durkop6ca23272020-12-03 06:01:32 -08002956 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2957 &source_operation->ctx.sha256 );
2958 break;
2959#endif
2960#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_256)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002961 case PSA_ALG_SHA_256:
2962 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2963 &source_operation->ctx.sha256 );
2964 break;
2965#endif
John Durkop6ca23272020-12-03 06:01:32 -08002966#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002967 case PSA_ALG_SHA_384:
John Durkop6ca23272020-12-03 06:01:32 -08002968 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2969 &source_operation->ctx.sha512 );
2970 break;
2971#endif
2972#if defined(MBEDTLS_PSA_BUILTIN_ALG_SHA_512)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002973 case PSA_ALG_SHA_512:
2974 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2975 &source_operation->ctx.sha512 );
2976 break;
2977#endif
2978 default:
2979 return( PSA_ERROR_NOT_SUPPORTED );
2980 }
2981
2982 target_operation->alg = source_operation->alg;
2983 return( PSA_SUCCESS );
2984}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002985
2986
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002987/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002988/* MAC */
2989/****************************************************************/
2990
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002991static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002992 psa_algorithm_t alg,
2993 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002994 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002995 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002997 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002998 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003000 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003001 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02003002
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
3004 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03003005 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003006 {
Bence Szépkúti1de907d2020-12-07 18:20:28 +01003007 case PSA_ALG_STREAM_CIPHER:
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 mode = MBEDTLS_MODE_STREAM;
3009 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 case PSA_ALG_CTR:
3011 mode = MBEDTLS_MODE_CTR;
3012 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003013 case PSA_ALG_CFB:
3014 mode = MBEDTLS_MODE_CFB;
3015 break;
3016 case PSA_ALG_OFB:
3017 mode = MBEDTLS_MODE_OFB;
3018 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02003019 case PSA_ALG_ECB_NO_PADDING:
3020 mode = MBEDTLS_MODE_ECB;
3021 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003022 case PSA_ALG_CBC_NO_PADDING:
3023 mode = MBEDTLS_MODE_CBC;
3024 break;
3025 case PSA_ALG_CBC_PKCS7:
3026 mode = MBEDTLS_MODE_CBC;
3027 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003028 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 mode = MBEDTLS_MODE_CCM;
3030 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02003031 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01003032 mode = MBEDTLS_MODE_GCM;
3033 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003034 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
3035 mode = MBEDTLS_MODE_CHACHAPOLY;
3036 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037 default:
3038 return( NULL );
3039 }
3040 }
3041 else if( alg == PSA_ALG_CMAC )
3042 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043 else
3044 return( NULL );
3045
3046 switch( key_type )
3047 {
3048 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003049 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003050 break;
3051 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003052 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3053 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003054 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003055 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003056 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003057 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003058 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3059 * but two-key Triple-DES is functionally three-key Triple-DES
3060 * with K1=K3, so that's how we present it to mbedtls. */
3061 if( key_bits == 128 )
3062 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003063 break;
3064 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003065 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003066 break;
3067 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003068 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003069 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003070 case PSA_KEY_TYPE_CHACHA20:
3071 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3072 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003073 default:
3074 return( NULL );
3075 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003076 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003077 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003078
Jaeden Amero23bbb752018-06-26 14:16:54 +01003079 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3080 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003081}
3082
John Durkop6ba40d12020-11-10 08:50:04 -08003083#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003084static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003085{
Gilles Peskine2d277862018-06-18 15:41:12 +02003086 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003087 {
3088 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003089 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003090 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003091 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003092 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003093 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003094 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003095 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003096 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003097 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003098 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003099 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003100 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003101 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003102 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003103 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003104 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003105 return( 128 );
3106 default:
3107 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003108 }
3109}
John Durkop07cc04a2020-11-16 22:08:34 -08003110#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003111
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003112/* Initialize the MAC operation structure. Once this function has been
3113 * called, psa_mac_abort can run and will do the right thing. */
3114static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3115 psa_algorithm_t alg )
3116{
3117 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3118
3119 operation->alg = alg;
3120 operation->key_set = 0;
3121 operation->iv_set = 0;
3122 operation->iv_required = 0;
3123 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003124 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003125
3126#if defined(MBEDTLS_CMAC_C)
3127 if( alg == PSA_ALG_CMAC )
3128 {
3129 operation->iv_required = 0;
3130 mbedtls_cipher_init( &operation->ctx.cmac );
3131 status = PSA_SUCCESS;
3132 }
3133 else
3134#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003135#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003136 if( PSA_ALG_IS_HMAC( operation->alg ) )
3137 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003138 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3139 operation->ctx.hmac.hash_ctx.alg = 0;
3140 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003141 }
3142 else
John Durkopd0321952020-10-29 21:37:36 -07003143#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003144 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003145 if( ! PSA_ALG_IS_MAC( alg ) )
3146 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003147 }
3148
3149 if( status != PSA_SUCCESS )
3150 memset( operation, 0, sizeof( *operation ) );
3151 return( status );
3152}
3153
John Durkop6ba40d12020-11-10 08:50:04 -08003154#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003155static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3156{
Gilles Peskine3f108122018-12-07 18:14:53 +01003157 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003158 return( psa_hash_abort( &hmac->hash_ctx ) );
3159}
John Durkop6ba40d12020-11-10 08:50:04 -08003160#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003161
Gilles Peskine8c9def32018-02-08 10:02:12 +01003162psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3163{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003164 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003165 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003166 /* The object has (apparently) been initialized but it is not
3167 * in use. It's ok to call abort on such an object, and there's
3168 * nothing to do. */
3169 return( PSA_SUCCESS );
3170 }
3171 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003172#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003173 if( operation->alg == PSA_ALG_CMAC )
3174 {
3175 mbedtls_cipher_free( &operation->ctx.cmac );
3176 }
3177 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003178#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003179#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003180 if( PSA_ALG_IS_HMAC( operation->alg ) )
3181 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003182 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003183 }
3184 else
John Durkopd0321952020-10-29 21:37:36 -07003185#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003186 {
3187 /* Sanity check (shouldn't happen: operation->alg should
3188 * always have been initialized to a valid value). */
3189 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003190 }
Moran Peker41deec42018-04-04 15:43:05 +03003191
Gilles Peskine8c9def32018-02-08 10:02:12 +01003192 operation->alg = 0;
3193 operation->key_set = 0;
3194 operation->iv_set = 0;
3195 operation->iv_required = 0;
3196 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003197 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003198
Gilles Peskine8c9def32018-02-08 10:02:12 +01003199 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003200
3201bad_state:
3202 /* If abort is called on an uninitialized object, we can't trust
3203 * anything. Wipe the object in case it contains confidential data.
3204 * This may result in a memory leak if a pointer gets overwritten,
3205 * but it's too late to do anything about this. */
3206 memset( operation, 0, sizeof( *operation ) );
3207 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003208}
3209
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003210#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003211static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003212 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003213 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003214 const mbedtls_cipher_info_t *cipher_info )
3215{
Janos Follath24eed8d2019-11-22 13:21:35 +00003216 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003217
3218 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219
3220 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3221 if( ret != 0 )
3222 return( ret );
3223
3224 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003225 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003226 key_bits );
3227 return( ret );
3228}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003229#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003230
John Durkop6ba40d12020-11-10 08:50:04 -08003231#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003232static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3233 const uint8_t *key,
3234 size_t key_length,
3235 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003236{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003237 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003238 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003239 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003240 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241 psa_status_t status;
3242
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003243 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3244 * overflow below. This should never trigger if the hash algorithm
3245 * is implemented correctly. */
3246 /* The size checks against the ipad and opad buffers cannot be written
3247 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3248 * because that triggers -Wlogical-op on GCC 7.3. */
3249 if( block_size > sizeof( ipad ) )
3250 return( PSA_ERROR_NOT_SUPPORTED );
3251 if( block_size > sizeof( hmac->opad ) )
3252 return( PSA_ERROR_NOT_SUPPORTED );
3253 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003254 return( PSA_ERROR_NOT_SUPPORTED );
3255
Gilles Peskined223b522018-06-11 18:12:58 +02003256 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003257 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003258 status = psa_hash_compute( hash_alg, key, key_length,
3259 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003260 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003261 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003262 }
Gilles Peskine96889972018-07-12 17:07:03 +02003263 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3264 * but it is permitted. It is common when HMAC is used in HKDF, for
3265 * example. Don't call `memcpy` in the 0-length because `key` could be
3266 * an invalid pointer which would make the behavior undefined. */
3267 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003268 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003269
Gilles Peskined223b522018-06-11 18:12:58 +02003270 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3271 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003272 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003273 ipad[i] ^= 0x36;
3274 memset( ipad + key_length, 0x36, block_size - key_length );
3275
3276 /* Copy the key material from ipad to opad, flipping the requisite bits,
3277 * and filling the rest of opad with the requisite constant. */
3278 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003279 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3280 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003281
Gilles Peskine01126fa2018-07-12 17:04:55 +02003282 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003283 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003284 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003285
Gilles Peskine01126fa2018-07-12 17:04:55 +02003286 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003287
3288cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003289 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003290
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003291 return( status );
3292}
John Durkop6ba40d12020-11-10 08:50:04 -08003293#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003294
Gilles Peskine89167cb2018-07-08 20:12:23 +02003295static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003296 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003297 psa_algorithm_t alg,
3298 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003299{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003300 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003301 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003302 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003303 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003304 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003305 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003306 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003307 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003308
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003309 /* A context must be freshly initialized before it can be set up. */
3310 if( operation->alg != 0 )
3311 {
3312 return( PSA_ERROR_BAD_STATE );
3313 }
3314
Gilles Peskined911eb72018-08-14 15:18:45 +02003315 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003316 if( status != PSA_SUCCESS )
3317 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003318 if( is_sign )
3319 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003320
Ronald Cron5c522922020-11-14 16:35:34 +01003321 status = psa_get_and_lock_transparent_key_slot_with_policy(
3322 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003323 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003324 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003325 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003326
Gilles Peskine8c9def32018-02-08 10:02:12 +01003327#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003328 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003329 {
3330 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003331 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003332 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003333 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003334 if( cipher_info == NULL )
3335 {
3336 status = PSA_ERROR_NOT_SUPPORTED;
3337 goto exit;
3338 }
3339 operation->mac_size = cipher_info->block_size;
3340 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3341 status = mbedtls_to_psa_error( ret );
3342 }
3343 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003344#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003345#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003346 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003347 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003348 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003349 if( hash_alg == 0 )
3350 {
3351 status = PSA_ERROR_NOT_SUPPORTED;
3352 goto exit;
3353 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003354
3355 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3356 /* Sanity check. This shouldn't fail on a valid configuration. */
3357 if( operation->mac_size == 0 ||
3358 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3359 {
3360 status = PSA_ERROR_NOT_SUPPORTED;
3361 goto exit;
3362 }
3363
Gilles Peskine8e338702019-07-30 20:06:31 +02003364 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003365 {
3366 status = PSA_ERROR_INVALID_ARGUMENT;
3367 goto exit;
3368 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003369
Gilles Peskine01126fa2018-07-12 17:04:55 +02003370 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003371 slot->data.key.data,
3372 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003373 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003374 }
3375 else
John Durkopd0321952020-10-29 21:37:36 -07003376#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003377 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003378 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003379 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003380 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003381
Gilles Peskined911eb72018-08-14 15:18:45 +02003382 if( truncated == 0 )
3383 {
3384 /* The "normal" case: untruncated algorithm. Nothing to do. */
3385 }
3386 else if( truncated < 4 )
3387 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003388 /* A very short MAC is too short for security since it can be
3389 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3390 * so we make this our minimum, even though 32 bits is still
3391 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003392 status = PSA_ERROR_NOT_SUPPORTED;
3393 }
3394 else if( truncated > operation->mac_size )
3395 {
3396 /* It's impossible to "truncate" to a larger length. */
3397 status = PSA_ERROR_INVALID_ARGUMENT;
3398 }
3399 else
3400 operation->mac_size = truncated;
3401
Gilles Peskinefbfac682018-07-08 20:51:54 +02003402exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003403 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003404 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003405 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003406 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003407 else
3408 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003409 operation->key_set = 1;
3410 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003411
Ronald Cron5c522922020-11-14 16:35:34 +01003412 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003413
Ronald Cron5c522922020-11-14 16:35:34 +01003414 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003415}
3416
Gilles Peskine89167cb2018-07-08 20:12:23 +02003417psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003418 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003419 psa_algorithm_t alg )
3420{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003421 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003422}
3423
3424psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003425 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003426 psa_algorithm_t alg )
3427{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003428 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003429}
3430
Gilles Peskine8c9def32018-02-08 10:02:12 +01003431psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3432 const uint8_t *input,
3433 size_t input_length )
3434{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003435 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003436 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003437 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003438 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003439 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003440 operation->has_input = 1;
3441
Gilles Peskine8c9def32018-02-08 10:02:12 +01003442#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003443 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003444 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003445 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3446 input, input_length );
3447 status = mbedtls_to_psa_error( ret );
3448 }
3449 else
3450#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003451#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003452 if( PSA_ALG_IS_HMAC( operation->alg ) )
3453 {
3454 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3455 input_length );
3456 }
3457 else
John Durkopd0321952020-10-29 21:37:36 -07003458#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003459 {
3460 /* This shouldn't happen if `operation` was initialized by
3461 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003462 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003463 }
3464
Gilles Peskinefbfac682018-07-08 20:51:54 +02003465 if( status != PSA_SUCCESS )
3466 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003467 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003468}
3469
John Durkop6ba40d12020-11-10 08:50:04 -08003470#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003471static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3472 uint8_t *mac,
3473 size_t mac_size )
3474{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003475 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003476 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3477 size_t hash_size = 0;
3478 size_t block_size = psa_get_hash_block_size( hash_alg );
3479 psa_status_t status;
3480
Gilles Peskine01126fa2018-07-12 17:04:55 +02003481 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3482 if( status != PSA_SUCCESS )
3483 return( status );
3484 /* From here on, tmp needs to be wiped. */
3485
3486 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3487 if( status != PSA_SUCCESS )
3488 goto exit;
3489
3490 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3491 if( status != PSA_SUCCESS )
3492 goto exit;
3493
3494 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3495 if( status != PSA_SUCCESS )
3496 goto exit;
3497
Gilles Peskined911eb72018-08-14 15:18:45 +02003498 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3499 if( status != PSA_SUCCESS )
3500 goto exit;
3501
3502 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003503
3504exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003505 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003506 return( status );
3507}
John Durkop6ba40d12020-11-10 08:50:04 -08003508#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003509
mohammad16036df908f2018-04-02 08:34:15 -07003510static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003511 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003512 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003513{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003514 if( ! operation->key_set )
3515 return( PSA_ERROR_BAD_STATE );
3516 if( operation->iv_required && ! operation->iv_set )
3517 return( PSA_ERROR_BAD_STATE );
3518
Gilles Peskine8c9def32018-02-08 10:02:12 +01003519 if( mac_size < operation->mac_size )
3520 return( PSA_ERROR_BUFFER_TOO_SMALL );
3521
Gilles Peskine8c9def32018-02-08 10:02:12 +01003522#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003523 if( operation->alg == PSA_ALG_CMAC )
3524 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003525 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3526 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3527 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003528 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003529 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003530 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003531 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003532 else
3533#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003534#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003535 if( PSA_ALG_IS_HMAC( operation->alg ) )
3536 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003537 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003538 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003539 }
3540 else
John Durkopd0321952020-10-29 21:37:36 -07003541#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003542 {
3543 /* This shouldn't happen if `operation` was initialized by
3544 * a setup function. */
3545 return( PSA_ERROR_BAD_STATE );
3546 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003547}
3548
Gilles Peskineacd4be32018-07-08 19:56:25 +02003549psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3550 uint8_t *mac,
3551 size_t mac_size,
3552 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003553{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003554 psa_status_t status;
3555
Jaeden Amero252ef282019-02-15 14:05:35 +00003556 if( operation->alg == 0 )
3557 {
3558 return( PSA_ERROR_BAD_STATE );
3559 }
3560
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003561 /* Fill the output buffer with something that isn't a valid mac
3562 * (barring an attack on the mac and deliberately-crafted input),
3563 * in case the caller doesn't check the return status properly. */
3564 *mac_length = mac_size;
3565 /* If mac_size is 0 then mac may be NULL and then the
3566 * call to memset would have undefined behavior. */
3567 if( mac_size != 0 )
3568 memset( mac, '!', mac_size );
3569
Gilles Peskine89167cb2018-07-08 20:12:23 +02003570 if( ! operation->is_sign )
3571 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003572 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003573 }
mohammad16036df908f2018-04-02 08:34:15 -07003574
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003575 status = psa_mac_finish_internal( operation, mac, mac_size );
3576
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003577 if( status == PSA_SUCCESS )
3578 {
3579 status = psa_mac_abort( operation );
3580 if( status == PSA_SUCCESS )
3581 *mac_length = operation->mac_size;
3582 else
3583 memset( mac, '!', mac_size );
3584 }
3585 else
3586 psa_mac_abort( operation );
3587 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003588}
3589
Gilles Peskineacd4be32018-07-08 19:56:25 +02003590psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3591 const uint8_t *mac,
3592 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003593{
Gilles Peskine828ed142018-06-18 23:25:51 +02003594 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003595 psa_status_t status;
3596
Jaeden Amero252ef282019-02-15 14:05:35 +00003597 if( operation->alg == 0 )
3598 {
3599 return( PSA_ERROR_BAD_STATE );
3600 }
3601
Gilles Peskine89167cb2018-07-08 20:12:23 +02003602 if( operation->is_sign )
3603 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003604 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003605 }
3606 if( operation->mac_size != mac_length )
3607 {
3608 status = PSA_ERROR_INVALID_SIGNATURE;
3609 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003610 }
mohammad16036df908f2018-04-02 08:34:15 -07003611
3612 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003613 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003614 if( status != PSA_SUCCESS )
3615 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003616
3617 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3618 status = PSA_ERROR_INVALID_SIGNATURE;
3619
3620cleanup:
3621 if( status == PSA_SUCCESS )
3622 status = psa_mac_abort( operation );
3623 else
3624 psa_mac_abort( operation );
3625
Gilles Peskine3f108122018-12-07 18:14:53 +01003626 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003627
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003628 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003629}
3630
3631
Gilles Peskine20035e32018-02-03 22:44:14 +01003632
Gilles Peskine20035e32018-02-03 22:44:14 +01003633/****************************************************************/
3634/* Asymmetric cryptography */
3635/****************************************************************/
3636
John Durkop6ba40d12020-11-10 08:50:04 -08003637#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3638 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003639/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003640 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003641static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3642 size_t hash_length,
3643 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003644{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003645 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003646 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003647 *md_alg = mbedtls_md_get_type( md_info );
3648
3649 /* The Mbed TLS RSA module uses an unsigned int for hash length
3650 * parameters. Validate that it fits so that we don't risk an
3651 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003652#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003653 if( hash_length > UINT_MAX )
3654 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003655#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003656
John Durkop0e005192020-10-31 22:06:54 -07003657#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003658 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3659 * must be correct. */
3660 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3661 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003662 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003663 if( md_info == NULL )
3664 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003665 if( mbedtls_md_get_size( md_info ) != hash_length )
3666 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003667 }
John Durkop0e005192020-10-31 22:06:54 -07003668#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003669
John Durkop0e005192020-10-31 22:06:54 -07003670#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003671 /* PSS requires a hash internally. */
3672 if( PSA_ALG_IS_RSA_PSS( alg ) )
3673 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003674 if( md_info == NULL )
3675 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003676 }
John Durkop0e005192020-10-31 22:06:54 -07003677#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003678
Gilles Peskine61b91d42018-06-08 16:09:36 +02003679 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003680}
3681
Gilles Peskine2b450e32018-06-27 15:42:46 +02003682static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3683 psa_algorithm_t alg,
3684 const uint8_t *hash,
3685 size_t hash_length,
3686 uint8_t *signature,
3687 size_t signature_size,
3688 size_t *signature_length )
3689{
3690 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003691 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003692 mbedtls_md_type_t md_alg;
3693
3694 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3695 if( status != PSA_SUCCESS )
3696 return( status );
3697
Gilles Peskine630a18a2018-06-29 17:49:35 +02003698 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003699 return( PSA_ERROR_BUFFER_TOO_SMALL );
3700
John Durkop0e005192020-10-31 22:06:54 -07003701#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003702 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3703 {
3704 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3705 MBEDTLS_MD_NONE );
3706 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003707 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003708 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003709 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003710 md_alg,
3711 (unsigned int) hash_length,
3712 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003713 signature );
3714 }
3715 else
John Durkop0e005192020-10-31 22:06:54 -07003716#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3717#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003718 if( PSA_ALG_IS_RSA_PSS( alg ) )
3719 {
3720 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3721 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003722 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003723 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003724 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003725 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003726 (unsigned int) hash_length,
3727 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003728 signature );
3729 }
3730 else
John Durkop0e005192020-10-31 22:06:54 -07003731#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003732 {
3733 return( PSA_ERROR_INVALID_ARGUMENT );
3734 }
3735
3736 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003737 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738 return( mbedtls_to_psa_error( ret ) );
3739}
3740
3741static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3742 psa_algorithm_t alg,
3743 const uint8_t *hash,
3744 size_t hash_length,
3745 const uint8_t *signature,
3746 size_t signature_length )
3747{
3748 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003749 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003750 mbedtls_md_type_t md_alg;
3751
3752 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3753 if( status != PSA_SUCCESS )
3754 return( status );
3755
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003756 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3757 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003758
John Durkop0e005192020-10-31 22:06:54 -07003759#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003760 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3761 {
3762 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3763 MBEDTLS_MD_NONE );
3764 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003765 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003766 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003767 MBEDTLS_RSA_PUBLIC,
3768 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003769 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003770 hash,
3771 signature );
3772 }
3773 else
John Durkop0e005192020-10-31 22:06:54 -07003774#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3775#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003776 if( PSA_ALG_IS_RSA_PSS( alg ) )
3777 {
3778 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3779 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003780 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003781 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003782 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003783 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003784 (unsigned int) hash_length,
3785 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003786 signature );
3787 }
3788 else
John Durkop0e005192020-10-31 22:06:54 -07003789#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003790 {
3791 return( PSA_ERROR_INVALID_ARGUMENT );
3792 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003793
3794 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3795 * the rest of the signature is invalid". This has little use in
3796 * practice and PSA doesn't report this distinction. */
3797 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3798 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003799 return( mbedtls_to_psa_error( ret ) );
3800}
John Durkop6ba40d12020-11-10 08:50:04 -08003801#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3802 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003803
John Durkop6ba40d12020-11-10 08:50:04 -08003804#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3805 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003806/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3807 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3808 * (even though these functions don't modify it). */
3809static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3810 psa_algorithm_t alg,
3811 const uint8_t *hash,
3812 size_t hash_length,
3813 uint8_t *signature,
3814 size_t signature_size,
3815 size_t *signature_length )
3816{
Janos Follath24eed8d2019-11-22 13:21:35 +00003817 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003818 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003819 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003820 mbedtls_mpi_init( &r );
3821 mbedtls_mpi_init( &s );
3822
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003823 if( signature_size < 2 * curve_bytes )
3824 {
3825 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3826 goto cleanup;
3827 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003828
John Durkop0ea39e02020-10-13 19:58:20 -07003829#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003830 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3831 {
3832 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3833 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3834 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003835 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3836 &ecp->d, hash,
3837 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003838 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003839 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003840 }
3841 else
John Durkop0ea39e02020-10-13 19:58:20 -07003842#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003843 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003844 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003845 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3846 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003847 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003848 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003849 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003850
3851 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3852 signature,
3853 curve_bytes ) );
3854 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3855 signature + curve_bytes,
3856 curve_bytes ) );
3857
3858cleanup:
3859 mbedtls_mpi_free( &r );
3860 mbedtls_mpi_free( &s );
3861 if( ret == 0 )
3862 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003863 return( mbedtls_to_psa_error( ret ) );
3864}
3865
3866static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3867 const uint8_t *hash,
3868 size_t hash_length,
3869 const uint8_t *signature,
3870 size_t signature_length )
3871{
Janos Follath24eed8d2019-11-22 13:21:35 +00003872 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003873 mbedtls_mpi r, s;
3874 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3875 mbedtls_mpi_init( &r );
3876 mbedtls_mpi_init( &s );
3877
3878 if( signature_length != 2 * curve_bytes )
3879 return( PSA_ERROR_INVALID_SIGNATURE );
3880
3881 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3882 signature,
3883 curve_bytes ) );
3884 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3885 signature + curve_bytes,
3886 curve_bytes ) );
3887
Steven Cooremanacda8342020-07-24 23:09:52 +02003888 /* Check whether the public part is loaded. If not, load it. */
3889 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3890 {
3891 MBEDTLS_MPI_CHK(
3892 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01003893 mbedtls_psa_get_random, MBEDTLS_PSA_RANDOM_STATE ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003894 }
3895
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003896 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3897 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003898
3899cleanup:
3900 mbedtls_mpi_free( &r );
3901 mbedtls_mpi_free( &s );
3902 return( mbedtls_to_psa_error( ret ) );
3903}
John Durkop6ba40d12020-11-10 08:50:04 -08003904#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3905 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003906
Ronald Croncf56a0a2020-08-04 09:51:30 +02003907psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003908 psa_algorithm_t alg,
3909 const uint8_t *hash,
3910 size_t hash_length,
3911 uint8_t *signature,
3912 size_t signature_size,
3913 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003914{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003915 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003916 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003917 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003918
3919 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003920 /* Immediately reject a zero-length signature buffer. This guarantees
3921 * that signature must be a valid pointer. (On the other hand, the hash
3922 * buffer can in principle be empty since it doesn't actually have
3923 * to be a hash.) */
3924 if( signature_size == 0 )
3925 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003926
Ronald Cron5c522922020-11-14 16:35:34 +01003927 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3928 PSA_KEY_USAGE_SIGN_HASH,
3929 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003930 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003931 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003932 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003933 {
3934 status = PSA_ERROR_INVALID_ARGUMENT;
3935 goto exit;
3936 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003937
Steven Cooremancd84cb42020-07-16 20:28:36 +02003938 /* Try any of the available accelerators first */
3939 status = psa_driver_wrapper_sign_hash( slot,
3940 alg,
3941 hash,
3942 hash_length,
3943 signature,
3944 signature_size,
3945 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003946 if( status != PSA_ERROR_NOT_SUPPORTED ||
3947 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003948 goto exit;
3949
Steven Cooreman7a250572020-07-17 16:43:05 +02003950 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003951#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3952 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003953 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003954 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003955 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003956
Steven Cooreman4fed4552020-08-03 14:46:03 +02003957 status = psa_load_rsa_representation( slot->attr.type,
3958 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003959 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003960 &rsa );
3961 if( status != PSA_SUCCESS )
3962 goto exit;
3963
Steven Cooremana2371e52020-07-28 14:30:39 +02003964 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003965 alg,
3966 hash, hash_length,
3967 signature, signature_size,
3968 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003969
Steven Cooremana2371e52020-07-28 14:30:39 +02003970 mbedtls_rsa_free( rsa );
3971 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003972 }
3973 else
John Durkop6ba40d12020-11-10 08:50:04 -08003974#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3975 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003976 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003977 {
John Durkop9814fa22020-11-04 12:28:15 -08003978#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3979 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003980 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003981#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003982 PSA_ALG_IS_ECDSA( alg )
3983#else
3984 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3985#endif
3986 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003987 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003988 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003989 status = psa_load_ecp_representation( slot->attr.type,
3990 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003991 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003992 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003993 if( status != PSA_SUCCESS )
3994 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003995 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003996 alg,
3997 hash, hash_length,
3998 signature, signature_size,
3999 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004000 mbedtls_ecp_keypair_free( ecp );
4001 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004002 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004003 else
John Durkop9814fa22020-11-04 12:28:15 -08004004#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4005 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004006 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004007 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004008 }
itayzafrir5c753392018-05-08 11:18:38 +03004009 }
4010 else
itayzafrir5c753392018-05-08 11:18:38 +03004011 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004012 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004013 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004014
4015exit:
4016 /* Fill the unused part of the output buffer (the whole buffer on error,
4017 * the trailing part on success) with something that isn't a valid mac
4018 * (barring an attack on the mac and deliberately-crafted input),
4019 * in case the caller doesn't check the return status properly. */
4020 if( status == PSA_SUCCESS )
4021 memset( signature + *signature_length, '!',
4022 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02004023 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02004024 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004025 /* If signature_size is 0 then we have nothing to do. We must not call
4026 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02004027
Ronald Cron5c522922020-11-14 16:35:34 +01004028 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004029
Ronald Cron5c522922020-11-14 16:35:34 +01004030 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03004031}
4032
Ronald Croncf56a0a2020-08-04 09:51:30 +02004033psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01004034 psa_algorithm_t alg,
4035 const uint8_t *hash,
4036 size_t hash_length,
4037 const uint8_t *signature,
4038 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004039{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004040 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004041 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004042 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004043
Ronald Cron5c522922020-11-14 16:35:34 +01004044 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4045 PSA_KEY_USAGE_VERIFY_HASH,
4046 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004047 if( status != PSA_SUCCESS )
4048 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004049
Steven Cooreman55ae2172020-07-17 19:46:15 +02004050 /* Try any of the available accelerators first */
4051 status = psa_driver_wrapper_verify_hash( slot,
4052 alg,
4053 hash,
4054 hash_length,
4055 signature,
4056 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004057 if( status != PSA_ERROR_NOT_SUPPORTED ||
4058 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004059 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004060
John Durkop6ba40d12020-11-10 08:50:04 -08004061#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4062 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004063 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004064 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004065 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004066
Steven Cooreman4fed4552020-08-03 14:46:03 +02004067 status = psa_load_rsa_representation( slot->attr.type,
4068 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004069 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004070 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004071 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004072 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004073
Steven Cooremana2371e52020-07-28 14:30:39 +02004074 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004075 alg,
4076 hash, hash_length,
4077 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004078 mbedtls_rsa_free( rsa );
4079 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004080 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004081 }
4082 else
John Durkop6ba40d12020-11-10 08:50:04 -08004083#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4084 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004085 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004086 {
John Durkop9814fa22020-11-04 12:28:15 -08004087#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4088 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004089 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004090 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004091 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004092 status = psa_load_ecp_representation( slot->attr.type,
4093 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004094 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004095 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004096 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004097 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004098 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004099 hash, hash_length,
4100 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004101 mbedtls_ecp_keypair_free( ecp );
4102 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004103 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004104 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004105 else
John Durkop9814fa22020-11-04 12:28:15 -08004106#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4107 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004108 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004109 status = PSA_ERROR_INVALID_ARGUMENT;
4110 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004111 }
itayzafrir5c753392018-05-08 11:18:38 +03004112 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004113 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004114 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004115 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004116 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004117
4118exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004119 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004120
Ronald Cron5c522922020-11-14 16:35:34 +01004121 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004122}
4123
John Durkop0e005192020-10-31 22:06:54 -07004124#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004125static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4126 mbedtls_rsa_context *rsa )
4127{
4128 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4129 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4130 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4131 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4132}
John Durkop0e005192020-10-31 22:06:54 -07004133#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004134
Ronald Croncf56a0a2020-08-04 09:51:30 +02004135psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004136 psa_algorithm_t alg,
4137 const uint8_t *input,
4138 size_t input_length,
4139 const uint8_t *salt,
4140 size_t salt_length,
4141 uint8_t *output,
4142 size_t output_size,
4143 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004144{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004145 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004146 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004147 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004148
Darryl Green5cc689a2018-07-24 15:34:10 +01004149 (void) input;
4150 (void) input_length;
4151 (void) salt;
4152 (void) output;
4153 (void) output_size;
4154
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004155 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004156
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004157 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4158 return( PSA_ERROR_INVALID_ARGUMENT );
4159
Ronald Cron5c522922020-11-14 16:35:34 +01004160 status = psa_get_and_lock_transparent_key_slot_with_policy(
4161 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004162 if( status != PSA_SUCCESS )
4163 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004164 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4165 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004166 {
4167 status = PSA_ERROR_INVALID_ARGUMENT;
4168 goto exit;
4169 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004170
John Durkop6ba40d12020-11-10 08:50:04 -08004171#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4172 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004173 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004174 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004175 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004176 status = psa_load_rsa_representation( slot->attr.type,
4177 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004178 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004179 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004180 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004181 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004182
4183 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004184 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004185 status = PSA_ERROR_BUFFER_TOO_SMALL;
4186 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004187 }
John Durkop0e005192020-10-31 22:06:54 -07004188#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004189 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004190 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004191 status = mbedtls_to_psa_error(
4192 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004193 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004194 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004195 MBEDTLS_RSA_PUBLIC,
4196 input_length,
4197 input,
4198 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199 }
4200 else
John Durkop0e005192020-10-31 22:06:54 -07004201#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4202#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004203 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004204 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004205 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004206 status = mbedtls_to_psa_error(
4207 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004208 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004209 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004210 MBEDTLS_RSA_PUBLIC,
4211 salt, salt_length,
4212 input_length,
4213 input,
4214 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004215 }
4216 else
John Durkop0e005192020-10-31 22:06:54 -07004217#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004218 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004219 status = PSA_ERROR_INVALID_ARGUMENT;
4220 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004222rsa_exit:
4223 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004224 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004225
Steven Cooremana2371e52020-07-28 14:30:39 +02004226 mbedtls_rsa_free( rsa );
4227 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004228 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004229 else
John Durkop9814fa22020-11-04 12:28:15 -08004230#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004231 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004232 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004233 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004235
4236exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004237 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004238
Ronald Cron5c522922020-11-14 16:35:34 +01004239 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004240}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004241
Ronald Croncf56a0a2020-08-04 09:51:30 +02004242psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004243 psa_algorithm_t alg,
4244 const uint8_t *input,
4245 size_t input_length,
4246 const uint8_t *salt,
4247 size_t salt_length,
4248 uint8_t *output,
4249 size_t output_size,
4250 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004251{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004252 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004253 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004254 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004255
Darryl Green5cc689a2018-07-24 15:34:10 +01004256 (void) input;
4257 (void) input_length;
4258 (void) salt;
4259 (void) output;
4260 (void) output_size;
4261
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004262 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004263
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004264 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4265 return( PSA_ERROR_INVALID_ARGUMENT );
4266
Ronald Cron5c522922020-11-14 16:35:34 +01004267 status = psa_get_and_lock_transparent_key_slot_with_policy(
4268 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004269 if( status != PSA_SUCCESS )
4270 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004271 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004272 {
4273 status = PSA_ERROR_INVALID_ARGUMENT;
4274 goto exit;
4275 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004276
John Durkop6ba40d12020-11-10 08:50:04 -08004277#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4278 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004279 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004280 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004281 mbedtls_rsa_context *rsa = NULL;
4282 status = psa_load_rsa_representation( slot->attr.type,
4283 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004284 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004285 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004286 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004287 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004288
Steven Cooremana2371e52020-07-28 14:30:39 +02004289 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004290 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004291 status = PSA_ERROR_INVALID_ARGUMENT;
4292 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004293 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004294
John Durkop0e005192020-10-31 22:06:54 -07004295#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004296 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004298 status = mbedtls_to_psa_error(
4299 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004300 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004301 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004302 MBEDTLS_RSA_PRIVATE,
4303 output_length,
4304 input,
4305 output,
4306 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004307 }
4308 else
John Durkop0e005192020-10-31 22:06:54 -07004309#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4310#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004311 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004312 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004313 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004314 status = mbedtls_to_psa_error(
4315 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004316 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004317 MBEDTLS_PSA_RANDOM_STATE,
Steven Cooreman4fed4552020-08-03 14:46:03 +02004318 MBEDTLS_RSA_PRIVATE,
4319 salt, salt_length,
4320 output_length,
4321 input,
4322 output,
4323 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004324 }
4325 else
John Durkop0e005192020-10-31 22:06:54 -07004326#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004327 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004328 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004329 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004330
Steven Cooreman4fed4552020-08-03 14:46:03 +02004331rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004332 mbedtls_rsa_free( rsa );
4333 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004334 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004335 else
John Durkop6ba40d12020-11-10 08:50:04 -08004336#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4337 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004338 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004339 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004340 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004341
4342exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004343 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004344
Ronald Cron5c522922020-11-14 16:35:34 +01004345 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004346}
Gilles Peskine20035e32018-02-03 22:44:14 +01004347
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004348
4349
mohammad1603503973b2018-03-12 15:59:30 +02004350/****************************************************************/
4351/* Symmetric cryptography */
4352/****************************************************************/
4353
Gilles Peskinee553c652018-06-04 16:22:46 +02004354static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004355 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004356 psa_algorithm_t alg,
4357 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004358{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004359 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004360 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004361 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004362 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004363 size_t key_bits;
4364 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004365 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4366 PSA_KEY_USAGE_ENCRYPT :
4367 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004368
Steven Cooremand3feccd2020-09-01 15:56:14 +02004369 /* A context must be freshly initialized before it can be set up. */
4370 if( operation->alg != 0 )
4371 return( PSA_ERROR_BAD_STATE );
4372
Steven Cooremana07b9972020-09-10 14:54:14 +02004373 /* The requested algorithm must be one that can be processed by cipher. */
4374 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004375 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004376
Steven Cooremanef8575e2020-09-11 11:44:50 +02004377 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004378 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004379 if( status != PSA_SUCCESS )
4380 goto exit;
4381
4382 /* Initialize the operation struct members, except for alg. The alg member
4383 * is used to indicate to psa_cipher_abort that there are resources to free,
4384 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004385 operation->key_set = 0;
4386 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004387 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004388 operation->iv_size = 0;
4389 operation->block_size = 0;
4390 if( alg == PSA_ALG_ECB_NO_PADDING )
4391 operation->iv_required = 0;
4392 else
4393 operation->iv_required = 1;
4394
Steven Cooremana07b9972020-09-10 14:54:14 +02004395 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004396 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004397 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004398 slot,
4399 alg );
4400 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004401 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004402 slot,
4403 alg );
4404
Steven Cooremanef8575e2020-09-11 11:44:50 +02004405 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004406 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004407 /* Once the driver context is initialised, it needs to be freed using
4408 * psa_cipher_abort. Indicate this through setting alg. */
4409 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004410 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004411
Steven Cooremand3feccd2020-09-01 15:56:14 +02004412 if( status != PSA_ERROR_NOT_SUPPORTED ||
4413 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4414 goto exit;
4415
Steven Cooremana07b9972020-09-10 14:54:14 +02004416 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004417 * available for the given algorithm & key. */
4418 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004419
Steven Cooremana07b9972020-09-10 14:54:14 +02004420 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004421 * psa_cipher_abort. Indicate there is something to be freed through setting
4422 * alg, and indicate the operation is being done using mbedtls crypto through
4423 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004424 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004425 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004426
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004427 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004428 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004429 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004430 {
4431 status = PSA_ERROR_NOT_SUPPORTED;
4432 goto exit;
4433 }
mohammad1603503973b2018-03-12 15:59:30 +02004434
mohammad1603503973b2018-03-12 15:59:30 +02004435 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004436 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004437 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004438
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004439#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004440 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004441 {
4442 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004443 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004444 memcpy( keys, slot->data.key.data, 16 );
4445 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004446 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4447 keys,
4448 192, cipher_operation );
4449 }
4450 else
4451#endif
4452 {
4453 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004454 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004455 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004456 }
Moran Peker41deec42018-04-04 15:43:05 +03004457 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004458 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004459
mohammad16038481e742018-03-18 13:57:31 +02004460#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004461 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004462 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004463 case PSA_ALG_CBC_NO_PADDING:
4464 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4465 MBEDTLS_PADDING_NONE );
4466 break;
4467 case PSA_ALG_CBC_PKCS7:
4468 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4469 MBEDTLS_PADDING_PKCS7 );
4470 break;
4471 default:
4472 /* The algorithm doesn't involve padding. */
4473 ret = 0;
4474 break;
4475 }
4476 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004477 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004478#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4479
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004480 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004481 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004482 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4483 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004484 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004485 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004486 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004487#if defined(MBEDTLS_CHACHA20_C)
4488 else
Bence Szépkúticbe39532020-12-08 00:01:31 +01004489 if( alg == PSA_ALG_STREAM_CIPHER && slot->attr.type == PSA_KEY_TYPE_CHACHA20 )
Gilles Peskine26869f22019-05-06 15:25:00 +02004490 operation->iv_size = 12;
4491#endif
mohammad1603503973b2018-03-12 15:59:30 +02004492
Steven Cooreman7df02922020-09-09 15:28:49 +02004493 status = PSA_SUCCESS;
4494
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004495exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004496 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004497 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004498 if( status == PSA_SUCCESS )
4499 {
4500 /* Update operation flags for both driver and software implementations */
4501 operation->key_set = 1;
4502 }
4503 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004504 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004505
Ronald Cron5c522922020-11-14 16:35:34 +01004506 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004507
Ronald Cron5c522922020-11-14 16:35:34 +01004508 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004509}
4510
Gilles Peskinefe119512018-07-08 21:39:34 +02004511psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004512 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004513 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004514{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004515 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004516}
4517
Gilles Peskinefe119512018-07-08 21:39:34 +02004518psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004519 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004520 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004521{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004522 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004523}
4524
Gilles Peskinefe119512018-07-08 21:39:34 +02004525psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004526 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004527 size_t iv_size,
4528 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004529{
itayzafrir534bd7c2018-08-02 13:56:32 +03004530 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004531 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004532 if( operation->iv_set || ! operation->iv_required )
4533 {
4534 return( PSA_ERROR_BAD_STATE );
4535 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004536
Steven Cooremanef8575e2020-09-11 11:44:50 +02004537 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004538 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004539 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004540 iv,
4541 iv_size,
4542 iv_length );
4543 goto exit;
4544 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004545
Moran Peker41deec42018-04-04 15:43:05 +03004546 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004547 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004548 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004549 goto exit;
4550 }
Gilles Peskine5894e8e2020-12-14 14:54:06 +01004551 ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004552 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004553 if( ret != 0 )
4554 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004555 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004556 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004557 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004558
mohammad16038481e742018-03-18 13:57:31 +02004559 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004560 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004561
Moran Peker395db872018-05-31 14:07:14 +03004562exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004563 if( status == PSA_SUCCESS )
4564 operation->iv_set = 1;
4565 else
Moran Peker395db872018-05-31 14:07:14 +03004566 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004567 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004568}
4569
Gilles Peskinefe119512018-07-08 21:39:34 +02004570psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004571 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004572 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004573{
itayzafrir534bd7c2018-08-02 13:56:32 +03004574 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004575 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004576 if( operation->iv_set || ! operation->iv_required )
4577 {
4578 return( PSA_ERROR_BAD_STATE );
4579 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004580
Steven Cooremanef8575e2020-09-11 11:44:50 +02004581 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004582 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004583 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004584 iv,
4585 iv_length );
4586 goto exit;
4587 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004588
Moran Pekera28258c2018-05-29 16:25:04 +03004589 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004590 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004591 status = PSA_ERROR_INVALID_ARGUMENT;
4592 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004593 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004594 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4595 status = mbedtls_to_psa_error( ret );
4596exit:
4597 if( status == PSA_SUCCESS )
4598 operation->iv_set = 1;
4599 else
mohammad1603503973b2018-03-12 15:59:30 +02004600 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004601 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004602}
4603
Steven Cooreman177deba2020-09-07 17:14:14 +02004604/* Process input for which the algorithm is set to ECB mode. This requires
4605 * manual processing, since the PSA API is defined as being able to process
4606 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4607 * underlying mbedtls_cipher_update only takes full blocks. */
4608static psa_status_t psa_cipher_update_ecb_internal(
4609 mbedtls_cipher_context_t *ctx,
4610 const uint8_t *input,
4611 size_t input_length,
4612 uint8_t *output,
4613 size_t output_size,
4614 size_t *output_length )
4615{
4616 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4617 size_t block_size = ctx->cipher_info->block_size;
4618 size_t internal_output_length = 0;
4619 *output_length = 0;
4620
4621 if( input_length == 0 )
4622 {
4623 status = PSA_SUCCESS;
4624 goto exit;
4625 }
4626
4627 if( ctx->unprocessed_len > 0 )
4628 {
4629 /* Fill up to block size, and run the block if there's a full one. */
4630 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4631
4632 if( input_length < bytes_to_copy )
4633 bytes_to_copy = input_length;
4634
4635 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4636 input, bytes_to_copy );
4637 input_length -= bytes_to_copy;
4638 input += bytes_to_copy;
4639 ctx->unprocessed_len += bytes_to_copy;
4640
4641 if( ctx->unprocessed_len == block_size )
4642 {
4643 status = mbedtls_to_psa_error(
4644 mbedtls_cipher_update( ctx,
4645 ctx->unprocessed_data,
4646 block_size,
4647 output, &internal_output_length ) );
4648
4649 if( status != PSA_SUCCESS )
4650 goto exit;
4651
4652 output += internal_output_length;
4653 output_size -= internal_output_length;
4654 *output_length += internal_output_length;
4655 ctx->unprocessed_len = 0;
4656 }
4657 }
4658
4659 while( input_length >= block_size )
4660 {
4661 /* Run all full blocks we have, one by one */
4662 status = mbedtls_to_psa_error(
4663 mbedtls_cipher_update( ctx, input,
4664 block_size,
4665 output, &internal_output_length ) );
4666
4667 if( status != PSA_SUCCESS )
4668 goto exit;
4669
4670 input_length -= block_size;
4671 input += block_size;
4672
4673 output += internal_output_length;
4674 output_size -= internal_output_length;
4675 *output_length += internal_output_length;
4676 }
4677
4678 if( input_length > 0 )
4679 {
4680 /* Save unprocessed bytes for later processing */
4681 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4682 input, input_length );
4683 ctx->unprocessed_len += input_length;
4684 }
4685
4686 status = PSA_SUCCESS;
4687
4688exit:
4689 return( status );
4690}
4691
Gilles Peskinee553c652018-06-04 16:22:46 +02004692psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4693 const uint8_t *input,
4694 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004695 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004696 size_t output_size,
4697 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004698{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004699 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004700 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004701 if( operation->alg == 0 )
4702 {
4703 return( PSA_ERROR_BAD_STATE );
4704 }
4705 if( operation->iv_required && ! operation->iv_set )
4706 {
4707 return( PSA_ERROR_BAD_STATE );
4708 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004709
Steven Cooremanef8575e2020-09-11 11:44:50 +02004710 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004711 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004712 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004713 input,
4714 input_length,
4715 output,
4716 output_size,
4717 output_length );
4718 goto exit;
4719 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004720
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004721 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004722 {
4723 /* Take the unprocessed partial block left over from previous
4724 * update calls, if any, plus the input to this call. Remove
4725 * the last partial block, if any. You get the data that will be
4726 * output in this call. */
4727 expected_output_size =
4728 ( operation->ctx.cipher.unprocessed_len + input_length )
4729 / operation->block_size * operation->block_size;
4730 }
4731 else
4732 {
4733 expected_output_size = input_length;
4734 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004735
Gilles Peskine89d789c2018-06-04 17:17:16 +02004736 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004737 {
4738 status = PSA_ERROR_BUFFER_TOO_SMALL;
4739 goto exit;
4740 }
mohammad160382759612018-03-12 18:16:40 +02004741
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004742 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4743 {
4744 /* mbedtls_cipher_update has an API inconsistency: it will only
4745 * process a single block at a time in ECB mode. Abstract away that
4746 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004747 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4748 input,
4749 input_length,
4750 output,
4751 output_size,
4752 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004753 }
4754 else
4755 {
4756 status = mbedtls_to_psa_error(
4757 mbedtls_cipher_update( &operation->ctx.cipher, input,
4758 input_length, output, output_length ) );
4759 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004760exit:
4761 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004762 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004763 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004764}
4765
Gilles Peskinee553c652018-06-04 16:22:46 +02004766psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4767 uint8_t *output,
4768 size_t output_size,
4769 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004770{
David Saadab4ecc272019-02-14 13:48:10 +02004771 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004772 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004773 if( operation->alg == 0 )
4774 {
4775 return( PSA_ERROR_BAD_STATE );
4776 }
4777 if( operation->iv_required && ! operation->iv_set )
4778 {
4779 return( PSA_ERROR_BAD_STATE );
4780 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004781
Steven Cooremanef8575e2020-09-11 11:44:50 +02004782 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004783 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004784 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004785 output,
4786 output_size,
4787 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004788 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004789 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004790
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004791 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004792 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004793 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004794 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004795 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004796 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004797 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004798 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004799 }
4800
Steven Cooremanef8575e2020-09-11 11:44:50 +02004801 status = mbedtls_to_psa_error(
4802 mbedtls_cipher_finish( &operation->ctx.cipher,
4803 temp_output_buffer,
4804 output_length ) );
4805 if( status != PSA_SUCCESS )
4806 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004807
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004808 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004809 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004810 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004811 memcpy( output, temp_output_buffer, *output_length );
4812 else
Janos Follath315b51c2018-07-09 16:04:51 +01004813 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004814
Steven Cooremanef8575e2020-09-11 11:44:50 +02004815exit:
4816 if( operation->mbedtls_in_use == 1 )
4817 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004818
Steven Cooremanef8575e2020-09-11 11:44:50 +02004819 if( status == PSA_SUCCESS )
4820 return( psa_cipher_abort( operation ) );
4821 else
4822 {
4823 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004824 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004825
Steven Cooremanef8575e2020-09-11 11:44:50 +02004826 return( status );
4827 }
mohammad1603503973b2018-03-12 15:59:30 +02004828}
4829
Gilles Peskinee553c652018-06-04 16:22:46 +02004830psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4831{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004832 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004833 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004834 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004835 * in use. It's ok to call abort on such an object, and there's
4836 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004837 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004838 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004839
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004840 /* Sanity check (shouldn't happen: operation->alg should
4841 * always have been initialized to a valid value). */
4842 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4843 return( PSA_ERROR_BAD_STATE );
4844
Steven Cooremanef8575e2020-09-11 11:44:50 +02004845 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004846 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004847 else
4848 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004849
Moran Peker41deec42018-04-04 15:43:05 +03004850 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004851 operation->key_set = 0;
4852 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004853 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004854 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004855 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004856 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004857
Moran Peker395db872018-05-31 14:07:14 +03004858 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004859}
4860
Gilles Peskinea0655c32018-04-30 17:06:50 +02004861
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004862
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004863
mohammad16035955c982018-04-26 00:53:03 +03004864/****************************************************************/
4865/* AEAD */
4866/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004867
Gilles Peskineedf9a652018-08-17 18:11:56 +02004868typedef struct
4869{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004870 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004871 const mbedtls_cipher_info_t *cipher_info;
4872 union
4873 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004874 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004875#if defined(MBEDTLS_CCM_C)
4876 mbedtls_ccm_context ccm;
4877#endif /* MBEDTLS_CCM_C */
4878#if defined(MBEDTLS_GCM_C)
4879 mbedtls_gcm_context gcm;
4880#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004881#if defined(MBEDTLS_CHACHAPOLY_C)
4882 mbedtls_chachapoly_context chachapoly;
4883#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004885 psa_algorithm_t core_alg;
4886 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887 uint8_t tag_length;
4888} aead_operation_t;
4889
Ronald Cronf95a2b12020-10-22 15:24:49 +02004890#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4891
Gilles Peskine30a9e412019-01-14 18:36:12 +01004892static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004894 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004895 {
4896#if defined(MBEDTLS_CCM_C)
4897 case PSA_ALG_CCM:
4898 mbedtls_ccm_free( &operation->ctx.ccm );
4899 break;
4900#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004901#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004902 case PSA_ALG_GCM:
4903 mbedtls_gcm_free( &operation->ctx.gcm );
4904 break;
4905#endif /* MBEDTLS_GCM_C */
4906 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004907
Ronald Cron5c522922020-11-14 16:35:34 +01004908 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004909}
4910
4911static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004912 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004913 psa_key_usage_t usage,
4914 psa_algorithm_t alg )
4915{
4916 psa_status_t status;
4917 size_t key_bits;
4918 mbedtls_cipher_id_t cipher_id;
4919
Ronald Cron5c522922020-11-14 16:35:34 +01004920 status = psa_get_and_lock_transparent_key_slot_with_policy(
4921 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004922 if( status != PSA_SUCCESS )
4923 return( status );
4924
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004925 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004926
4927 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004928 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004929 &cipher_id );
4930 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004931 {
4932 status = PSA_ERROR_NOT_SUPPORTED;
4933 goto cleanup;
4934 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004935
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004936 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004937 {
4938#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004939 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4940 operation->core_alg = PSA_ALG_CCM;
4941 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004942 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4943 * The call to mbedtls_ccm_encrypt_and_tag or
4944 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004945 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004946 {
4947 status = PSA_ERROR_INVALID_ARGUMENT;
4948 goto cleanup;
4949 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004950 mbedtls_ccm_init( &operation->ctx.ccm );
4951 status = mbedtls_to_psa_error(
4952 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004953 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004954 (unsigned int) key_bits ) );
4955 if( status != 0 )
4956 goto cleanup;
4957 break;
4958#endif /* MBEDTLS_CCM_C */
4959
4960#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004961 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4962 operation->core_alg = PSA_ALG_GCM;
4963 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004964 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4965 * The call to mbedtls_gcm_crypt_and_tag or
4966 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004967 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004968 {
4969 status = PSA_ERROR_INVALID_ARGUMENT;
4970 goto cleanup;
4971 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004972 mbedtls_gcm_init( &operation->ctx.gcm );
4973 status = mbedtls_to_psa_error(
4974 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004975 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004976 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004977 if( status != 0 )
4978 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004979 break;
4980#endif /* MBEDTLS_GCM_C */
4981
Gilles Peskine26869f22019-05-06 15:25:00 +02004982#if defined(MBEDTLS_CHACHAPOLY_C)
4983 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4984 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4985 operation->full_tag_length = 16;
4986 /* We only support the default tag length. */
4987 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004988 {
4989 status = PSA_ERROR_NOT_SUPPORTED;
4990 goto cleanup;
4991 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004992 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4993 status = mbedtls_to_psa_error(
4994 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004995 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004996 if( status != 0 )
4997 goto cleanup;
4998 break;
4999#endif /* MBEDTLS_CHACHAPOLY_C */
5000
Gilles Peskineedf9a652018-08-17 18:11:56 +02005001 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02005002 status = PSA_ERROR_NOT_SUPPORTED;
5003 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005004 }
5005
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005006 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
5007 {
5008 status = PSA_ERROR_INVALID_ARGUMENT;
5009 goto cleanup;
5010 }
5011 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005012
Gilles Peskineedf9a652018-08-17 18:11:56 +02005013 return( PSA_SUCCESS );
5014
5015cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005016 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005017 return( status );
5018}
5019
Ronald Croncf56a0a2020-08-04 09:51:30 +02005020psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005021 psa_algorithm_t alg,
5022 const uint8_t *nonce,
5023 size_t nonce_length,
5024 const uint8_t *additional_data,
5025 size_t additional_data_length,
5026 const uint8_t *plaintext,
5027 size_t plaintext_length,
5028 uint8_t *ciphertext,
5029 size_t ciphertext_size,
5030 size_t *ciphertext_length )
5031{
mohammad16035955c982018-04-26 00:53:03 +03005032 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005033 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03005034 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02005035
mohammad1603f08a5502018-06-03 15:05:47 +03005036 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07005037
Ronald Croncf56a0a2020-08-04 09:51:30 +02005038 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005039 if( status != PSA_SUCCESS )
5040 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005041
Gilles Peskineedf9a652018-08-17 18:11:56 +02005042 /* For all currently supported modes, the tag is at the end of the
5043 * ciphertext. */
5044 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5045 {
5046 status = PSA_ERROR_BUFFER_TOO_SMALL;
5047 goto exit;
5048 }
5049 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005050
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005051#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005052 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005053 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005054 status = mbedtls_to_psa_error(
5055 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5056 MBEDTLS_GCM_ENCRYPT,
5057 plaintext_length,
5058 nonce, nonce_length,
5059 additional_data, additional_data_length,
5060 plaintext, ciphertext,
5061 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005062 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005063 else
5064#endif /* MBEDTLS_GCM_C */
5065#if defined(MBEDTLS_CCM_C)
5066 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005067 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005068 status = mbedtls_to_psa_error(
5069 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5070 plaintext_length,
5071 nonce, nonce_length,
5072 additional_data,
5073 additional_data_length,
5074 plaintext, ciphertext,
5075 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005076 }
mohammad16035c8845f2018-05-09 05:40:09 -07005077 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005078#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005079#if defined(MBEDTLS_CHACHAPOLY_C)
5080 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5081 {
5082 if( nonce_length != 12 || operation.tag_length != 16 )
5083 {
5084 status = PSA_ERROR_NOT_SUPPORTED;
5085 goto exit;
5086 }
5087 status = mbedtls_to_psa_error(
5088 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5089 plaintext_length,
5090 nonce,
5091 additional_data,
5092 additional_data_length,
5093 plaintext,
5094 ciphertext,
5095 tag ) );
5096 }
5097 else
5098#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005099 {
mohammad1603554faad2018-06-03 15:07:38 +03005100 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005101 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005102
Gilles Peskineedf9a652018-08-17 18:11:56 +02005103 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5104 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005105
Gilles Peskineedf9a652018-08-17 18:11:56 +02005106exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005107 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005108 if( status == PSA_SUCCESS )
5109 *ciphertext_length = plaintext_length + operation.tag_length;
5110 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005111}
5112
Gilles Peskineee652a32018-06-01 19:23:52 +02005113/* Locate the tag in a ciphertext buffer containing the encrypted data
5114 * followed by the tag. Return the length of the part preceding the tag in
5115 * *plaintext_length. This is the size of the plaintext in modes where
5116 * the encrypted data has the same size as the plaintext, such as
5117 * CCM and GCM. */
5118static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5119 const uint8_t *ciphertext,
5120 size_t ciphertext_length,
5121 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005122 const uint8_t **p_tag )
5123{
5124 size_t payload_length;
5125 if( tag_length > ciphertext_length )
5126 return( PSA_ERROR_INVALID_ARGUMENT );
5127 payload_length = ciphertext_length - tag_length;
5128 if( payload_length > plaintext_size )
5129 return( PSA_ERROR_BUFFER_TOO_SMALL );
5130 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005131 return( PSA_SUCCESS );
5132}
5133
Ronald Croncf56a0a2020-08-04 09:51:30 +02005134psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005135 psa_algorithm_t alg,
5136 const uint8_t *nonce,
5137 size_t nonce_length,
5138 const uint8_t *additional_data,
5139 size_t additional_data_length,
5140 const uint8_t *ciphertext,
5141 size_t ciphertext_length,
5142 uint8_t *plaintext,
5143 size_t plaintext_size,
5144 size_t *plaintext_length )
5145{
mohammad16035955c982018-04-26 00:53:03 +03005146 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005147 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005148 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005149
Gilles Peskineee652a32018-06-01 19:23:52 +02005150 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005151
Ronald Croncf56a0a2020-08-04 09:51:30 +02005152 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005153 if( status != PSA_SUCCESS )
5154 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005155
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005156 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5157 ciphertext, ciphertext_length,
5158 plaintext_size, &tag );
5159 if( status != PSA_SUCCESS )
5160 goto exit;
5161
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005162#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005163 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005164 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005165 status = mbedtls_to_psa_error(
5166 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5167 ciphertext_length - operation.tag_length,
5168 nonce, nonce_length,
5169 additional_data,
5170 additional_data_length,
5171 tag, operation.tag_length,
5172 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005173 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005174 else
5175#endif /* MBEDTLS_GCM_C */
5176#if defined(MBEDTLS_CCM_C)
5177 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005178 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005179 status = mbedtls_to_psa_error(
5180 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5181 ciphertext_length - operation.tag_length,
5182 nonce, nonce_length,
5183 additional_data,
5184 additional_data_length,
5185 ciphertext, plaintext,
5186 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005187 }
mohammad160339574652018-06-01 04:39:53 -07005188 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005189#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005190#if defined(MBEDTLS_CHACHAPOLY_C)
5191 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5192 {
5193 if( nonce_length != 12 || operation.tag_length != 16 )
5194 {
5195 status = PSA_ERROR_NOT_SUPPORTED;
5196 goto exit;
5197 }
5198 status = mbedtls_to_psa_error(
5199 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5200 ciphertext_length - operation.tag_length,
5201 nonce,
5202 additional_data,
5203 additional_data_length,
5204 tag,
5205 ciphertext,
5206 plaintext ) );
5207 }
5208 else
5209#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005210 {
mohammad1603554faad2018-06-03 15:07:38 +03005211 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005212 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005213
Gilles Peskineedf9a652018-08-17 18:11:56 +02005214 if( status != PSA_SUCCESS && plaintext_size != 0 )
5215 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005216
Gilles Peskineedf9a652018-08-17 18:11:56 +02005217exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005218 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005219 if( status == PSA_SUCCESS )
5220 *plaintext_length = ciphertext_length - operation.tag_length;
5221 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005222}
5223
Gilles Peskinea0655c32018-04-30 17:06:50 +02005224
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005225
Gilles Peskine20035e32018-02-03 22:44:14 +01005226/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005227/* Generators */
5228/****************************************************************/
5229
John Durkop07cc04a2020-11-16 22:08:34 -08005230#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5231 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5232 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5233#define AT_LEAST_ONE_BUILTIN_KDF
5234#endif
5235
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236#define HKDF_STATE_INIT 0 /* no input yet */
5237#define HKDF_STATE_STARTED 1 /* got salt */
5238#define HKDF_STATE_KEYED 2 /* got key */
5239#define HKDF_STATE_OUTPUT 3 /* output started */
5240
Gilles Peskinecbe66502019-05-16 16:59:18 +02005241static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005242 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005243{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005244 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5245 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005246 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005247 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005248}
5249
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005250psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005251{
5252 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005253 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005254 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005255 {
5256 /* The object has (apparently) been initialized but it is not
5257 * in use. It's ok to call abort on such an object, and there's
5258 * nothing to do. */
5259 }
5260 else
John Durkopd0321952020-10-29 21:37:36 -07005261#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005262 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005263 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005264 mbedtls_free( operation->ctx.hkdf.info );
5265 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005266 }
John Durkop07cc04a2020-11-16 22:08:34 -08005267 else
5268#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5269#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5270 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5271 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005272 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005273 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005274 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005275 if( operation->ctx.tls12_prf.seed != NULL )
5276 {
5277 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5278 operation->ctx.tls12_prf.seed_length );
5279 mbedtls_free( operation->ctx.tls12_prf.seed );
5280 }
5281
5282 if( operation->ctx.tls12_prf.label != NULL )
5283 {
5284 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5285 operation->ctx.tls12_prf.label_length );
5286 mbedtls_free( operation->ctx.tls12_prf.label );
5287 }
5288
5289 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5290
5291 /* We leave the fields Ai and output_block to be erased safely by the
5292 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005293 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005294 else
John Durkop07cc04a2020-11-16 22:08:34 -08005295#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5296 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005297 {
5298 status = PSA_ERROR_BAD_STATE;
5299 }
Janos Follath083036a2019-06-11 10:22:26 +01005300 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005301 return( status );
5302}
5303
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005304psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005305 size_t *capacity)
5306{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005307 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005308 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005309 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005310 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005311 }
5312
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005313 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005314 return( PSA_SUCCESS );
5315}
5316
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005317psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005318 size_t capacity )
5319{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005320 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005321 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005322 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005323 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005324 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005325 return( PSA_SUCCESS );
5326}
5327
John Durkopd0321952020-10-29 21:37:36 -07005328#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005329/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005330 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005331static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005332 psa_algorithm_t hash_alg,
5333 uint8_t *output,
5334 size_t output_length )
5335{
5336 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5337 psa_status_t status;
5338
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005339 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5340 return( PSA_ERROR_BAD_STATE );
5341 hkdf->state = HKDF_STATE_OUTPUT;
5342
Gilles Peskinebef7f142018-07-12 17:22:21 +02005343 while( output_length != 0 )
5344 {
5345 /* Copy what remains of the current block */
5346 uint8_t n = hash_length - hkdf->offset_in_block;
5347 if( n > output_length )
5348 n = (uint8_t) output_length;
5349 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5350 output += n;
5351 output_length -= n;
5352 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005353 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005354 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005355 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005356 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005357 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005358 * object was corrupted or if this function is called directly
5359 * inside the library. */
5360 if( hkdf->block_number == 0xff )
5361 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005362
5363 /* We need a new block */
5364 ++hkdf->block_number;
5365 hkdf->offset_in_block = 0;
5366 status = psa_hmac_setup_internal( &hkdf->hmac,
5367 hkdf->prk, hash_length,
5368 hash_alg );
5369 if( status != PSA_SUCCESS )
5370 return( status );
5371 if( hkdf->block_number != 1 )
5372 {
5373 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5374 hkdf->output_block,
5375 hash_length );
5376 if( status != PSA_SUCCESS )
5377 return( status );
5378 }
5379 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5380 hkdf->info,
5381 hkdf->info_length );
5382 if( status != PSA_SUCCESS )
5383 return( status );
5384 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5385 &hkdf->block_number, 1 );
5386 if( status != PSA_SUCCESS )
5387 return( status );
5388 status = psa_hmac_finish_internal( &hkdf->hmac,
5389 hkdf->output_block,
5390 sizeof( hkdf->output_block ) );
5391 if( status != PSA_SUCCESS )
5392 return( status );
5393 }
5394
5395 return( PSA_SUCCESS );
5396}
John Durkop07cc04a2020-11-16 22:08:34 -08005397#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005398
John Durkop07cc04a2020-11-16 22:08:34 -08005399#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5400 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005401static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5402 psa_tls12_prf_key_derivation_t *tls12_prf,
5403 psa_algorithm_t alg )
5404{
5405 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5406 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005407 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5408 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005409
Janos Follath7742fee2019-06-17 12:58:10 +01005410 /* We can't be wanting more output after block 0xff, otherwise
5411 * the capacity check in psa_key_derivation_output_bytes() would have
5412 * prevented this call. It could happen only if the operation
5413 * object was corrupted or if this function is called directly
5414 * inside the library. */
5415 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005416 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005417
5418 /* We need a new block */
5419 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005420 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005421
5422 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5423 *
5424 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5425 *
5426 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5427 * HMAC_hash(secret, A(2) + seed) +
5428 * HMAC_hash(secret, A(3) + seed) + ...
5429 *
5430 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005431 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005432 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005433 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005434 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005435 * is currently extracted as `output_block` and where i is
5436 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005437 */
5438
Janos Follathea29bfb2019-06-19 12:21:20 +01005439 /* Save the hash context before using it, to preserve the hash state with
5440 * only the inner padding in it. We need this, because inner padding depends
5441 * on the key (secret in the RFC's terminology). */
5442 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5443 if( status != PSA_SUCCESS )
5444 goto cleanup;
5445
5446 /* Calculate A(i) where i = tls12_prf->block_number. */
5447 if( tls12_prf->block_number == 1 )
5448 {
5449 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5450 * the variable seed and in this instance means it in the context of the
5451 * P_hash function, where seed = label + seed.) */
5452 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5453 tls12_prf->label, tls12_prf->label_length );
5454 if( status != PSA_SUCCESS )
5455 goto cleanup;
5456 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5457 tls12_prf->seed, tls12_prf->seed_length );
5458 if( status != PSA_SUCCESS )
5459 goto cleanup;
5460 }
5461 else
5462 {
5463 /* A(i) = HMAC_hash(secret, A(i-1)) */
5464 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5465 tls12_prf->Ai, hash_length );
5466 if( status != PSA_SUCCESS )
5467 goto cleanup;
5468 }
5469
5470 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5471 tls12_prf->Ai, hash_length );
5472 if( status != PSA_SUCCESS )
5473 goto cleanup;
5474 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5475 if( status != PSA_SUCCESS )
5476 goto cleanup;
5477
5478 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5479 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5480 tls12_prf->Ai, hash_length );
5481 if( status != PSA_SUCCESS )
5482 goto cleanup;
5483 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5484 tls12_prf->label, tls12_prf->label_length );
5485 if( status != PSA_SUCCESS )
5486 goto cleanup;
5487 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5488 tls12_prf->seed, tls12_prf->seed_length );
5489 if( status != PSA_SUCCESS )
5490 goto cleanup;
5491 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5492 tls12_prf->output_block, hash_length );
5493 if( status != PSA_SUCCESS )
5494 goto cleanup;
5495 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5496 if( status != PSA_SUCCESS )
5497 goto cleanup;
5498
Janos Follath7742fee2019-06-17 12:58:10 +01005499
5500cleanup:
5501
Janos Follathea29bfb2019-06-19 12:21:20 +01005502 cleanup_status = psa_hash_abort( &backup );
5503 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5504 status = cleanup_status;
5505
5506 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005507}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005508
Janos Follath844eb0e2019-06-19 12:10:49 +01005509static psa_status_t psa_key_derivation_tls12_prf_read(
5510 psa_tls12_prf_key_derivation_t *tls12_prf,
5511 psa_algorithm_t alg,
5512 uint8_t *output,
5513 size_t output_length )
5514{
5515 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5516 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5517 psa_status_t status;
5518 uint8_t offset, length;
5519
5520 while( output_length != 0 )
5521 {
5522 /* Check if we have fully processed the current block. */
5523 if( tls12_prf->left_in_block == 0 )
5524 {
5525 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5526 alg );
5527 if( status != PSA_SUCCESS )
5528 return( status );
5529
5530 continue;
5531 }
5532
5533 if( tls12_prf->left_in_block > output_length )
5534 length = (uint8_t) output_length;
5535 else
5536 length = tls12_prf->left_in_block;
5537
5538 offset = hash_length - tls12_prf->left_in_block;
5539 memcpy( output, tls12_prf->output_block + offset, length );
5540 output += length;
5541 output_length -= length;
5542 tls12_prf->left_in_block -= length;
5543 }
5544
5545 return( PSA_SUCCESS );
5546}
John Durkop07cc04a2020-11-16 22:08:34 -08005547#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5548 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005549
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005550psa_status_t psa_key_derivation_output_bytes(
5551 psa_key_derivation_operation_t *operation,
5552 uint8_t *output,
5553 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005554{
5555 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005556 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005557
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005558 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005559 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005561 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005562 }
5563
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005564 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005565 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005566 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005567 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005568 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005569 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005570 goto exit;
5571 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005572 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005573 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005574 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005575 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005576 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5577 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005578 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005579 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005580 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005582 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005583
John Durkopd0321952020-10-29 21:37:36 -07005584#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005585 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005586 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005587 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005588 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005589 output, output_length );
5590 }
Janos Follathadbec812019-06-14 11:05:39 +01005591 else
John Durkop07cc04a2020-11-16 22:08:34 -08005592#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5593#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5594 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005595 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005596 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005597 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005598 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005599 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005600 output_length );
5601 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005602 else
John Durkop07cc04a2020-11-16 22:08:34 -08005603#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5604 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005605 {
5606 return( PSA_ERROR_BAD_STATE );
5607 }
5608
5609exit:
5610 if( status != PSA_SUCCESS )
5611 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005612 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005613 * This allows us to differentiate between exhausted operations and
5614 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5615 * operations. */
5616 psa_algorithm_t alg = operation->alg;
5617 psa_key_derivation_abort( operation );
5618 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005619 memset( output, '!', output_length );
5620 }
5621 return( status );
5622}
5623
Gilles Peskine08542d82018-07-19 17:05:42 +02005624#if defined(MBEDTLS_DES_C)
5625static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5626{
5627 if( data_size >= 8 )
5628 mbedtls_des_key_set_parity( data );
5629 if( data_size >= 16 )
5630 mbedtls_des_key_set_parity( data + 8 );
5631 if( data_size >= 24 )
5632 mbedtls_des_key_set_parity( data + 16 );
5633}
5634#endif /* MBEDTLS_DES_C */
5635
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005636static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 psa_key_slot_t *slot,
5638 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005639 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005640{
5641 uint8_t *data = NULL;
5642 size_t bytes = PSA_BITS_TO_BYTES( bits );
5643 psa_status_t status;
5644
Gilles Peskine8e338702019-07-30 20:06:31 +02005645 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005646 return( PSA_ERROR_INVALID_ARGUMENT );
5647 if( bits % 8 != 0 )
5648 return( PSA_ERROR_INVALID_ARGUMENT );
5649 data = mbedtls_calloc( 1, bytes );
5650 if( data == NULL )
5651 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5652
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005653 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005654 if( status != PSA_SUCCESS )
5655 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005656#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005657 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005658 psa_des_set_key_parity( data, bytes );
5659#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005661
5662exit:
5663 mbedtls_free( data );
5664 return( status );
5665}
5666
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005667psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005668 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005669 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005670{
5671 psa_status_t status;
5672 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005673 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005674
Ronald Cron81709fc2020-11-14 12:10:32 +01005675 *key = MBEDTLS_SVC_KEY_ID_INIT;
5676
Gilles Peskine0f84d622019-09-12 19:03:13 +02005677 /* Reject any attempt to create a zero-length key so that we don't
5678 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5679 if( psa_get_key_bits( attributes ) == 0 )
5680 return( PSA_ERROR_INVALID_ARGUMENT );
5681
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005682 if( ! operation->can_output_key )
5683 return( PSA_ERROR_NOT_PERMITTED );
5684
Ronald Cron81709fc2020-11-14 12:10:32 +01005685 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5686 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5688 if( driver != NULL )
5689 {
5690 /* Deriving a key in a secure element is not implemented yet. */
5691 status = PSA_ERROR_NOT_SUPPORTED;
5692 }
5693#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005694 if( status == PSA_SUCCESS )
5695 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005696 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005697 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005698 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005699 }
5700 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005701 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005702 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005703 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005704
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005705 return( status );
5706}
5707
Gilles Peskineeab56e42018-07-12 17:12:33 +02005708
5709
5710/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005711/* Key derivation */
5712/****************************************************************/
5713
John Durkop07cc04a2020-11-16 22:08:34 -08005714#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005715static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005716 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005717 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005718{
John Durkop07cc04a2020-11-16 22:08:34 -08005719 int is_kdf_alg_supported;
5720
Janos Follath5fe19732019-06-20 15:09:30 +01005721 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5722 * initialisers for this union leave some bytes unspecified.) */
5723 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5724
Gilles Peskine969c5d62019-01-16 15:53:06 +01005725 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005726#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005727 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5728 is_kdf_alg_supported = 1;
5729 else
5730#endif
5731#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5732 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5733 is_kdf_alg_supported = 1;
5734 else
5735#endif
5736#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5737 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5738 is_kdf_alg_supported = 1;
5739 else
5740#endif
5741 is_kdf_alg_supported = 0;
5742
5743 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005744 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005745 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5747 if( hash_size == 0 )
5748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005749 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5750 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005751 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752 {
5753 return( PSA_ERROR_NOT_SUPPORTED );
5754 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005755 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005756 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005757 }
John Durkop07cc04a2020-11-16 22:08:34 -08005758
5759 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005760}
John Durkop07cc04a2020-11-16 22:08:34 -08005761#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005762
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005763psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005764 psa_algorithm_t alg )
5765{
5766 psa_status_t status;
5767
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005768 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005769 return( PSA_ERROR_BAD_STATE );
5770
Gilles Peskine6843c292019-01-18 16:44:49 +01005771 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5772 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005773#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005774 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005775 {
5776 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005777 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005778 }
5779 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5780 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005781 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005782 }
John Durkop07cc04a2020-11-16 22:08:34 -08005783#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005784 else
5785 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005786
5787 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005788 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005789 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005790}
5791
John Durkopd0321952020-10-29 21:37:36 -07005792#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005793static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005794 psa_algorithm_t hash_alg,
5795 psa_key_derivation_step_t step,
5796 const uint8_t *data,
5797 size_t data_length )
5798{
5799 psa_status_t status;
5800 switch( step )
5801 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005802 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005803 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005804 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005805 status = psa_hmac_setup_internal( &hkdf->hmac,
5806 data, data_length,
5807 hash_alg );
5808 if( status != PSA_SUCCESS )
5809 return( status );
5810 hkdf->state = HKDF_STATE_STARTED;
5811 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005812 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005813 /* If no salt was provided, use an empty salt. */
5814 if( hkdf->state == HKDF_STATE_INIT )
5815 {
5816 status = psa_hmac_setup_internal( &hkdf->hmac,
5817 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005818 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005819 if( status != PSA_SUCCESS )
5820 return( status );
5821 hkdf->state = HKDF_STATE_STARTED;
5822 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005823 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005824 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005825 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5826 data, data_length );
5827 if( status != PSA_SUCCESS )
5828 return( status );
5829 status = psa_hmac_finish_internal( &hkdf->hmac,
5830 hkdf->prk,
5831 sizeof( hkdf->prk ) );
5832 if( status != PSA_SUCCESS )
5833 return( status );
5834 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5835 hkdf->block_number = 0;
5836 hkdf->state = HKDF_STATE_KEYED;
5837 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005838 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005839 if( hkdf->state == HKDF_STATE_OUTPUT )
5840 return( PSA_ERROR_BAD_STATE );
5841 if( hkdf->info_set )
5842 return( PSA_ERROR_BAD_STATE );
5843 hkdf->info_length = data_length;
5844 if( data_length != 0 )
5845 {
5846 hkdf->info = mbedtls_calloc( 1, data_length );
5847 if( hkdf->info == NULL )
5848 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5849 memcpy( hkdf->info, data, data_length );
5850 }
5851 hkdf->info_set = 1;
5852 return( PSA_SUCCESS );
5853 default:
5854 return( PSA_ERROR_INVALID_ARGUMENT );
5855 }
5856}
John Durkop07cc04a2020-11-16 22:08:34 -08005857#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005858
John Durkop07cc04a2020-11-16 22:08:34 -08005859#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5860 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005861static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5862 const uint8_t *data,
5863 size_t data_length )
5864{
5865 if( prf->state != TLS12_PRF_STATE_INIT )
5866 return( PSA_ERROR_BAD_STATE );
5867
Janos Follathd6dce9f2019-07-04 09:11:38 +01005868 if( data_length != 0 )
5869 {
5870 prf->seed = mbedtls_calloc( 1, data_length );
5871 if( prf->seed == NULL )
5872 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005873
Janos Follathd6dce9f2019-07-04 09:11:38 +01005874 memcpy( prf->seed, data, data_length );
5875 prf->seed_length = data_length;
5876 }
Janos Follathf08e2652019-06-13 09:05:41 +01005877
5878 prf->state = TLS12_PRF_STATE_SEED_SET;
5879
5880 return( PSA_SUCCESS );
5881}
5882
Janos Follath81550542019-06-13 14:26:34 +01005883static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5884 psa_algorithm_t hash_alg,
5885 const uint8_t *data,
5886 size_t data_length )
5887{
5888 psa_status_t status;
5889 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5890 return( PSA_ERROR_BAD_STATE );
5891
5892 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5893 if( status != PSA_SUCCESS )
5894 return( status );
5895
5896 prf->state = TLS12_PRF_STATE_KEY_SET;
5897
5898 return( PSA_SUCCESS );
5899}
5900
Janos Follath63028dd2019-06-13 09:15:47 +01005901static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5902 const uint8_t *data,
5903 size_t data_length )
5904{
5905 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5906 return( PSA_ERROR_BAD_STATE );
5907
Janos Follathd6dce9f2019-07-04 09:11:38 +01005908 if( data_length != 0 )
5909 {
5910 prf->label = mbedtls_calloc( 1, data_length );
5911 if( prf->label == NULL )
5912 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005913
Janos Follathd6dce9f2019-07-04 09:11:38 +01005914 memcpy( prf->label, data, data_length );
5915 prf->label_length = data_length;
5916 }
Janos Follath63028dd2019-06-13 09:15:47 +01005917
5918 prf->state = TLS12_PRF_STATE_LABEL_SET;
5919
5920 return( PSA_SUCCESS );
5921}
5922
Janos Follathb03233e2019-06-11 15:30:30 +01005923static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5924 psa_algorithm_t hash_alg,
5925 psa_key_derivation_step_t step,
5926 const uint8_t *data,
5927 size_t data_length )
5928{
Janos Follathb03233e2019-06-11 15:30:30 +01005929 switch( step )
5930 {
Janos Follathf08e2652019-06-13 09:05:41 +01005931 case PSA_KEY_DERIVATION_INPUT_SEED:
5932 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005933 case PSA_KEY_DERIVATION_INPUT_SECRET:
5934 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005935 case PSA_KEY_DERIVATION_INPUT_LABEL:
5936 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005937 default:
5938 return( PSA_ERROR_INVALID_ARGUMENT );
5939 }
5940}
John Durkop07cc04a2020-11-16 22:08:34 -08005941#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5942 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5943
5944#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5945static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5946 psa_tls12_prf_key_derivation_t *prf,
5947 psa_algorithm_t hash_alg,
5948 const uint8_t *data,
5949 size_t data_length )
5950{
5951 psa_status_t status;
5952 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5953 uint8_t *cur = pms;
5954
5955 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5956 return( PSA_ERROR_INVALID_ARGUMENT );
5957
5958 /* Quoting RFC 4279, Section 2:
5959 *
5960 * The premaster secret is formed as follows: if the PSK is N octets
5961 * long, concatenate a uint16 with the value N, N zero octets, a second
5962 * uint16 with the value N, and the PSK itself.
5963 */
5964
5965 *cur++ = ( data_length >> 8 ) & 0xff;
5966 *cur++ = ( data_length >> 0 ) & 0xff;
5967 memset( cur, 0, data_length );
5968 cur += data_length;
5969 *cur++ = pms[0];
5970 *cur++ = pms[1];
5971 memcpy( cur, data, data_length );
5972 cur += data_length;
5973
5974 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5975
5976 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5977 return( status );
5978}
Janos Follath6660f0e2019-06-17 08:44:03 +01005979
5980static psa_status_t psa_tls12_prf_psk_to_ms_input(
5981 psa_tls12_prf_key_derivation_t *prf,
5982 psa_algorithm_t hash_alg,
5983 psa_key_derivation_step_t step,
5984 const uint8_t *data,
5985 size_t data_length )
5986{
5987 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005988 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005989 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5990 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005991 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005992
5993 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5994}
John Durkop07cc04a2020-11-16 22:08:34 -08005995#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005996
Gilles Peskineb8965192019-09-24 16:21:10 +02005997/** Check whether the given key type is acceptable for the given
5998 * input step of a key derivation.
5999 *
6000 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
6001 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
6002 * Both secret and non-secret inputs can alternatively have the type
6003 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
6004 * that the input was passed as a buffer rather than via a key object.
6005 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02006006static int psa_key_derivation_check_input_type(
6007 psa_key_derivation_step_t step,
6008 psa_key_type_t key_type )
6009{
6010 switch( step )
6011 {
6012 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02006013 if( key_type == PSA_KEY_TYPE_DERIVE )
6014 return( PSA_SUCCESS );
6015 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006016 return( PSA_SUCCESS );
6017 break;
6018 case PSA_KEY_DERIVATION_INPUT_LABEL:
6019 case PSA_KEY_DERIVATION_INPUT_SALT:
6020 case PSA_KEY_DERIVATION_INPUT_INFO:
6021 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02006022 if( key_type == PSA_KEY_TYPE_RAW_DATA )
6023 return( PSA_SUCCESS );
6024 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02006025 return( PSA_SUCCESS );
6026 break;
6027 }
6028 return( PSA_ERROR_INVALID_ARGUMENT );
6029}
6030
Janos Follathb80a94e2019-06-12 15:54:46 +01006031static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006032 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006033 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006034 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006035 const uint8_t *data,
6036 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006037{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006038 psa_status_t status;
6039 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6040
6041 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006042 if( status != PSA_SUCCESS )
6043 goto exit;
6044
John Durkopd0321952020-10-29 21:37:36 -07006045#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006046 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006047 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006048 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006049 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006050 step, data, data_length );
6051 }
John Durkop07cc04a2020-11-16 22:08:34 -08006052 else
6053#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6054#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6055 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006056 {
Janos Follathb03233e2019-06-11 15:30:30 +01006057 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6058 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6059 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006060 }
John Durkop07cc04a2020-11-16 22:08:34 -08006061 else
6062#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6063#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6064 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006065 {
6066 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6067 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6068 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006069 }
6070 else
John Durkop07cc04a2020-11-16 22:08:34 -08006071#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006072 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006073 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006074 return( PSA_ERROR_BAD_STATE );
6075 }
6076
Gilles Peskine224b0d62019-09-23 18:13:17 +02006077exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006078 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006079 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006080 return( status );
6081}
6082
Janos Follath51f4a0f2019-06-14 11:35:55 +01006083psa_status_t psa_key_derivation_input_bytes(
6084 psa_key_derivation_operation_t *operation,
6085 psa_key_derivation_step_t step,
6086 const uint8_t *data,
6087 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006088{
Gilles Peskineb8965192019-09-24 16:21:10 +02006089 return( psa_key_derivation_input_internal( operation, step,
6090 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006091 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006092}
6093
Janos Follath51f4a0f2019-06-14 11:35:55 +01006094psa_status_t psa_key_derivation_input_key(
6095 psa_key_derivation_operation_t *operation,
6096 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006097 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006098{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006099 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006100 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006101 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006102
Ronald Cron5c522922020-11-14 16:35:34 +01006103 status = psa_get_and_lock_transparent_key_slot_with_policy(
6104 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006105 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006106 {
6107 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006108 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006109 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006110
6111 /* Passing a key object as a SECRET input unlocks the permission
6112 * to output to a key object. */
6113 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6114 operation->can_output_key = 1;
6115
Ronald Cronf95a2b12020-10-22 15:24:49 +02006116 status = psa_key_derivation_input_internal( operation,
6117 step, slot->attr.type,
6118 slot->data.key.data,
6119 slot->data.key.bytes );
6120
Ronald Cron5c522922020-11-14 16:35:34 +01006121 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006122
Ronald Cron5c522922020-11-14 16:35:34 +01006123 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006124}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006125
6126
6127
6128/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006129/* Key agreement */
6130/****************************************************************/
6131
John Durkop6ba40d12020-11-10 08:50:04 -08006132#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006133static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6134 size_t peer_key_length,
6135 const mbedtls_ecp_keypair *our_key,
6136 uint8_t *shared_secret,
6137 size_t shared_secret_size,
6138 size_t *shared_secret_length )
6139{
Steven Cooremand4867872020-08-05 16:31:39 +02006140 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006141 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006142 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006143 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006144 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006145 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006146
Steven Cooreman4fed4552020-08-03 14:46:03 +02006147 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6148 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006149 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006150 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006151 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006152 goto exit;
6153
Jaeden Amero97271b32019-01-10 19:38:51 +00006154 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006155 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006156 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006157 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006158 status = mbedtls_to_psa_error(
6159 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6160 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006161 goto exit;
6162
Jaeden Amero97271b32019-01-10 19:38:51 +00006163 status = mbedtls_to_psa_error(
6164 mbedtls_ecdh_calc_secret( &ecdh,
6165 shared_secret_length,
6166 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006167 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006168 MBEDTLS_PSA_RANDOM_STATE ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006169 if( status != PSA_SUCCESS )
6170 goto exit;
6171 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6172 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006173
6174exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006175 if( status != PSA_SUCCESS )
6176 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006177 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006178 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006179 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006180
Jaeden Amero97271b32019-01-10 19:38:51 +00006181 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006182}
John Durkop6ba40d12020-11-10 08:50:04 -08006183#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006184
Gilles Peskine01d718c2018-09-18 12:01:02 +02006185#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6186
Gilles Peskine0216fe12019-04-11 21:23:21 +02006187static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6188 psa_key_slot_t *private_key,
6189 const uint8_t *peer_key,
6190 size_t peer_key_length,
6191 uint8_t *shared_secret,
6192 size_t shared_secret_size,
6193 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006194{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006195 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006196 {
John Durkop6ba40d12020-11-10 08:50:04 -08006197#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006198 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006199 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006200 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006201 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006202 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006203 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006204 private_key->data.key.data,
6205 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006206 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006207 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006208 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006209 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006210 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006211 shared_secret, shared_secret_size,
6212 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006213 mbedtls_ecp_keypair_free( ecp );
6214 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006215 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006216#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006217 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006218 (void) private_key;
6219 (void) peer_key;
6220 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006221 (void) shared_secret;
6222 (void) shared_secret_size;
6223 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006224 return( PSA_ERROR_NOT_SUPPORTED );
6225 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006226}
6227
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006228/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006229 * to potentially free embedded data structures and wipe confidential data.
6230 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006231static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006232 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006233 psa_key_slot_t *private_key,
6234 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006235 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006236{
6237 psa_status_t status;
6238 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6239 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006240 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006241
6242 /* Step 1: run the secret agreement algorithm to generate the shared
6243 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006244 status = psa_key_agreement_raw_internal( ka_alg,
6245 private_key,
6246 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006247 shared_secret,
6248 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006249 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006250 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006251 goto exit;
6252
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006253 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006254 * the shared secret. A shared secret is permitted wherever a key
6255 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006256 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006257 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006258 shared_secret,
6259 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006260exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006261 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006262 return( status );
6263}
6264
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006265psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006266 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006267 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006268 const uint8_t *peer_key,
6269 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006270{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006271 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006272 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006273 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006275 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006276 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006277 status = psa_get_and_lock_transparent_key_slot_with_policy(
6278 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006279 if( status != PSA_SUCCESS )
6280 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006281 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006282 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006283 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006284 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006285 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006286 else
6287 {
6288 /* If a private key has been added as SECRET, we allow the derived
6289 * key material to be used as a key in PSA Crypto. */
6290 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6291 operation->can_output_key = 1;
6292 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006293
Ronald Cron5c522922020-11-14 16:35:34 +01006294 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006295
Ronald Cron5c522922020-11-14 16:35:34 +01006296 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006297}
6298
Gilles Peskinebe697d82019-05-16 18:00:41 +02006299psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006300 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006301 const uint8_t *peer_key,
6302 size_t peer_key_length,
6303 uint8_t *output,
6304 size_t output_size,
6305 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006306{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006307 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006308 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006309 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006310
6311 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6312 {
6313 status = PSA_ERROR_INVALID_ARGUMENT;
6314 goto exit;
6315 }
Ronald Cron5c522922020-11-14 16:35:34 +01006316 status = psa_get_and_lock_transparent_key_slot_with_policy(
6317 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006318 if( status != PSA_SUCCESS )
6319 goto exit;
6320
6321 status = psa_key_agreement_raw_internal( alg, slot,
6322 peer_key, peer_key_length,
6323 output, output_size,
6324 output_length );
6325
6326exit:
6327 if( status != PSA_SUCCESS )
6328 {
6329 /* If an error happens and is not handled properly, the output
6330 * may be used as a key to protect sensitive data. Arrange for such
6331 * a key to be random, which is likely to result in decryption or
6332 * verification errors. This is better than filling the buffer with
6333 * some constant data such as zeros, which would result in the data
6334 * being protected with a reproducible, easily knowable key.
6335 */
6336 psa_generate_random( output, output_size );
6337 *output_length = output_size;
6338 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006339
Ronald Cron5c522922020-11-14 16:35:34 +01006340 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006341
Ronald Cron5c522922020-11-14 16:35:34 +01006342 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006343}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006344
6345
Gilles Peskine30524eb2020-11-13 17:02:26 +01006346
Gilles Peskine86a440b2018-11-12 18:39:40 +01006347/****************************************************************/
6348/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006349/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006350
Gilles Peskine30524eb2020-11-13 17:02:26 +01006351/** Initialize the PSA random generator.
6352 */
6353static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6354{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006355#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6356 memset( rng, 0, sizeof( *rng ) );
6357#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6358
Gilles Peskine30524eb2020-11-13 17:02:26 +01006359 /* Set default configuration if
6360 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6361 if( rng->entropy_init == NULL )
6362 rng->entropy_init = mbedtls_entropy_init;
6363 if( rng->entropy_free == NULL )
6364 rng->entropy_free = mbedtls_entropy_free;
6365
6366 rng->entropy_init( &rng->entropy );
6367#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6368 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6369 /* The PSA entropy injection feature depends on using NV seed as an entropy
6370 * source. Add NV seed as an entropy source for PSA entropy injection. */
6371 mbedtls_entropy_add_source( &rng->entropy,
6372 mbedtls_nv_seed_poll, NULL,
6373 MBEDTLS_ENTROPY_BLOCK_SIZE,
6374 MBEDTLS_ENTROPY_SOURCE_STRONG );
6375#endif
6376
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006377 mbedtls_psa_drbg_init( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006378#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006379}
6380
6381/** Deinitialize the PSA random generator.
6382 */
6383static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6384{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006385#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6386 memset( rng, 0, sizeof( *rng ) );
6387#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006388 mbedtls_psa_drbg_free( MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006389 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006390#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006391}
6392
6393/** Seed the PSA random generator.
6394 */
6395static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6396{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006397#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6398 /* Do nothing: the external RNG seeds itself. */
6399 (void) rng;
6400 return( PSA_SUCCESS );
6401#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006402 const unsigned char drbg_seed[] = "PSA";
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006403 int ret = mbedtls_psa_drbg_seed( &rng->entropy,
6404 drbg_seed, sizeof( drbg_seed ) - 1 );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006405 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006406#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006407}
6408
Gilles Peskinee59236f2018-01-27 23:32:46 +01006409psa_status_t psa_generate_random( uint8_t *output,
6410 size_t output_size )
6411{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006412 GUARD_MODULE_INITIALIZED;
6413
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006414#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6415
6416 size_t output_length = 0;
6417 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6418 output, output_size,
6419 &output_length );
6420 if( status != PSA_SUCCESS )
6421 return( status );
6422 /* Breaking up a request into smaller chunks is currently not supported
6423 * for the extrernal RNG interface. */
6424 if( output_length != output_size )
6425 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6426 return( PSA_SUCCESS );
6427
6428#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6429
Gilles Peskine71ddab92021-01-04 21:01:07 +01006430 while( output_size > 0 )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006431 {
Gilles Peskine71ddab92021-01-04 21:01:07 +01006432 size_t request_size =
6433 ( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST ?
6434 MBEDTLS_PSA_RANDOM_MAX_REQUEST :
6435 output_size );
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006436 int ret = mbedtls_psa_get_random( MBEDTLS_PSA_RANDOM_STATE,
6437 output, request_size );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006438 if( ret != 0 )
6439 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine71ddab92021-01-04 21:01:07 +01006440 output_size -= request_size;
6441 output += request_size;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006442 }
Gilles Peskine0c59ba82021-01-05 14:10:59 +01006443 return( PSA_SUCCESS );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006444#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006445}
6446
Gilles Peskine8814fc42020-12-14 15:33:44 +01006447/* Wrapper function allowing the classic API to use the PSA RNG.
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006448 *
6449 * `mbedtls_psa_get_random(MBEDTLS_PSA_RANDOM_STATE, ...)` calls
6450 * `psa_generate_random(...)`. The state parameter is ignored since the
6451 * PSA API doesn't support passing an explicit state.
6452 *
6453 * In the non-external case, psa_generate_random() calls an
6454 * `mbedtls_xxx_drbg_random` function which has exactly the same signature
6455 * and semantics as mbedtls_psa_get_random(). As an optimization,
6456 * instead of doing this back-and-forth between the PSA API and the
6457 * classic API, psa_crypto_random_impl.h defines `mbedtls_psa_get_random`
6458 * as a constant function pointer to `mbedtls_xxx_drbg_random`.
Gilles Peskine8814fc42020-12-14 15:33:44 +01006459 */
6460#if defined (MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6461int mbedtls_psa_get_random( void *p_rng,
6462 unsigned char *output,
6463 size_t output_size )
6464{
Gilles Peskine9c3e0602021-01-05 16:03:55 +01006465 /* This function takes a pointer to the RNG state because that's what
6466 * classic mbedtls functions using an RNG expect. The PSA RNG manages
6467 * its own state internally and doesn't let the caller access that state.
6468 * So we just ignore the state parameter, and in practice we'll pass
6469 * NULL. */
Gilles Peskine8814fc42020-12-14 15:33:44 +01006470 (void) p_rng;
6471 psa_status_t status = psa_generate_random( output, output_size );
6472 if( status == PSA_SUCCESS )
6473 return( 0 );
6474 else
6475 return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
6476}
6477#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6478
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006479#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6480#include "mbedtls/entropy_poll.h"
6481
Gilles Peskine7228da22019-07-15 11:06:15 +02006482psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006483 size_t seed_size )
6484{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006485 if( global_data.initialized )
6486 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006487
6488 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6489 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6490 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6491 return( PSA_ERROR_INVALID_ARGUMENT );
6492
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006493 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006494}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006495#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006496
John Durkop07cc04a2020-11-16 22:08:34 -08006497#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006498static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6499 size_t domain_parameters_size,
6500 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006501{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006502 size_t i;
6503 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006504
Gilles Peskinee56e8782019-04-26 17:34:02 +02006505 if( domain_parameters_size == 0 )
6506 {
6507 *exponent = 65537;
6508 return( PSA_SUCCESS );
6509 }
6510
6511 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6512 * support values that fit in a 32-bit integer, which is larger than
6513 * int on just about every platform anyway. */
6514 if( domain_parameters_size > sizeof( acc ) )
6515 return( PSA_ERROR_NOT_SUPPORTED );
6516 for( i = 0; i < domain_parameters_size; i++ )
6517 acc = ( acc << 8 ) | domain_parameters[i];
6518 if( acc > INT_MAX )
6519 return( PSA_ERROR_NOT_SUPPORTED );
6520 *exponent = acc;
6521 return( PSA_SUCCESS );
6522}
John Durkop07cc04a2020-11-16 22:08:34 -08006523#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006524
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006525static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006526 psa_key_slot_t *slot, size_t bits,
6527 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006528{
Gilles Peskine8e338702019-07-30 20:06:31 +02006529 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006530
Gilles Peskinee56e8782019-04-26 17:34:02 +02006531 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006532 return( PSA_ERROR_INVALID_ARGUMENT );
6533
Gilles Peskinee59236f2018-01-27 23:32:46 +01006534 if( key_type_is_raw_bytes( type ) )
6535 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006536 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006537
6538 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006539 if( status != PSA_SUCCESS )
6540 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006541
6542 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006543 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6544 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006545 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006546
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006547 status = psa_generate_random( slot->data.key.data,
6548 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006549 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006550 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006551
6552 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006553#if defined(MBEDTLS_DES_C)
6554 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006555 psa_des_set_key_parity( slot->data.key.data,
6556 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006557#endif /* MBEDTLS_DES_C */
6558 }
6559 else
6560
John Durkop07cc04a2020-11-16 22:08:34 -08006561#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006562 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006564 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006565 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006566 int exponent;
6567 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006568 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6569 return( PSA_ERROR_NOT_SUPPORTED );
6570 /* Accept only byte-aligned keys, for the same reasons as
6571 * in psa_import_rsa_key(). */
6572 if( bits % 8 != 0 )
6573 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006574 status = psa_read_rsa_exponent( domain_parameters,
6575 domain_parameters_size,
6576 &exponent );
6577 if( status != PSA_SUCCESS )
6578 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006579 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6580 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006581 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006582 MBEDTLS_PSA_RANDOM_STATE,
Gilles Peskinee59236f2018-01-27 23:32:46 +01006583 (unsigned int) bits,
6584 exponent );
6585 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006586 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006587
6588 /* Make sure to always have an export representation available */
6589 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6590
Steven Cooreman75b74362020-07-28 14:30:13 +02006591 status = psa_allocate_buffer_to_slot( slot, bytes );
6592 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006593 {
6594 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006595 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006596 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006597
6598 status = psa_export_rsa_key( type,
6599 &rsa,
6600 slot->data.key.data,
6601 bytes,
6602 &slot->data.key.bytes );
6603 mbedtls_rsa_free( &rsa );
6604 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006605 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006606 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006607 }
6608 else
John Durkop07cc04a2020-11-16 22:08:34 -08006609#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006610
John Durkop9814fa22020-11-04 12:28:15 -08006611#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006612 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006613 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006614 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006615 mbedtls_ecp_group_id grp_id =
6616 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006617 const mbedtls_ecp_curve_info *curve_info =
6618 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006619 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006620 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006621 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006622 return( PSA_ERROR_NOT_SUPPORTED );
6623 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6624 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006625 mbedtls_ecp_keypair_init( &ecp );
6626 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006627 mbedtls_psa_get_random,
Gilles Peskine5894e8e2020-12-14 14:54:06 +01006628 MBEDTLS_PSA_RANDOM_STATE );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006629 if( ret != 0 )
6630 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006631 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006632 return( mbedtls_to_psa_error( ret ) );
6633 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006634
6635
6636 /* Make sure to always have an export representation available */
6637 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006638 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6639 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006640 {
6641 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006642 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006643 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006644
6645 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006646 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6647
6648 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006649 if( status != PSA_SUCCESS ) {
6650 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006651 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006652 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006653 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006654 }
6655 else
John Durkop9814fa22020-11-04 12:28:15 -08006656#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006657 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006658 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006659 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006660
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006661 return( PSA_SUCCESS );
6662}
Darryl Green0c6575a2018-11-07 16:05:30 +00006663
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006664psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006665 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006666{
6667 psa_status_t status;
6668 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006669 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006670
Ronald Cron81709fc2020-11-14 12:10:32 +01006671 *key = MBEDTLS_SVC_KEY_ID_INIT;
6672
Gilles Peskine0f84d622019-09-12 19:03:13 +02006673 /* Reject any attempt to create a zero-length key so that we don't
6674 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6675 if( psa_get_key_bits( attributes ) == 0 )
6676 return( PSA_ERROR_INVALID_ARGUMENT );
6677
Ronald Cron81709fc2020-11-14 12:10:32 +01006678 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6679 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006680 if( status != PSA_SUCCESS )
6681 goto exit;
6682
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006683 status = psa_driver_wrapper_generate_key( attributes,
6684 slot );
6685 if( status != PSA_ERROR_NOT_SUPPORTED ||
6686 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6687 goto exit;
6688
6689 status = psa_generate_key_internal(
6690 slot, attributes->core.bits,
6691 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006692
6693exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006694 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006695 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006696 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006697 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006698
Darryl Green0c6575a2018-11-07 16:05:30 +00006699 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006700}
6701
6702
Gilles Peskinee59236f2018-01-27 23:32:46 +01006703
6704/****************************************************************/
6705/* Module setup */
6706/****************************************************************/
6707
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006708#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006709psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6710 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6711 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6712{
6713 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6714 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006715 global_data.rng.entropy_init = entropy_init;
6716 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006717 return( PSA_SUCCESS );
6718}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006719#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006720
Gilles Peskinee59236f2018-01-27 23:32:46 +01006721void mbedtls_psa_crypto_free( void )
6722{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006723 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006724 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6725 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006726 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006727 }
6728 /* Wipe all remaining data, including configuration.
6729 * In particular, this sets all state indicator to the value
6730 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006731 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006732#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006733 /* Unregister all secure element drivers, so that we restart from
6734 * a pristine state. */
6735 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006736#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006737}
6738
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006739#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6740/** Recover a transaction that was interrupted by a power failure.
6741 *
6742 * This function is called during initialization, before psa_crypto_init()
6743 * returns. If this function returns a failure status, the initialization
6744 * fails.
6745 */
6746static psa_status_t psa_crypto_recover_transaction(
6747 const psa_crypto_transaction_t *transaction )
6748{
6749 switch( transaction->unknown.type )
6750 {
6751 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6752 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006753 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006754 * is implemented.
6755 * https://github.com/ARMmbed/mbed-crypto/issues/218
6756 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006757 default:
6758 /* We found an unsupported transaction in the storage.
6759 * We don't know what state the storage is in. Give up. */
6760 return( PSA_ERROR_STORAGE_FAILURE );
6761 }
6762}
6763#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6764
Gilles Peskinee59236f2018-01-27 23:32:46 +01006765psa_status_t psa_crypto_init( void )
6766{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006767 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006768
Gilles Peskinec6b69072018-11-20 21:42:52 +01006769 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006770 if( global_data.initialized != 0 )
6771 return( PSA_SUCCESS );
6772
Gilles Peskine30524eb2020-11-13 17:02:26 +01006773 /* Initialize and seed the random generator. */
6774 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006775 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006776 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006777 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006778 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006779 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006780
Gilles Peskine66fb1262018-12-10 16:29:04 +01006781 status = psa_initialize_key_slots( );
6782 if( status != PSA_SUCCESS )
6783 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006784
Gilles Peskined9348f22019-10-01 15:22:29 +02006785#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6786 status = psa_init_all_se_drivers( );
6787 if( status != PSA_SUCCESS )
6788 goto exit;
6789#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6790
Gilles Peskine4b734222019-07-24 15:56:31 +02006791#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006792 status = psa_crypto_load_transaction( );
6793 if( status == PSA_SUCCESS )
6794 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006795 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6796 if( status != PSA_SUCCESS )
6797 goto exit;
6798 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006799 }
6800 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6801 {
6802 /* There's no transaction to complete. It's all good. */
6803 status = PSA_SUCCESS;
6804 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006805#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006806
Gilles Peskinec6b69072018-11-20 21:42:52 +01006807 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006808 global_data.initialized = 1;
6809
6810exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006811 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006812 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006813 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006814}
6815
6816#endif /* MBEDTLS_PSA_CRYPTO_C */