blob: b3d7eb4650518cd303e2814ab15bea4e6c6302c9 [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 Peskine90edc992020-11-13 15:39:19 +010043#include "psa_crypto_random.h"
44
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
itayzafrir0adf0fc2018-09-06 16:24:41 +0300127#define GUARD_MODULE_INITIALIZED \
128 if( global_data.initialized == 0 ) \
129 return( PSA_ERROR_BAD_STATE );
130
Steven Cooreman01164162020-07-20 15:31:37 +0200131psa_status_t mbedtls_to_psa_error( int ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132{
Gilles Peskinea5905292018-02-07 20:59:33 +0100133 /* If there's both a high-level code and low-level code, dispatch on
134 * the high-level code. */
135 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100136 {
137 case 0:
138 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100139
140 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
141 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
142 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
143 return( PSA_ERROR_NOT_SUPPORTED );
144 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
145 return( PSA_ERROR_HARDWARE_FAILURE );
146
147 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
148 return( PSA_ERROR_HARDWARE_FAILURE );
149
Gilles Peskine9a944802018-06-21 09:35:35 +0200150 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
151 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
152 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
153 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
154 case MBEDTLS_ERR_ASN1_INVALID_DATA:
155 return( PSA_ERROR_INVALID_ARGUMENT );
156 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
157 return( PSA_ERROR_INSUFFICIENT_MEMORY );
158 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
159 return( PSA_ERROR_BUFFER_TOO_SMALL );
160
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000162 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
164 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
165#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100166 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
167 return( PSA_ERROR_NOT_SUPPORTED );
168 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
169 return( PSA_ERROR_HARDWARE_FAILURE );
170
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000172 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
174 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
175#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100176 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
177 return( PSA_ERROR_NOT_SUPPORTED );
178 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
179 return( PSA_ERROR_HARDWARE_FAILURE );
180
181 case MBEDTLS_ERR_CCM_BAD_INPUT:
182 return( PSA_ERROR_INVALID_ARGUMENT );
183 case MBEDTLS_ERR_CCM_AUTH_FAILED:
184 return( PSA_ERROR_INVALID_SIGNATURE );
185 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
186 return( PSA_ERROR_HARDWARE_FAILURE );
187
Gilles Peskine26869f22019-05-06 15:25:00 +0200188 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
189 return( PSA_ERROR_INVALID_ARGUMENT );
190
191 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
192 return( PSA_ERROR_BAD_STATE );
193 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
194 return( PSA_ERROR_INVALID_SIGNATURE );
195
Gilles Peskinea5905292018-02-07 20:59:33 +0100196 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
197 return( PSA_ERROR_NOT_SUPPORTED );
198 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
199 return( PSA_ERROR_INVALID_ARGUMENT );
200 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
201 return( PSA_ERROR_INSUFFICIENT_MEMORY );
202 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
203 return( PSA_ERROR_INVALID_PADDING );
204 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
Fredrik Strupef90e3012020-09-28 16:11:33 +0200205 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100206 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
207 return( PSA_ERROR_INVALID_SIGNATURE );
208 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200209 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100210 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
211 return( PSA_ERROR_HARDWARE_FAILURE );
212
213 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
214 return( PSA_ERROR_HARDWARE_FAILURE );
215
Gilles Peskine82e57d12020-11-13 21:31:17 +0100216#if !( defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) || \
217 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE) )
Gilles Peskinebee96c82020-11-23 21:00:09 +0100218 /* Only check CTR_DRBG error codes if underlying mbedtls_xxx
219 * functions are passed a CTR_DRBG instance. */
Gilles Peskinea5905292018-02-07 20:59:33 +0100220 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
221 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
222 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
223 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
224 return( PSA_ERROR_NOT_SUPPORTED );
225 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
226 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100227#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100228
229 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
230 return( PSA_ERROR_NOT_SUPPORTED );
231 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
232 return( PSA_ERROR_HARDWARE_FAILURE );
233
Gilles Peskinee59236f2018-01-27 23:32:46 +0100234 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
235 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
236 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
237 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100238
239 case MBEDTLS_ERR_GCM_AUTH_FAILED:
240 return( PSA_ERROR_INVALID_SIGNATURE );
241 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200242 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100243 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
244 return( PSA_ERROR_HARDWARE_FAILURE );
245
Gilles Peskine82e57d12020-11-13 21:31:17 +0100246#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
247 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
Gilles Peskinebee96c82020-11-23 21:00:09 +0100248 /* Only check HMAC_DRBG error codes if underlying mbedtls_xxx
249 * functions are passed a HMAC_DRBG instance. */
Gilles Peskine82e57d12020-11-13 21:31:17 +0100250 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
251 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
252 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
253 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
254 return( PSA_ERROR_NOT_SUPPORTED );
255 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
256 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
257#endif
258
Gilles Peskinea5905292018-02-07 20:59:33 +0100259 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
260 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
261 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
262 return( PSA_ERROR_HARDWARE_FAILURE );
263
264 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
265 return( PSA_ERROR_NOT_SUPPORTED );
266 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MD_ALLOC_FAILED:
269 return( PSA_ERROR_INSUFFICIENT_MEMORY );
270 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
271 return( PSA_ERROR_STORAGE_FAILURE );
272 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
273 return( PSA_ERROR_HARDWARE_FAILURE );
274
Gilles Peskinef76aa772018-10-29 19:24:33 +0100275 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
276 return( PSA_ERROR_STORAGE_FAILURE );
277 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
278 return( PSA_ERROR_INVALID_ARGUMENT );
279 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
282 return( PSA_ERROR_BUFFER_TOO_SMALL );
283 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
286 return( PSA_ERROR_INVALID_ARGUMENT );
287 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
290 return( PSA_ERROR_INSUFFICIENT_MEMORY );
291
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100292 case MBEDTLS_ERR_PK_ALLOC_FAILED:
293 return( PSA_ERROR_INSUFFICIENT_MEMORY );
294 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
295 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
296 return( PSA_ERROR_INVALID_ARGUMENT );
297 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100299 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
300 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
301 return( PSA_ERROR_INVALID_ARGUMENT );
302 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
303 return( PSA_ERROR_NOT_SUPPORTED );
304 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
305 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
306 return( PSA_ERROR_NOT_PERMITTED );
307 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
308 return( PSA_ERROR_INVALID_ARGUMENT );
309 case MBEDTLS_ERR_PK_INVALID_ALG:
310 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
311 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
312 return( PSA_ERROR_NOT_SUPPORTED );
313 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
314 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100315 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
316 return( PSA_ERROR_HARDWARE_FAILURE );
317
Gilles Peskineff2d2002019-05-06 15:26:23 +0200318 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
319 return( PSA_ERROR_HARDWARE_FAILURE );
320 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
321 return( PSA_ERROR_NOT_SUPPORTED );
322
Gilles Peskinea5905292018-02-07 20:59:33 +0100323 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
324 return( PSA_ERROR_HARDWARE_FAILURE );
325
326 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
327 return( PSA_ERROR_INVALID_ARGUMENT );
328 case MBEDTLS_ERR_RSA_INVALID_PADDING:
329 return( PSA_ERROR_INVALID_PADDING );
330 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
333 return( PSA_ERROR_INVALID_ARGUMENT );
334 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
335 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200336 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100337 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
338 return( PSA_ERROR_INVALID_SIGNATURE );
339 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
340 return( PSA_ERROR_BUFFER_TOO_SMALL );
341 case MBEDTLS_ERR_RSA_RNG_FAILED:
342 return( PSA_ERROR_INSUFFICIENT_MEMORY );
343 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
344 return( PSA_ERROR_NOT_SUPPORTED );
345 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
346 return( PSA_ERROR_HARDWARE_FAILURE );
347
348 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
349 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
350 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
352
353 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
354 return( PSA_ERROR_INVALID_ARGUMENT );
355 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
356 return( PSA_ERROR_HARDWARE_FAILURE );
357
itayzafrir5c753392018-05-08 11:18:38 +0300358 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300359 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300360 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300361 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
362 return( PSA_ERROR_BUFFER_TOO_SMALL );
363 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
364 return( PSA_ERROR_NOT_SUPPORTED );
365 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
366 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
367 return( PSA_ERROR_INVALID_SIGNATURE );
368 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
369 return( PSA_ERROR_INSUFFICIENT_MEMORY );
370 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
371 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000372 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
373 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300374
Gilles Peskinee59236f2018-01-27 23:32:46 +0100375 default:
David Saadab4ecc272019-02-14 13:48:10 +0200376 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100377 }
378}
379
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200380
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200381
382
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100383/****************************************************************/
384/* Key management */
385/****************************************************************/
386
Gilles Peskine73167e12019-07-12 23:44:37 +0200387#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
388static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
389{
Gilles Peskine8e338702019-07-30 20:06:31 +0200390 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200391}
392#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
393
John Durkop07cc04a2020-11-16 22:08:34 -0800394/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
395 * current test driver in key_management.c is using this function
396 * when accelerators are used for ECC key pair and public key.
397 * Once that dependency is resolved these guards can be removed.
398 */
John Durkop9814fa22020-11-04 12:28:15 -0800399#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800400 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
401 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
402 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100403mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100404 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200405{
406 switch( curve )
407 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100408 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100409 switch( byte_length )
410 {
411 case PSA_BITS_TO_BYTES( 192 ):
412 return( MBEDTLS_ECP_DP_SECP192R1 );
413 case PSA_BITS_TO_BYTES( 224 ):
414 return( MBEDTLS_ECP_DP_SECP224R1 );
415 case PSA_BITS_TO_BYTES( 256 ):
416 return( MBEDTLS_ECP_DP_SECP256R1 );
417 case PSA_BITS_TO_BYTES( 384 ):
418 return( MBEDTLS_ECP_DP_SECP384R1 );
419 case PSA_BITS_TO_BYTES( 521 ):
420 return( MBEDTLS_ECP_DP_SECP521R1 );
421 default:
422 return( MBEDTLS_ECP_DP_NONE );
423 }
424 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100425
Paul Elliott8ff510a2020-06-02 17:19:28 +0100426 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100427 switch( byte_length )
428 {
429 case PSA_BITS_TO_BYTES( 256 ):
430 return( MBEDTLS_ECP_DP_BP256R1 );
431 case PSA_BITS_TO_BYTES( 384 ):
432 return( MBEDTLS_ECP_DP_BP384R1 );
433 case PSA_BITS_TO_BYTES( 512 ):
434 return( MBEDTLS_ECP_DP_BP512R1 );
435 default:
436 return( MBEDTLS_ECP_DP_NONE );
437 }
438 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100439
Paul Elliott8ff510a2020-06-02 17:19:28 +0100440 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100441 switch( byte_length )
442 {
443 case PSA_BITS_TO_BYTES( 255 ):
444 return( MBEDTLS_ECP_DP_CURVE25519 );
445 case PSA_BITS_TO_BYTES( 448 ):
446 return( MBEDTLS_ECP_DP_CURVE448 );
447 default:
448 return( MBEDTLS_ECP_DP_NONE );
449 }
450 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100451
Paul Elliott8ff510a2020-06-02 17:19:28 +0100452 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100453 switch( byte_length )
454 {
455 case PSA_BITS_TO_BYTES( 192 ):
456 return( MBEDTLS_ECP_DP_SECP192K1 );
457 case PSA_BITS_TO_BYTES( 224 ):
458 return( MBEDTLS_ECP_DP_SECP224K1 );
459 case PSA_BITS_TO_BYTES( 256 ):
460 return( MBEDTLS_ECP_DP_SECP256K1 );
461 default:
462 return( MBEDTLS_ECP_DP_NONE );
463 }
464 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100465
Gilles Peskine12313cd2018-06-20 00:20:32 +0200466 default:
467 return( MBEDTLS_ECP_DP_NONE );
468 }
469}
John Durkop9814fa22020-11-04 12:28:15 -0800470#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800471 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
472 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
473 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200474
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200475static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
476 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200477{
478 /* Check that the bit size is acceptable for the key type */
479 switch( type )
480 {
481 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200482 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200483 case PSA_KEY_TYPE_DERIVE:
484 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200485#if defined(MBEDTLS_AES_C)
486 case PSA_KEY_TYPE_AES:
487 if( bits != 128 && bits != 192 && bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
491#if defined(MBEDTLS_CAMELLIA_C)
492 case PSA_KEY_TYPE_CAMELLIA:
493 if( bits != 128 && bits != 192 && bits != 256 )
494 return( PSA_ERROR_INVALID_ARGUMENT );
495 break;
496#endif
497#if defined(MBEDTLS_DES_C)
498 case PSA_KEY_TYPE_DES:
499 if( bits != 64 && bits != 128 && bits != 192 )
500 return( PSA_ERROR_INVALID_ARGUMENT );
501 break;
502#endif
503#if defined(MBEDTLS_ARC4_C)
504 case PSA_KEY_TYPE_ARC4:
505 if( bits < 8 || bits > 2048 )
506 return( PSA_ERROR_INVALID_ARGUMENT );
507 break;
508#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200509#if defined(MBEDTLS_CHACHA20_C)
510 case PSA_KEY_TYPE_CHACHA20:
511 if( bits != 256 )
512 return( PSA_ERROR_INVALID_ARGUMENT );
513 break;
514#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200515 default:
516 return( PSA_ERROR_NOT_SUPPORTED );
517 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200518 if( bits % 8 != 0 )
519 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200520
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200521 return( PSA_SUCCESS );
522}
523
John Durkop0e005192020-10-31 22:06:54 -0700524#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
525 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
526 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800527 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
528 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
529 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200530
Gilles Peskine86a440b2018-11-12 18:39:40 +0100531/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
532 * that are not a multiple of 8) well. For example, there is only
533 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
534 * way to return the exact bit size of a key.
535 * To keep things simple, reject non-byte-aligned key sizes. */
536static psa_status_t psa_check_rsa_key_byte_aligned(
537 const mbedtls_rsa_context *rsa )
538{
539 mbedtls_mpi n;
540 psa_status_t status;
541 mbedtls_mpi_init( &n );
542 status = mbedtls_to_psa_error(
543 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
544 if( status == PSA_SUCCESS )
545 {
546 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
547 status = PSA_ERROR_NOT_SUPPORTED;
548 }
549 mbedtls_mpi_free( &n );
550 return( status );
551}
552
Steven Cooreman7f391872020-07-30 14:57:44 +0200553/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200554 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200555 * \param[in] type The type of key contained in \p data.
556 * \param[in] data The buffer from which to load the representation.
557 * \param[in] data_length The size in bytes of \p data.
558 * \param[out] p_rsa Returns a pointer to an RSA context on success.
559 * The caller is responsible for freeing both the
560 * contents of the context and the context itself
561 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200562 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200563static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
564 const uint8_t *data,
565 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200566 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200567{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000568 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200569 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000570 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200571 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000572
573 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200574 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000575 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200576 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200577 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000578 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200579 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000580 if( status != PSA_SUCCESS )
581 goto exit;
582
583 /* We have something that the pkparse module recognizes. If it is a
584 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200585 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200586 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000587 status = PSA_ERROR_INVALID_ARGUMENT;
588 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200589 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000590
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000591 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
592 * supports non-byte-aligned key sizes, but not well. For example,
593 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200594 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000595 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
596 {
597 status = PSA_ERROR_NOT_SUPPORTED;
598 goto exit;
599 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200600 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200601 if( status != PSA_SUCCESS )
602 goto exit;
603
Steven Cooremana2371e52020-07-28 14:30:39 +0200604 /* Copy out the pointer to the RSA context, and reset the PK context
605 * such that pk_free doesn't free the RSA context we just grabbed. */
606 *p_rsa = mbedtls_pk_rsa( ctx );
607 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000608
609exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200610 mbedtls_pk_free( &ctx );
611 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200612}
613
John Durkop6ba40d12020-11-10 08:50:04 -0800614#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
615 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
616 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
617 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
618 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
619 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
620
621#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
622 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
623
Steven Cooremana01795d2020-07-24 22:48:15 +0200624/** Export an RSA key to export representation
625 *
626 * \param[in] type The type of key (public/private) to export
627 * \param[in] rsa The internal RSA representation from which to export
628 * \param[out] data The buffer to export to
629 * \param[in] data_size The length of the buffer to export to
630 * \param[out] data_length The amount of bytes written to \p data
631 */
632static psa_status_t psa_export_rsa_key( psa_key_type_t type,
633 mbedtls_rsa_context *rsa,
634 uint8_t *data,
635 size_t data_size,
636 size_t *data_length )
637{
638#if defined(MBEDTLS_PK_WRITE_C)
639 int ret;
640 mbedtls_pk_context pk;
641 uint8_t *pos = data + data_size;
642
643 mbedtls_pk_init( &pk );
644 pk.pk_info = &mbedtls_rsa_info;
645 pk.pk_ctx = rsa;
646
647 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200648 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
649 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200650 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
651 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
652 else
653 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
654
655 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200656 {
657 /* Clean up in case pk_write failed halfway through. */
658 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200659 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200660 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200661
662 /* The mbedtls_pk_xxx functions write to the end of the buffer.
663 * Move the data to the beginning and erase remaining data
664 * at the original location. */
665 if( 2 * (size_t) ret <= data_size )
666 {
667 memcpy( data, data + data_size - ret, ret );
668 memset( data + data_size - ret, 0, ret );
669 }
670 else if( (size_t) ret < data_size )
671 {
672 memmove( data, data + data_size - ret, ret );
673 memset( data + ret, 0, data_size - ret );
674 }
675
676 *data_length = ret;
677 return( PSA_SUCCESS );
678#else
679 (void) type;
680 (void) rsa;
681 (void) data;
682 (void) data_size;
683 (void) data_length;
684 return( PSA_ERROR_NOT_SUPPORTED );
685#endif /* MBEDTLS_PK_WRITE_C */
686}
687
688/** Import an RSA key from import representation to a slot
689 *
690 * \param[in,out] slot The slot where to store the export representation to
691 * \param[in] data The buffer containing the import representation
692 * \param[in] data_length The amount of bytes in \p data
693 */
694static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
695 const uint8_t *data,
696 size_t data_length )
697{
698 psa_status_t status;
699 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200700 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200701
Steven Cooremana01795d2020-07-24 22:48:15 +0200702 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200703 status = psa_load_rsa_representation( slot->attr.type,
704 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200705 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200706 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200707 if( status != PSA_SUCCESS )
708 goto exit;
709
710 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200711 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200712
Steven Cooreman75b74362020-07-28 14:30:13 +0200713 /* Re-export the data to PSA export format, such that we can store export
714 * representation in the key slot. Export representation in case of RSA is
715 * the smallest representation that's allowed as input, so a straight-up
716 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200717 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200718 if( output == NULL )
719 {
720 status = PSA_ERROR_INSUFFICIENT_MEMORY;
721 goto exit;
722 }
723
Steven Cooremana01795d2020-07-24 22:48:15 +0200724 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200725 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200726 output,
727 data_length,
728 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200729exit:
730 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200731 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200732 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200733
734 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000735 if( status != PSA_SUCCESS )
736 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200737 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000738 return( status );
739 }
740
Steven Cooremana01795d2020-07-24 22:48:15 +0200741 /* On success, store the allocated export-formatted key. */
742 slot->data.key.data = output;
743 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000744
745 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200746}
John Durkop6ba40d12020-11-10 08:50:04 -0800747
748#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800749 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200750
John Durkop9814fa22020-11-04 12:28:15 -0800751#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800752 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
753 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
754 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
755 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200756/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200757 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200758 * \param[in] type The type of key contained in \p data.
759 * \param[in] data The buffer from which to load the representation.
760 * \param[in] data_length The size in bytes of \p data.
761 * \param[out] p_ecp Returns a pointer to an ECP context on success.
762 * The caller is responsible for freeing both the
763 * contents of the context and the context itself
764 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200765 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200766static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
767 const uint8_t *data,
768 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200769 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100770{
771 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200772 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200773 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200774 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100775
Steven Cooreman4fed4552020-08-03 14:46:03 +0200776 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
777 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100778 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200779 /* A Weierstrass public key is represented as:
780 * - The byte 0x04;
781 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
782 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200783 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200784 */
785 if( ( data_length & 1 ) == 0 )
786 return( PSA_ERROR_INVALID_ARGUMENT );
787 curve_size = data_length / 2;
788
789 /* Montgomery public keys are represented in compressed format, meaning
790 * their curve_size is equal to the amount of input. */
791
792 /* Private keys are represented in uncompressed private random integer
793 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100794 }
795
Steven Cooreman6d839f02020-07-30 11:36:45 +0200796 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200797 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200798 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200799 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200800 mbedtls_ecp_keypair_init( ecp );
801
Gilles Peskine4cd32772019-12-02 20:49:42 +0100802 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200803 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
804 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100805 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200806 {
807 status = PSA_ERROR_INVALID_ARGUMENT;
808 goto exit;
809 }
810
Steven Cooremanacda8342020-07-24 23:09:52 +0200811 status = mbedtls_to_psa_error(
812 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
813 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200814 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200815
Steven Cooreman6d839f02020-07-30 11:36:45 +0200816 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200817 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200818 {
819 /* Load the public value. */
820 status = mbedtls_to_psa_error(
821 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200822 data,
823 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200824 if( status != PSA_SUCCESS )
825 goto exit;
826
827 /* Check that the point is on the curve. */
828 status = mbedtls_to_psa_error(
829 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
830 if( status != PSA_SUCCESS )
831 goto exit;
832 }
833 else
834 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200835 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200836 status = mbedtls_to_psa_error(
837 mbedtls_ecp_read_key( ecp->grp.id,
838 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200839 data,
840 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200841 if( status != PSA_SUCCESS )
842 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200843 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200844
845 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200846exit:
847 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200848 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200849 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200850 mbedtls_free( ecp );
851 }
852
Steven Cooreman29149862020-08-05 15:43:42 +0200853 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100854}
John Durkop6ba40d12020-11-10 08:50:04 -0800855#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
856 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
857 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
858 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
859 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000860
John Durkop6ba40d12020-11-10 08:50:04 -0800861#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
862 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200863/** Export an ECP key to export representation
864 *
865 * \param[in] type The type of key (public/private) to export
866 * \param[in] ecp The internal ECP representation from which to export
867 * \param[out] data The buffer to export to
868 * \param[in] data_size The length of the buffer to export to
869 * \param[out] data_length The amount of bytes written to \p data
870 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200871static psa_status_t psa_export_ecp_key( psa_key_type_t type,
872 mbedtls_ecp_keypair *ecp,
873 uint8_t *data,
874 size_t data_size,
875 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200876{
Steven Cooremanacda8342020-07-24 23:09:52 +0200877 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000878
Steven Cooremanacda8342020-07-24 23:09:52 +0200879 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200880 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200881 /* Check whether the public part is loaded */
882 if( mbedtls_ecp_is_zero( &ecp->Q ) )
883 {
884 /* Calculate the public key */
885 status = mbedtls_to_psa_error(
886 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +0100887 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200888 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200889 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200890 }
891
Steven Cooreman4fed4552020-08-03 14:46:03 +0200892 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200893 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
894 MBEDTLS_ECP_PF_UNCOMPRESSED,
895 data_length,
896 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200897 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200898 if( status != PSA_SUCCESS )
899 memset( data, 0, data_size );
900
Steven Cooreman29149862020-08-05 15:43:42 +0200901 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200902 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200903 else
904 {
Steven Cooreman29149862020-08-05 15:43:42 +0200905 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200906 return( PSA_ERROR_BUFFER_TOO_SMALL );
907
908 status = mbedtls_to_psa_error(
909 mbedtls_ecp_write_key( ecp,
910 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200911 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200912 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200913 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200914 else
915 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200916
917 return( status );
918 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200919}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200920
Steven Cooreman4fed4552020-08-03 14:46:03 +0200921/** Import an ECP key from import representation to a slot
922 *
923 * \param[in,out] slot The slot where to store the export representation to
924 * \param[in] data The buffer containing the import representation
925 * \param[in] data_length The amount of bytes in \p data
926 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200927static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
928 const uint8_t *data,
929 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100930{
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 psa_status_t status;
932 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200933 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100934
Steven Cooremanacda8342020-07-24 23:09:52 +0200935 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200936 status = psa_load_ecp_representation( slot->attr.type,
937 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200938 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200939 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100940 if( status != PSA_SUCCESS )
941 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100942
Steven Cooremanacda8342020-07-24 23:09:52 +0200943 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200944 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200945 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200946 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200947
Steven Cooremanacda8342020-07-24 23:09:52 +0200948 /* Re-export the data to PSA export format. There is currently no support
949 * for other input formats then the export format, so this is a 1-1
950 * copy operation. */
951 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200952 if( output == NULL )
953 {
954 status = PSA_ERROR_INSUFFICIENT_MEMORY;
955 goto exit;
956 }
957
958 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200959 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200960 output,
961 data_length,
962 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100963exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200964 /* Always free the PK object (will also free contained ECP context) */
965 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200966 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200967
968 /* Free the allocated buffer only on error. */
969 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100970 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200971 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200972 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100973 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200974
975 /* On success, store the allocated export-formatted key. */
976 slot->data.key.data = output;
977 slot->data.key.bytes = data_length;
978
979 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100980}
John Durkop9814fa22020-11-04 12:28:15 -0800981#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
982 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100983
Gilles Peskineb46bef22019-07-30 21:32:04 +0200984/** Return the size of the key in the given slot, in bits.
985 *
986 * \param[in] slot A key slot.
987 *
988 * \return The key size in bits, read from the metadata in the slot.
989 */
990static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
991{
992 return( slot->attr.bits );
993}
994
Steven Cooreman75b74362020-07-28 14:30:13 +0200995/** Try to allocate a buffer to an empty key slot.
996 *
997 * \param[in,out] slot Key slot to attach buffer to.
998 * \param[in] buffer_length Requested size of the buffer.
999 *
1000 * \retval #PSA_SUCCESS
1001 * The buffer has been successfully allocated.
1002 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1003 * Not enough memory was available for allocation.
1004 * \retval #PSA_ERROR_ALREADY_EXISTS
1005 * Trying to allocate a buffer to a non-empty key slot.
1006 */
1007static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1008 size_t buffer_length )
1009{
1010 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001011 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001012
1013 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1014 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001015 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001016
1017 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001018 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001019}
1020
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001021psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1022 const uint8_t* data,
1023 size_t data_length )
1024{
1025 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1026 data_length );
1027 if( status != PSA_SUCCESS )
1028 return( status );
1029
1030 memcpy( slot->data.key.data, data, data_length );
1031 return( PSA_SUCCESS );
1032}
1033
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001034/** Import key data into a slot.
1035 *
1036 * `slot->type` must have been set previously.
1037 * This function assumes that the slot does not contain any key material yet.
1038 * On failure, the slot content is unchanged.
1039 *
1040 * Persistent storage is not affected.
1041 *
1042 * \param[in,out] slot The key slot to import data into.
1043 * Its `type` field must have previously been set to
1044 * the desired key type.
1045 * It must not contain any key material yet.
1046 * \param[in] data Buffer containing the key material to parse and import.
1047 * \param data_length Size of \p data in bytes.
1048 *
1049 * \retval #PSA_SUCCESS
1050 * \retval #PSA_ERROR_INVALID_ARGUMENT
1051 * \retval #PSA_ERROR_NOT_SUPPORTED
1052 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1053 */
1054static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1055 const uint8_t *data,
1056 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001057{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001058 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001059 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001060
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001061 /* zero-length keys are never supported. */
1062 if( data_length == 0 )
1063 return( PSA_ERROR_NOT_SUPPORTED );
1064
Gilles Peskine8e338702019-07-30 20:06:31 +02001065 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001066 {
Steven Cooreman04524762020-10-13 17:43:44 +02001067 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001068
Steven Cooreman75b74362020-07-28 14:30:13 +02001069 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1070 if( data_length > SIZE_MAX / 8 )
1071 return( PSA_ERROR_NOT_SUPPORTED );
1072
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001073 /* Enforce a size limit, and in particular ensure that the bit
1074 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001075 if( bit_size > PSA_MAX_KEY_BITS )
1076 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001077
1078 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001079 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001080 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001081
1082 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001083 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001084 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001085 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001086
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001087 /* Write the actual key size to the slot.
1088 * psa_start_key_creation() wrote the size declared by the
1089 * caller, which may be 0 (meaning unspecified) or wrong. */
1090 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001091
1092 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001093 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001094 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001095 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001096 /* Try validation through accelerators first. */
1097 bit_size = slot->attr.bits;
1098 psa_key_attributes_t attributes = {
1099 .core = slot->attr
1100 };
1101 status = psa_driver_wrapper_validate_key( &attributes,
1102 data,
1103 data_length,
1104 &bit_size );
1105 if( status == PSA_SUCCESS )
1106 {
1107 /* Key has been validated successfully by an accelerator.
1108 * Copy key material into slot. */
1109 status = psa_copy_key_material_into_slot( slot, data, data_length );
1110 if( status != PSA_SUCCESS )
1111 return( status );
1112
1113 slot->attr.bits = (psa_key_bits_t) bit_size;
1114 return( PSA_SUCCESS );
1115 }
1116 else if( status != PSA_ERROR_NOT_SUPPORTED )
1117 return( status );
1118
1119 /* Key format is not supported by any accelerator, try software fallback
1120 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001121#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1122 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001123 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1124 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001125 return( psa_import_ecp_key( slot, data, data_length ) );
1126 }
John Durkop9814fa22020-11-04 12:28:15 -08001127#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1128 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001129#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1130 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001131 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001132 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001133 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001134 }
John Durkop9814fa22020-11-04 12:28:15 -08001135#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1136 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001137
1138 /* Fell through the fallback as well, so have nothing else to try. */
1139 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140 }
1141 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001142 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001143 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001144 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001145 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001146}
1147
Gilles Peskinef603c712019-01-19 13:40:11 +01001148/** Calculate the intersection of two algorithm usage policies.
1149 *
1150 * Return 0 (which allows no operation) on incompatibility.
1151 */
1152static psa_algorithm_t psa_key_policy_algorithm_intersection(
1153 psa_algorithm_t alg1,
1154 psa_algorithm_t alg2 )
1155{
Gilles Peskine549ea862019-05-22 11:45:59 +02001156 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001157 if( alg1 == alg2 )
1158 return( alg1 );
1159 /* If the policies are from the same hash-and-sign family, check
1160 * if one is a wildcard. If so the other has the specific algorithm. */
1161 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1162 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1163 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1164 {
1165 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1166 return( alg2 );
1167 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1168 return( alg1 );
1169 }
1170 /* If the policies are incompatible, allow nothing. */
1171 return( 0 );
1172}
1173
Gilles Peskined6f371b2019-05-10 19:33:38 +02001174static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1175 psa_algorithm_t requested_alg )
1176{
Gilles Peskine549ea862019-05-22 11:45:59 +02001177 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001178 if( requested_alg == policy_alg )
1179 return( 1 );
1180 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001181 * and requested_alg is the same hash-and-sign family with any hash,
1182 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001183 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1184 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1185 {
1186 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1187 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1188 }
Steven Cooremance48e852020-10-05 16:02:45 +02001189 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001190 * a key derivation with that key agreement should also be allowed. This
1191 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001192 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1193 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1194 {
1195 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1196 policy_alg );
1197 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001198 /* If it isn't permitted, it's forbidden. */
1199 return( 0 );
1200}
1201
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001202/** Test whether a policy permits an algorithm.
1203 *
1204 * The caller must test usage flags separately.
1205 */
1206static int psa_key_policy_permits( const psa_key_policy_t *policy,
1207 psa_algorithm_t alg )
1208{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001209 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1210 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001211}
1212
Gilles Peskinef603c712019-01-19 13:40:11 +01001213/** Restrict a key policy based on a constraint.
1214 *
1215 * \param[in,out] policy The policy to restrict.
1216 * \param[in] constraint The policy constraint to apply.
1217 *
1218 * \retval #PSA_SUCCESS
1219 * \c *policy contains the intersection of the original value of
1220 * \c *policy and \c *constraint.
1221 * \retval #PSA_ERROR_INVALID_ARGUMENT
1222 * \c *policy and \c *constraint are incompatible.
1223 * \c *policy is unchanged.
1224 */
1225static psa_status_t psa_restrict_key_policy(
1226 psa_key_policy_t *policy,
1227 const psa_key_policy_t *constraint )
1228{
1229 psa_algorithm_t intersection_alg =
1230 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001231 psa_algorithm_t intersection_alg2 =
1232 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001233 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1234 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001235 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1236 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001237 policy->usage &= constraint->usage;
1238 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001239 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001240 return( PSA_SUCCESS );
1241}
1242
Ronald Cron5c522922020-11-14 16:35:34 +01001243/** Get the description of a key given its identifier and policy constraints
1244 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001245 *
Ronald Cron5c522922020-11-14 16:35:34 +01001246 * The key must have allow all the usage flags set in \p usage. If \p alg is
1247 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001248 *
Ronald Cron5c522922020-11-14 16:35:34 +01001249 * In case of a persistent key, the function loads the description of the key
1250 * into a key slot if not already done.
1251 *
1252 * On success, the returned key slot is locked. It is the responsibility of
1253 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001254 */
Ronald Cron5c522922020-11-14 16:35:34 +01001255static psa_status_t psa_get_and_lock_key_slot_with_policy(
1256 mbedtls_svc_key_id_t key,
1257 psa_key_slot_t **p_slot,
1258 psa_key_usage_t usage,
1259 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001260{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001261 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1262 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001263
Ronald Cron5c522922020-11-14 16:35:34 +01001264 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001265 if( status != PSA_SUCCESS )
1266 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001267 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001268
1269 /* Enforce that usage policy for the key slot contains all the flags
1270 * required by the usage parameter. There is one exception: public
1271 * keys can always be exported, so we treat public key objects as
1272 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001273 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001274 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001275
1276 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001278 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001279
1280 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001282 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001283
Darryl Green06fd18d2018-07-16 11:21:11 +01001284 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001285
1286error:
1287 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001288 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001289
1290 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001291}
Darryl Green940d72c2018-07-13 13:18:51 +01001292
Ronald Cron5c522922020-11-14 16:35:34 +01001293/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001294 *
1295 * A transparent key is a key for which the key material is directly
1296 * available, as opposed to a key in a secure element.
1297 *
Ronald Cron5c522922020-11-14 16:35:34 +01001298 * This is a temporary function to use instead of
1299 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1300 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001301 *
Ronald Cron5c522922020-11-14 16:35:34 +01001302 * On success, the returned key slot is locked. It is the responsibility of the
1303 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001304 */
1305#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001306static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1307 mbedtls_svc_key_id_t key,
1308 psa_key_slot_t **p_slot,
1309 psa_key_usage_t usage,
1310 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001311{
Ronald Cron5c522922020-11-14 16:35:34 +01001312 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1313 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001314 if( status != PSA_SUCCESS )
1315 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001316
Gilles Peskineadad8132019-07-25 11:31:23 +02001317 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001318 {
Ronald Cron5c522922020-11-14 16:35:34 +01001319 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001320 *p_slot = NULL;
1321 return( PSA_ERROR_NOT_SUPPORTED );
1322 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001323
Gilles Peskine28f8f302019-07-24 13:30:31 +02001324 return( PSA_SUCCESS );
1325}
1326#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1327/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001328#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1329 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001330#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1331
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001332/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001333static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001334{
Gilles Peskine73167e12019-07-12 23:44:37 +02001335#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1336 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001337 {
1338 /* No key material to clean. */
1339 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001340 else
1341#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001342 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001343 /* Data pointer will always be either a valid pointer or NULL in an
1344 * initialized slot, so we can just free it. */
1345 if( slot->data.key.data != NULL )
1346 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001347 mbedtls_free( slot->data.key.data );
1348 slot->data.key.data = NULL;
1349 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001350 }
Darryl Green40225ba2018-11-15 14:48:15 +00001351
1352 return( PSA_SUCCESS );
1353}
1354
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001355/** Completely wipe a slot in memory, including its policy.
1356 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001357psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001358{
1359 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001360
1361 /*
1362 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001363 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001364 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1365 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001366 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001367 */
Ronald Cron5c522922020-11-14 16:35:34 +01001368 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001369 {
1370#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001371 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001372#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001373 status = PSA_ERROR_CORRUPTION_DETECTED;
1374 }
1375
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001376 /* Multipart operations may still be using the key. This is safe
1377 * because all multipart operation objects are independent from
1378 * the key slot: if they need to access the key after the setup
1379 * phase, they have a copy of the key. Note that this means that
1380 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001381 /* At this point, key material and other type-specific content has
1382 * been wiped. Clear remaining metadata. We can call memset and not
1383 * zeroize because the metadata is not particularly sensitive. */
1384 memset( slot, 0, sizeof( *slot ) );
1385 return( status );
1386}
1387
Ronald Croncf56a0a2020-08-04 09:51:30 +02001388psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001389{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001390 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001391 psa_status_t status; /* status of the last operation */
1392 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001393#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1394 psa_se_drv_table_entry_t *driver;
1395#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001396
Ronald Croncf56a0a2020-08-04 09:51:30 +02001397 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001398 return( PSA_SUCCESS );
1399
Ronald Cronf2911112020-10-29 17:51:10 +01001400 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001401 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001402 * key, this will load the key description from persistent memory if not
1403 * done yet. We cannot avoid this loading as without it we don't know if
1404 * the key is operated by an SE or not and this information is needed by
1405 * the current implementation.
1406 */
Ronald Cron5c522922020-11-14 16:35:34 +01001407 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001408 if( status != PSA_SUCCESS )
1409 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001410
Ronald Cronf2911112020-10-29 17:51:10 +01001411 /*
1412 * If the key slot containing the key description is under access by the
1413 * library (apart from the present access), the key cannot be destroyed
1414 * yet. For the time being, just return in error. Eventually (to be
1415 * implemented), the key should be destroyed when all accesses have
1416 * stopped.
1417 */
Ronald Cron5c522922020-11-14 16:35:34 +01001418 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001419 {
Ronald Cron5c522922020-11-14 16:35:34 +01001420 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001421 return( PSA_ERROR_GENERIC_ERROR );
1422 }
1423
Gilles Peskine354f7672019-07-12 23:46:38 +02001424#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001425 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001426 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001427 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001428 /* For a key in a secure element, we need to do three things:
1429 * remove the key file in internal storage, destroy the
1430 * key inside the secure element, and update the driver's
1431 * persistent data. Start a transaction that will encompass these
1432 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001433 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001434 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001435 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001436 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001437 status = psa_crypto_save_transaction( );
1438 if( status != PSA_SUCCESS )
1439 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001440 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001441 /* We should still try to destroy the key in the secure
1442 * element and the key metadata in storage. This is especially
1443 * important if the error is that the storage is full.
1444 * But how to do it exactly without risking an inconsistent
1445 * state after a reset?
1446 * https://github.com/ARMmbed/mbed-crypto/issues/215
1447 */
1448 overall_status = status;
1449 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001450 }
1451
Gilles Peskine354f7672019-07-12 23:46:38 +02001452 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001453 if( overall_status == PSA_SUCCESS )
1454 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001455 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001456#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1457
Darryl Greend49a4992018-06-18 17:27:26 +01001458#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001459 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001460 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001461 status = psa_destroy_persistent_key( slot->attr.id );
1462 if( overall_status == PSA_SUCCESS )
1463 overall_status = status;
1464
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001465 /* TODO: other slots may have a copy of the same key. We should
1466 * invalidate them.
1467 * https://github.com/ARMmbed/mbed-crypto/issues/214
1468 */
Darryl Greend49a4992018-06-18 17:27:26 +01001469 }
1470#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001471
Gilles Peskinefc762652019-07-22 19:30:34 +02001472#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1473 if( driver != NULL )
1474 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001475 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001476 if( overall_status == PSA_SUCCESS )
1477 overall_status = status;
1478 status = psa_crypto_stop_transaction( );
1479 if( overall_status == PSA_SUCCESS )
1480 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001481 }
1482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1483
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001484#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1485exit:
1486#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001487 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001488 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1489 if( overall_status == PSA_SUCCESS )
1490 overall_status = status;
1491 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001492}
1493
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001494void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001495{
Gilles Peskineb699f072019-04-26 16:06:02 +02001496 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001497 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001498}
1499
Gilles Peskineb699f072019-04-26 16:06:02 +02001500psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1501 psa_key_type_t type,
1502 const uint8_t *data,
1503 size_t data_length )
1504{
1505 uint8_t *copy = NULL;
1506
1507 if( data_length != 0 )
1508 {
1509 copy = mbedtls_calloc( 1, data_length );
1510 if( copy == NULL )
1511 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1512 memcpy( copy, data, data_length );
1513 }
1514 /* After this point, this function is guaranteed to succeed, so it
1515 * can start modifying `*attributes`. */
1516
1517 if( attributes->domain_parameters != NULL )
1518 {
1519 mbedtls_free( attributes->domain_parameters );
1520 attributes->domain_parameters = NULL;
1521 attributes->domain_parameters_size = 0;
1522 }
1523
1524 attributes->domain_parameters = copy;
1525 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001526 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001527 return( PSA_SUCCESS );
1528}
1529
1530psa_status_t psa_get_key_domain_parameters(
1531 const psa_key_attributes_t *attributes,
1532 uint8_t *data, size_t data_size, size_t *data_length )
1533{
1534 if( attributes->domain_parameters_size > data_size )
1535 return( PSA_ERROR_BUFFER_TOO_SMALL );
1536 *data_length = attributes->domain_parameters_size;
1537 if( attributes->domain_parameters_size != 0 )
1538 memcpy( data, attributes->domain_parameters,
1539 attributes->domain_parameters_size );
1540 return( PSA_SUCCESS );
1541}
1542
John Durkop07cc04a2020-11-16 22:08:34 -08001543#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001544 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001545static psa_status_t psa_get_rsa_public_exponent(
1546 const mbedtls_rsa_context *rsa,
1547 psa_key_attributes_t *attributes )
1548{
1549 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001550 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001551 uint8_t *buffer = NULL;
1552 size_t buflen;
1553 mbedtls_mpi_init( &mpi );
1554
1555 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1556 if( ret != 0 )
1557 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001558 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1559 {
1560 /* It's the default value, which is reported as an empty string,
1561 * so there's nothing to do. */
1562 goto exit;
1563 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001564
1565 buflen = mbedtls_mpi_size( &mpi );
1566 buffer = mbedtls_calloc( 1, buflen );
1567 if( buffer == NULL )
1568 {
1569 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1570 goto exit;
1571 }
1572 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1573 if( ret != 0 )
1574 goto exit;
1575 attributes->domain_parameters = buffer;
1576 attributes->domain_parameters_size = buflen;
1577
1578exit:
1579 mbedtls_mpi_free( &mpi );
1580 if( ret != 0 )
1581 mbedtls_free( buffer );
1582 return( mbedtls_to_psa_error( ret ) );
1583}
John Durkop07cc04a2020-11-16 22:08:34 -08001584#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001585 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001586
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001587/** Retrieve all the publicly-accessible attributes of a key.
1588 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001589psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001590 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001592 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001593 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001594 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001595
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001596 psa_reset_key_attributes( attributes );
1597
Ronald Cron5c522922020-11-14 16:35:34 +01001598 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001599 if( status != PSA_SUCCESS )
1600 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001601
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001602 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001603 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1604 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1605
1606#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1607 if( psa_key_slot_is_external( slot ) )
1608 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1609#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001610
Gilles Peskine8e338702019-07-30 20:06:31 +02001611 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001612 {
John Durkop07cc04a2020-11-16 22:08:34 -08001613#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001614 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001615 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001616 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001617#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001618 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001619 * is not yet implemented.
1620 * https://github.com/ARMmbed/mbed-crypto/issues/216
1621 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001622 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001623 break;
1624#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001625 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001626 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001627
Steven Cooreman4fed4552020-08-03 14:46:03 +02001628 status = psa_load_rsa_representation( slot->attr.type,
1629 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001630 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001631 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001632 if( status != PSA_SUCCESS )
1633 break;
1634
Steven Cooremana2371e52020-07-28 14:30:39 +02001635 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001636 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001637 mbedtls_rsa_free( rsa );
1638 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001639 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001640 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001641#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001642 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001643 default:
1644 /* Nothing else to do. */
1645 break;
1646 }
1647
1648 if( status != PSA_SUCCESS )
1649 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001650
Ronald Cron5c522922020-11-14 16:35:34 +01001651 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001652
Ronald Cron5c522922020-11-14 16:35:34 +01001653 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001654}
1655
Gilles Peskinec8000c02019-08-02 20:15:51 +02001656#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1657psa_status_t psa_get_key_slot_number(
1658 const psa_key_attributes_t *attributes,
1659 psa_key_slot_number_t *slot_number )
1660{
1661 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1662 {
1663 *slot_number = attributes->slot_number;
1664 return( PSA_SUCCESS );
1665 }
1666 else
1667 return( PSA_ERROR_INVALID_ARGUMENT );
1668}
1669#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1670
Steven Cooremana01795d2020-07-24 22:48:15 +02001671static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1672 uint8_t *data,
1673 size_t data_size,
1674 size_t *data_length )
1675{
1676 if( slot->data.key.bytes > data_size )
1677 return( PSA_ERROR_BUFFER_TOO_SMALL );
1678 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1679 memset( data + slot->data.key.bytes, 0,
1680 data_size - slot->data.key.bytes );
1681 *data_length = slot->data.key.bytes;
1682 return( PSA_SUCCESS );
1683}
1684
Gilles Peskinef603c712019-01-19 13:40:11 +01001685static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1686 uint8_t *data,
1687 size_t data_size,
1688 size_t *data_length,
1689 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001690{
Gilles Peskine5d309672019-07-12 23:47:28 +02001691#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1692 const psa_drv_se_t *drv;
1693 psa_drv_se_context_t *drv_context;
1694#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1695
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001696 *data_length = 0;
1697
Gilles Peskine8e338702019-07-30 20:06:31 +02001698 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001699 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001700
Gilles Peskinef9168942019-09-12 19:20:29 +02001701 /* Reject a zero-length output buffer now, since this can never be a
1702 * valid key representation. This way we know that data must be a valid
1703 * pointer and we can do things like memset(data, ..., data_size). */
1704 if( data_size == 0 )
1705 return( PSA_ERROR_BUFFER_TOO_SMALL );
1706
Gilles Peskine5d309672019-07-12 23:47:28 +02001707#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001708 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001709 {
1710 psa_drv_se_export_key_t method;
1711 if( drv->key_management == NULL )
1712 return( PSA_ERROR_NOT_SUPPORTED );
1713 method = ( export_public_key ?
1714 drv->key_management->p_export_public :
1715 drv->key_management->p_export );
1716 if( method == NULL )
1717 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001718 return( method( drv_context,
1719 slot->data.se.slot_number,
1720 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001721 }
1722#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1723
Gilles Peskine8e338702019-07-30 20:06:31 +02001724 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001725 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001726 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001727 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001728 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1729 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001730 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001731 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001732 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001733 /* Exporting public -> public */
1734 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1735 }
1736 else if( !export_public_key )
1737 {
1738 /* Exporting private -> private */
1739 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1740 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001741
Steven Cooreman560c28a2020-07-24 23:20:24 +02001742 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001743 * so conversion is needed. Try the accelerators first. */
1744 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1745 data,
1746 data_size,
1747 data_length );
1748
1749 if( status != PSA_ERROR_NOT_SUPPORTED ||
1750 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1751 return( status );
1752
Steven Cooreman560c28a2020-07-24 23:20:24 +02001753 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1754 {
John Durkop0e005192020-10-31 22:06:54 -07001755#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1756 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001757 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001758 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001759 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001760 slot->data.key.data,
1761 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001762 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001763 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001764 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001765
Steven Cooreman560c28a2020-07-24 23:20:24 +02001766 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001767 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001768 data,
1769 data_size,
1770 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001771
Steven Cooremana2371e52020-07-28 14:30:39 +02001772 mbedtls_rsa_free( rsa );
1773 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001774
Steven Cooreman560c28a2020-07-24 23:20:24 +02001775 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001776#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001777 /* We don't know how to convert a private RSA key to public. */
1778 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001779#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1780 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001781 }
1782 else
Moran Pekera998bc62018-04-16 18:16:20 +03001783 {
John Durkop6ba40d12020-11-10 08:50:04 -08001784#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1785 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001786 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001787 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001788 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001789 slot->data.key.data,
1790 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001791 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001792 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001793 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001794
1795 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1796 PSA_KEY_TYPE_ECC_GET_FAMILY(
1797 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001798 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001799 data,
1800 data_size,
1801 data_length );
1802
Steven Cooremana2371e52020-07-28 14:30:39 +02001803 mbedtls_ecp_keypair_free( ecp );
1804 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001805 return( status );
1806#else
1807 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001808 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001809#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1810 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001811 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001812 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001813 else
1814 {
1815 /* This shouldn't happen in the reference implementation, but
1816 it is valid for a special-purpose implementation to omit
1817 support for exporting certain key types. */
1818 return( PSA_ERROR_NOT_SUPPORTED );
1819 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001820}
1821
Ronald Croncf56a0a2020-08-04 09:51:30 +02001822psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001823 uint8_t *data,
1824 size_t data_size,
1825 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001826{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001827 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001828 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001829 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001830
1831 /* Set the key to empty now, so that even when there are errors, we always
1832 * set data_length to a value between 0 and data_size. On error, setting
1833 * the key to empty is a good choice because an empty key representation is
1834 * unlikely to be accepted anywhere. */
1835 *data_length = 0;
1836
1837 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001838 * which don't require any flag, but
1839 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1840 */
1841 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1842 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001843 if( status != PSA_SUCCESS )
1844 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001845
1846 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001847 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001848
Ronald Cron5c522922020-11-14 16:35:34 +01001849 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001850}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001851
Ronald Croncf56a0a2020-08-04 09:51:30 +02001852psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001853 uint8_t *data,
1854 size_t data_size,
1855 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001856{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001857 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001858 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001859 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001860
1861 /* Set the key to empty now, so that even when there are errors, we always
1862 * set data_length to a value between 0 and data_size. On error, setting
1863 * the key to empty is a good choice because an empty key representation is
1864 * unlikely to be accepted anywhere. */
1865 *data_length = 0;
1866
1867 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001868 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001869 if( status != PSA_SUCCESS )
1870 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001871
1872 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001873 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001874
Ronald Cron5c522922020-11-14 16:35:34 +01001875 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001876}
1877
Gilles Peskine91e8c332019-08-02 19:19:39 +02001878#if defined(static_assert)
1879static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1880 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001881static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001882 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001883static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001884 "One or more key attribute flag is listed as both internal-only and external-only" );
1885#endif
1886
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001887/** Validate that a key policy is internally well-formed.
1888 *
1889 * This function only rejects invalid policies. It does not validate the
1890 * consistency of the policy with respect to other attributes of the key
1891 * such as the key type.
1892 */
1893static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001894{
1895 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001896 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 PSA_KEY_USAGE_ENCRYPT |
1898 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001899 PSA_KEY_USAGE_SIGN_HASH |
1900 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001901 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1902 return( PSA_ERROR_INVALID_ARGUMENT );
1903
Gilles Peskine4747d192019-04-17 15:05:45 +02001904 return( PSA_SUCCESS );
1905}
1906
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001907/** Validate the internal consistency of key attributes.
1908 *
1909 * This function only rejects invalid attribute values. If does not
1910 * validate the consistency of the attributes with any key data that may
1911 * be involved in the creation of the key.
1912 *
1913 * Call this function early in the key creation process.
1914 *
1915 * \param[in] attributes Key attributes for the new key.
1916 * \param[out] p_drv On any return, the driver for the key, if any.
1917 * NULL for a transparent key.
1918 *
1919 */
1920static psa_status_t psa_validate_key_attributes(
1921 const psa_key_attributes_t *attributes,
1922 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001923{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001924 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001925 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001926 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001927
Ronald Cron54b90082020-10-29 15:26:43 +01001928 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001929 if( status != PSA_SUCCESS )
1930 return( status );
1931
Ronald Crond2ed4812020-07-17 16:11:30 +02001932 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001933 if( status != PSA_SUCCESS )
1934 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001935
Ronald Cron65f38a32020-10-23 17:11:13 +02001936 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1937 {
1938 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1939 return( PSA_ERROR_INVALID_ARGUMENT );
1940 }
1941 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001942 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001943 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001944 if( status != PSA_SUCCESS )
1945 return( status );
1946 }
1947
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001948 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001949 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001950 return( status );
1951
1952 /* Refuse to create overly large keys.
1953 * Note that this doesn't trigger on import if the attributes don't
1954 * explicitly specify a size (so psa_get_key_bits returns 0), so
1955 * psa_import_key() needs its own checks. */
1956 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1957 return( PSA_ERROR_NOT_SUPPORTED );
1958
Gilles Peskine91e8c332019-08-02 19:19:39 +02001959 /* Reject invalid flags. These should not be reachable through the API. */
1960 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1961 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1962 return( PSA_ERROR_INVALID_ARGUMENT );
1963
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001964 return( PSA_SUCCESS );
1965}
1966
Gilles Peskine4747d192019-04-17 15:05:45 +02001967/** Prepare a key slot to receive key material.
1968 *
1969 * This function allocates a key slot and sets its metadata.
1970 *
1971 * If this function fails, call psa_fail_key_creation().
1972 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 * This function is intended to be used as follows:
1974 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001975 * it with the specified attributes, and in case of a volatile key assign it
1976 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001977 * -# Populate the slot with the key material.
1978 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1979 * In case of failure at any step, stop the sequence and call
1980 * psa_fail_key_creation().
1981 *
Ronald Cron5c522922020-11-14 16:35:34 +01001982 * On success, the key slot is locked. It is the responsibility of the caller
1983 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001984 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001985 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001986 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001987 * \param[out] p_slot On success, a pointer to the prepared slot.
1988 * \param[out] p_drv On any return, the driver for the key, if any.
1989 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001990 *
1991 * \retval #PSA_SUCCESS
1992 * The key slot is ready to receive key material.
1993 * \return If this function fails, the key slot is an invalid state.
1994 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001995 */
1996static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001997 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001998 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001999 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002000 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02002001{
2002 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02002003 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002004 psa_key_slot_t *slot;
2005
Gilles Peskinedf179142019-07-15 22:02:14 +02002006 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002007 *p_drv = NULL;
2008
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002009 status = psa_validate_key_attributes( attributes, p_drv );
2010 if( status != PSA_SUCCESS )
2011 return( status );
2012
Ronald Cronc4d1b512020-07-31 11:26:37 +02002013 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002014 if( status != PSA_SUCCESS )
2015 return( status );
2016 slot = *p_slot;
2017
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002018 /* We're storing the declared bit-size of the key. It's up to each
2019 * creation mechanism to verify that this information is correct.
2020 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002021 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002022 * is optional (import, copy). In case of a volatile key, assign it the
2023 * volatile key identifier associated to the slot returned to contain its
2024 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002025
2026 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002027 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2028 {
2029#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2030 slot->attr.id = volatile_key_id;
2031#else
2032 slot->attr.id.key_id = volatile_key_id;
2033#endif
2034 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002035
Gilles Peskine91e8c332019-08-02 19:19:39 +02002036 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002037 * external-only flags, query `attributes`. Thanks to the check
2038 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002039 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002040 * may have set. */
2041 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002042
Gilles Peskinecbaff462019-07-12 23:46:04 +02002043#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002044 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002045 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002046 * create the key file in internal storage, create the
2047 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002048 * persistent data. This is done by starting a transaction that will
2049 * encompass these three actions.
2050 * For registering a volatile key, we just need to find an appropriate
2051 * slot number inside the SE. Since the key is designated volatile, creating
2052 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002053 /* The first thing to do is to find a slot number for the new key.
2054 * We save the slot number in persistent storage as part of the
2055 * transaction data. It will be needed to recover if the power
2056 * fails during the key creation process, to clean up on the secure
2057 * element side after restarting. Obtaining a slot number from the
2058 * secure element driver updates its persistent state, but we do not yet
2059 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002060 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002061 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002062 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002063 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002064 &slot->data.se.slot_number );
2065 if( status != PSA_SUCCESS )
2066 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002067
2068 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002069 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002070 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2071 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2072 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2073 psa_crypto_transaction.key.id = slot->attr.id;
2074 status = psa_crypto_save_transaction( );
2075 if( status != PSA_SUCCESS )
2076 {
2077 (void) psa_crypto_stop_transaction( );
2078 return( status );
2079 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002080 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002081 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002082
2083 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2084 {
2085 /* Key registration only makes sense with a secure element. */
2086 return( PSA_ERROR_INVALID_ARGUMENT );
2087 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002088#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2089
Ronald Cronc4d1b512020-07-31 11:26:37 +02002090 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002091}
Gilles Peskine4747d192019-04-17 15:05:45 +02002092
2093/** Finalize the creation of a key once its key material has been set.
2094 *
2095 * This entails writing the key to persistent storage.
2096 *
2097 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002098 * See the documentation of psa_start_key_creation() for the intended use
2099 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002100 *
Ronald Cron5c522922020-11-14 16:35:34 +01002101 * If the finalization succeeds, the function unlocks the key slot (it was
2102 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2103 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002104 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002105 * \param[in,out] slot Pointer to the slot with key material.
2106 * \param[in] driver The secure element driver for the key,
2107 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002108 * \param[out] key On success, identifier of the key. Note that the
2109 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002110 *
2111 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002112 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002113 * \return If this function fails, the key slot is an invalid state.
2114 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002115 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002116static psa_status_t psa_finish_key_creation(
2117 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002118 psa_se_drv_table_entry_t *driver,
2119 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002120{
2121 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002122 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002123 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002124
2125#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002126 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002127 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002128#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2129 if( driver != NULL )
2130 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002131 psa_se_key_data_storage_t data;
2132#if defined(static_assert)
2133 static_assert( sizeof( slot->data.se.slot_number ) ==
2134 sizeof( data.slot_number ),
2135 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002136#endif
2137 memcpy( &data.slot_number, &slot->data.se.slot_number,
2138 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002139 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002140 (uint8_t*) &data,
2141 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002142 }
2143 else
2144#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2145 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002146 /* Key material is saved in export representation in the slot, so
2147 * just pass the slot buffer for storage. */
2148 status = psa_save_persistent_key( &slot->attr,
2149 slot->data.key.data,
2150 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002151 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002152 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002153#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2154
Gilles Peskinecbaff462019-07-12 23:46:04 +02002155#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002156 /* Finish the transaction for a key creation. This does not
2157 * happen when registering an existing key. Detect this case
2158 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002159 * creation of a persistent key in a secure element requires a transaction,
2160 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002161 if( driver != NULL &&
2162 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002163 {
2164 status = psa_save_se_persistent_data( driver );
2165 if( status != PSA_SUCCESS )
2166 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002167 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002168 return( status );
2169 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002170 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002171 }
2172#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2173
Ronald Cron50972942020-11-14 11:28:25 +01002174 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002175 {
2176 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002177 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002178 if( status != PSA_SUCCESS )
2179 *key = MBEDTLS_SVC_KEY_ID_INIT;
2180 }
Ronald Cron50972942020-11-14 11:28:25 +01002181
Gilles Peskine4747d192019-04-17 15:05:45 +02002182 return( status );
2183}
2184
2185/** Abort the creation of a key.
2186 *
2187 * You may call this function after calling psa_start_key_creation(),
2188 * or after psa_finish_key_creation() fails. In other circumstances, this
2189 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002190 * See the documentation of psa_start_key_creation() for the intended use
2191 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002192 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002193 * \param[in,out] slot Pointer to the slot with key material.
2194 * \param[in] driver The secure element driver for the key,
2195 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002196 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002197static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002198 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002199{
Gilles Peskine011e4282019-06-26 18:34:38 +02002200 (void) driver;
2201
Gilles Peskine4747d192019-04-17 15:05:45 +02002202 if( slot == NULL )
2203 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002204
2205#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002206 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002207 * element, and the failure happened later (when saving metadata
2208 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002209 * element.
2210 * https://github.com/ARMmbed/mbed-crypto/issues/217
2211 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002212
Gilles Peskined7729582019-08-05 15:55:54 +02002213 /* Abort the ongoing transaction if any (there may not be one if
2214 * the creation process failed before starting one, or if the
2215 * key creation is a registration of a key in a secure element).
2216 * Earlier functions must already have done what it takes to undo any
2217 * partial creation. All that's left is to update the transaction data
2218 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002219 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002220#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2221
Gilles Peskine4747d192019-04-17 15:05:45 +02002222 psa_wipe_key_slot( slot );
2223}
2224
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002225/** Validate optional attributes during key creation.
2226 *
2227 * Some key attributes are optional during key creation. If they are
2228 * specified in the attributes structure, check that they are consistent
2229 * with the data in the slot.
2230 *
2231 * This function should be called near the end of key creation, after
2232 * the slot in memory is fully populated but before saving persistent data.
2233 */
2234static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235 const psa_key_slot_t *slot,
2236 const psa_key_attributes_t *attributes )
2237{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002238 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002239 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002240 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002241 return( PSA_ERROR_INVALID_ARGUMENT );
2242 }
2243
2244 if( attributes->domain_parameters_size != 0 )
2245 {
John Durkop0e005192020-10-31 22:06:54 -07002246#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2247 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002248 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002249 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002250 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002251 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002252 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002253
Steven Cooreman7f391872020-07-30 14:57:44 +02002254 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002255 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002256 slot->data.key.data,
2257 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002258 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002259 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002260 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002261
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002262 mbedtls_mpi_init( &actual );
2263 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002264 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002265 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002266 mbedtls_rsa_free( rsa );
2267 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002268 if( ret != 0 )
2269 goto rsa_exit;
2270 ret = mbedtls_mpi_read_binary( &required,
2271 attributes->domain_parameters,
2272 attributes->domain_parameters_size );
2273 if( ret != 0 )
2274 goto rsa_exit;
2275 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2276 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2277 rsa_exit:
2278 mbedtls_mpi_free( &actual );
2279 mbedtls_mpi_free( &required );
2280 if( ret != 0)
2281 return( mbedtls_to_psa_error( ret ) );
2282 }
2283 else
John Durkop9814fa22020-11-04 12:28:15 -08002284#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2285 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002286 {
2287 return( PSA_ERROR_INVALID_ARGUMENT );
2288 }
2289 }
2290
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002291 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002292 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002293 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002294 return( PSA_ERROR_INVALID_ARGUMENT );
2295 }
2296
2297 return( PSA_SUCCESS );
2298}
2299
Gilles Peskine4747d192019-04-17 15:05:45 +02002300psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002301 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002302 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002303 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002304{
2305 psa_status_t status;
2306 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002307 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002308
Ronald Cron81709fc2020-11-14 12:10:32 +01002309 *key = MBEDTLS_SVC_KEY_ID_INIT;
2310
Gilles Peskine0f84d622019-09-12 19:03:13 +02002311 /* Reject zero-length symmetric keys (including raw data key objects).
2312 * This also rejects any key which might be encoded as an empty string,
2313 * which is never valid. */
2314 if( data_length == 0 )
2315 return( PSA_ERROR_INVALID_ARGUMENT );
2316
Gilles Peskinedf179142019-07-15 22:02:14 +02002317 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002318 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002319 if( status != PSA_SUCCESS )
2320 goto exit;
2321
Gilles Peskine5d309672019-07-12 23:47:28 +02002322#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2323 if( driver != NULL )
2324 {
2325 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002326 /* The driver should set the number of key bits, however in
2327 * case it doesn't, we initialize bits to an invalid value. */
2328 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002329 if( drv->key_management == NULL ||
2330 drv->key_management->p_import == NULL )
2331 {
2332 status = PSA_ERROR_NOT_SUPPORTED;
2333 goto exit;
2334 }
2335 status = drv->key_management->p_import(
2336 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002337 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002338 &bits );
2339 if( status != PSA_SUCCESS )
2340 goto exit;
2341 if( bits > PSA_MAX_KEY_BITS )
2342 {
2343 status = PSA_ERROR_NOT_SUPPORTED;
2344 goto exit;
2345 }
2346 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002347 }
2348 else
2349#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2350 {
2351 status = psa_import_key_into_slot( slot, data, data_length );
2352 if( status != PSA_SUCCESS )
2353 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002354 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002355 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002356 if( status != PSA_SUCCESS )
2357 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002358
Ronald Cron81709fc2020-11-14 12:10:32 +01002359 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002360exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002361 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002362 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002363
Gilles Peskine4747d192019-04-17 15:05:45 +02002364 return( status );
2365}
2366
Gilles Peskined7729582019-08-05 15:55:54 +02002367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2368psa_status_t mbedtls_psa_register_se_key(
2369 const psa_key_attributes_t *attributes )
2370{
2371 psa_status_t status;
2372 psa_key_slot_t *slot = NULL;
2373 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002374 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002375
2376 /* Leaving attributes unspecified is not currently supported.
2377 * It could make sense to query the key type and size from the
2378 * secure element, but not all secure elements support this
2379 * and the driver HAL doesn't currently support it. */
2380 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2381 return( PSA_ERROR_NOT_SUPPORTED );
2382 if( psa_get_key_bits( attributes ) == 0 )
2383 return( PSA_ERROR_NOT_SUPPORTED );
2384
2385 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002386 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002387 if( status != PSA_SUCCESS )
2388 goto exit;
2389
Ronald Cron81709fc2020-11-14 12:10:32 +01002390 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002391
2392exit:
2393 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002394 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002395
Gilles Peskined7729582019-08-05 15:55:54 +02002396 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002397 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002398 return( status );
2399}
2400#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2401
Gilles Peskinef603c712019-01-19 13:40:11 +01002402static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002403 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002404{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002405 psa_status_t status = psa_copy_key_material_into_slot( target,
2406 source->data.key.data,
2407 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002408 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002409 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002410
Steven Cooreman398aee52020-10-13 14:35:45 +02002411 target->attr.type = source->attr.type;
2412 target->attr.bits = source->attr.bits;
2413
2414 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002415}
2416
Ronald Croncf56a0a2020-08-04 09:51:30 +02002417psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002418 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002419 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002420{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002421 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002422 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002423 psa_key_slot_t *source_slot = NULL;
2424 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002425 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002426 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002427
Ronald Cron81709fc2020-11-14 12:10:32 +01002428 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2429
Ronald Cron5c522922020-11-14 16:35:34 +01002430 status = psa_get_and_lock_transparent_key_slot_with_policy(
2431 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002432 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002433 goto exit;
2434
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002435 status = psa_validate_optional_attributes( source_slot,
2436 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002437 if( status != PSA_SUCCESS )
2438 goto exit;
2439
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002440 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002441 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002442 if( status != PSA_SUCCESS )
2443 goto exit;
2444
Ronald Cron81709fc2020-11-14 12:10:32 +01002445 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2446 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002447 if( status != PSA_SUCCESS )
2448 goto exit;
2449
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002450#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2451 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002452 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002453 /* Copying to a secure element is not implemented yet. */
2454 status = PSA_ERROR_NOT_SUPPORTED;
2455 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002456 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002457#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002458
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002459 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002460 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002461 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002462
Ronald Cron81709fc2020-11-14 12:10:32 +01002463 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002464exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002465 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002466 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002467
Ronald Cron5c522922020-11-14 16:35:34 +01002468 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002469
Ronald Cron5c522922020-11-14 16:35:34 +01002470 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002471}
2472
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002473
2474
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002475/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002476/* Message digests */
2477/****************************************************************/
2478
John Durkop07cc04a2020-11-16 22:08:34 -08002479#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002480 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2481 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002482 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002483static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002484{
2485 switch( alg )
2486 {
2487#if defined(MBEDTLS_MD2_C)
2488 case PSA_ALG_MD2:
2489 return( &mbedtls_md2_info );
2490#endif
2491#if defined(MBEDTLS_MD4_C)
2492 case PSA_ALG_MD4:
2493 return( &mbedtls_md4_info );
2494#endif
2495#if defined(MBEDTLS_MD5_C)
2496 case PSA_ALG_MD5:
2497 return( &mbedtls_md5_info );
2498#endif
2499#if defined(MBEDTLS_RIPEMD160_C)
2500 case PSA_ALG_RIPEMD160:
2501 return( &mbedtls_ripemd160_info );
2502#endif
2503#if defined(MBEDTLS_SHA1_C)
2504 case PSA_ALG_SHA_1:
2505 return( &mbedtls_sha1_info );
2506#endif
2507#if defined(MBEDTLS_SHA256_C)
2508 case PSA_ALG_SHA_224:
2509 return( &mbedtls_sha224_info );
2510 case PSA_ALG_SHA_256:
2511 return( &mbedtls_sha256_info );
2512#endif
2513#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002514#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002515 case PSA_ALG_SHA_384:
2516 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002517#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002518 case PSA_ALG_SHA_512:
2519 return( &mbedtls_sha512_info );
2520#endif
2521 default:
2522 return( NULL );
2523 }
2524}
John Durkop07cc04a2020-11-16 22:08:34 -08002525#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002526 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2527 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2528 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002529
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002530psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2531{
2532 switch( operation->alg )
2533 {
Gilles Peskine81736312018-06-26 15:04:31 +02002534 case 0:
2535 /* The object has (apparently) been initialized but it is not
2536 * in use. It's ok to call abort on such an object, and there's
2537 * nothing to do. */
2538 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002539#if defined(MBEDTLS_MD2_C)
2540 case PSA_ALG_MD2:
2541 mbedtls_md2_free( &operation->ctx.md2 );
2542 break;
2543#endif
2544#if defined(MBEDTLS_MD4_C)
2545 case PSA_ALG_MD4:
2546 mbedtls_md4_free( &operation->ctx.md4 );
2547 break;
2548#endif
2549#if defined(MBEDTLS_MD5_C)
2550 case PSA_ALG_MD5:
2551 mbedtls_md5_free( &operation->ctx.md5 );
2552 break;
2553#endif
2554#if defined(MBEDTLS_RIPEMD160_C)
2555 case PSA_ALG_RIPEMD160:
2556 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2557 break;
2558#endif
2559#if defined(MBEDTLS_SHA1_C)
2560 case PSA_ALG_SHA_1:
2561 mbedtls_sha1_free( &operation->ctx.sha1 );
2562 break;
2563#endif
2564#if defined(MBEDTLS_SHA256_C)
2565 case PSA_ALG_SHA_224:
2566 case PSA_ALG_SHA_256:
2567 mbedtls_sha256_free( &operation->ctx.sha256 );
2568 break;
2569#endif
2570#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002571#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002572 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002573#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002574 case PSA_ALG_SHA_512:
2575 mbedtls_sha512_free( &operation->ctx.sha512 );
2576 break;
2577#endif
2578 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002579 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002580 }
2581 operation->alg = 0;
2582 return( PSA_SUCCESS );
2583}
2584
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002585psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002586 psa_algorithm_t alg )
2587{
Janos Follath24eed8d2019-11-22 13:21:35 +00002588 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002589
2590 /* A context must be freshly initialized before it can be set up. */
2591 if( operation->alg != 0 )
2592 {
2593 return( PSA_ERROR_BAD_STATE );
2594 }
2595
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002596 switch( alg )
2597 {
2598#if defined(MBEDTLS_MD2_C)
2599 case PSA_ALG_MD2:
2600 mbedtls_md2_init( &operation->ctx.md2 );
2601 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2602 break;
2603#endif
2604#if defined(MBEDTLS_MD4_C)
2605 case PSA_ALG_MD4:
2606 mbedtls_md4_init( &operation->ctx.md4 );
2607 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2608 break;
2609#endif
2610#if defined(MBEDTLS_MD5_C)
2611 case PSA_ALG_MD5:
2612 mbedtls_md5_init( &operation->ctx.md5 );
2613 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2614 break;
2615#endif
2616#if defined(MBEDTLS_RIPEMD160_C)
2617 case PSA_ALG_RIPEMD160:
2618 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2619 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2620 break;
2621#endif
2622#if defined(MBEDTLS_SHA1_C)
2623 case PSA_ALG_SHA_1:
2624 mbedtls_sha1_init( &operation->ctx.sha1 );
2625 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2626 break;
2627#endif
2628#if defined(MBEDTLS_SHA256_C)
2629 case PSA_ALG_SHA_224:
2630 mbedtls_sha256_init( &operation->ctx.sha256 );
2631 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2632 break;
2633 case PSA_ALG_SHA_256:
2634 mbedtls_sha256_init( &operation->ctx.sha256 );
2635 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2636 break;
2637#endif
2638#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002639#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002640 case PSA_ALG_SHA_384:
2641 mbedtls_sha512_init( &operation->ctx.sha512 );
2642 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2643 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002644#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002645 case PSA_ALG_SHA_512:
2646 mbedtls_sha512_init( &operation->ctx.sha512 );
2647 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2648 break;
2649#endif
2650 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002651 return( PSA_ALG_IS_HASH( alg ) ?
2652 PSA_ERROR_NOT_SUPPORTED :
2653 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002654 }
2655 if( ret == 0 )
2656 operation->alg = alg;
2657 else
2658 psa_hash_abort( operation );
2659 return( mbedtls_to_psa_error( ret ) );
2660}
2661
2662psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2663 const uint8_t *input,
2664 size_t input_length )
2665{
Janos Follath24eed8d2019-11-22 13:21:35 +00002666 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002667
2668 /* Don't require hash implementations to behave correctly on a
2669 * zero-length input, which may have an invalid pointer. */
2670 if( input_length == 0 )
2671 return( PSA_SUCCESS );
2672
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002673 switch( operation->alg )
2674 {
2675#if defined(MBEDTLS_MD2_C)
2676 case PSA_ALG_MD2:
2677 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2678 input, input_length );
2679 break;
2680#endif
2681#if defined(MBEDTLS_MD4_C)
2682 case PSA_ALG_MD4:
2683 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2684 input, input_length );
2685 break;
2686#endif
2687#if defined(MBEDTLS_MD5_C)
2688 case PSA_ALG_MD5:
2689 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2690 input, input_length );
2691 break;
2692#endif
2693#if defined(MBEDTLS_RIPEMD160_C)
2694 case PSA_ALG_RIPEMD160:
2695 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2696 input, input_length );
2697 break;
2698#endif
2699#if defined(MBEDTLS_SHA1_C)
2700 case PSA_ALG_SHA_1:
2701 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2702 input, input_length );
2703 break;
2704#endif
2705#if defined(MBEDTLS_SHA256_C)
2706 case PSA_ALG_SHA_224:
2707 case PSA_ALG_SHA_256:
2708 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2709 input, input_length );
2710 break;
2711#endif
2712#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002713#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002714 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002715#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002716 case PSA_ALG_SHA_512:
2717 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2718 input, input_length );
2719 break;
2720#endif
2721 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002722 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002723 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002724
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002725 if( ret != 0 )
2726 psa_hash_abort( operation );
2727 return( mbedtls_to_psa_error( ret ) );
2728}
2729
2730psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2731 uint8_t *hash,
2732 size_t hash_size,
2733 size_t *hash_length )
2734{
itayzafrir40835d42018-08-02 13:14:17 +03002735 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002736 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002737 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002738
2739 /* Fill the output buffer with something that isn't a valid hash
2740 * (barring an attack on the hash and deliberately-crafted input),
2741 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002742 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002743 /* If hash_size is 0 then hash may be NULL and then the
2744 * call to memset would have undefined behavior. */
2745 if( hash_size != 0 )
2746 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002747
2748 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002749 {
2750 status = PSA_ERROR_BUFFER_TOO_SMALL;
2751 goto exit;
2752 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002753
2754 switch( operation->alg )
2755 {
2756#if defined(MBEDTLS_MD2_C)
2757 case PSA_ALG_MD2:
2758 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2759 break;
2760#endif
2761#if defined(MBEDTLS_MD4_C)
2762 case PSA_ALG_MD4:
2763 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2764 break;
2765#endif
2766#if defined(MBEDTLS_MD5_C)
2767 case PSA_ALG_MD5:
2768 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2769 break;
2770#endif
2771#if defined(MBEDTLS_RIPEMD160_C)
2772 case PSA_ALG_RIPEMD160:
2773 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2774 break;
2775#endif
2776#if defined(MBEDTLS_SHA1_C)
2777 case PSA_ALG_SHA_1:
2778 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2779 break;
2780#endif
2781#if defined(MBEDTLS_SHA256_C)
2782 case PSA_ALG_SHA_224:
2783 case PSA_ALG_SHA_256:
2784 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2785 break;
2786#endif
2787#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002788#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002789 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002790#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002791 case PSA_ALG_SHA_512:
2792 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2793 break;
2794#endif
2795 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002796 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002797 }
itayzafrir40835d42018-08-02 13:14:17 +03002798 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002799
itayzafrir40835d42018-08-02 13:14:17 +03002800exit:
2801 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002802 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002803 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002804 return( psa_hash_abort( operation ) );
2805 }
2806 else
2807 {
2808 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002809 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002810 }
2811}
2812
Gilles Peskine2d277862018-06-18 15:41:12 +02002813psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2814 const uint8_t *hash,
2815 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002816{
2817 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2818 size_t actual_hash_length;
2819 psa_status_t status = psa_hash_finish( operation,
2820 actual_hash, sizeof( actual_hash ),
2821 &actual_hash_length );
2822 if( status != PSA_SUCCESS )
2823 return( status );
2824 if( actual_hash_length != hash_length )
2825 return( PSA_ERROR_INVALID_SIGNATURE );
2826 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2827 return( PSA_ERROR_INVALID_SIGNATURE );
2828 return( PSA_SUCCESS );
2829}
2830
Gilles Peskine0a749c82019-11-28 19:33:58 +01002831psa_status_t psa_hash_compute( psa_algorithm_t alg,
2832 const uint8_t *input, size_t input_length,
2833 uint8_t *hash, size_t hash_size,
2834 size_t *hash_length )
2835{
2836 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2837 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2838
2839 *hash_length = hash_size;
2840 status = psa_hash_setup( &operation, alg );
2841 if( status != PSA_SUCCESS )
2842 goto exit;
2843 status = psa_hash_update( &operation, input, input_length );
2844 if( status != PSA_SUCCESS )
2845 goto exit;
2846 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2847 if( status != PSA_SUCCESS )
2848 goto exit;
2849
2850exit:
2851 if( status == PSA_SUCCESS )
2852 status = psa_hash_abort( &operation );
2853 else
2854 psa_hash_abort( &operation );
2855 return( status );
2856}
2857
2858psa_status_t psa_hash_compare( psa_algorithm_t alg,
2859 const uint8_t *input, size_t input_length,
2860 const uint8_t *hash, size_t hash_length )
2861{
2862 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2863 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2864
2865 status = psa_hash_setup( &operation, alg );
2866 if( status != PSA_SUCCESS )
2867 goto exit;
2868 status = psa_hash_update( &operation, input, input_length );
2869 if( status != PSA_SUCCESS )
2870 goto exit;
2871 status = psa_hash_verify( &operation, hash, hash_length );
2872 if( status != PSA_SUCCESS )
2873 goto exit;
2874
2875exit:
2876 if( status == PSA_SUCCESS )
2877 status = psa_hash_abort( &operation );
2878 else
2879 psa_hash_abort( &operation );
2880 return( status );
2881}
2882
Gilles Peskineeb35d782019-01-22 17:56:16 +01002883psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2884 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002885{
2886 if( target_operation->alg != 0 )
2887 return( PSA_ERROR_BAD_STATE );
2888
2889 switch( source_operation->alg )
2890 {
2891 case 0:
2892 return( PSA_ERROR_BAD_STATE );
2893#if defined(MBEDTLS_MD2_C)
2894 case PSA_ALG_MD2:
2895 mbedtls_md2_clone( &target_operation->ctx.md2,
2896 &source_operation->ctx.md2 );
2897 break;
2898#endif
2899#if defined(MBEDTLS_MD4_C)
2900 case PSA_ALG_MD4:
2901 mbedtls_md4_clone( &target_operation->ctx.md4,
2902 &source_operation->ctx.md4 );
2903 break;
2904#endif
2905#if defined(MBEDTLS_MD5_C)
2906 case PSA_ALG_MD5:
2907 mbedtls_md5_clone( &target_operation->ctx.md5,
2908 &source_operation->ctx.md5 );
2909 break;
2910#endif
2911#if defined(MBEDTLS_RIPEMD160_C)
2912 case PSA_ALG_RIPEMD160:
2913 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2914 &source_operation->ctx.ripemd160 );
2915 break;
2916#endif
2917#if defined(MBEDTLS_SHA1_C)
2918 case PSA_ALG_SHA_1:
2919 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2920 &source_operation->ctx.sha1 );
2921 break;
2922#endif
2923#if defined(MBEDTLS_SHA256_C)
2924 case PSA_ALG_SHA_224:
2925 case PSA_ALG_SHA_256:
2926 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2927 &source_operation->ctx.sha256 );
2928 break;
2929#endif
2930#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002931#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002932 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002933#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002934 case PSA_ALG_SHA_512:
2935 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2936 &source_operation->ctx.sha512 );
2937 break;
2938#endif
2939 default:
2940 return( PSA_ERROR_NOT_SUPPORTED );
2941 }
2942
2943 target_operation->alg = source_operation->alg;
2944 return( PSA_SUCCESS );
2945}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002946
2947
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002948/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949/* MAC */
2950/****************************************************************/
2951
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002952static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953 psa_algorithm_t alg,
2954 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002955 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002956 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002957{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002959 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002961 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002962 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002963
Gilles Peskine8c9def32018-02-08 10:02:12 +01002964 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2965 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002966 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002967 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002968 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002969 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002970 mode = MBEDTLS_MODE_STREAM;
2971 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002972 case PSA_ALG_CTR:
2973 mode = MBEDTLS_MODE_CTR;
2974 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002975 case PSA_ALG_CFB:
2976 mode = MBEDTLS_MODE_CFB;
2977 break;
2978 case PSA_ALG_OFB:
2979 mode = MBEDTLS_MODE_OFB;
2980 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002981 case PSA_ALG_ECB_NO_PADDING:
2982 mode = MBEDTLS_MODE_ECB;
2983 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002984 case PSA_ALG_CBC_NO_PADDING:
2985 mode = MBEDTLS_MODE_CBC;
2986 break;
2987 case PSA_ALG_CBC_PKCS7:
2988 mode = MBEDTLS_MODE_CBC;
2989 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002990 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002991 mode = MBEDTLS_MODE_CCM;
2992 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002993 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002994 mode = MBEDTLS_MODE_GCM;
2995 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002996 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2997 mode = MBEDTLS_MODE_CHACHAPOLY;
2998 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002999 default:
3000 return( NULL );
3001 }
3002 }
3003 else if( alg == PSA_ALG_CMAC )
3004 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005 else
3006 return( NULL );
3007
3008 switch( key_type )
3009 {
3010 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003011 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012 break;
3013 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003014 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3015 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003016 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003017 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003018 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003019 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003020 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3021 * but two-key Triple-DES is functionally three-key Triple-DES
3022 * with K1=K3, so that's how we present it to mbedtls. */
3023 if( key_bits == 128 )
3024 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003025 break;
3026 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003027 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003028 break;
3029 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003030 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003032 case PSA_KEY_TYPE_CHACHA20:
3033 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3034 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003035 default:
3036 return( NULL );
3037 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003038 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003039 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003040
Jaeden Amero23bbb752018-06-26 14:16:54 +01003041 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3042 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003043}
3044
John Durkop6ba40d12020-11-10 08:50:04 -08003045#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003046static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003047{
Gilles Peskine2d277862018-06-18 15:41:12 +02003048 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003049 {
3050 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003051 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003053 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003054 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003055 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003056 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003057 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003058 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003059 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003060 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003061 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003062 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003063 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003064 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003065 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003066 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003067 return( 128 );
3068 default:
3069 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003070 }
3071}
John Durkop07cc04a2020-11-16 22:08:34 -08003072#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003073
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003074/* Initialize the MAC operation structure. Once this function has been
3075 * called, psa_mac_abort can run and will do the right thing. */
3076static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3077 psa_algorithm_t alg )
3078{
3079 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3080
3081 operation->alg = alg;
3082 operation->key_set = 0;
3083 operation->iv_set = 0;
3084 operation->iv_required = 0;
3085 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003086 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003087
3088#if defined(MBEDTLS_CMAC_C)
3089 if( alg == PSA_ALG_CMAC )
3090 {
3091 operation->iv_required = 0;
3092 mbedtls_cipher_init( &operation->ctx.cmac );
3093 status = PSA_SUCCESS;
3094 }
3095 else
3096#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003097#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003098 if( PSA_ALG_IS_HMAC( operation->alg ) )
3099 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003100 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3101 operation->ctx.hmac.hash_ctx.alg = 0;
3102 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003103 }
3104 else
John Durkopd0321952020-10-29 21:37:36 -07003105#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003106 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003107 if( ! PSA_ALG_IS_MAC( alg ) )
3108 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003109 }
3110
3111 if( status != PSA_SUCCESS )
3112 memset( operation, 0, sizeof( *operation ) );
3113 return( status );
3114}
3115
John Durkop6ba40d12020-11-10 08:50:04 -08003116#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003117static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3118{
Gilles Peskine3f108122018-12-07 18:14:53 +01003119 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003120 return( psa_hash_abort( &hmac->hash_ctx ) );
3121}
John Durkop6ba40d12020-11-10 08:50:04 -08003122#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003123
Gilles Peskine8c9def32018-02-08 10:02:12 +01003124psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3125{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003126 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003128 /* The object has (apparently) been initialized but it is not
3129 * in use. It's ok to call abort on such an object, and there's
3130 * nothing to do. */
3131 return( PSA_SUCCESS );
3132 }
3133 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003134#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003135 if( operation->alg == PSA_ALG_CMAC )
3136 {
3137 mbedtls_cipher_free( &operation->ctx.cmac );
3138 }
3139 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003140#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003141#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003142 if( PSA_ALG_IS_HMAC( operation->alg ) )
3143 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003144 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003145 }
3146 else
John Durkopd0321952020-10-29 21:37:36 -07003147#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003148 {
3149 /* Sanity check (shouldn't happen: operation->alg should
3150 * always have been initialized to a valid value). */
3151 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003152 }
Moran Peker41deec42018-04-04 15:43:05 +03003153
Gilles Peskine8c9def32018-02-08 10:02:12 +01003154 operation->alg = 0;
3155 operation->key_set = 0;
3156 operation->iv_set = 0;
3157 operation->iv_required = 0;
3158 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003159 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003160
Gilles Peskine8c9def32018-02-08 10:02:12 +01003161 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003162
3163bad_state:
3164 /* If abort is called on an uninitialized object, we can't trust
3165 * anything. Wipe the object in case it contains confidential data.
3166 * This may result in a memory leak if a pointer gets overwritten,
3167 * but it's too late to do anything about this. */
3168 memset( operation, 0, sizeof( *operation ) );
3169 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003170}
3171
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003172#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003173static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003174 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003175 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003176 const mbedtls_cipher_info_t *cipher_info )
3177{
Janos Follath24eed8d2019-11-22 13:21:35 +00003178 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003179
3180 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003181
3182 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3183 if( ret != 0 )
3184 return( ret );
3185
3186 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003187 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003188 key_bits );
3189 return( ret );
3190}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003191#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003192
John Durkop6ba40d12020-11-10 08:50:04 -08003193#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003194static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3195 const uint8_t *key,
3196 size_t key_length,
3197 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003198{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003199 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003200 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003201 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003202 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003203 psa_status_t status;
3204
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003205 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3206 * overflow below. This should never trigger if the hash algorithm
3207 * is implemented correctly. */
3208 /* The size checks against the ipad and opad buffers cannot be written
3209 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3210 * because that triggers -Wlogical-op on GCC 7.3. */
3211 if( block_size > sizeof( ipad ) )
3212 return( PSA_ERROR_NOT_SUPPORTED );
3213 if( block_size > sizeof( hmac->opad ) )
3214 return( PSA_ERROR_NOT_SUPPORTED );
3215 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003216 return( PSA_ERROR_NOT_SUPPORTED );
3217
Gilles Peskined223b522018-06-11 18:12:58 +02003218 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003219 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003220 status = psa_hash_compute( hash_alg, key, key_length,
3221 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003222 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003223 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003224 }
Gilles Peskine96889972018-07-12 17:07:03 +02003225 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3226 * but it is permitted. It is common when HMAC is used in HKDF, for
3227 * example. Don't call `memcpy` in the 0-length because `key` could be
3228 * an invalid pointer which would make the behavior undefined. */
3229 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003230 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003231
Gilles Peskined223b522018-06-11 18:12:58 +02003232 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3233 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003234 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003235 ipad[i] ^= 0x36;
3236 memset( ipad + key_length, 0x36, block_size - key_length );
3237
3238 /* Copy the key material from ipad to opad, flipping the requisite bits,
3239 * and filling the rest of opad with the requisite constant. */
3240 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003241 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3242 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243
Gilles Peskine01126fa2018-07-12 17:04:55 +02003244 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003245 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003246 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003247
Gilles Peskine01126fa2018-07-12 17:04:55 +02003248 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003249
3250cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003251 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003252
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003253 return( status );
3254}
John Durkop6ba40d12020-11-10 08:50:04 -08003255#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003256
Gilles Peskine89167cb2018-07-08 20:12:23 +02003257static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003258 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003259 psa_algorithm_t alg,
3260 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003261{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003262 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003263 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003264 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003265 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003266 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003267 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003268 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003269 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003270
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003271 /* A context must be freshly initialized before it can be set up. */
3272 if( operation->alg != 0 )
3273 {
3274 return( PSA_ERROR_BAD_STATE );
3275 }
3276
Gilles Peskined911eb72018-08-14 15:18:45 +02003277 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003278 if( status != PSA_SUCCESS )
3279 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003280 if( is_sign )
3281 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003282
Ronald Cron5c522922020-11-14 16:35:34 +01003283 status = psa_get_and_lock_transparent_key_slot_with_policy(
3284 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003285 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003286 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003287 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003288
Gilles Peskine8c9def32018-02-08 10:02:12 +01003289#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003290 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003291 {
3292 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003293 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003294 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003296 if( cipher_info == NULL )
3297 {
3298 status = PSA_ERROR_NOT_SUPPORTED;
3299 goto exit;
3300 }
3301 operation->mac_size = cipher_info->block_size;
3302 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3303 status = mbedtls_to_psa_error( ret );
3304 }
3305 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003306#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003307#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003308 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003309 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003310 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003311 if( hash_alg == 0 )
3312 {
3313 status = PSA_ERROR_NOT_SUPPORTED;
3314 goto exit;
3315 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003316
3317 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3318 /* Sanity check. This shouldn't fail on a valid configuration. */
3319 if( operation->mac_size == 0 ||
3320 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3321 {
3322 status = PSA_ERROR_NOT_SUPPORTED;
3323 goto exit;
3324 }
3325
Gilles Peskine8e338702019-07-30 20:06:31 +02003326 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003327 {
3328 status = PSA_ERROR_INVALID_ARGUMENT;
3329 goto exit;
3330 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003331
Gilles Peskine01126fa2018-07-12 17:04:55 +02003332 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003333 slot->data.key.data,
3334 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003335 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003336 }
3337 else
John Durkopd0321952020-10-29 21:37:36 -07003338#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003339 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003340 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003341 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003342 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003343
Gilles Peskined911eb72018-08-14 15:18:45 +02003344 if( truncated == 0 )
3345 {
3346 /* The "normal" case: untruncated algorithm. Nothing to do. */
3347 }
3348 else if( truncated < 4 )
3349 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003350 /* A very short MAC is too short for security since it can be
3351 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3352 * so we make this our minimum, even though 32 bits is still
3353 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003354 status = PSA_ERROR_NOT_SUPPORTED;
3355 }
3356 else if( truncated > operation->mac_size )
3357 {
3358 /* It's impossible to "truncate" to a larger length. */
3359 status = PSA_ERROR_INVALID_ARGUMENT;
3360 }
3361 else
3362 operation->mac_size = truncated;
3363
Gilles Peskinefbfac682018-07-08 20:51:54 +02003364exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003365 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003366 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003367 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003368 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003369 else
3370 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003371 operation->key_set = 1;
3372 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003373
Ronald Cron5c522922020-11-14 16:35:34 +01003374 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003375
Ronald Cron5c522922020-11-14 16:35:34 +01003376 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003377}
3378
Gilles Peskine89167cb2018-07-08 20:12:23 +02003379psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003380 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003381 psa_algorithm_t alg )
3382{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003383 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003384}
3385
3386psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003387 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003388 psa_algorithm_t alg )
3389{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003390 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003391}
3392
Gilles Peskine8c9def32018-02-08 10:02:12 +01003393psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3394 const uint8_t *input,
3395 size_t input_length )
3396{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003397 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003398 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003399 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003400 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003401 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003402 operation->has_input = 1;
3403
Gilles Peskine8c9def32018-02-08 10:02:12 +01003404#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003405 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003406 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003407 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3408 input, input_length );
3409 status = mbedtls_to_psa_error( ret );
3410 }
3411 else
3412#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003413#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003414 if( PSA_ALG_IS_HMAC( operation->alg ) )
3415 {
3416 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3417 input_length );
3418 }
3419 else
John Durkopd0321952020-10-29 21:37:36 -07003420#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003421 {
3422 /* This shouldn't happen if `operation` was initialized by
3423 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003424 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003425 }
3426
Gilles Peskinefbfac682018-07-08 20:51:54 +02003427 if( status != PSA_SUCCESS )
3428 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003429 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003430}
3431
John Durkop6ba40d12020-11-10 08:50:04 -08003432#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003433static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3434 uint8_t *mac,
3435 size_t mac_size )
3436{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003437 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003438 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3439 size_t hash_size = 0;
3440 size_t block_size = psa_get_hash_block_size( hash_alg );
3441 psa_status_t status;
3442
Gilles Peskine01126fa2018-07-12 17:04:55 +02003443 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3444 if( status != PSA_SUCCESS )
3445 return( status );
3446 /* From here on, tmp needs to be wiped. */
3447
3448 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3449 if( status != PSA_SUCCESS )
3450 goto exit;
3451
3452 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3453 if( status != PSA_SUCCESS )
3454 goto exit;
3455
3456 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3457 if( status != PSA_SUCCESS )
3458 goto exit;
3459
Gilles Peskined911eb72018-08-14 15:18:45 +02003460 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3461 if( status != PSA_SUCCESS )
3462 goto exit;
3463
3464 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003465
3466exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003467 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003468 return( status );
3469}
John Durkop6ba40d12020-11-10 08:50:04 -08003470#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003471
mohammad16036df908f2018-04-02 08:34:15 -07003472static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003473 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003474 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003475{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003476 if( ! operation->key_set )
3477 return( PSA_ERROR_BAD_STATE );
3478 if( operation->iv_required && ! operation->iv_set )
3479 return( PSA_ERROR_BAD_STATE );
3480
Gilles Peskine8c9def32018-02-08 10:02:12 +01003481 if( mac_size < operation->mac_size )
3482 return( PSA_ERROR_BUFFER_TOO_SMALL );
3483
Gilles Peskine8c9def32018-02-08 10:02:12 +01003484#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003485 if( operation->alg == PSA_ALG_CMAC )
3486 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003487 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3488 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3489 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003490 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003491 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003492 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003493 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003494 else
3495#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003496#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003497 if( PSA_ALG_IS_HMAC( operation->alg ) )
3498 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003499 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003500 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003501 }
3502 else
John Durkopd0321952020-10-29 21:37:36 -07003503#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003504 {
3505 /* This shouldn't happen if `operation` was initialized by
3506 * a setup function. */
3507 return( PSA_ERROR_BAD_STATE );
3508 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003509}
3510
Gilles Peskineacd4be32018-07-08 19:56:25 +02003511psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3512 uint8_t *mac,
3513 size_t mac_size,
3514 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003515{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003516 psa_status_t status;
3517
Jaeden Amero252ef282019-02-15 14:05:35 +00003518 if( operation->alg == 0 )
3519 {
3520 return( PSA_ERROR_BAD_STATE );
3521 }
3522
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003523 /* Fill the output buffer with something that isn't a valid mac
3524 * (barring an attack on the mac and deliberately-crafted input),
3525 * in case the caller doesn't check the return status properly. */
3526 *mac_length = mac_size;
3527 /* If mac_size is 0 then mac may be NULL and then the
3528 * call to memset would have undefined behavior. */
3529 if( mac_size != 0 )
3530 memset( mac, '!', mac_size );
3531
Gilles Peskine89167cb2018-07-08 20:12:23 +02003532 if( ! operation->is_sign )
3533 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003534 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003535 }
mohammad16036df908f2018-04-02 08:34:15 -07003536
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003537 status = psa_mac_finish_internal( operation, mac, mac_size );
3538
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003539 if( status == PSA_SUCCESS )
3540 {
3541 status = psa_mac_abort( operation );
3542 if( status == PSA_SUCCESS )
3543 *mac_length = operation->mac_size;
3544 else
3545 memset( mac, '!', mac_size );
3546 }
3547 else
3548 psa_mac_abort( operation );
3549 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003550}
3551
Gilles Peskineacd4be32018-07-08 19:56:25 +02003552psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3553 const uint8_t *mac,
3554 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003555{
Gilles Peskine828ed142018-06-18 23:25:51 +02003556 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003557 psa_status_t status;
3558
Jaeden Amero252ef282019-02-15 14:05:35 +00003559 if( operation->alg == 0 )
3560 {
3561 return( PSA_ERROR_BAD_STATE );
3562 }
3563
Gilles Peskine89167cb2018-07-08 20:12:23 +02003564 if( operation->is_sign )
3565 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003566 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003567 }
3568 if( operation->mac_size != mac_length )
3569 {
3570 status = PSA_ERROR_INVALID_SIGNATURE;
3571 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003572 }
mohammad16036df908f2018-04-02 08:34:15 -07003573
3574 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003575 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003576 if( status != PSA_SUCCESS )
3577 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003578
3579 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3580 status = PSA_ERROR_INVALID_SIGNATURE;
3581
3582cleanup:
3583 if( status == PSA_SUCCESS )
3584 status = psa_mac_abort( operation );
3585 else
3586 psa_mac_abort( operation );
3587
Gilles Peskine3f108122018-12-07 18:14:53 +01003588 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003589
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003590 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003591}
3592
3593
Gilles Peskine20035e32018-02-03 22:44:14 +01003594
Gilles Peskine20035e32018-02-03 22:44:14 +01003595/****************************************************************/
3596/* Asymmetric cryptography */
3597/****************************************************************/
3598
John Durkop6ba40d12020-11-10 08:50:04 -08003599#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3600 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003601/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003602 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003603static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3604 size_t hash_length,
3605 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003606{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003607 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003608 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003609 *md_alg = mbedtls_md_get_type( md_info );
3610
3611 /* The Mbed TLS RSA module uses an unsigned int for hash length
3612 * parameters. Validate that it fits so that we don't risk an
3613 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003614#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003615 if( hash_length > UINT_MAX )
3616 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003617#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003618
John Durkop0e005192020-10-31 22:06:54 -07003619#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003620 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3621 * must be correct. */
3622 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3623 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003624 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003625 if( md_info == NULL )
3626 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003627 if( mbedtls_md_get_size( md_info ) != hash_length )
3628 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003629 }
John Durkop0e005192020-10-31 22:06:54 -07003630#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003631
John Durkop0e005192020-10-31 22:06:54 -07003632#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003633 /* PSS requires a hash internally. */
3634 if( PSA_ALG_IS_RSA_PSS( alg ) )
3635 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003636 if( md_info == NULL )
3637 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003638 }
John Durkop0e005192020-10-31 22:06:54 -07003639#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003640
Gilles Peskine61b91d42018-06-08 16:09:36 +02003641 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003642}
3643
Gilles Peskine2b450e32018-06-27 15:42:46 +02003644static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3645 psa_algorithm_t alg,
3646 const uint8_t *hash,
3647 size_t hash_length,
3648 uint8_t *signature,
3649 size_t signature_size,
3650 size_t *signature_length )
3651{
3652 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003653 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003654 mbedtls_md_type_t md_alg;
3655
3656 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3657 if( status != PSA_SUCCESS )
3658 return( status );
3659
Gilles Peskine630a18a2018-06-29 17:49:35 +02003660 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003661 return( PSA_ERROR_BUFFER_TOO_SMALL );
3662
John Durkop0e005192020-10-31 22:06:54 -07003663#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003664 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3665 {
3666 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3667 MBEDTLS_MD_NONE );
3668 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003669 mbedtls_psa_get_random,
3670 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003671 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003672 md_alg,
3673 (unsigned int) hash_length,
3674 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003675 signature );
3676 }
3677 else
John Durkop0e005192020-10-31 22:06:54 -07003678#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3679#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003680 if( PSA_ALG_IS_RSA_PSS( alg ) )
3681 {
3682 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3683 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003684 mbedtls_psa_get_random,
3685 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003686 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003687 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003688 (unsigned int) hash_length,
3689 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003690 signature );
3691 }
3692 else
John Durkop0e005192020-10-31 22:06:54 -07003693#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003694 {
3695 return( PSA_ERROR_INVALID_ARGUMENT );
3696 }
3697
3698 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003699 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003700 return( mbedtls_to_psa_error( ret ) );
3701}
3702
3703static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3704 psa_algorithm_t alg,
3705 const uint8_t *hash,
3706 size_t hash_length,
3707 const uint8_t *signature,
3708 size_t signature_length )
3709{
3710 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003712 mbedtls_md_type_t md_alg;
3713
3714 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3715 if( status != PSA_SUCCESS )
3716 return( status );
3717
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003718 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3719 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003720
John Durkop0e005192020-10-31 22:06:54 -07003721#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003722 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3723 {
3724 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3725 MBEDTLS_MD_NONE );
3726 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003727 mbedtls_psa_get_random,
3728 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003729 MBEDTLS_RSA_PUBLIC,
3730 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003731 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003732 hash,
3733 signature );
3734 }
3735 else
John Durkop0e005192020-10-31 22:06:54 -07003736#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3737#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003738 if( PSA_ALG_IS_RSA_PSS( alg ) )
3739 {
3740 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3741 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003742 mbedtls_psa_get_random,
3743 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003744 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003745 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003746 (unsigned int) hash_length,
3747 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003748 signature );
3749 }
3750 else
John Durkop0e005192020-10-31 22:06:54 -07003751#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003752 {
3753 return( PSA_ERROR_INVALID_ARGUMENT );
3754 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003755
3756 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3757 * the rest of the signature is invalid". This has little use in
3758 * practice and PSA doesn't report this distinction. */
3759 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3760 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003761 return( mbedtls_to_psa_error( ret ) );
3762}
John Durkop6ba40d12020-11-10 08:50:04 -08003763#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3764 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003765
John Durkop6ba40d12020-11-10 08:50:04 -08003766#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3767 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003768/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3769 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3770 * (even though these functions don't modify it). */
3771static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3772 psa_algorithm_t alg,
3773 const uint8_t *hash,
3774 size_t hash_length,
3775 uint8_t *signature,
3776 size_t signature_size,
3777 size_t *signature_length )
3778{
Janos Follath24eed8d2019-11-22 13:21:35 +00003779 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003780 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003781 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003782 mbedtls_mpi_init( &r );
3783 mbedtls_mpi_init( &s );
3784
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003785 if( signature_size < 2 * curve_bytes )
3786 {
3787 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3788 goto cleanup;
3789 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003790
John Durkop0ea39e02020-10-13 19:58:20 -07003791#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003792 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3793 {
3794 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3795 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3796 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003797 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3798 &ecp->d, hash,
3799 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003800 mbedtls_psa_get_random,
3801 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003802 }
3803 else
John Durkop0ea39e02020-10-13 19:58:20 -07003804#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003805 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003806 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003807 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3808 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003809 mbedtls_psa_get_random,
3810 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003811 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003812
3813 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3814 signature,
3815 curve_bytes ) );
3816 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3817 signature + curve_bytes,
3818 curve_bytes ) );
3819
3820cleanup:
3821 mbedtls_mpi_free( &r );
3822 mbedtls_mpi_free( &s );
3823 if( ret == 0 )
3824 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003825 return( mbedtls_to_psa_error( ret ) );
3826}
3827
3828static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3829 const uint8_t *hash,
3830 size_t hash_length,
3831 const uint8_t *signature,
3832 size_t signature_length )
3833{
Janos Follath24eed8d2019-11-22 13:21:35 +00003834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003835 mbedtls_mpi r, s;
3836 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3837 mbedtls_mpi_init( &r );
3838 mbedtls_mpi_init( &s );
3839
3840 if( signature_length != 2 * curve_bytes )
3841 return( PSA_ERROR_INVALID_SIGNATURE );
3842
3843 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3844 signature,
3845 curve_bytes ) );
3846 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3847 signature + curve_bytes,
3848 curve_bytes ) );
3849
Steven Cooremanacda8342020-07-24 23:09:52 +02003850 /* Check whether the public part is loaded. If not, load it. */
3851 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3852 {
3853 MBEDTLS_MPI_CHK(
3854 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003855 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003856 }
3857
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003858 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3859 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003860
3861cleanup:
3862 mbedtls_mpi_free( &r );
3863 mbedtls_mpi_free( &s );
3864 return( mbedtls_to_psa_error( ret ) );
3865}
John Durkop6ba40d12020-11-10 08:50:04 -08003866#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3867 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003868
Ronald Croncf56a0a2020-08-04 09:51:30 +02003869psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003870 psa_algorithm_t alg,
3871 const uint8_t *hash,
3872 size_t hash_length,
3873 uint8_t *signature,
3874 size_t signature_size,
3875 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003876{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003877 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003878 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003879 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003880
3881 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003882 /* Immediately reject a zero-length signature buffer. This guarantees
3883 * that signature must be a valid pointer. (On the other hand, the hash
3884 * buffer can in principle be empty since it doesn't actually have
3885 * to be a hash.) */
3886 if( signature_size == 0 )
3887 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003888
Ronald Cron5c522922020-11-14 16:35:34 +01003889 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3890 PSA_KEY_USAGE_SIGN_HASH,
3891 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003892 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003893 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003894 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003895 {
3896 status = PSA_ERROR_INVALID_ARGUMENT;
3897 goto exit;
3898 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003899
Steven Cooremancd84cb42020-07-16 20:28:36 +02003900 /* Try any of the available accelerators first */
3901 status = psa_driver_wrapper_sign_hash( slot,
3902 alg,
3903 hash,
3904 hash_length,
3905 signature,
3906 signature_size,
3907 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003908 if( status != PSA_ERROR_NOT_SUPPORTED ||
3909 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003910 goto exit;
3911
Steven Cooreman7a250572020-07-17 16:43:05 +02003912 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003913#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3914 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003915 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003916 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003917 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003918
Steven Cooreman4fed4552020-08-03 14:46:03 +02003919 status = psa_load_rsa_representation( slot->attr.type,
3920 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003921 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003922 &rsa );
3923 if( status != PSA_SUCCESS )
3924 goto exit;
3925
Steven Cooremana2371e52020-07-28 14:30:39 +02003926 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003927 alg,
3928 hash, hash_length,
3929 signature, signature_size,
3930 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003931
Steven Cooremana2371e52020-07-28 14:30:39 +02003932 mbedtls_rsa_free( rsa );
3933 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003934 }
3935 else
John Durkop6ba40d12020-11-10 08:50:04 -08003936#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3937 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003938 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003939 {
John Durkop9814fa22020-11-04 12:28:15 -08003940#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3941 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003942 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003943#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003944 PSA_ALG_IS_ECDSA( alg )
3945#else
3946 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3947#endif
3948 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003949 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003950 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003951 status = psa_load_ecp_representation( slot->attr.type,
3952 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003953 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003954 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003955 if( status != PSA_SUCCESS )
3956 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003957 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003958 alg,
3959 hash, hash_length,
3960 signature, signature_size,
3961 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003962 mbedtls_ecp_keypair_free( ecp );
3963 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003964 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003965 else
John Durkop9814fa22020-11-04 12:28:15 -08003966#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3967 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003968 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003969 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003970 }
itayzafrir5c753392018-05-08 11:18:38 +03003971 }
3972 else
itayzafrir5c753392018-05-08 11:18:38 +03003973 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003974 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003975 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003976
3977exit:
3978 /* Fill the unused part of the output buffer (the whole buffer on error,
3979 * the trailing part on success) with something that isn't a valid mac
3980 * (barring an attack on the mac and deliberately-crafted input),
3981 * in case the caller doesn't check the return status properly. */
3982 if( status == PSA_SUCCESS )
3983 memset( signature + *signature_length, '!',
3984 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003985 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003986 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003987 /* If signature_size is 0 then we have nothing to do. We must not call
3988 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003989
Ronald Cron5c522922020-11-14 16:35:34 +01003990 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003991
Ronald Cron5c522922020-11-14 16:35:34 +01003992 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003993}
3994
Ronald Croncf56a0a2020-08-04 09:51:30 +02003995psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003996 psa_algorithm_t alg,
3997 const uint8_t *hash,
3998 size_t hash_length,
3999 const uint8_t *signature,
4000 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03004001{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004002 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004003 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004004 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004005
Ronald Cron5c522922020-11-14 16:35:34 +01004006 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4007 PSA_KEY_USAGE_VERIFY_HASH,
4008 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004009 if( status != PSA_SUCCESS )
4010 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004011
Steven Cooreman55ae2172020-07-17 19:46:15 +02004012 /* Try any of the available accelerators first */
4013 status = psa_driver_wrapper_verify_hash( slot,
4014 alg,
4015 hash,
4016 hash_length,
4017 signature,
4018 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004019 if( status != PSA_ERROR_NOT_SUPPORTED ||
4020 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004021 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004022
John Durkop6ba40d12020-11-10 08:50:04 -08004023#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4024 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004025 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004026 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004027 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004028
Steven Cooreman4fed4552020-08-03 14:46:03 +02004029 status = psa_load_rsa_representation( slot->attr.type,
4030 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004031 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004032 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004033 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004034 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004035
Steven Cooremana2371e52020-07-28 14:30:39 +02004036 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004037 alg,
4038 hash, hash_length,
4039 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004040 mbedtls_rsa_free( rsa );
4041 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004042 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004043 }
4044 else
John Durkop6ba40d12020-11-10 08:50:04 -08004045#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4046 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004047 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004048 {
John Durkop9814fa22020-11-04 12:28:15 -08004049#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4050 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004051 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004052 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004053 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004054 status = psa_load_ecp_representation( slot->attr.type,
4055 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004056 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004057 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004058 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004059 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004060 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004061 hash, hash_length,
4062 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004063 mbedtls_ecp_keypair_free( ecp );
4064 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004065 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004066 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004067 else
John Durkop9814fa22020-11-04 12:28:15 -08004068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4069 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004070 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004071 status = PSA_ERROR_INVALID_ARGUMENT;
4072 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004073 }
itayzafrir5c753392018-05-08 11:18:38 +03004074 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004075 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004076 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004077 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004078 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004079
4080exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004081 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004082
Ronald Cron5c522922020-11-14 16:35:34 +01004083 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004084}
4085
John Durkop0e005192020-10-31 22:06:54 -07004086#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004087static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4088 mbedtls_rsa_context *rsa )
4089{
4090 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4091 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4092 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4093 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4094}
John Durkop0e005192020-10-31 22:06:54 -07004095#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004096
Ronald Croncf56a0a2020-08-04 09:51:30 +02004097psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004098 psa_algorithm_t alg,
4099 const uint8_t *input,
4100 size_t input_length,
4101 const uint8_t *salt,
4102 size_t salt_length,
4103 uint8_t *output,
4104 size_t output_size,
4105 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004106{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004107 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004108 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004109 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004110
Darryl Green5cc689a2018-07-24 15:34:10 +01004111 (void) input;
4112 (void) input_length;
4113 (void) salt;
4114 (void) output;
4115 (void) output_size;
4116
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004117 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004118
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004119 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4120 return( PSA_ERROR_INVALID_ARGUMENT );
4121
Ronald Cron5c522922020-11-14 16:35:34 +01004122 status = psa_get_and_lock_transparent_key_slot_with_policy(
4123 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004124 if( status != PSA_SUCCESS )
4125 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004126 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4127 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004128 {
4129 status = PSA_ERROR_INVALID_ARGUMENT;
4130 goto exit;
4131 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132
John Durkop6ba40d12020-11-10 08:50:04 -08004133#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4134 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004135 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004136 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004137 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004138 status = psa_load_rsa_representation( slot->attr.type,
4139 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004140 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004141 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004142 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004143 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004144
4145 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004146 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004147 status = PSA_ERROR_BUFFER_TOO_SMALL;
4148 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004149 }
John Durkop0e005192020-10-31 22:06:54 -07004150#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004151 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004152 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153 status = mbedtls_to_psa_error(
4154 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004155 mbedtls_psa_get_random,
4156 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004157 MBEDTLS_RSA_PUBLIC,
4158 input_length,
4159 input,
4160 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004161 }
4162 else
John Durkop0e005192020-10-31 22:06:54 -07004163#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4164#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004165 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004166 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004167 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004168 status = mbedtls_to_psa_error(
4169 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004170 mbedtls_psa_get_random,
4171 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004172 MBEDTLS_RSA_PUBLIC,
4173 salt, salt_length,
4174 input_length,
4175 input,
4176 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004177 }
4178 else
John Durkop0e005192020-10-31 22:06:54 -07004179#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004180 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004181 status = PSA_ERROR_INVALID_ARGUMENT;
4182 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004183 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004184rsa_exit:
4185 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004186 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004187
Steven Cooremana2371e52020-07-28 14:30:39 +02004188 mbedtls_rsa_free( rsa );
4189 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004190 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004191 else
John Durkop9814fa22020-11-04 12:28:15 -08004192#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004193 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004194 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004195 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004196 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004197
4198exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004199 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004200
Ronald Cron5c522922020-11-14 16:35:34 +01004201 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004202}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004203
Ronald Croncf56a0a2020-08-04 09:51:30 +02004204psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004205 psa_algorithm_t alg,
4206 const uint8_t *input,
4207 size_t input_length,
4208 const uint8_t *salt,
4209 size_t salt_length,
4210 uint8_t *output,
4211 size_t output_size,
4212 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004213{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004214 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004215 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004216 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004217
Darryl Green5cc689a2018-07-24 15:34:10 +01004218 (void) input;
4219 (void) input_length;
4220 (void) salt;
4221 (void) output;
4222 (void) output_size;
4223
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004224 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004225
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004226 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4227 return( PSA_ERROR_INVALID_ARGUMENT );
4228
Ronald Cron5c522922020-11-14 16:35:34 +01004229 status = psa_get_and_lock_transparent_key_slot_with_policy(
4230 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004231 if( status != PSA_SUCCESS )
4232 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004233 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004234 {
4235 status = PSA_ERROR_INVALID_ARGUMENT;
4236 goto exit;
4237 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004238
John Durkop6ba40d12020-11-10 08:50:04 -08004239#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4240 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004241 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004242 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004243 mbedtls_rsa_context *rsa = NULL;
4244 status = psa_load_rsa_representation( slot->attr.type,
4245 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004246 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004247 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004248 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004249 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004250
Steven Cooremana2371e52020-07-28 14:30:39 +02004251 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004252 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004253 status = PSA_ERROR_INVALID_ARGUMENT;
4254 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004255 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004256
John Durkop0e005192020-10-31 22:06:54 -07004257#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004258 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004259 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004260 status = mbedtls_to_psa_error(
4261 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004262 mbedtls_psa_get_random,
4263 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004264 MBEDTLS_RSA_PRIVATE,
4265 output_length,
4266 input,
4267 output,
4268 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004269 }
4270 else
John Durkop0e005192020-10-31 22:06:54 -07004271#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4272#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004273 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004274 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004275 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004276 status = mbedtls_to_psa_error(
4277 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004278 mbedtls_psa_get_random,
4279 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004280 MBEDTLS_RSA_PRIVATE,
4281 salt, salt_length,
4282 output_length,
4283 input,
4284 output,
4285 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004286 }
4287 else
John Durkop0e005192020-10-31 22:06:54 -07004288#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004289 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004290 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004291 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004292
Steven Cooreman4fed4552020-08-03 14:46:03 +02004293rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004294 mbedtls_rsa_free( rsa );
4295 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004296 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004297 else
John Durkop6ba40d12020-11-10 08:50:04 -08004298#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4299 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004300 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004301 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004302 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004303
4304exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004305 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004306
Ronald Cron5c522922020-11-14 16:35:34 +01004307 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004308}
Gilles Peskine20035e32018-02-03 22:44:14 +01004309
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004310
4311
mohammad1603503973b2018-03-12 15:59:30 +02004312/****************************************************************/
4313/* Symmetric cryptography */
4314/****************************************************************/
4315
Gilles Peskinee553c652018-06-04 16:22:46 +02004316static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004317 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004318 psa_algorithm_t alg,
4319 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004320{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004321 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004322 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004323 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004324 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004325 size_t key_bits;
4326 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004327 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4328 PSA_KEY_USAGE_ENCRYPT :
4329 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004330
Steven Cooremand3feccd2020-09-01 15:56:14 +02004331 /* A context must be freshly initialized before it can be set up. */
4332 if( operation->alg != 0 )
4333 return( PSA_ERROR_BAD_STATE );
4334
Steven Cooremana07b9972020-09-10 14:54:14 +02004335 /* The requested algorithm must be one that can be processed by cipher. */
4336 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004337 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004338
Steven Cooremanef8575e2020-09-11 11:44:50 +02004339 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004340 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004341 if( status != PSA_SUCCESS )
4342 goto exit;
4343
4344 /* Initialize the operation struct members, except for alg. The alg member
4345 * is used to indicate to psa_cipher_abort that there are resources to free,
4346 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004347 operation->key_set = 0;
4348 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004349 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004350 operation->iv_size = 0;
4351 operation->block_size = 0;
4352 if( alg == PSA_ALG_ECB_NO_PADDING )
4353 operation->iv_required = 0;
4354 else
4355 operation->iv_required = 1;
4356
Steven Cooremana07b9972020-09-10 14:54:14 +02004357 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004358 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004359 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004360 slot,
4361 alg );
4362 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004363 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004364 slot,
4365 alg );
4366
Steven Cooremanef8575e2020-09-11 11:44:50 +02004367 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004368 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004369 /* Once the driver context is initialised, it needs to be freed using
4370 * psa_cipher_abort. Indicate this through setting alg. */
4371 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004372 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004373
Steven Cooremand3feccd2020-09-01 15:56:14 +02004374 if( status != PSA_ERROR_NOT_SUPPORTED ||
4375 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4376 goto exit;
4377
Steven Cooremana07b9972020-09-10 14:54:14 +02004378 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004379 * available for the given algorithm & key. */
4380 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004381
Steven Cooremana07b9972020-09-10 14:54:14 +02004382 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004383 * psa_cipher_abort. Indicate there is something to be freed through setting
4384 * alg, and indicate the operation is being done using mbedtls crypto through
4385 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004386 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004387 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004388
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004389 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004390 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004391 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004392 {
4393 status = PSA_ERROR_NOT_SUPPORTED;
4394 goto exit;
4395 }
mohammad1603503973b2018-03-12 15:59:30 +02004396
mohammad1603503973b2018-03-12 15:59:30 +02004397 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004398 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004399 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004400
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004401#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004402 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004403 {
4404 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004405 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004406 memcpy( keys, slot->data.key.data, 16 );
4407 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004408 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4409 keys,
4410 192, cipher_operation );
4411 }
4412 else
4413#endif
4414 {
4415 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004416 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004417 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004418 }
Moran Peker41deec42018-04-04 15:43:05 +03004419 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004420 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004421
mohammad16038481e742018-03-18 13:57:31 +02004422#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004423 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004424 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004425 case PSA_ALG_CBC_NO_PADDING:
4426 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4427 MBEDTLS_PADDING_NONE );
4428 break;
4429 case PSA_ALG_CBC_PKCS7:
4430 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4431 MBEDTLS_PADDING_PKCS7 );
4432 break;
4433 default:
4434 /* The algorithm doesn't involve padding. */
4435 ret = 0;
4436 break;
4437 }
4438 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004439 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004440#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4441
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004442 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004443 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004444 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4445 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004446 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004447 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004448 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004449#if defined(MBEDTLS_CHACHA20_C)
4450 else
4451 if( alg == PSA_ALG_CHACHA20 )
4452 operation->iv_size = 12;
4453#endif
mohammad1603503973b2018-03-12 15:59:30 +02004454
Steven Cooreman7df02922020-09-09 15:28:49 +02004455 status = PSA_SUCCESS;
4456
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004457exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004458 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004459 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004460 if( status == PSA_SUCCESS )
4461 {
4462 /* Update operation flags for both driver and software implementations */
4463 operation->key_set = 1;
4464 }
4465 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004466 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004467
Ronald Cron5c522922020-11-14 16:35:34 +01004468 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004469
Ronald Cron5c522922020-11-14 16:35:34 +01004470 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004471}
4472
Gilles Peskinefe119512018-07-08 21:39:34 +02004473psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004474 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004475 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004476{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004477 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004478}
4479
Gilles Peskinefe119512018-07-08 21:39:34 +02004480psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004481 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004482 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004483{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004484 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004485}
4486
Gilles Peskinefe119512018-07-08 21:39:34 +02004487psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004488 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004489 size_t iv_size,
4490 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004491{
itayzafrir534bd7c2018-08-02 13:56:32 +03004492 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004493 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004494 if( operation->iv_set || ! operation->iv_required )
4495 {
4496 return( PSA_ERROR_BAD_STATE );
4497 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004498
Steven Cooremanef8575e2020-09-11 11:44:50 +02004499 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004500 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004501 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004502 iv,
4503 iv_size,
4504 iv_length );
4505 goto exit;
4506 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004507
Moran Peker41deec42018-04-04 15:43:05 +03004508 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004509 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004510 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004511 goto exit;
4512 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01004513 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
4514 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004515 if( ret != 0 )
4516 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004517 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004518 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004519 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004520
mohammad16038481e742018-03-18 13:57:31 +02004521 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004522 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004523
Moran Peker395db872018-05-31 14:07:14 +03004524exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004525 if( status == PSA_SUCCESS )
4526 operation->iv_set = 1;
4527 else
Moran Peker395db872018-05-31 14:07:14 +03004528 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004529 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004530}
4531
Gilles Peskinefe119512018-07-08 21:39:34 +02004532psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004533 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004534 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004535{
itayzafrir534bd7c2018-08-02 13:56:32 +03004536 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004537 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004538 if( operation->iv_set || ! operation->iv_required )
4539 {
4540 return( PSA_ERROR_BAD_STATE );
4541 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004542
Steven Cooremanef8575e2020-09-11 11:44:50 +02004543 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004544 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004545 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004546 iv,
4547 iv_length );
4548 goto exit;
4549 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004550
Moran Pekera28258c2018-05-29 16:25:04 +03004551 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004552 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004553 status = PSA_ERROR_INVALID_ARGUMENT;
4554 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004555 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004556 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4557 status = mbedtls_to_psa_error( ret );
4558exit:
4559 if( status == PSA_SUCCESS )
4560 operation->iv_set = 1;
4561 else
mohammad1603503973b2018-03-12 15:59:30 +02004562 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004563 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004564}
4565
Steven Cooreman177deba2020-09-07 17:14:14 +02004566/* Process input for which the algorithm is set to ECB mode. This requires
4567 * manual processing, since the PSA API is defined as being able to process
4568 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4569 * underlying mbedtls_cipher_update only takes full blocks. */
4570static psa_status_t psa_cipher_update_ecb_internal(
4571 mbedtls_cipher_context_t *ctx,
4572 const uint8_t *input,
4573 size_t input_length,
4574 uint8_t *output,
4575 size_t output_size,
4576 size_t *output_length )
4577{
4578 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4579 size_t block_size = ctx->cipher_info->block_size;
4580 size_t internal_output_length = 0;
4581 *output_length = 0;
4582
4583 if( input_length == 0 )
4584 {
4585 status = PSA_SUCCESS;
4586 goto exit;
4587 }
4588
4589 if( ctx->unprocessed_len > 0 )
4590 {
4591 /* Fill up to block size, and run the block if there's a full one. */
4592 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4593
4594 if( input_length < bytes_to_copy )
4595 bytes_to_copy = input_length;
4596
4597 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4598 input, bytes_to_copy );
4599 input_length -= bytes_to_copy;
4600 input += bytes_to_copy;
4601 ctx->unprocessed_len += bytes_to_copy;
4602
4603 if( ctx->unprocessed_len == block_size )
4604 {
4605 status = mbedtls_to_psa_error(
4606 mbedtls_cipher_update( ctx,
4607 ctx->unprocessed_data,
4608 block_size,
4609 output, &internal_output_length ) );
4610
4611 if( status != PSA_SUCCESS )
4612 goto exit;
4613
4614 output += internal_output_length;
4615 output_size -= internal_output_length;
4616 *output_length += internal_output_length;
4617 ctx->unprocessed_len = 0;
4618 }
4619 }
4620
4621 while( input_length >= block_size )
4622 {
4623 /* Run all full blocks we have, one by one */
4624 status = mbedtls_to_psa_error(
4625 mbedtls_cipher_update( ctx, input,
4626 block_size,
4627 output, &internal_output_length ) );
4628
4629 if( status != PSA_SUCCESS )
4630 goto exit;
4631
4632 input_length -= block_size;
4633 input += block_size;
4634
4635 output += internal_output_length;
4636 output_size -= internal_output_length;
4637 *output_length += internal_output_length;
4638 }
4639
4640 if( input_length > 0 )
4641 {
4642 /* Save unprocessed bytes for later processing */
4643 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4644 input, input_length );
4645 ctx->unprocessed_len += input_length;
4646 }
4647
4648 status = PSA_SUCCESS;
4649
4650exit:
4651 return( status );
4652}
4653
Gilles Peskinee553c652018-06-04 16:22:46 +02004654psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4655 const uint8_t *input,
4656 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004657 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004658 size_t output_size,
4659 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004660{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004661 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004662 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004663 if( operation->alg == 0 )
4664 {
4665 return( PSA_ERROR_BAD_STATE );
4666 }
4667 if( operation->iv_required && ! operation->iv_set )
4668 {
4669 return( PSA_ERROR_BAD_STATE );
4670 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004671
Steven Cooremanef8575e2020-09-11 11:44:50 +02004672 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004673 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004674 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004675 input,
4676 input_length,
4677 output,
4678 output_size,
4679 output_length );
4680 goto exit;
4681 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004682
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004683 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004684 {
4685 /* Take the unprocessed partial block left over from previous
4686 * update calls, if any, plus the input to this call. Remove
4687 * the last partial block, if any. You get the data that will be
4688 * output in this call. */
4689 expected_output_size =
4690 ( operation->ctx.cipher.unprocessed_len + input_length )
4691 / operation->block_size * operation->block_size;
4692 }
4693 else
4694 {
4695 expected_output_size = input_length;
4696 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004697
Gilles Peskine89d789c2018-06-04 17:17:16 +02004698 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004699 {
4700 status = PSA_ERROR_BUFFER_TOO_SMALL;
4701 goto exit;
4702 }
mohammad160382759612018-03-12 18:16:40 +02004703
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004704 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4705 {
4706 /* mbedtls_cipher_update has an API inconsistency: it will only
4707 * process a single block at a time in ECB mode. Abstract away that
4708 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004709 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4710 input,
4711 input_length,
4712 output,
4713 output_size,
4714 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004715 }
4716 else
4717 {
4718 status = mbedtls_to_psa_error(
4719 mbedtls_cipher_update( &operation->ctx.cipher, input,
4720 input_length, output, output_length ) );
4721 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004722exit:
4723 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004724 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004725 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004726}
4727
Gilles Peskinee553c652018-06-04 16:22:46 +02004728psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4729 uint8_t *output,
4730 size_t output_size,
4731 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004732{
David Saadab4ecc272019-02-14 13:48:10 +02004733 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004734 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004735 if( operation->alg == 0 )
4736 {
4737 return( PSA_ERROR_BAD_STATE );
4738 }
4739 if( operation->iv_required && ! operation->iv_set )
4740 {
4741 return( PSA_ERROR_BAD_STATE );
4742 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004743
Steven Cooremanef8575e2020-09-11 11:44:50 +02004744 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004745 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004746 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004747 output,
4748 output_size,
4749 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004750 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004751 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004752
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004753 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004754 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004755 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004756 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004757 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004758 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004759 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004760 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004761 }
4762
Steven Cooremanef8575e2020-09-11 11:44:50 +02004763 status = mbedtls_to_psa_error(
4764 mbedtls_cipher_finish( &operation->ctx.cipher,
4765 temp_output_buffer,
4766 output_length ) );
4767 if( status != PSA_SUCCESS )
4768 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004769
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004770 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004771 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004772 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004773 memcpy( output, temp_output_buffer, *output_length );
4774 else
Janos Follath315b51c2018-07-09 16:04:51 +01004775 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004776
Steven Cooremanef8575e2020-09-11 11:44:50 +02004777exit:
4778 if( operation->mbedtls_in_use == 1 )
4779 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004780
Steven Cooremanef8575e2020-09-11 11:44:50 +02004781 if( status == PSA_SUCCESS )
4782 return( psa_cipher_abort( operation ) );
4783 else
4784 {
4785 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004786 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004787
Steven Cooremanef8575e2020-09-11 11:44:50 +02004788 return( status );
4789 }
mohammad1603503973b2018-03-12 15:59:30 +02004790}
4791
Gilles Peskinee553c652018-06-04 16:22:46 +02004792psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4793{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004794 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004795 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004796 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004797 * in use. It's ok to call abort on such an object, and there's
4798 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004799 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004800 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004801
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004802 /* Sanity check (shouldn't happen: operation->alg should
4803 * always have been initialized to a valid value). */
4804 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4805 return( PSA_ERROR_BAD_STATE );
4806
Steven Cooremanef8575e2020-09-11 11:44:50 +02004807 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004808 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004809 else
4810 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004811
Moran Peker41deec42018-04-04 15:43:05 +03004812 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004813 operation->key_set = 0;
4814 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004815 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004816 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004817 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004818 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004819
Moran Peker395db872018-05-31 14:07:14 +03004820 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004821}
4822
Gilles Peskinea0655c32018-04-30 17:06:50 +02004823
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004824
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004825
mohammad16035955c982018-04-26 00:53:03 +03004826/****************************************************************/
4827/* AEAD */
4828/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004829
Gilles Peskineedf9a652018-08-17 18:11:56 +02004830typedef struct
4831{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004832 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833 const mbedtls_cipher_info_t *cipher_info;
4834 union
4835 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004836 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004837#if defined(MBEDTLS_CCM_C)
4838 mbedtls_ccm_context ccm;
4839#endif /* MBEDTLS_CCM_C */
4840#if defined(MBEDTLS_GCM_C)
4841 mbedtls_gcm_context gcm;
4842#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004843#if defined(MBEDTLS_CHACHAPOLY_C)
4844 mbedtls_chachapoly_context chachapoly;
4845#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004846 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004847 psa_algorithm_t core_alg;
4848 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004849 uint8_t tag_length;
4850} aead_operation_t;
4851
Ronald Cronf95a2b12020-10-22 15:24:49 +02004852#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4853
Gilles Peskine30a9e412019-01-14 18:36:12 +01004854static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004855{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004856 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004857 {
4858#if defined(MBEDTLS_CCM_C)
4859 case PSA_ALG_CCM:
4860 mbedtls_ccm_free( &operation->ctx.ccm );
4861 break;
4862#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004863#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004864 case PSA_ALG_GCM:
4865 mbedtls_gcm_free( &operation->ctx.gcm );
4866 break;
4867#endif /* MBEDTLS_GCM_C */
4868 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004869
Ronald Cron5c522922020-11-14 16:35:34 +01004870 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004871}
4872
4873static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004874 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004875 psa_key_usage_t usage,
4876 psa_algorithm_t alg )
4877{
4878 psa_status_t status;
4879 size_t key_bits;
4880 mbedtls_cipher_id_t cipher_id;
4881
Ronald Cron5c522922020-11-14 16:35:34 +01004882 status = psa_get_and_lock_transparent_key_slot_with_policy(
4883 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884 if( status != PSA_SUCCESS )
4885 return( status );
4886
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004887 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004888
4889 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004890 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004891 &cipher_id );
4892 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004893 {
4894 status = PSA_ERROR_NOT_SUPPORTED;
4895 goto cleanup;
4896 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004897
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004898 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004899 {
4900#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004901 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4902 operation->core_alg = PSA_ALG_CCM;
4903 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004904 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4905 * The call to mbedtls_ccm_encrypt_and_tag or
4906 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004907 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004908 {
4909 status = PSA_ERROR_INVALID_ARGUMENT;
4910 goto cleanup;
4911 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004912 mbedtls_ccm_init( &operation->ctx.ccm );
4913 status = mbedtls_to_psa_error(
4914 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004915 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004916 (unsigned int) key_bits ) );
4917 if( status != 0 )
4918 goto cleanup;
4919 break;
4920#endif /* MBEDTLS_CCM_C */
4921
4922#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004923 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4924 operation->core_alg = PSA_ALG_GCM;
4925 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004926 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4927 * The call to mbedtls_gcm_crypt_and_tag or
4928 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004929 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004930 {
4931 status = PSA_ERROR_INVALID_ARGUMENT;
4932 goto cleanup;
4933 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004934 mbedtls_gcm_init( &operation->ctx.gcm );
4935 status = mbedtls_to_psa_error(
4936 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004937 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004938 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004939 if( status != 0 )
4940 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004941 break;
4942#endif /* MBEDTLS_GCM_C */
4943
Gilles Peskine26869f22019-05-06 15:25:00 +02004944#if defined(MBEDTLS_CHACHAPOLY_C)
4945 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4946 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4947 operation->full_tag_length = 16;
4948 /* We only support the default tag length. */
4949 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004950 {
4951 status = PSA_ERROR_NOT_SUPPORTED;
4952 goto cleanup;
4953 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004954 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4955 status = mbedtls_to_psa_error(
4956 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004957 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004958 if( status != 0 )
4959 goto cleanup;
4960 break;
4961#endif /* MBEDTLS_CHACHAPOLY_C */
4962
Gilles Peskineedf9a652018-08-17 18:11:56 +02004963 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004964 status = PSA_ERROR_NOT_SUPPORTED;
4965 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004966 }
4967
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004968 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4969 {
4970 status = PSA_ERROR_INVALID_ARGUMENT;
4971 goto cleanup;
4972 }
4973 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004974
Gilles Peskineedf9a652018-08-17 18:11:56 +02004975 return( PSA_SUCCESS );
4976
4977cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004978 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004979 return( status );
4980}
4981
Ronald Croncf56a0a2020-08-04 09:51:30 +02004982psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004983 psa_algorithm_t alg,
4984 const uint8_t *nonce,
4985 size_t nonce_length,
4986 const uint8_t *additional_data,
4987 size_t additional_data_length,
4988 const uint8_t *plaintext,
4989 size_t plaintext_length,
4990 uint8_t *ciphertext,
4991 size_t ciphertext_size,
4992 size_t *ciphertext_length )
4993{
mohammad16035955c982018-04-26 00:53:03 +03004994 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004995 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004996 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004997
mohammad1603f08a5502018-06-03 15:05:47 +03004998 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004999
Ronald Croncf56a0a2020-08-04 09:51:30 +02005000 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005001 if( status != PSA_SUCCESS )
5002 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005003
Gilles Peskineedf9a652018-08-17 18:11:56 +02005004 /* For all currently supported modes, the tag is at the end of the
5005 * ciphertext. */
5006 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5007 {
5008 status = PSA_ERROR_BUFFER_TOO_SMALL;
5009 goto exit;
5010 }
5011 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005012
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005013#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005014 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005015 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005016 status = mbedtls_to_psa_error(
5017 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5018 MBEDTLS_GCM_ENCRYPT,
5019 plaintext_length,
5020 nonce, nonce_length,
5021 additional_data, additional_data_length,
5022 plaintext, ciphertext,
5023 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005024 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005025 else
5026#endif /* MBEDTLS_GCM_C */
5027#if defined(MBEDTLS_CCM_C)
5028 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005029 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005030 status = mbedtls_to_psa_error(
5031 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5032 plaintext_length,
5033 nonce, nonce_length,
5034 additional_data,
5035 additional_data_length,
5036 plaintext, ciphertext,
5037 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005038 }
mohammad16035c8845f2018-05-09 05:40:09 -07005039 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005040#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005041#if defined(MBEDTLS_CHACHAPOLY_C)
5042 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5043 {
5044 if( nonce_length != 12 || operation.tag_length != 16 )
5045 {
5046 status = PSA_ERROR_NOT_SUPPORTED;
5047 goto exit;
5048 }
5049 status = mbedtls_to_psa_error(
5050 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5051 plaintext_length,
5052 nonce,
5053 additional_data,
5054 additional_data_length,
5055 plaintext,
5056 ciphertext,
5057 tag ) );
5058 }
5059 else
5060#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005061 {
mohammad1603554faad2018-06-03 15:07:38 +03005062 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005063 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005064
Gilles Peskineedf9a652018-08-17 18:11:56 +02005065 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5066 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005067
Gilles Peskineedf9a652018-08-17 18:11:56 +02005068exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005069 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005070 if( status == PSA_SUCCESS )
5071 *ciphertext_length = plaintext_length + operation.tag_length;
5072 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005073}
5074
Gilles Peskineee652a32018-06-01 19:23:52 +02005075/* Locate the tag in a ciphertext buffer containing the encrypted data
5076 * followed by the tag. Return the length of the part preceding the tag in
5077 * *plaintext_length. This is the size of the plaintext in modes where
5078 * the encrypted data has the same size as the plaintext, such as
5079 * CCM and GCM. */
5080static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5081 const uint8_t *ciphertext,
5082 size_t ciphertext_length,
5083 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005084 const uint8_t **p_tag )
5085{
5086 size_t payload_length;
5087 if( tag_length > ciphertext_length )
5088 return( PSA_ERROR_INVALID_ARGUMENT );
5089 payload_length = ciphertext_length - tag_length;
5090 if( payload_length > plaintext_size )
5091 return( PSA_ERROR_BUFFER_TOO_SMALL );
5092 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005093 return( PSA_SUCCESS );
5094}
5095
Ronald Croncf56a0a2020-08-04 09:51:30 +02005096psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005097 psa_algorithm_t alg,
5098 const uint8_t *nonce,
5099 size_t nonce_length,
5100 const uint8_t *additional_data,
5101 size_t additional_data_length,
5102 const uint8_t *ciphertext,
5103 size_t ciphertext_length,
5104 uint8_t *plaintext,
5105 size_t plaintext_size,
5106 size_t *plaintext_length )
5107{
mohammad16035955c982018-04-26 00:53:03 +03005108 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005109 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005110 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005111
Gilles Peskineee652a32018-06-01 19:23:52 +02005112 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005113
Ronald Croncf56a0a2020-08-04 09:51:30 +02005114 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005115 if( status != PSA_SUCCESS )
5116 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005117
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005118 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5119 ciphertext, ciphertext_length,
5120 plaintext_size, &tag );
5121 if( status != PSA_SUCCESS )
5122 goto exit;
5123
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005124#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005125 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005126 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005127 status = mbedtls_to_psa_error(
5128 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5129 ciphertext_length - operation.tag_length,
5130 nonce, nonce_length,
5131 additional_data,
5132 additional_data_length,
5133 tag, operation.tag_length,
5134 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005135 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005136 else
5137#endif /* MBEDTLS_GCM_C */
5138#if defined(MBEDTLS_CCM_C)
5139 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005140 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005141 status = mbedtls_to_psa_error(
5142 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5143 ciphertext_length - operation.tag_length,
5144 nonce, nonce_length,
5145 additional_data,
5146 additional_data_length,
5147 ciphertext, plaintext,
5148 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005149 }
mohammad160339574652018-06-01 04:39:53 -07005150 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005151#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005152#if defined(MBEDTLS_CHACHAPOLY_C)
5153 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5154 {
5155 if( nonce_length != 12 || operation.tag_length != 16 )
5156 {
5157 status = PSA_ERROR_NOT_SUPPORTED;
5158 goto exit;
5159 }
5160 status = mbedtls_to_psa_error(
5161 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5162 ciphertext_length - operation.tag_length,
5163 nonce,
5164 additional_data,
5165 additional_data_length,
5166 tag,
5167 ciphertext,
5168 plaintext ) );
5169 }
5170 else
5171#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005172 {
mohammad1603554faad2018-06-03 15:07:38 +03005173 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005174 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005175
Gilles Peskineedf9a652018-08-17 18:11:56 +02005176 if( status != PSA_SUCCESS && plaintext_size != 0 )
5177 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005178
Gilles Peskineedf9a652018-08-17 18:11:56 +02005179exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005180 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005181 if( status == PSA_SUCCESS )
5182 *plaintext_length = ciphertext_length - operation.tag_length;
5183 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005184}
5185
Gilles Peskinea0655c32018-04-30 17:06:50 +02005186
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005187
Gilles Peskine20035e32018-02-03 22:44:14 +01005188/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005189/* Generators */
5190/****************************************************************/
5191
John Durkop07cc04a2020-11-16 22:08:34 -08005192#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5193 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5194 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5195#define AT_LEAST_ONE_BUILTIN_KDF
5196#endif
5197
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005198#define HKDF_STATE_INIT 0 /* no input yet */
5199#define HKDF_STATE_STARTED 1 /* got salt */
5200#define HKDF_STATE_KEYED 2 /* got key */
5201#define HKDF_STATE_OUTPUT 3 /* output started */
5202
Gilles Peskinecbe66502019-05-16 16:59:18 +02005203static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005205{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005206 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5207 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005208 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005210}
5211
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005212psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213{
5214 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005215 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005216 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005217 {
5218 /* The object has (apparently) been initialized but it is not
5219 * in use. It's ok to call abort on such an object, and there's
5220 * nothing to do. */
5221 }
5222 else
John Durkopd0321952020-10-29 21:37:36 -07005223#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005224 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005225 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005226 mbedtls_free( operation->ctx.hkdf.info );
5227 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005228 }
John Durkop07cc04a2020-11-16 22:08:34 -08005229 else
5230#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5231#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5232 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5233 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005234 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005235 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005236 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005237 if( operation->ctx.tls12_prf.seed != NULL )
5238 {
5239 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5240 operation->ctx.tls12_prf.seed_length );
5241 mbedtls_free( operation->ctx.tls12_prf.seed );
5242 }
5243
5244 if( operation->ctx.tls12_prf.label != NULL )
5245 {
5246 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5247 operation->ctx.tls12_prf.label_length );
5248 mbedtls_free( operation->ctx.tls12_prf.label );
5249 }
5250
5251 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5252
5253 /* We leave the fields Ai and output_block to be erased safely by the
5254 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005255 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005256 else
John Durkop07cc04a2020-11-16 22:08:34 -08005257#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5258 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 {
5260 status = PSA_ERROR_BAD_STATE;
5261 }
Janos Follath083036a2019-06-11 10:22:26 +01005262 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005263 return( status );
5264}
5265
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005266psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005267 size_t *capacity)
5268{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005269 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005270 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005272 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005273 }
5274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005276 return( PSA_SUCCESS );
5277}
5278
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005279psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005280 size_t capacity )
5281{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005283 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005284 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005285 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005286 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005287 return( PSA_SUCCESS );
5288}
5289
John Durkopd0321952020-10-29 21:37:36 -07005290#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005291/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005292 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005293static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005294 psa_algorithm_t hash_alg,
5295 uint8_t *output,
5296 size_t output_length )
5297{
5298 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5299 psa_status_t status;
5300
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005301 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5302 return( PSA_ERROR_BAD_STATE );
5303 hkdf->state = HKDF_STATE_OUTPUT;
5304
Gilles Peskinebef7f142018-07-12 17:22:21 +02005305 while( output_length != 0 )
5306 {
5307 /* Copy what remains of the current block */
5308 uint8_t n = hash_length - hkdf->offset_in_block;
5309 if( n > output_length )
5310 n = (uint8_t) output_length;
5311 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5312 output += n;
5313 output_length -= n;
5314 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005315 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005316 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005317 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005318 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005319 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005320 * object was corrupted or if this function is called directly
5321 * inside the library. */
5322 if( hkdf->block_number == 0xff )
5323 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005324
5325 /* We need a new block */
5326 ++hkdf->block_number;
5327 hkdf->offset_in_block = 0;
5328 status = psa_hmac_setup_internal( &hkdf->hmac,
5329 hkdf->prk, hash_length,
5330 hash_alg );
5331 if( status != PSA_SUCCESS )
5332 return( status );
5333 if( hkdf->block_number != 1 )
5334 {
5335 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5336 hkdf->output_block,
5337 hash_length );
5338 if( status != PSA_SUCCESS )
5339 return( status );
5340 }
5341 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5342 hkdf->info,
5343 hkdf->info_length );
5344 if( status != PSA_SUCCESS )
5345 return( status );
5346 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5347 &hkdf->block_number, 1 );
5348 if( status != PSA_SUCCESS )
5349 return( status );
5350 status = psa_hmac_finish_internal( &hkdf->hmac,
5351 hkdf->output_block,
5352 sizeof( hkdf->output_block ) );
5353 if( status != PSA_SUCCESS )
5354 return( status );
5355 }
5356
5357 return( PSA_SUCCESS );
5358}
John Durkop07cc04a2020-11-16 22:08:34 -08005359#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005360
John Durkop07cc04a2020-11-16 22:08:34 -08005361#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5362 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005363static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5364 psa_tls12_prf_key_derivation_t *tls12_prf,
5365 psa_algorithm_t alg )
5366{
5367 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5368 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005369 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5370 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005371
Janos Follath7742fee2019-06-17 12:58:10 +01005372 /* We can't be wanting more output after block 0xff, otherwise
5373 * the capacity check in psa_key_derivation_output_bytes() would have
5374 * prevented this call. It could happen only if the operation
5375 * object was corrupted or if this function is called directly
5376 * inside the library. */
5377 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005378 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005379
5380 /* We need a new block */
5381 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005382 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005383
5384 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5385 *
5386 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5387 *
5388 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5389 * HMAC_hash(secret, A(2) + seed) +
5390 * HMAC_hash(secret, A(3) + seed) + ...
5391 *
5392 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005393 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005394 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005395 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005396 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005397 * is currently extracted as `output_block` and where i is
5398 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005399 */
5400
Janos Follathea29bfb2019-06-19 12:21:20 +01005401 /* Save the hash context before using it, to preserve the hash state with
5402 * only the inner padding in it. We need this, because inner padding depends
5403 * on the key (secret in the RFC's terminology). */
5404 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5405 if( status != PSA_SUCCESS )
5406 goto cleanup;
5407
5408 /* Calculate A(i) where i = tls12_prf->block_number. */
5409 if( tls12_prf->block_number == 1 )
5410 {
5411 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5412 * the variable seed and in this instance means it in the context of the
5413 * P_hash function, where seed = label + seed.) */
5414 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5415 tls12_prf->label, tls12_prf->label_length );
5416 if( status != PSA_SUCCESS )
5417 goto cleanup;
5418 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5419 tls12_prf->seed, tls12_prf->seed_length );
5420 if( status != PSA_SUCCESS )
5421 goto cleanup;
5422 }
5423 else
5424 {
5425 /* A(i) = HMAC_hash(secret, A(i-1)) */
5426 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5427 tls12_prf->Ai, hash_length );
5428 if( status != PSA_SUCCESS )
5429 goto cleanup;
5430 }
5431
5432 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5433 tls12_prf->Ai, hash_length );
5434 if( status != PSA_SUCCESS )
5435 goto cleanup;
5436 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5437 if( status != PSA_SUCCESS )
5438 goto cleanup;
5439
5440 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5441 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5442 tls12_prf->Ai, hash_length );
5443 if( status != PSA_SUCCESS )
5444 goto cleanup;
5445 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5446 tls12_prf->label, tls12_prf->label_length );
5447 if( status != PSA_SUCCESS )
5448 goto cleanup;
5449 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5450 tls12_prf->seed, tls12_prf->seed_length );
5451 if( status != PSA_SUCCESS )
5452 goto cleanup;
5453 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5454 tls12_prf->output_block, hash_length );
5455 if( status != PSA_SUCCESS )
5456 goto cleanup;
5457 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5458 if( status != PSA_SUCCESS )
5459 goto cleanup;
5460
Janos Follath7742fee2019-06-17 12:58:10 +01005461
5462cleanup:
5463
Janos Follathea29bfb2019-06-19 12:21:20 +01005464 cleanup_status = psa_hash_abort( &backup );
5465 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5466 status = cleanup_status;
5467
5468 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005469}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005470
Janos Follath844eb0e2019-06-19 12:10:49 +01005471static psa_status_t psa_key_derivation_tls12_prf_read(
5472 psa_tls12_prf_key_derivation_t *tls12_prf,
5473 psa_algorithm_t alg,
5474 uint8_t *output,
5475 size_t output_length )
5476{
5477 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5478 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5479 psa_status_t status;
5480 uint8_t offset, length;
5481
5482 while( output_length != 0 )
5483 {
5484 /* Check if we have fully processed the current block. */
5485 if( tls12_prf->left_in_block == 0 )
5486 {
5487 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5488 alg );
5489 if( status != PSA_SUCCESS )
5490 return( status );
5491
5492 continue;
5493 }
5494
5495 if( tls12_prf->left_in_block > output_length )
5496 length = (uint8_t) output_length;
5497 else
5498 length = tls12_prf->left_in_block;
5499
5500 offset = hash_length - tls12_prf->left_in_block;
5501 memcpy( output, tls12_prf->output_block + offset, length );
5502 output += length;
5503 output_length -= length;
5504 tls12_prf->left_in_block -= length;
5505 }
5506
5507 return( PSA_SUCCESS );
5508}
John Durkop07cc04a2020-11-16 22:08:34 -08005509#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5510 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005511
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005512psa_status_t psa_key_derivation_output_bytes(
5513 psa_key_derivation_operation_t *operation,
5514 uint8_t *output,
5515 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005516{
5517 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005519
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005520 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005521 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005523 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005524 }
5525
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005527 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005528 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005529 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005531 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005532 goto exit;
5533 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005534 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005535 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005536 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005537 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005538 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5539 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005541 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005542 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005543 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005544 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005545
John Durkopd0321952020-10-29 21:37:36 -07005546#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005547 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005548 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005549 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005550 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005551 output, output_length );
5552 }
Janos Follathadbec812019-06-14 11:05:39 +01005553 else
John Durkop07cc04a2020-11-16 22:08:34 -08005554#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5555#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5556 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005557 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005558 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005559 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005560 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005561 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005562 output_length );
5563 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005564 else
John Durkop07cc04a2020-11-16 22:08:34 -08005565#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5566 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005567 {
5568 return( PSA_ERROR_BAD_STATE );
5569 }
5570
5571exit:
5572 if( status != PSA_SUCCESS )
5573 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005574 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005575 * This allows us to differentiate between exhausted operations and
5576 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5577 * operations. */
5578 psa_algorithm_t alg = operation->alg;
5579 psa_key_derivation_abort( operation );
5580 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005581 memset( output, '!', output_length );
5582 }
5583 return( status );
5584}
5585
Gilles Peskine08542d82018-07-19 17:05:42 +02005586#if defined(MBEDTLS_DES_C)
5587static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5588{
5589 if( data_size >= 8 )
5590 mbedtls_des_key_set_parity( data );
5591 if( data_size >= 16 )
5592 mbedtls_des_key_set_parity( data + 8 );
5593 if( data_size >= 24 )
5594 mbedtls_des_key_set_parity( data + 16 );
5595}
5596#endif /* MBEDTLS_DES_C */
5597
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005598static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 psa_key_slot_t *slot,
5600 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005601 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005602{
5603 uint8_t *data = NULL;
5604 size_t bytes = PSA_BITS_TO_BYTES( bits );
5605 psa_status_t status;
5606
Gilles Peskine8e338702019-07-30 20:06:31 +02005607 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005608 return( PSA_ERROR_INVALID_ARGUMENT );
5609 if( bits % 8 != 0 )
5610 return( PSA_ERROR_INVALID_ARGUMENT );
5611 data = mbedtls_calloc( 1, bytes );
5612 if( data == NULL )
5613 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5614
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005615 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005616 if( status != PSA_SUCCESS )
5617 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005618#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005619 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005620 psa_des_set_key_parity( data, bytes );
5621#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005622 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005623
5624exit:
5625 mbedtls_free( data );
5626 return( status );
5627}
5628
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005629psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005630 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005631 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005632{
5633 psa_status_t status;
5634 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005635 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005636
Ronald Cron81709fc2020-11-14 12:10:32 +01005637 *key = MBEDTLS_SVC_KEY_ID_INIT;
5638
Gilles Peskine0f84d622019-09-12 19:03:13 +02005639 /* Reject any attempt to create a zero-length key so that we don't
5640 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5641 if( psa_get_key_bits( attributes ) == 0 )
5642 return( PSA_ERROR_INVALID_ARGUMENT );
5643
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005644 if( ! operation->can_output_key )
5645 return( PSA_ERROR_NOT_PERMITTED );
5646
Ronald Cron81709fc2020-11-14 12:10:32 +01005647 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5648 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005649#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5650 if( driver != NULL )
5651 {
5652 /* Deriving a key in a secure element is not implemented yet. */
5653 status = PSA_ERROR_NOT_SUPPORTED;
5654 }
5655#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005656 if( status == PSA_SUCCESS )
5657 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005658 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005659 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005660 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005661 }
5662 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005663 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005665 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005666
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 return( status );
5668}
5669
Gilles Peskineeab56e42018-07-12 17:12:33 +02005670
5671
5672/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005673/* Key derivation */
5674/****************************************************************/
5675
John Durkop07cc04a2020-11-16 22:08:34 -08005676#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005677static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005678 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005679 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005680{
John Durkop07cc04a2020-11-16 22:08:34 -08005681 int is_kdf_alg_supported;
5682
Janos Follath5fe19732019-06-20 15:09:30 +01005683 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5684 * initialisers for this union leave some bytes unspecified.) */
5685 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5686
Gilles Peskine969c5d62019-01-16 15:53:06 +01005687 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005688#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005689 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5690 is_kdf_alg_supported = 1;
5691 else
5692#endif
5693#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5694 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5695 is_kdf_alg_supported = 1;
5696 else
5697#endif
5698#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5699 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5700 is_kdf_alg_supported = 1;
5701 else
5702#endif
5703 is_kdf_alg_supported = 0;
5704
5705 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005706 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005707 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005708 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5709 if( hash_size == 0 )
5710 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005711 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5712 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005713 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005714 {
5715 return( PSA_ERROR_NOT_SUPPORTED );
5716 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005717 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005718 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005719 }
John Durkop07cc04a2020-11-16 22:08:34 -08005720
5721 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722}
John Durkop07cc04a2020-11-16 22:08:34 -08005723#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005724
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005725psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005726 psa_algorithm_t alg )
5727{
5728 psa_status_t status;
5729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005730 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005731 return( PSA_ERROR_BAD_STATE );
5732
Gilles Peskine6843c292019-01-18 16:44:49 +01005733 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5734 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005735#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005736 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005737 {
5738 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005740 }
5741 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5742 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005743 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005744 }
John Durkop07cc04a2020-11-16 22:08:34 -08005745#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005746 else
5747 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005748
5749 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005750 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005751 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752}
5753
John Durkopd0321952020-10-29 21:37:36 -07005754#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005755static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005756 psa_algorithm_t hash_alg,
5757 psa_key_derivation_step_t step,
5758 const uint8_t *data,
5759 size_t data_length )
5760{
5761 psa_status_t status;
5762 switch( step )
5763 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005764 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005765 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005766 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005767 status = psa_hmac_setup_internal( &hkdf->hmac,
5768 data, data_length,
5769 hash_alg );
5770 if( status != PSA_SUCCESS )
5771 return( status );
5772 hkdf->state = HKDF_STATE_STARTED;
5773 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005774 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005775 /* If no salt was provided, use an empty salt. */
5776 if( hkdf->state == HKDF_STATE_INIT )
5777 {
5778 status = psa_hmac_setup_internal( &hkdf->hmac,
5779 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005780 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005781 if( status != PSA_SUCCESS )
5782 return( status );
5783 hkdf->state = HKDF_STATE_STARTED;
5784 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005785 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005786 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005787 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5788 data, data_length );
5789 if( status != PSA_SUCCESS )
5790 return( status );
5791 status = psa_hmac_finish_internal( &hkdf->hmac,
5792 hkdf->prk,
5793 sizeof( hkdf->prk ) );
5794 if( status != PSA_SUCCESS )
5795 return( status );
5796 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5797 hkdf->block_number = 0;
5798 hkdf->state = HKDF_STATE_KEYED;
5799 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005800 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005801 if( hkdf->state == HKDF_STATE_OUTPUT )
5802 return( PSA_ERROR_BAD_STATE );
5803 if( hkdf->info_set )
5804 return( PSA_ERROR_BAD_STATE );
5805 hkdf->info_length = data_length;
5806 if( data_length != 0 )
5807 {
5808 hkdf->info = mbedtls_calloc( 1, data_length );
5809 if( hkdf->info == NULL )
5810 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5811 memcpy( hkdf->info, data, data_length );
5812 }
5813 hkdf->info_set = 1;
5814 return( PSA_SUCCESS );
5815 default:
5816 return( PSA_ERROR_INVALID_ARGUMENT );
5817 }
5818}
John Durkop07cc04a2020-11-16 22:08:34 -08005819#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005820
John Durkop07cc04a2020-11-16 22:08:34 -08005821#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5822 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005823static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5824 const uint8_t *data,
5825 size_t data_length )
5826{
5827 if( prf->state != TLS12_PRF_STATE_INIT )
5828 return( PSA_ERROR_BAD_STATE );
5829
Janos Follathd6dce9f2019-07-04 09:11:38 +01005830 if( data_length != 0 )
5831 {
5832 prf->seed = mbedtls_calloc( 1, data_length );
5833 if( prf->seed == NULL )
5834 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005835
Janos Follathd6dce9f2019-07-04 09:11:38 +01005836 memcpy( prf->seed, data, data_length );
5837 prf->seed_length = data_length;
5838 }
Janos Follathf08e2652019-06-13 09:05:41 +01005839
5840 prf->state = TLS12_PRF_STATE_SEED_SET;
5841
5842 return( PSA_SUCCESS );
5843}
5844
Janos Follath81550542019-06-13 14:26:34 +01005845static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5846 psa_algorithm_t hash_alg,
5847 const uint8_t *data,
5848 size_t data_length )
5849{
5850 psa_status_t status;
5851 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5852 return( PSA_ERROR_BAD_STATE );
5853
5854 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5855 if( status != PSA_SUCCESS )
5856 return( status );
5857
5858 prf->state = TLS12_PRF_STATE_KEY_SET;
5859
5860 return( PSA_SUCCESS );
5861}
5862
Janos Follath63028dd2019-06-13 09:15:47 +01005863static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5864 const uint8_t *data,
5865 size_t data_length )
5866{
5867 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5868 return( PSA_ERROR_BAD_STATE );
5869
Janos Follathd6dce9f2019-07-04 09:11:38 +01005870 if( data_length != 0 )
5871 {
5872 prf->label = mbedtls_calloc( 1, data_length );
5873 if( prf->label == NULL )
5874 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005875
Janos Follathd6dce9f2019-07-04 09:11:38 +01005876 memcpy( prf->label, data, data_length );
5877 prf->label_length = data_length;
5878 }
Janos Follath63028dd2019-06-13 09:15:47 +01005879
5880 prf->state = TLS12_PRF_STATE_LABEL_SET;
5881
5882 return( PSA_SUCCESS );
5883}
5884
Janos Follathb03233e2019-06-11 15:30:30 +01005885static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5886 psa_algorithm_t hash_alg,
5887 psa_key_derivation_step_t step,
5888 const uint8_t *data,
5889 size_t data_length )
5890{
Janos Follathb03233e2019-06-11 15:30:30 +01005891 switch( step )
5892 {
Janos Follathf08e2652019-06-13 09:05:41 +01005893 case PSA_KEY_DERIVATION_INPUT_SEED:
5894 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005895 case PSA_KEY_DERIVATION_INPUT_SECRET:
5896 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005897 case PSA_KEY_DERIVATION_INPUT_LABEL:
5898 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005899 default:
5900 return( PSA_ERROR_INVALID_ARGUMENT );
5901 }
5902}
John Durkop07cc04a2020-11-16 22:08:34 -08005903#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5904 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5905
5906#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5907static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5908 psa_tls12_prf_key_derivation_t *prf,
5909 psa_algorithm_t hash_alg,
5910 const uint8_t *data,
5911 size_t data_length )
5912{
5913 psa_status_t status;
5914 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5915 uint8_t *cur = pms;
5916
5917 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5918 return( PSA_ERROR_INVALID_ARGUMENT );
5919
5920 /* Quoting RFC 4279, Section 2:
5921 *
5922 * The premaster secret is formed as follows: if the PSK is N octets
5923 * long, concatenate a uint16 with the value N, N zero octets, a second
5924 * uint16 with the value N, and the PSK itself.
5925 */
5926
5927 *cur++ = ( data_length >> 8 ) & 0xff;
5928 *cur++ = ( data_length >> 0 ) & 0xff;
5929 memset( cur, 0, data_length );
5930 cur += data_length;
5931 *cur++ = pms[0];
5932 *cur++ = pms[1];
5933 memcpy( cur, data, data_length );
5934 cur += data_length;
5935
5936 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5937
5938 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5939 return( status );
5940}
Janos Follath6660f0e2019-06-17 08:44:03 +01005941
5942static psa_status_t psa_tls12_prf_psk_to_ms_input(
5943 psa_tls12_prf_key_derivation_t *prf,
5944 psa_algorithm_t hash_alg,
5945 psa_key_derivation_step_t step,
5946 const uint8_t *data,
5947 size_t data_length )
5948{
5949 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005950 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005951 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5952 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005953 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005954
5955 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5956}
John Durkop07cc04a2020-11-16 22:08:34 -08005957#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005958
Gilles Peskineb8965192019-09-24 16:21:10 +02005959/** Check whether the given key type is acceptable for the given
5960 * input step of a key derivation.
5961 *
5962 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5963 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5964 * Both secret and non-secret inputs can alternatively have the type
5965 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5966 * that the input was passed as a buffer rather than via a key object.
5967 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005968static int psa_key_derivation_check_input_type(
5969 psa_key_derivation_step_t step,
5970 psa_key_type_t key_type )
5971{
5972 switch( step )
5973 {
5974 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005975 if( key_type == PSA_KEY_TYPE_DERIVE )
5976 return( PSA_SUCCESS );
5977 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005978 return( PSA_SUCCESS );
5979 break;
5980 case PSA_KEY_DERIVATION_INPUT_LABEL:
5981 case PSA_KEY_DERIVATION_INPUT_SALT:
5982 case PSA_KEY_DERIVATION_INPUT_INFO:
5983 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005984 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5985 return( PSA_SUCCESS );
5986 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005987 return( PSA_SUCCESS );
5988 break;
5989 }
5990 return( PSA_ERROR_INVALID_ARGUMENT );
5991}
5992
Janos Follathb80a94e2019-06-12 15:54:46 +01005993static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005994 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005995 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005996 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005997 const uint8_t *data,
5998 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005999{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02006000 psa_status_t status;
6001 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
6002
6003 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006004 if( status != PSA_SUCCESS )
6005 goto exit;
6006
John Durkopd0321952020-10-29 21:37:36 -07006007#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006008 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006009 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006010 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006011 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006012 step, data, data_length );
6013 }
John Durkop07cc04a2020-11-16 22:08:34 -08006014 else
6015#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6016#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6017 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006018 {
Janos Follathb03233e2019-06-11 15:30:30 +01006019 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6020 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6021 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006022 }
John Durkop07cc04a2020-11-16 22:08:34 -08006023 else
6024#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6025#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6026 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006027 {
6028 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6029 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6030 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006031 }
6032 else
John Durkop07cc04a2020-11-16 22:08:34 -08006033#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006034 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006035 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006036 return( PSA_ERROR_BAD_STATE );
6037 }
6038
Gilles Peskine224b0d62019-09-23 18:13:17 +02006039exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006040 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006041 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006042 return( status );
6043}
6044
Janos Follath51f4a0f2019-06-14 11:35:55 +01006045psa_status_t psa_key_derivation_input_bytes(
6046 psa_key_derivation_operation_t *operation,
6047 psa_key_derivation_step_t step,
6048 const uint8_t *data,
6049 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006050{
Gilles Peskineb8965192019-09-24 16:21:10 +02006051 return( psa_key_derivation_input_internal( operation, step,
6052 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006053 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006054}
6055
Janos Follath51f4a0f2019-06-14 11:35:55 +01006056psa_status_t psa_key_derivation_input_key(
6057 psa_key_derivation_operation_t *operation,
6058 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006059 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006060{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006061 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006062 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006064
Ronald Cron5c522922020-11-14 16:35:34 +01006065 status = psa_get_and_lock_transparent_key_slot_with_policy(
6066 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006067 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006068 {
6069 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006070 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006071 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006072
6073 /* Passing a key object as a SECRET input unlocks the permission
6074 * to output to a key object. */
6075 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6076 operation->can_output_key = 1;
6077
Ronald Cronf95a2b12020-10-22 15:24:49 +02006078 status = psa_key_derivation_input_internal( operation,
6079 step, slot->attr.type,
6080 slot->data.key.data,
6081 slot->data.key.bytes );
6082
Ronald Cron5c522922020-11-14 16:35:34 +01006083 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006084
Ronald Cron5c522922020-11-14 16:35:34 +01006085 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006086}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006087
6088
6089
6090/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006091/* Key agreement */
6092/****************************************************************/
6093
John Durkop6ba40d12020-11-10 08:50:04 -08006094#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006095static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6096 size_t peer_key_length,
6097 const mbedtls_ecp_keypair *our_key,
6098 uint8_t *shared_secret,
6099 size_t shared_secret_size,
6100 size_t *shared_secret_length )
6101{
Steven Cooremand4867872020-08-05 16:31:39 +02006102 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006103 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006104 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006105 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006106 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006107 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006108
Steven Cooreman4fed4552020-08-03 14:46:03 +02006109 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6110 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006111 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006112 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006113 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006114 goto exit;
6115
Jaeden Amero97271b32019-01-10 19:38:51 +00006116 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006117 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006118 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006119 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006120 status = mbedtls_to_psa_error(
6121 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6122 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006123 goto exit;
6124
Jaeden Amero97271b32019-01-10 19:38:51 +00006125 status = mbedtls_to_psa_error(
6126 mbedtls_ecdh_calc_secret( &ecdh,
6127 shared_secret_length,
6128 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006129 mbedtls_psa_get_random,
6130 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006131 if( status != PSA_SUCCESS )
6132 goto exit;
6133 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6134 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006135
6136exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006137 if( status != PSA_SUCCESS )
6138 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006139 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006140 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006141 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006142
Jaeden Amero97271b32019-01-10 19:38:51 +00006143 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006144}
John Durkop6ba40d12020-11-10 08:50:04 -08006145#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006146
Gilles Peskine01d718c2018-09-18 12:01:02 +02006147#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6148
Gilles Peskine0216fe12019-04-11 21:23:21 +02006149static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6150 psa_key_slot_t *private_key,
6151 const uint8_t *peer_key,
6152 size_t peer_key_length,
6153 uint8_t *shared_secret,
6154 size_t shared_secret_size,
6155 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006156{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006157 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006158 {
John Durkop6ba40d12020-11-10 08:50:04 -08006159#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006160 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006161 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006162 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006163 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006164 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006165 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006166 private_key->data.key.data,
6167 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006168 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006169 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006170 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006171 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006172 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006173 shared_secret, shared_secret_size,
6174 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006175 mbedtls_ecp_keypair_free( ecp );
6176 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006177 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006178#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006179 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006180 (void) private_key;
6181 (void) peer_key;
6182 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006183 (void) shared_secret;
6184 (void) shared_secret_size;
6185 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006186 return( PSA_ERROR_NOT_SUPPORTED );
6187 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006188}
6189
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006190/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006191 * to potentially free embedded data structures and wipe confidential data.
6192 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006193static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006194 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006195 psa_key_slot_t *private_key,
6196 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006197 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006198{
6199 psa_status_t status;
6200 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6201 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006202 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006203
6204 /* Step 1: run the secret agreement algorithm to generate the shared
6205 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006206 status = psa_key_agreement_raw_internal( ka_alg,
6207 private_key,
6208 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006209 shared_secret,
6210 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006211 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006212 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006213 goto exit;
6214
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006215 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006216 * the shared secret. A shared secret is permitted wherever a key
6217 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006218 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006219 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006220 shared_secret,
6221 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006222exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006223 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006224 return( status );
6225}
6226
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006227psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006228 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006229 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006230 const uint8_t *peer_key,
6231 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006232{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006233 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006234 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006235 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006236
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006237 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006238 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006239 status = psa_get_and_lock_transparent_key_slot_with_policy(
6240 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006241 if( status != PSA_SUCCESS )
6242 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006243 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006244 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006245 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006246 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006247 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006248 else
6249 {
6250 /* If a private key has been added as SECRET, we allow the derived
6251 * key material to be used as a key in PSA Crypto. */
6252 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6253 operation->can_output_key = 1;
6254 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006255
Ronald Cron5c522922020-11-14 16:35:34 +01006256 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006257
Ronald Cron5c522922020-11-14 16:35:34 +01006258 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006259}
6260
Gilles Peskinebe697d82019-05-16 18:00:41 +02006261psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006262 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006263 const uint8_t *peer_key,
6264 size_t peer_key_length,
6265 uint8_t *output,
6266 size_t output_size,
6267 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006268{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006269 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006270 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006271 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006272
6273 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6274 {
6275 status = PSA_ERROR_INVALID_ARGUMENT;
6276 goto exit;
6277 }
Ronald Cron5c522922020-11-14 16:35:34 +01006278 status = psa_get_and_lock_transparent_key_slot_with_policy(
6279 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006280 if( status != PSA_SUCCESS )
6281 goto exit;
6282
6283 status = psa_key_agreement_raw_internal( alg, slot,
6284 peer_key, peer_key_length,
6285 output, output_size,
6286 output_length );
6287
6288exit:
6289 if( status != PSA_SUCCESS )
6290 {
6291 /* If an error happens and is not handled properly, the output
6292 * may be used as a key to protect sensitive data. Arrange for such
6293 * a key to be random, which is likely to result in decryption or
6294 * verification errors. This is better than filling the buffer with
6295 * some constant data such as zeros, which would result in the data
6296 * being protected with a reproducible, easily knowable key.
6297 */
6298 psa_generate_random( output, output_size );
6299 *output_length = output_size;
6300 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006301
Ronald Cron5c522922020-11-14 16:35:34 +01006302 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006303
Ronald Cron5c522922020-11-14 16:35:34 +01006304 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006305}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006306
6307
Gilles Peskine30524eb2020-11-13 17:02:26 +01006308
Gilles Peskine86a440b2018-11-12 18:39:40 +01006309/****************************************************************/
6310/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006311/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006312
Gilles Peskine30524eb2020-11-13 17:02:26 +01006313/** Initialize the PSA random generator.
6314 */
6315static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6316{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006317#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6318 memset( rng, 0, sizeof( *rng ) );
6319#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6320
Gilles Peskine30524eb2020-11-13 17:02:26 +01006321 /* Set default configuration if
6322 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6323 if( rng->entropy_init == NULL )
6324 rng->entropy_init = mbedtls_entropy_init;
6325 if( rng->entropy_free == NULL )
6326 rng->entropy_free = mbedtls_entropy_free;
6327
6328 rng->entropy_init( &rng->entropy );
6329#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6330 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6331 /* The PSA entropy injection feature depends on using NV seed as an entropy
6332 * source. Add NV seed as an entropy source for PSA entropy injection. */
6333 mbedtls_entropy_add_source( &rng->entropy,
6334 mbedtls_nv_seed_poll, NULL,
6335 MBEDTLS_ENTROPY_BLOCK_SIZE,
6336 MBEDTLS_ENTROPY_SOURCE_STRONG );
6337#endif
6338
6339 mbedtls_psa_drbg_init( mbedtls_psa_random_state( rng ) );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006340#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006341}
6342
6343/** Deinitialize the PSA random generator.
6344 */
6345static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6346{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006347#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6348 memset( rng, 0, sizeof( *rng ) );
6349#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006350 mbedtls_psa_drbg_free( mbedtls_psa_random_state( rng ) );
6351 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006352#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006353}
6354
6355/** Seed the PSA random generator.
6356 */
6357static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6358{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006359#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6360 /* Do nothing: the external RNG seeds itself. */
6361 (void) rng;
6362 return( PSA_SUCCESS );
6363#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006364 const unsigned char drbg_seed[] = "PSA";
6365 int ret = mbedtls_psa_drbg_seed( rng, drbg_seed, sizeof( drbg_seed ) - 1 );
6366 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006367#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006368}
6369
Gilles Peskinee59236f2018-01-27 23:32:46 +01006370psa_status_t psa_generate_random( uint8_t *output,
6371 size_t output_size )
6372{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006373 GUARD_MODULE_INITIALIZED;
6374
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006375#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6376
6377 size_t output_length = 0;
6378 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6379 output, output_size,
6380 &output_length );
6381 if( status != PSA_SUCCESS )
6382 return( status );
6383 /* Breaking up a request into smaller chunks is currently not supported
6384 * for the extrernal RNG interface. */
6385 if( output_length != output_size )
6386 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6387 return( PSA_SUCCESS );
6388
6389#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6390
6391 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6392
Gilles Peskine30524eb2020-11-13 17:02:26 +01006393 while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006394 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006395 ret = mbedtls_psa_get_random(
6396 mbedtls_psa_random_state( &global_data.rng ),
6397 output, MBEDTLS_PSA_RANDOM_MAX_REQUEST );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006398 if( ret != 0 )
6399 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006400 output += MBEDTLS_PSA_RANDOM_MAX_REQUEST;
6401 output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006402 }
6403
Gilles Peskine30524eb2020-11-13 17:02:26 +01006404 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
6405 output, output_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006406 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006407#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006408}
6409
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006410#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6411#include "mbedtls/entropy_poll.h"
6412
Gilles Peskine7228da22019-07-15 11:06:15 +02006413psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006414 size_t seed_size )
6415{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006416 if( global_data.initialized )
6417 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006418
6419 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6420 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6421 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6422 return( PSA_ERROR_INVALID_ARGUMENT );
6423
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006424 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006425}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006426#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006427
John Durkop07cc04a2020-11-16 22:08:34 -08006428#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006429static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6430 size_t domain_parameters_size,
6431 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006432{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006433 size_t i;
6434 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006435
Gilles Peskinee56e8782019-04-26 17:34:02 +02006436 if( domain_parameters_size == 0 )
6437 {
6438 *exponent = 65537;
6439 return( PSA_SUCCESS );
6440 }
6441
6442 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6443 * support values that fit in a 32-bit integer, which is larger than
6444 * int on just about every platform anyway. */
6445 if( domain_parameters_size > sizeof( acc ) )
6446 return( PSA_ERROR_NOT_SUPPORTED );
6447 for( i = 0; i < domain_parameters_size; i++ )
6448 acc = ( acc << 8 ) | domain_parameters[i];
6449 if( acc > INT_MAX )
6450 return( PSA_ERROR_NOT_SUPPORTED );
6451 *exponent = acc;
6452 return( PSA_SUCCESS );
6453}
John Durkop07cc04a2020-11-16 22:08:34 -08006454#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006455
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006456static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006457 psa_key_slot_t *slot, size_t bits,
6458 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459{
Gilles Peskine8e338702019-07-30 20:06:31 +02006460 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461
Gilles Peskinee56e8782019-04-26 17:34:02 +02006462 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006463 return( PSA_ERROR_INVALID_ARGUMENT );
6464
Gilles Peskinee59236f2018-01-27 23:32:46 +01006465 if( key_type_is_raw_bytes( type ) )
6466 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006467 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006468
6469 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006470 if( status != PSA_SUCCESS )
6471 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006472
6473 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006474 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6475 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006476 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006477
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006478 status = psa_generate_random( slot->data.key.data,
6479 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006480 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006481 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006482
6483 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006484#if defined(MBEDTLS_DES_C)
6485 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006486 psa_des_set_key_parity( slot->data.key.data,
6487 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006488#endif /* MBEDTLS_DES_C */
6489 }
6490 else
6491
John Durkop07cc04a2020-11-16 22:08:34 -08006492#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006493 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006494 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006495 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006496 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006497 int exponent;
6498 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006499 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6500 return( PSA_ERROR_NOT_SUPPORTED );
6501 /* Accept only byte-aligned keys, for the same reasons as
6502 * in psa_import_rsa_key(). */
6503 if( bits % 8 != 0 )
6504 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006505 status = psa_read_rsa_exponent( domain_parameters,
6506 domain_parameters_size,
6507 &exponent );
6508 if( status != PSA_SUCCESS )
6509 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006510 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6511 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006512 mbedtls_psa_get_random,
6513 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskinee59236f2018-01-27 23:32:46 +01006514 (unsigned int) bits,
6515 exponent );
6516 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006517 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006518
6519 /* Make sure to always have an export representation available */
6520 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6521
Steven Cooreman75b74362020-07-28 14:30:13 +02006522 status = psa_allocate_buffer_to_slot( slot, bytes );
6523 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006524 {
6525 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006526 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006527 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006528
6529 status = psa_export_rsa_key( type,
6530 &rsa,
6531 slot->data.key.data,
6532 bytes,
6533 &slot->data.key.bytes );
6534 mbedtls_rsa_free( &rsa );
6535 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006536 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006537 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006538 }
6539 else
John Durkop07cc04a2020-11-16 22:08:34 -08006540#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006541
John Durkop9814fa22020-11-04 12:28:15 -08006542#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006543 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006545 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006546 mbedtls_ecp_group_id grp_id =
6547 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006548 const mbedtls_ecp_curve_info *curve_info =
6549 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006550 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006551 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006552 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006553 return( PSA_ERROR_NOT_SUPPORTED );
6554 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6555 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006556 mbedtls_ecp_keypair_init( &ecp );
6557 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006558 mbedtls_psa_get_random,
6559 mbedtls_psa_random_state( &global_data.rng ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006560 if( ret != 0 )
6561 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006562 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006563 return( mbedtls_to_psa_error( ret ) );
6564 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006565
6566
6567 /* Make sure to always have an export representation available */
6568 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006569 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6570 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006571 {
6572 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006573 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006574 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006575
6576 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006577 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6578
6579 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006580 if( status != PSA_SUCCESS ) {
6581 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006582 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006583 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006584 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006585 }
6586 else
John Durkop9814fa22020-11-04 12:28:15 -08006587#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006588 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006589 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006590 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006591
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006592 return( PSA_SUCCESS );
6593}
Darryl Green0c6575a2018-11-07 16:05:30 +00006594
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006595psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006596 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006597{
6598 psa_status_t status;
6599 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006600 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006601
Ronald Cron81709fc2020-11-14 12:10:32 +01006602 *key = MBEDTLS_SVC_KEY_ID_INIT;
6603
Gilles Peskine0f84d622019-09-12 19:03:13 +02006604 /* Reject any attempt to create a zero-length key so that we don't
6605 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6606 if( psa_get_key_bits( attributes ) == 0 )
6607 return( PSA_ERROR_INVALID_ARGUMENT );
6608
Ronald Cron81709fc2020-11-14 12:10:32 +01006609 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6610 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006611 if( status != PSA_SUCCESS )
6612 goto exit;
6613
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006614 status = psa_driver_wrapper_generate_key( attributes,
6615 slot );
6616 if( status != PSA_ERROR_NOT_SUPPORTED ||
6617 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6618 goto exit;
6619
6620 status = psa_generate_key_internal(
6621 slot, attributes->core.bits,
6622 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006623
6624exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006625 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006626 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006627 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006628 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006629
Darryl Green0c6575a2018-11-07 16:05:30 +00006630 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006631}
6632
6633
Gilles Peskinee59236f2018-01-27 23:32:46 +01006634
6635/****************************************************************/
6636/* Module setup */
6637/****************************************************************/
6638
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006639#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006640psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6641 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6642 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6643{
6644 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6645 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006646 global_data.rng.entropy_init = entropy_init;
6647 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006648 return( PSA_SUCCESS );
6649}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006650#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006651
Gilles Peskinee59236f2018-01-27 23:32:46 +01006652void mbedtls_psa_crypto_free( void )
6653{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006654 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006655 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6656 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006657 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006658 }
6659 /* Wipe all remaining data, including configuration.
6660 * In particular, this sets all state indicator to the value
6661 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006662 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006663#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006664 /* Unregister all secure element drivers, so that we restart from
6665 * a pristine state. */
6666 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006667#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006668}
6669
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006670#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6671/** Recover a transaction that was interrupted by a power failure.
6672 *
6673 * This function is called during initialization, before psa_crypto_init()
6674 * returns. If this function returns a failure status, the initialization
6675 * fails.
6676 */
6677static psa_status_t psa_crypto_recover_transaction(
6678 const psa_crypto_transaction_t *transaction )
6679{
6680 switch( transaction->unknown.type )
6681 {
6682 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6683 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006684 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006685 * is implemented.
6686 * https://github.com/ARMmbed/mbed-crypto/issues/218
6687 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006688 default:
6689 /* We found an unsupported transaction in the storage.
6690 * We don't know what state the storage is in. Give up. */
6691 return( PSA_ERROR_STORAGE_FAILURE );
6692 }
6693}
6694#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6695
Gilles Peskinee59236f2018-01-27 23:32:46 +01006696psa_status_t psa_crypto_init( void )
6697{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006698 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006699
Gilles Peskinec6b69072018-11-20 21:42:52 +01006700 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006701 if( global_data.initialized != 0 )
6702 return( PSA_SUCCESS );
6703
Gilles Peskine30524eb2020-11-13 17:02:26 +01006704 /* Initialize and seed the random generator. */
6705 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006706 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006707 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006708 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006709 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006710 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006711
Gilles Peskine66fb1262018-12-10 16:29:04 +01006712 status = psa_initialize_key_slots( );
6713 if( status != PSA_SUCCESS )
6714 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006715
Gilles Peskined9348f22019-10-01 15:22:29 +02006716#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6717 status = psa_init_all_se_drivers( );
6718 if( status != PSA_SUCCESS )
6719 goto exit;
6720#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6721
Gilles Peskine4b734222019-07-24 15:56:31 +02006722#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006723 status = psa_crypto_load_transaction( );
6724 if( status == PSA_SUCCESS )
6725 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006726 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6727 if( status != PSA_SUCCESS )
6728 goto exit;
6729 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006730 }
6731 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6732 {
6733 /* There's no transaction to complete. It's all good. */
6734 status = PSA_SUCCESS;
6735 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006736#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006737
Gilles Peskinec6b69072018-11-20 21:42:52 +01006738 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006739 global_data.initialized = 1;
6740
6741exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006742 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006743 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006744 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006745}
6746
6747#endif /* MBEDTLS_PSA_CRYPTO_C */