blob: 66c4f7de03c85872169f0559946220643e656d4c [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 Peskinea5905292018-02-07 20:59:33 +0100218 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
221 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskine82e57d12020-11-13 21:31:17 +0100225#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100226
227 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
228 return( PSA_ERROR_NOT_SUPPORTED );
229 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
230 return( PSA_ERROR_HARDWARE_FAILURE );
231
Gilles Peskinee59236f2018-01-27 23:32:46 +0100232 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
233 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
234 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
235 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236
237 case MBEDTLS_ERR_GCM_AUTH_FAILED:
238 return( PSA_ERROR_INVALID_SIGNATURE );
239 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200240 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100241 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
Gilles Peskine82e57d12020-11-13 21:31:17 +0100244#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) && \
245 defined(MBEDTLS_PSA_HMAC_DRBG_MD_TYPE)
246 case MBEDTLS_ERR_HMAC_DRBG_ENTROPY_SOURCE_FAILED:
247 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
248 case MBEDTLS_ERR_HMAC_DRBG_REQUEST_TOO_BIG:
249 case MBEDTLS_ERR_HMAC_DRBG_INPUT_TOO_BIG:
250 return( PSA_ERROR_NOT_SUPPORTED );
251 case MBEDTLS_ERR_HMAC_DRBG_FILE_IO_ERROR:
252 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
253#endif
254
Gilles Peskinea5905292018-02-07 20:59:33 +0100255 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
256 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
257 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
258 return( PSA_ERROR_HARDWARE_FAILURE );
259
260 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
261 return( PSA_ERROR_NOT_SUPPORTED );
262 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MD_ALLOC_FAILED:
265 return( PSA_ERROR_INSUFFICIENT_MEMORY );
266 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
267 return( PSA_ERROR_STORAGE_FAILURE );
268 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
269 return( PSA_ERROR_HARDWARE_FAILURE );
270
Gilles Peskinef76aa772018-10-29 19:24:33 +0100271 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
272 return( PSA_ERROR_STORAGE_FAILURE );
273 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
274 return( PSA_ERROR_INVALID_ARGUMENT );
275 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
278 return( PSA_ERROR_BUFFER_TOO_SMALL );
279 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
282 return( PSA_ERROR_INVALID_ARGUMENT );
283 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
286 return( PSA_ERROR_INSUFFICIENT_MEMORY );
287
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100288 case MBEDTLS_ERR_PK_ALLOC_FAILED:
289 return( PSA_ERROR_INSUFFICIENT_MEMORY );
290 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
291 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
292 return( PSA_ERROR_INVALID_ARGUMENT );
293 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100294 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100295 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
296 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
297 return( PSA_ERROR_INVALID_ARGUMENT );
298 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
299 return( PSA_ERROR_NOT_SUPPORTED );
300 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
301 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
302 return( PSA_ERROR_NOT_PERMITTED );
303 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
304 return( PSA_ERROR_INVALID_ARGUMENT );
305 case MBEDTLS_ERR_PK_INVALID_ALG:
306 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
307 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
308 return( PSA_ERROR_NOT_SUPPORTED );
309 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
310 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100311 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
312 return( PSA_ERROR_HARDWARE_FAILURE );
313
Gilles Peskineff2d2002019-05-06 15:26:23 +0200314 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
317 return( PSA_ERROR_NOT_SUPPORTED );
318
Gilles Peskinea5905292018-02-07 20:59:33 +0100319 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
320 return( PSA_ERROR_HARDWARE_FAILURE );
321
322 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
323 return( PSA_ERROR_INVALID_ARGUMENT );
324 case MBEDTLS_ERR_RSA_INVALID_PADDING:
325 return( PSA_ERROR_INVALID_PADDING );
326 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
327 return( PSA_ERROR_HARDWARE_FAILURE );
328 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
329 return( PSA_ERROR_INVALID_ARGUMENT );
330 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
331 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200332 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100333 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
334 return( PSA_ERROR_INVALID_SIGNATURE );
335 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
336 return( PSA_ERROR_BUFFER_TOO_SMALL );
337 case MBEDTLS_ERR_RSA_RNG_FAILED:
338 return( PSA_ERROR_INSUFFICIENT_MEMORY );
339 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
340 return( PSA_ERROR_NOT_SUPPORTED );
341 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
342 return( PSA_ERROR_HARDWARE_FAILURE );
343
344 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
345 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
346 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
347 return( PSA_ERROR_HARDWARE_FAILURE );
348
349 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
350 return( PSA_ERROR_INVALID_ARGUMENT );
351 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
352 return( PSA_ERROR_HARDWARE_FAILURE );
353
itayzafrir5c753392018-05-08 11:18:38 +0300354 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300355 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300356 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300357 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
358 return( PSA_ERROR_BUFFER_TOO_SMALL );
359 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
360 return( PSA_ERROR_NOT_SUPPORTED );
361 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
362 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
363 return( PSA_ERROR_INVALID_SIGNATURE );
364 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
365 return( PSA_ERROR_INSUFFICIENT_MEMORY );
366 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
367 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000368 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
369 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300370
Gilles Peskinee59236f2018-01-27 23:32:46 +0100371 default:
David Saadab4ecc272019-02-14 13:48:10 +0200372 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100373 }
374}
375
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200376
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200377
378
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100379/****************************************************************/
380/* Key management */
381/****************************************************************/
382
Gilles Peskine73167e12019-07-12 23:44:37 +0200383#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
384static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
385{
Gilles Peskine8e338702019-07-30 20:06:31 +0200386 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200387}
388#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
389
John Durkop07cc04a2020-11-16 22:08:34 -0800390/* For now the MBEDTLS_PSA_ACCEL_ guards are also used here since the
391 * current test driver in key_management.c is using this function
392 * when accelerators are used for ECC key pair and public key.
393 * Once that dependency is resolved these guards can be removed.
394 */
John Durkop9814fa22020-11-04 12:28:15 -0800395#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800396 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
397 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) || \
398 defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100399mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100400 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200401{
402 switch( curve )
403 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100404 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100405 switch( byte_length )
406 {
407 case PSA_BITS_TO_BYTES( 192 ):
408 return( MBEDTLS_ECP_DP_SECP192R1 );
409 case PSA_BITS_TO_BYTES( 224 ):
410 return( MBEDTLS_ECP_DP_SECP224R1 );
411 case PSA_BITS_TO_BYTES( 256 ):
412 return( MBEDTLS_ECP_DP_SECP256R1 );
413 case PSA_BITS_TO_BYTES( 384 ):
414 return( MBEDTLS_ECP_DP_SECP384R1 );
415 case PSA_BITS_TO_BYTES( 521 ):
416 return( MBEDTLS_ECP_DP_SECP521R1 );
417 default:
418 return( MBEDTLS_ECP_DP_NONE );
419 }
420 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100421
Paul Elliott8ff510a2020-06-02 17:19:28 +0100422 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100423 switch( byte_length )
424 {
425 case PSA_BITS_TO_BYTES( 256 ):
426 return( MBEDTLS_ECP_DP_BP256R1 );
427 case PSA_BITS_TO_BYTES( 384 ):
428 return( MBEDTLS_ECP_DP_BP384R1 );
429 case PSA_BITS_TO_BYTES( 512 ):
430 return( MBEDTLS_ECP_DP_BP512R1 );
431 default:
432 return( MBEDTLS_ECP_DP_NONE );
433 }
434 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100435
Paul Elliott8ff510a2020-06-02 17:19:28 +0100436 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100437 switch( byte_length )
438 {
439 case PSA_BITS_TO_BYTES( 255 ):
440 return( MBEDTLS_ECP_DP_CURVE25519 );
441 case PSA_BITS_TO_BYTES( 448 ):
442 return( MBEDTLS_ECP_DP_CURVE448 );
443 default:
444 return( MBEDTLS_ECP_DP_NONE );
445 }
446 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100447
Paul Elliott8ff510a2020-06-02 17:19:28 +0100448 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100449 switch( byte_length )
450 {
451 case PSA_BITS_TO_BYTES( 192 ):
452 return( MBEDTLS_ECP_DP_SECP192K1 );
453 case PSA_BITS_TO_BYTES( 224 ):
454 return( MBEDTLS_ECP_DP_SECP224K1 );
455 case PSA_BITS_TO_BYTES( 256 ):
456 return( MBEDTLS_ECP_DP_SECP256K1 );
457 default:
458 return( MBEDTLS_ECP_DP_NONE );
459 }
460 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100461
Gilles Peskine12313cd2018-06-20 00:20:32 +0200462 default:
463 return( MBEDTLS_ECP_DP_NONE );
464 }
465}
John Durkop9814fa22020-11-04 12:28:15 -0800466#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
John Durkop6ba40d12020-11-10 08:50:04 -0800467 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
468 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_KEY_PAIR) ||
469 * defined(MBEDTLS_PSA_ACCEL_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200470
Steven Cooreman81be2fa2020-07-24 22:04:59 +0200471static psa_status_t validate_unstructured_key_bit_size( psa_key_type_t type,
472 size_t bits )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200473{
474 /* Check that the bit size is acceptable for the key type */
475 switch( type )
476 {
477 case PSA_KEY_TYPE_RAW_DATA:
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200478 case PSA_KEY_TYPE_HMAC:
Gilles Peskineea0fb492018-07-12 17:17:20 +0200479 case PSA_KEY_TYPE_DERIVE:
480 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200481#if defined(MBEDTLS_AES_C)
482 case PSA_KEY_TYPE_AES:
483 if( bits != 128 && bits != 192 && bits != 256 )
484 return( PSA_ERROR_INVALID_ARGUMENT );
485 break;
486#endif
487#if defined(MBEDTLS_CAMELLIA_C)
488 case PSA_KEY_TYPE_CAMELLIA:
489 if( bits != 128 && bits != 192 && bits != 256 )
490 return( PSA_ERROR_INVALID_ARGUMENT );
491 break;
492#endif
493#if defined(MBEDTLS_DES_C)
494 case PSA_KEY_TYPE_DES:
495 if( bits != 64 && bits != 128 && bits != 192 )
496 return( PSA_ERROR_INVALID_ARGUMENT );
497 break;
498#endif
499#if defined(MBEDTLS_ARC4_C)
500 case PSA_KEY_TYPE_ARC4:
501 if( bits < 8 || bits > 2048 )
502 return( PSA_ERROR_INVALID_ARGUMENT );
503 break;
504#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200505#if defined(MBEDTLS_CHACHA20_C)
506 case PSA_KEY_TYPE_CHACHA20:
507 if( bits != 256 )
508 return( PSA_ERROR_INVALID_ARGUMENT );
509 break;
510#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200511 default:
512 return( PSA_ERROR_NOT_SUPPORTED );
513 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200514 if( bits % 8 != 0 )
515 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200516
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200517 return( PSA_SUCCESS );
518}
519
John Durkop0e005192020-10-31 22:06:54 -0700520#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
521 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
522 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
John Durkop9814fa22020-11-04 12:28:15 -0800523 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
524 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
525 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana01795d2020-07-24 22:48:15 +0200526
Gilles Peskine86a440b2018-11-12 18:39:40 +0100527/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
528 * that are not a multiple of 8) well. For example, there is only
529 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
530 * way to return the exact bit size of a key.
531 * To keep things simple, reject non-byte-aligned key sizes. */
532static psa_status_t psa_check_rsa_key_byte_aligned(
533 const mbedtls_rsa_context *rsa )
534{
535 mbedtls_mpi n;
536 psa_status_t status;
537 mbedtls_mpi_init( &n );
538 status = mbedtls_to_psa_error(
539 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
540 if( status == PSA_SUCCESS )
541 {
542 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
543 status = PSA_ERROR_NOT_SUPPORTED;
544 }
545 mbedtls_mpi_free( &n );
546 return( status );
547}
548
Steven Cooreman7f391872020-07-30 14:57:44 +0200549/** Load the contents of a key buffer into an internal RSA representation
Steven Cooremana01795d2020-07-24 22:48:15 +0200550 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200551 * \param[in] type The type of key contained in \p data.
552 * \param[in] data The buffer from which to load the representation.
553 * \param[in] data_length The size in bytes of \p data.
554 * \param[out] p_rsa Returns a pointer to an RSA context on success.
555 * The caller is responsible for freeing both the
556 * contents of the context and the context itself
557 * when done.
Steven Cooremana01795d2020-07-24 22:48:15 +0200558 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200559static psa_status_t psa_load_rsa_representation( psa_key_type_t type,
560 const uint8_t *data,
561 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200562 mbedtls_rsa_context **p_rsa )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200563{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000564 psa_status_t status;
Steven Cooremana01795d2020-07-24 22:48:15 +0200565 mbedtls_pk_context ctx;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000566 size_t bits;
Steven Cooremana01795d2020-07-24 22:48:15 +0200567 mbedtls_pk_init( &ctx );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000568
569 /* Parse the data. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200570 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000571 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200572 mbedtls_pk_parse_key( &ctx, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200573 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000574 status = mbedtls_to_psa_error(
Steven Cooreman4fed4552020-08-03 14:46:03 +0200575 mbedtls_pk_parse_public_key( &ctx, data, data_length ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000576 if( status != PSA_SUCCESS )
577 goto exit;
578
579 /* We have something that the pkparse module recognizes. If it is a
580 * valid RSA key, store it. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200581 if( mbedtls_pk_get_type( &ctx ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200582 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000583 status = PSA_ERROR_INVALID_ARGUMENT;
584 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200585 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000586
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000587 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
588 * supports non-byte-aligned key sizes, but not well. For example,
589 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200590 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( mbedtls_pk_rsa( ctx ) ) );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000591 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
592 {
593 status = PSA_ERROR_NOT_SUPPORTED;
594 goto exit;
595 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200596 status = psa_check_rsa_key_byte_aligned( mbedtls_pk_rsa( ctx ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200597 if( status != PSA_SUCCESS )
598 goto exit;
599
Steven Cooremana2371e52020-07-28 14:30:39 +0200600 /* Copy out the pointer to the RSA context, and reset the PK context
601 * such that pk_free doesn't free the RSA context we just grabbed. */
602 *p_rsa = mbedtls_pk_rsa( ctx );
603 ctx.pk_info = NULL;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000604
605exit:
Steven Cooremana01795d2020-07-24 22:48:15 +0200606 mbedtls_pk_free( &ctx );
607 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +0200608}
609
John Durkop6ba40d12020-11-10 08:50:04 -0800610#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
611 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
612 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
613 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
614 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
615 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
616
617#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
618 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
619
Steven Cooremana01795d2020-07-24 22:48:15 +0200620/** Export an RSA key to export representation
621 *
622 * \param[in] type The type of key (public/private) to export
623 * \param[in] rsa The internal RSA representation from which to export
624 * \param[out] data The buffer to export to
625 * \param[in] data_size The length of the buffer to export to
626 * \param[out] data_length The amount of bytes written to \p data
627 */
628static psa_status_t psa_export_rsa_key( psa_key_type_t type,
629 mbedtls_rsa_context *rsa,
630 uint8_t *data,
631 size_t data_size,
632 size_t *data_length )
633{
634#if defined(MBEDTLS_PK_WRITE_C)
635 int ret;
636 mbedtls_pk_context pk;
637 uint8_t *pos = data + data_size;
638
639 mbedtls_pk_init( &pk );
640 pk.pk_info = &mbedtls_rsa_info;
641 pk.pk_ctx = rsa;
642
643 /* PSA Crypto API defines the format of an RSA key as a DER-encoded
Steven Cooreman75b74362020-07-28 14:30:13 +0200644 * representation of the non-encrypted PKCS#1 RSAPrivateKey for a
645 * private key and of the RFC3279 RSAPublicKey for a public key. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200646 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
647 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
648 else
649 ret = mbedtls_pk_write_pubkey( &pos, data, &pk );
650
651 if( ret < 0 )
Steven Cooreman4fed4552020-08-03 14:46:03 +0200652 {
653 /* Clean up in case pk_write failed halfway through. */
654 memset( data, 0, data_size );
Steven Cooreman29149862020-08-05 15:43:42 +0200655 return( mbedtls_to_psa_error( ret ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200656 }
Steven Cooremana01795d2020-07-24 22:48:15 +0200657
658 /* The mbedtls_pk_xxx functions write to the end of the buffer.
659 * Move the data to the beginning and erase remaining data
660 * at the original location. */
661 if( 2 * (size_t) ret <= data_size )
662 {
663 memcpy( data, data + data_size - ret, ret );
664 memset( data + data_size - ret, 0, ret );
665 }
666 else if( (size_t) ret < data_size )
667 {
668 memmove( data, data + data_size - ret, ret );
669 memset( data + ret, 0, data_size - ret );
670 }
671
672 *data_length = ret;
673 return( PSA_SUCCESS );
674#else
675 (void) type;
676 (void) rsa;
677 (void) data;
678 (void) data_size;
679 (void) data_length;
680 return( PSA_ERROR_NOT_SUPPORTED );
681#endif /* MBEDTLS_PK_WRITE_C */
682}
683
684/** Import an RSA key from import representation to a slot
685 *
686 * \param[in,out] slot The slot where to store the export representation to
687 * \param[in] data The buffer containing the import representation
688 * \param[in] data_length The amount of bytes in \p data
689 */
690static psa_status_t psa_import_rsa_key( psa_key_slot_t *slot,
691 const uint8_t *data,
692 size_t data_length )
693{
694 psa_status_t status;
695 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200696 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +0200697
Steven Cooremana01795d2020-07-24 22:48:15 +0200698 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200699 status = psa_load_rsa_representation( slot->attr.type,
700 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200701 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200702 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200703 if( status != PSA_SUCCESS )
704 goto exit;
705
706 slot->attr.bits = (psa_key_bits_t) PSA_BYTES_TO_BITS(
Steven Cooremana2371e52020-07-28 14:30:39 +0200707 mbedtls_rsa_get_len( rsa ) );
Steven Cooremana01795d2020-07-24 22:48:15 +0200708
Steven Cooreman75b74362020-07-28 14:30:13 +0200709 /* Re-export the data to PSA export format, such that we can store export
710 * representation in the key slot. Export representation in case of RSA is
711 * the smallest representation that's allowed as input, so a straight-up
712 * allocation of the same size as the input buffer will be large enough. */
Steven Cooremana01795d2020-07-24 22:48:15 +0200713 output = mbedtls_calloc( 1, data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +0200714 if( output == NULL )
715 {
716 status = PSA_ERROR_INSUFFICIENT_MEMORY;
717 goto exit;
718 }
719
Steven Cooremana01795d2020-07-24 22:48:15 +0200720 status = psa_export_rsa_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200721 rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +0200722 output,
723 data_length,
724 &data_length);
Steven Cooremana01795d2020-07-24 22:48:15 +0200725exit:
726 /* Always free the RSA object */
Steven Cooremana2371e52020-07-28 14:30:39 +0200727 mbedtls_rsa_free( rsa );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200728 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +0200729
730 /* Free the allocated buffer only on error. */
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000731 if( status != PSA_SUCCESS )
732 {
Steven Cooremana01795d2020-07-24 22:48:15 +0200733 mbedtls_free( output );
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000734 return( status );
735 }
736
Steven Cooremana01795d2020-07-24 22:48:15 +0200737 /* On success, store the allocated export-formatted key. */
738 slot->data.key.data = output;
739 slot->data.key.bytes = data_length;
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000740
741 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200742}
John Durkop6ba40d12020-11-10 08:50:04 -0800743
744#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -0800745 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200746
John Durkop9814fa22020-11-04 12:28:15 -0800747#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
John Durkop6ba40d12020-11-10 08:50:04 -0800748 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) || \
749 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
750 defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) || \
751 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Steven Cooreman7f391872020-07-30 14:57:44 +0200752/** Load the contents of a key buffer into an internal ECP representation
Steven Cooremana2371e52020-07-28 14:30:39 +0200753 *
Steven Cooreman4fed4552020-08-03 14:46:03 +0200754 * \param[in] type The type of key contained in \p data.
755 * \param[in] data The buffer from which to load the representation.
756 * \param[in] data_length The size in bytes of \p data.
757 * \param[out] p_ecp Returns a pointer to an ECP context on success.
758 * The caller is responsible for freeing both the
759 * contents of the context and the context itself
760 * when done.
Steven Cooremana2371e52020-07-28 14:30:39 +0200761 */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200762static psa_status_t psa_load_ecp_representation( psa_key_type_t type,
763 const uint8_t *data,
764 size_t data_length,
Steven Cooremana2371e52020-07-28 14:30:39 +0200765 mbedtls_ecp_keypair **p_ecp )
Gilles Peskine4cd32772019-12-02 20:49:42 +0100766{
767 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
Steven Cooremanacda8342020-07-24 23:09:52 +0200768 psa_status_t status;
Steven Cooreman6d839f02020-07-30 11:36:45 +0200769 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +0200770 size_t curve_size = data_length;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100771
Steven Cooreman4fed4552020-08-03 14:46:03 +0200772 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) &&
773 PSA_KEY_TYPE_ECC_GET_FAMILY( type ) != PSA_ECC_FAMILY_MONTGOMERY )
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100774 {
Steven Cooreman4fed4552020-08-03 14:46:03 +0200775 /* A Weierstrass public key is represented as:
776 * - The byte 0x04;
777 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
778 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
Steven Cooreman3ea0ce42020-10-23 11:37:05 +0200779 * So its data length is 2m+1 where m is the curve size in bits.
Steven Cooreman4fed4552020-08-03 14:46:03 +0200780 */
781 if( ( data_length & 1 ) == 0 )
782 return( PSA_ERROR_INVALID_ARGUMENT );
783 curve_size = data_length / 2;
784
785 /* Montgomery public keys are represented in compressed format, meaning
786 * their curve_size is equal to the amount of input. */
787
788 /* Private keys are represented in uncompressed private random integer
789 * format, meaning their curve_size is equal to the amount of input. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100790 }
791
Steven Cooreman6d839f02020-07-30 11:36:45 +0200792 /* Allocate and initialize a key representation. */
Steven Cooreman29149862020-08-05 15:43:42 +0200793 ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200794 if( ecp == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +0200795 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooremana2371e52020-07-28 14:30:39 +0200796 mbedtls_ecp_keypair_init( ecp );
797
Gilles Peskine4cd32772019-12-02 20:49:42 +0100798 /* Load the group. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200799 grp_id = mbedtls_ecc_group_of_psa( PSA_KEY_TYPE_ECC_GET_FAMILY( type ),
800 curve_size );
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100801 if( grp_id == MBEDTLS_ECP_DP_NONE )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200802 {
803 status = PSA_ERROR_INVALID_ARGUMENT;
804 goto exit;
805 }
806
Steven Cooremanacda8342020-07-24 23:09:52 +0200807 status = mbedtls_to_psa_error(
808 mbedtls_ecp_group_load( &ecp->grp, grp_id ) );
809 if( status != PSA_SUCCESS )
Steven Cooreman6d839f02020-07-30 11:36:45 +0200810 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200811
Steven Cooreman6d839f02020-07-30 11:36:45 +0200812 /* Load the key material. */
Steven Cooreman7f391872020-07-30 14:57:44 +0200813 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200814 {
815 /* Load the public value. */
816 status = mbedtls_to_psa_error(
817 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200818 data,
819 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200820 if( status != PSA_SUCCESS )
821 goto exit;
822
823 /* Check that the point is on the curve. */
824 status = mbedtls_to_psa_error(
825 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
826 if( status != PSA_SUCCESS )
827 goto exit;
828 }
829 else
830 {
Steven Cooreman6d839f02020-07-30 11:36:45 +0200831 /* Load and validate the secret value. */
Steven Cooremanacda8342020-07-24 23:09:52 +0200832 status = mbedtls_to_psa_error(
833 mbedtls_ecp_read_key( ecp->grp.id,
834 ecp,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200835 data,
836 data_length ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200837 if( status != PSA_SUCCESS )
838 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +0200839 }
Steven Cooremana2371e52020-07-28 14:30:39 +0200840
841 *p_ecp = ecp;
Steven Cooremanacda8342020-07-24 23:09:52 +0200842exit:
843 if( status != PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +0200844 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200845 mbedtls_ecp_keypair_free( ecp );
Steven Cooremana2371e52020-07-28 14:30:39 +0200846 mbedtls_free( ecp );
847 }
848
Steven Cooreman29149862020-08-05 15:43:42 +0200849 return( status );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100850}
John Durkop6ba40d12020-11-10 08:50:04 -0800851#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
852 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) ||
853 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
854 * defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH) ||
855 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000856
John Durkop6ba40d12020-11-10 08:50:04 -0800857#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
858 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman4fed4552020-08-03 14:46:03 +0200859/** Export an ECP key to export representation
860 *
861 * \param[in] type The type of key (public/private) to export
862 * \param[in] ecp The internal ECP representation from which to export
863 * \param[out] data The buffer to export to
864 * \param[in] data_size The length of the buffer to export to
865 * \param[out] data_length The amount of bytes written to \p data
866 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200867static psa_status_t psa_export_ecp_key( psa_key_type_t type,
868 mbedtls_ecp_keypair *ecp,
869 uint8_t *data,
870 size_t data_size,
871 size_t *data_length )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200872{
Steven Cooremanacda8342020-07-24 23:09:52 +0200873 psa_status_t status;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000874
Steven Cooremanacda8342020-07-24 23:09:52 +0200875 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( type ) )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200876 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200877 /* Check whether the public part is loaded */
878 if( mbedtls_ecp_is_zero( &ecp->Q ) )
879 {
880 /* Calculate the public key */
881 status = mbedtls_to_psa_error(
882 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +0100883 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200884 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200885 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +0200886 }
887
Steven Cooreman4fed4552020-08-03 14:46:03 +0200888 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +0200889 mbedtls_ecp_point_write_binary( &ecp->grp, &ecp->Q,
890 MBEDTLS_ECP_PF_UNCOMPRESSED,
891 data_length,
892 data,
Steven Cooreman4fed4552020-08-03 14:46:03 +0200893 data_size ) );
Steven Cooreman4fed4552020-08-03 14:46:03 +0200894 if( status != PSA_SUCCESS )
895 memset( data, 0, data_size );
896
Steven Cooreman29149862020-08-05 15:43:42 +0200897 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200898 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200899 else
900 {
Steven Cooreman29149862020-08-05 15:43:42 +0200901 if( data_size < PSA_BITS_TO_BYTES( ecp->grp.nbits ) )
Steven Cooremanacda8342020-07-24 23:09:52 +0200902 return( PSA_ERROR_BUFFER_TOO_SMALL );
903
904 status = mbedtls_to_psa_error(
905 mbedtls_ecp_write_key( ecp,
906 data,
Steven Cooreman29149862020-08-05 15:43:42 +0200907 PSA_BITS_TO_BYTES( ecp->grp.nbits ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +0200908 if( status == PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +0200909 *data_length = PSA_BITS_TO_BYTES( ecp->grp.nbits );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +0200910 else
911 memset( data, 0, data_size );
Steven Cooremanacda8342020-07-24 23:09:52 +0200912
913 return( status );
914 }
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200915}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200916
Steven Cooreman4fed4552020-08-03 14:46:03 +0200917/** Import an ECP key from import representation to a slot
918 *
919 * \param[in,out] slot The slot where to store the export representation to
920 * \param[in] data The buffer containing the import representation
921 * \param[in] data_length The amount of bytes in \p data
922 */
Steven Cooremanacda8342020-07-24 23:09:52 +0200923static psa_status_t psa_import_ecp_key( psa_key_slot_t *slot,
924 const uint8_t *data,
925 size_t data_length )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100926{
Steven Cooremanacda8342020-07-24 23:09:52 +0200927 psa_status_t status;
928 uint8_t* output = NULL;
Steven Cooremana2371e52020-07-28 14:30:39 +0200929 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100930
Steven Cooremanacda8342020-07-24 23:09:52 +0200931 /* Parse input */
Steven Cooreman4fed4552020-08-03 14:46:03 +0200932 status = psa_load_ecp_representation( slot->attr.type,
933 data,
Steven Cooreman7f391872020-07-30 14:57:44 +0200934 data_length,
Steven Cooreman7f391872020-07-30 14:57:44 +0200935 &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100936 if( status != PSA_SUCCESS )
937 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100938
Steven Cooremanacda8342020-07-24 23:09:52 +0200939 if( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ) == PSA_ECC_FAMILY_MONTGOMERY)
Steven Cooremana2371e52020-07-28 14:30:39 +0200940 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits + 1;
Steven Cooremanacda8342020-07-24 23:09:52 +0200941 else
Steven Cooremana2371e52020-07-28 14:30:39 +0200942 slot->attr.bits = (psa_key_bits_t) ecp->grp.nbits;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200943
Steven Cooremanacda8342020-07-24 23:09:52 +0200944 /* Re-export the data to PSA export format. There is currently no support
945 * for other input formats then the export format, so this is a 1-1
946 * copy operation. */
947 output = mbedtls_calloc( 1, data_length );
Steven Cooremanacda8342020-07-24 23:09:52 +0200948 if( output == NULL )
949 {
950 status = PSA_ERROR_INSUFFICIENT_MEMORY;
951 goto exit;
952 }
953
954 status = psa_export_ecp_key( slot->attr.type,
Steven Cooremana2371e52020-07-28 14:30:39 +0200955 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +0200956 output,
957 data_length,
958 &data_length);
Gilles Peskinef76aa772018-10-29 19:24:33 +0100959exit:
Steven Cooremana2371e52020-07-28 14:30:39 +0200960 /* Always free the PK object (will also free contained ECP context) */
961 mbedtls_ecp_keypair_free( ecp );
Steven Cooreman6d839f02020-07-30 11:36:45 +0200962 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +0200963
964 /* Free the allocated buffer only on error. */
965 if( status != PSA_SUCCESS )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100966 {
Steven Cooremanacda8342020-07-24 23:09:52 +0200967 mbedtls_free( output );
Steven Cooremanacda8342020-07-24 23:09:52 +0200968 return( status );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100969 }
Steven Cooremanacda8342020-07-24 23:09:52 +0200970
971 /* On success, store the allocated export-formatted key. */
972 slot->data.key.data = output;
973 slot->data.key.bytes = data_length;
974
975 return( PSA_SUCCESS );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100976}
John Durkop9814fa22020-11-04 12:28:15 -0800977#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
978 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100979
Gilles Peskineb46bef22019-07-30 21:32:04 +0200980/** Return the size of the key in the given slot, in bits.
981 *
982 * \param[in] slot A key slot.
983 *
984 * \return The key size in bits, read from the metadata in the slot.
985 */
986static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
987{
988 return( slot->attr.bits );
989}
990
Steven Cooreman75b74362020-07-28 14:30:13 +0200991/** Try to allocate a buffer to an empty key slot.
992 *
993 * \param[in,out] slot Key slot to attach buffer to.
994 * \param[in] buffer_length Requested size of the buffer.
995 *
996 * \retval #PSA_SUCCESS
997 * The buffer has been successfully allocated.
998 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
999 * Not enough memory was available for allocation.
1000 * \retval #PSA_ERROR_ALREADY_EXISTS
1001 * Trying to allocate a buffer to a non-empty key slot.
1002 */
1003static psa_status_t psa_allocate_buffer_to_slot( psa_key_slot_t *slot,
1004 size_t buffer_length )
1005{
1006 if( slot->data.key.data != NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001007 return( PSA_ERROR_ALREADY_EXISTS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001008
1009 slot->data.key.data = mbedtls_calloc( 1, buffer_length );
1010 if( slot->data.key.data == NULL )
Steven Cooreman29149862020-08-05 15:43:42 +02001011 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Steven Cooreman75b74362020-07-28 14:30:13 +02001012
1013 slot->data.key.bytes = buffer_length;
Steven Cooreman29149862020-08-05 15:43:42 +02001014 return( PSA_SUCCESS );
Steven Cooreman75b74362020-07-28 14:30:13 +02001015}
1016
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001017psa_status_t psa_copy_key_material_into_slot( psa_key_slot_t *slot,
1018 const uint8_t* data,
1019 size_t data_length )
1020{
1021 psa_status_t status = psa_allocate_buffer_to_slot( slot,
1022 data_length );
1023 if( status != PSA_SUCCESS )
1024 return( status );
1025
1026 memcpy( slot->data.key.data, data, data_length );
1027 return( PSA_SUCCESS );
1028}
1029
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001030/** Import key data into a slot.
1031 *
1032 * `slot->type` must have been set previously.
1033 * This function assumes that the slot does not contain any key material yet.
1034 * On failure, the slot content is unchanged.
1035 *
1036 * Persistent storage is not affected.
1037 *
1038 * \param[in,out] slot The key slot to import data into.
1039 * Its `type` field must have previously been set to
1040 * the desired key type.
1041 * It must not contain any key material yet.
1042 * \param[in] data Buffer containing the key material to parse and import.
1043 * \param data_length Size of \p data in bytes.
1044 *
1045 * \retval #PSA_SUCCESS
1046 * \retval #PSA_ERROR_INVALID_ARGUMENT
1047 * \retval #PSA_ERROR_NOT_SUPPORTED
1048 * \retval #PSA_ERROR_INSUFFICIENT_MEMORY
1049 */
1050static psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
1051 const uint8_t *data,
1052 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001053{
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001054 psa_status_t status = PSA_SUCCESS;
Steven Cooreman04524762020-10-13 17:43:44 +02001055 size_t bit_size;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001056
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001057 /* zero-length keys are never supported. */
1058 if( data_length == 0 )
1059 return( PSA_ERROR_NOT_SUPPORTED );
1060
Gilles Peskine8e338702019-07-30 20:06:31 +02001061 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001062 {
Steven Cooreman04524762020-10-13 17:43:44 +02001063 bit_size = PSA_BYTES_TO_BITS( data_length );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001064
Steven Cooreman75b74362020-07-28 14:30:13 +02001065 /* Ensure that the bytes-to-bits conversion hasn't overflown. */
1066 if( data_length > SIZE_MAX / 8 )
1067 return( PSA_ERROR_NOT_SUPPORTED );
1068
Gilles Peskine1b9505c2019-08-07 10:59:45 +02001069 /* Enforce a size limit, and in particular ensure that the bit
1070 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +02001071 if( bit_size > PSA_MAX_KEY_BITS )
1072 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001073
1074 status = validate_unstructured_key_bit_size( slot->attr.type, bit_size );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +02001075 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001076 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001077
1078 /* Allocate memory for the key */
Steven Cooremanf7cebd42020-10-13 20:27:40 +02001079 status = psa_copy_key_material_into_slot( slot, data, data_length );
Steven Cooreman75b74362020-07-28 14:30:13 +02001080 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001081 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001082
Steven Cooreman81be2fa2020-07-24 22:04:59 +02001083 /* Write the actual key size to the slot.
1084 * psa_start_key_creation() wrote the size declared by the
1085 * caller, which may be 0 (meaning unspecified) or wrong. */
1086 slot->attr.bits = (psa_key_bits_t) bit_size;
Steven Cooreman40120f62020-10-29 11:42:22 +01001087
1088 return( PSA_SUCCESS );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001089 }
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001090 else if( PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Steven Cooreman19fd5742020-07-24 23:31:01 +02001091 {
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001092 /* Try validation through accelerators first. */
1093 bit_size = slot->attr.bits;
1094 psa_key_attributes_t attributes = {
1095 .core = slot->attr
1096 };
1097 status = psa_driver_wrapper_validate_key( &attributes,
1098 data,
1099 data_length,
1100 &bit_size );
1101 if( status == PSA_SUCCESS )
1102 {
1103 /* Key has been validated successfully by an accelerator.
1104 * Copy key material into slot. */
1105 status = psa_copy_key_material_into_slot( slot, data, data_length );
1106 if( status != PSA_SUCCESS )
1107 return( status );
1108
1109 slot->attr.bits = (psa_key_bits_t) bit_size;
1110 return( PSA_SUCCESS );
1111 }
1112 else if( status != PSA_ERROR_NOT_SUPPORTED )
1113 return( status );
1114
1115 /* Key format is not supported by any accelerator, try software fallback
1116 * if present. */
John Durkop9814fa22020-11-04 12:28:15 -08001117#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1118 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001119 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
1120 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001121 return( psa_import_ecp_key( slot, data, data_length ) );
1122 }
John Durkop9814fa22020-11-04 12:28:15 -08001123#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1124 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
John Durkop0e005192020-10-31 22:06:54 -07001125#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1126 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooreman40120f62020-10-29 11:42:22 +01001127 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001128 {
Steven Cooreman40120f62020-10-29 11:42:22 +01001129 return( psa_import_rsa_key( slot, data, data_length ) );
Steven Cooreman3ea0ce42020-10-23 11:37:05 +02001130 }
John Durkop9814fa22020-11-04 12:28:15 -08001131#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1132 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Steven Cooreman40120f62020-10-29 11:42:22 +01001133
1134 /* Fell through the fallback as well, so have nothing else to try. */
1135 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001136 }
1137 else
Gilles Peskinec66ea6a2018-02-03 22:43:28 +01001138 {
Steven Cooreman19fd5742020-07-24 23:31:01 +02001139 /* Unknown key type */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001140 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969ac722018-01-28 18:16:59 +01001141 }
Darryl Green06fd18d2018-07-16 11:21:11 +01001142}
1143
Gilles Peskinef603c712019-01-19 13:40:11 +01001144/** Calculate the intersection of two algorithm usage policies.
1145 *
1146 * Return 0 (which allows no operation) on incompatibility.
1147 */
1148static psa_algorithm_t psa_key_policy_algorithm_intersection(
1149 psa_algorithm_t alg1,
1150 psa_algorithm_t alg2 )
1151{
Gilles Peskine549ea862019-05-22 11:45:59 +02001152 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +01001153 if( alg1 == alg2 )
1154 return( alg1 );
1155 /* If the policies are from the same hash-and-sign family, check
1156 * if one is a wildcard. If so the other has the specific algorithm. */
1157 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
1158 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
1159 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
1160 {
1161 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
1162 return( alg2 );
1163 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
1164 return( alg1 );
1165 }
1166 /* If the policies are incompatible, allow nothing. */
1167 return( 0 );
1168}
1169
Gilles Peskined6f371b2019-05-10 19:33:38 +02001170static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
1171 psa_algorithm_t requested_alg )
1172{
Gilles Peskine549ea862019-05-22 11:45:59 +02001173 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001174 if( requested_alg == policy_alg )
1175 return( 1 );
1176 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +02001177 * and requested_alg is the same hash-and-sign family with any hash,
1178 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +02001179 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
1180 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
1181 {
1182 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
1183 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
1184 }
Steven Cooremance48e852020-10-05 16:02:45 +02001185 /* If policy_alg is a generic key agreement operation, then using it for
Steven Cooremanfa5e6312020-10-15 17:07:12 +02001186 * a key derivation with that key agreement should also be allowed. This
1187 * behaviour is expected to be defined in a future specification version. */
Steven Cooremance48e852020-10-05 16:02:45 +02001188 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( policy_alg ) &&
1189 PSA_ALG_IS_KEY_AGREEMENT( requested_alg ) )
1190 {
1191 return( PSA_ALG_KEY_AGREEMENT_GET_BASE( requested_alg ) ==
1192 policy_alg );
1193 }
Gilles Peskined6f371b2019-05-10 19:33:38 +02001194 /* If it isn't permitted, it's forbidden. */
1195 return( 0 );
1196}
1197
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001198/** Test whether a policy permits an algorithm.
1199 *
1200 * The caller must test usage flags separately.
1201 */
1202static int psa_key_policy_permits( const psa_key_policy_t *policy,
1203 psa_algorithm_t alg )
1204{
Gilles Peskined6f371b2019-05-10 19:33:38 +02001205 return( psa_key_algorithm_permits( policy->alg, alg ) ||
1206 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001207}
1208
Gilles Peskinef603c712019-01-19 13:40:11 +01001209/** Restrict a key policy based on a constraint.
1210 *
1211 * \param[in,out] policy The policy to restrict.
1212 * \param[in] constraint The policy constraint to apply.
1213 *
1214 * \retval #PSA_SUCCESS
1215 * \c *policy contains the intersection of the original value of
1216 * \c *policy and \c *constraint.
1217 * \retval #PSA_ERROR_INVALID_ARGUMENT
1218 * \c *policy and \c *constraint are incompatible.
1219 * \c *policy is unchanged.
1220 */
1221static psa_status_t psa_restrict_key_policy(
1222 psa_key_policy_t *policy,
1223 const psa_key_policy_t *constraint )
1224{
1225 psa_algorithm_t intersection_alg =
1226 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001227 psa_algorithm_t intersection_alg2 =
1228 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001229 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
1230 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +02001231 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
1232 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +01001233 policy->usage &= constraint->usage;
1234 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +02001235 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +01001236 return( PSA_SUCCESS );
1237}
1238
Ronald Cron5c522922020-11-14 16:35:34 +01001239/** Get the description of a key given its identifier and policy constraints
1240 * and lock it.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001241 *
Ronald Cron5c522922020-11-14 16:35:34 +01001242 * The key must have allow all the usage flags set in \p usage. If \p alg is
1243 * nonzero, the key must allow operations with this algorithm.
Ronald Cron7587ae42020-11-11 15:04:25 +01001244 *
Ronald Cron5c522922020-11-14 16:35:34 +01001245 * In case of a persistent key, the function loads the description of the key
1246 * into a key slot if not already done.
1247 *
1248 * On success, the returned key slot is locked. It is the responsibility of
1249 * the caller to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001250 */
Ronald Cron5c522922020-11-14 16:35:34 +01001251static psa_status_t psa_get_and_lock_key_slot_with_policy(
1252 mbedtls_svc_key_id_t key,
1253 psa_key_slot_t **p_slot,
1254 psa_key_usage_t usage,
1255 psa_algorithm_t alg )
Darryl Green06fd18d2018-07-16 11:21:11 +01001256{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001257 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
1258 psa_key_slot_t *slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001259
Ronald Cron5c522922020-11-14 16:35:34 +01001260 status = psa_get_and_lock_key_slot( key, p_slot );
Darryl Green06fd18d2018-07-16 11:21:11 +01001261 if( status != PSA_SUCCESS )
1262 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001263 slot = *p_slot;
Darryl Green06fd18d2018-07-16 11:21:11 +01001264
1265 /* Enforce that usage policy for the key slot contains all the flags
1266 * required by the usage parameter. There is one exception: public
1267 * keys can always be exported, so we treat public key objects as
1268 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001269 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +01001270 usage &= ~PSA_KEY_USAGE_EXPORT;
Ronald Cronf95a2b12020-10-22 15:24:49 +02001271
1272 status = PSA_ERROR_NOT_PERMITTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001273 if( ( slot->attr.policy.usage & usage ) != usage )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001274 goto error;
Gilles Peskine30f77cd2019-01-14 16:06:39 +01001275
1276 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +02001277 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02001278 goto error;
Darryl Green06fd18d2018-07-16 11:21:11 +01001279
Darryl Green06fd18d2018-07-16 11:21:11 +01001280 return( PSA_SUCCESS );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001281
1282error:
1283 *p_slot = NULL;
Ronald Cron5c522922020-11-14 16:35:34 +01001284 psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001285
1286 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +01001287}
Darryl Green940d72c2018-07-13 13:18:51 +01001288
Ronald Cron5c522922020-11-14 16:35:34 +01001289/** Get a key slot containing a transparent key and lock it.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001290 *
1291 * A transparent key is a key for which the key material is directly
1292 * available, as opposed to a key in a secure element.
1293 *
Ronald Cron5c522922020-11-14 16:35:34 +01001294 * This is a temporary function to use instead of
1295 * psa_get_and_lock_key_slot_with_policy() until secure element support is
1296 * fully implemented.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001297 *
Ronald Cron5c522922020-11-14 16:35:34 +01001298 * On success, the returned key slot is locked. It is the responsibility of the
1299 * caller to unlock the key slot when it does not access it anymore.
Gilles Peskine28f8f302019-07-24 13:30:31 +02001300 */
1301#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Ronald Cron5c522922020-11-14 16:35:34 +01001302static psa_status_t psa_get_and_lock_transparent_key_slot_with_policy(
1303 mbedtls_svc_key_id_t key,
1304 psa_key_slot_t **p_slot,
1305 psa_key_usage_t usage,
1306 psa_algorithm_t alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001307{
Ronald Cron5c522922020-11-14 16:35:34 +01001308 psa_status_t status = psa_get_and_lock_key_slot_with_policy( key, p_slot,
1309 usage, alg );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001310 if( status != PSA_SUCCESS )
1311 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001312
Gilles Peskineadad8132019-07-25 11:31:23 +02001313 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001314 {
Ronald Cron5c522922020-11-14 16:35:34 +01001315 psa_unlock_key_slot( *p_slot );
Gilles Peskine28f8f302019-07-24 13:30:31 +02001316 *p_slot = NULL;
1317 return( PSA_ERROR_NOT_SUPPORTED );
1318 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02001319
Gilles Peskine28f8f302019-07-24 13:30:31 +02001320 return( PSA_SUCCESS );
1321}
1322#else /* MBEDTLS_PSA_CRYPTO_SE_C */
1323/* With no secure element support, all keys are transparent. */
Ronald Cron5c522922020-11-14 16:35:34 +01001324#define psa_get_and_lock_transparent_key_slot_with_policy( key, p_slot, usage, alg ) \
1325 psa_get_and_lock_key_slot_with_policy( key, p_slot, usage, alg )
Gilles Peskine28f8f302019-07-24 13:30:31 +02001326#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1327
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001328/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +01001329static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +00001330{
Gilles Peskine73167e12019-07-12 23:44:37 +02001331#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1332 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +00001333 {
1334 /* No key material to clean. */
1335 }
Gilles Peskine73167e12019-07-12 23:44:37 +02001336 else
1337#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Darryl Green40225ba2018-11-15 14:48:15 +00001338 {
Steven Cooremanfd4d69a2020-08-05 15:46:33 +02001339 /* Data pointer will always be either a valid pointer or NULL in an
1340 * initialized slot, so we can just free it. */
1341 if( slot->data.key.data != NULL )
1342 mbedtls_platform_zeroize( slot->data.key.data, slot->data.key.bytes);
Steven Cooremana01795d2020-07-24 22:48:15 +02001343 mbedtls_free( slot->data.key.data );
1344 slot->data.key.data = NULL;
1345 slot->data.key.bytes = 0;
Darryl Green40225ba2018-11-15 14:48:15 +00001346 }
Darryl Green40225ba2018-11-15 14:48:15 +00001347
1348 return( PSA_SUCCESS );
1349}
1350
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001351/** Completely wipe a slot in memory, including its policy.
1352 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001353psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001354{
1355 psa_status_t status = psa_remove_key_data_from_memory( slot );
Ronald Cronddd3d052020-10-30 14:07:07 +01001356
1357 /*
1358 * As the return error code may not be handled in case of multiple errors,
Ronald Cron5c522922020-11-14 16:35:34 +01001359 * do our best to report an unexpected lock counter: if available
Ronald Cronddd3d052020-10-30 14:07:07 +01001360 * call MBEDTLS_PARAM_FAILED that may terminate execution (if called as
1361 * part of the execution of a test suite this will stop the test suite
Ronald Cron4640c152020-11-13 10:11:01 +01001362 * execution).
Ronald Cronddd3d052020-10-30 14:07:07 +01001363 */
Ronald Cron5c522922020-11-14 16:35:34 +01001364 if( slot->lock_count != 1 )
Ronald Cronddd3d052020-10-30 14:07:07 +01001365 {
1366#ifdef MBEDTLS_CHECK_PARAMS
Ronald Cron5c522922020-11-14 16:35:34 +01001367 MBEDTLS_PARAM_FAILED( slot->lock_count == 1 );
Ronald Cronddd3d052020-10-30 14:07:07 +01001368#endif
Ronald Cronddd3d052020-10-30 14:07:07 +01001369 status = PSA_ERROR_CORRUPTION_DETECTED;
1370 }
1371
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001372 /* Multipart operations may still be using the key. This is safe
1373 * because all multipart operation objects are independent from
1374 * the key slot: if they need to access the key after the setup
1375 * phase, they have a copy of the key. Note that this means that
1376 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001377 /* At this point, key material and other type-specific content has
1378 * been wiped. Clear remaining metadata. We can call memset and not
1379 * zeroize because the metadata is not particularly sensitive. */
1380 memset( slot, 0, sizeof( *slot ) );
1381 return( status );
1382}
1383
Ronald Croncf56a0a2020-08-04 09:51:30 +02001384psa_status_t psa_destroy_key( mbedtls_svc_key_id_t key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001385{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001386 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001387 psa_status_t status; /* status of the last operation */
1388 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001389#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1390 psa_se_drv_table_entry_t *driver;
1391#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001392
Ronald Croncf56a0a2020-08-04 09:51:30 +02001393 if( mbedtls_svc_key_id_is_null( key ) )
Gilles Peskine1841cf42019-10-08 15:48:25 +02001394 return( PSA_SUCCESS );
1395
Ronald Cronf2911112020-10-29 17:51:10 +01001396 /*
Ronald Cron19daca92020-11-10 18:08:03 +01001397 * Get the description of the key in a key slot. In case of a persistent
Ronald Cronf2911112020-10-29 17:51:10 +01001398 * key, this will load the key description from persistent memory if not
1399 * done yet. We cannot avoid this loading as without it we don't know if
1400 * the key is operated by an SE or not and this information is needed by
1401 * the current implementation.
1402 */
Ronald Cron5c522922020-11-14 16:35:34 +01001403 status = psa_get_and_lock_key_slot( key, &slot );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001404 if( status != PSA_SUCCESS )
1405 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001406
Ronald Cronf2911112020-10-29 17:51:10 +01001407 /*
1408 * If the key slot containing the key description is under access by the
1409 * library (apart from the present access), the key cannot be destroyed
1410 * yet. For the time being, just return in error. Eventually (to be
1411 * implemented), the key should be destroyed when all accesses have
1412 * stopped.
1413 */
Ronald Cron5c522922020-11-14 16:35:34 +01001414 if( slot->lock_count > 1 )
Ronald Cronf2911112020-10-29 17:51:10 +01001415 {
Ronald Cron5c522922020-11-14 16:35:34 +01001416 psa_unlock_key_slot( slot );
Ronald Cronf2911112020-10-29 17:51:10 +01001417 return( PSA_ERROR_GENERIC_ERROR );
1418 }
1419
Gilles Peskine354f7672019-07-12 23:46:38 +02001420#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001421 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001422 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001423 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001424 /* For a key in a secure element, we need to do three things:
1425 * remove the key file in internal storage, destroy the
1426 * key inside the secure element, and update the driver's
1427 * persistent data. Start a transaction that will encompass these
1428 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001429 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001430 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001431 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001432 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001433 status = psa_crypto_save_transaction( );
1434 if( status != PSA_SUCCESS )
1435 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001436 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001437 /* We should still try to destroy the key in the secure
1438 * element and the key metadata in storage. This is especially
1439 * important if the error is that the storage is full.
1440 * But how to do it exactly without risking an inconsistent
1441 * state after a reset?
1442 * https://github.com/ARMmbed/mbed-crypto/issues/215
1443 */
1444 overall_status = status;
1445 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001446 }
1447
Gilles Peskine354f7672019-07-12 23:46:38 +02001448 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001449 if( overall_status == PSA_SUCCESS )
1450 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001451 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001452#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1453
Darryl Greend49a4992018-06-18 17:27:26 +01001454#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Ronald Crond98059d2020-10-23 18:00:55 +02001455 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Darryl Greend49a4992018-06-18 17:27:26 +01001456 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001457 status = psa_destroy_persistent_key( slot->attr.id );
1458 if( overall_status == PSA_SUCCESS )
1459 overall_status = status;
1460
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001461 /* TODO: other slots may have a copy of the same key. We should
1462 * invalidate them.
1463 * https://github.com/ARMmbed/mbed-crypto/issues/214
1464 */
Darryl Greend49a4992018-06-18 17:27:26 +01001465 }
1466#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001467
Gilles Peskinefc762652019-07-22 19:30:34 +02001468#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1469 if( driver != NULL )
1470 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001471 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001472 if( overall_status == PSA_SUCCESS )
1473 overall_status = status;
1474 status = psa_crypto_stop_transaction( );
1475 if( overall_status == PSA_SUCCESS )
1476 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001477 }
1478#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1479
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001480#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1481exit:
1482#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001483 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001484 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1485 if( overall_status == PSA_SUCCESS )
1486 overall_status = status;
1487 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001488}
1489
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001490void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001491{
Gilles Peskineb699f072019-04-26 16:06:02 +02001492 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001493 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001494}
1495
Gilles Peskineb699f072019-04-26 16:06:02 +02001496psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1497 psa_key_type_t type,
1498 const uint8_t *data,
1499 size_t data_length )
1500{
1501 uint8_t *copy = NULL;
1502
1503 if( data_length != 0 )
1504 {
1505 copy = mbedtls_calloc( 1, data_length );
1506 if( copy == NULL )
1507 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1508 memcpy( copy, data, data_length );
1509 }
1510 /* After this point, this function is guaranteed to succeed, so it
1511 * can start modifying `*attributes`. */
1512
1513 if( attributes->domain_parameters != NULL )
1514 {
1515 mbedtls_free( attributes->domain_parameters );
1516 attributes->domain_parameters = NULL;
1517 attributes->domain_parameters_size = 0;
1518 }
1519
1520 attributes->domain_parameters = copy;
1521 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001522 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001523 return( PSA_SUCCESS );
1524}
1525
1526psa_status_t psa_get_key_domain_parameters(
1527 const psa_key_attributes_t *attributes,
1528 uint8_t *data, size_t data_size, size_t *data_length )
1529{
1530 if( attributes->domain_parameters_size > data_size )
1531 return( PSA_ERROR_BUFFER_TOO_SMALL );
1532 *data_length = attributes->domain_parameters_size;
1533 if( attributes->domain_parameters_size != 0 )
1534 memcpy( data, attributes->domain_parameters,
1535 attributes->domain_parameters_size );
1536 return( PSA_SUCCESS );
1537}
1538
John Durkop07cc04a2020-11-16 22:08:34 -08001539#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001540 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskineb699f072019-04-26 16:06:02 +02001541static psa_status_t psa_get_rsa_public_exponent(
1542 const mbedtls_rsa_context *rsa,
1543 psa_key_attributes_t *attributes )
1544{
1545 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001547 uint8_t *buffer = NULL;
1548 size_t buflen;
1549 mbedtls_mpi_init( &mpi );
1550
1551 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1552 if( ret != 0 )
1553 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001554 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1555 {
1556 /* It's the default value, which is reported as an empty string,
1557 * so there's nothing to do. */
1558 goto exit;
1559 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001560
1561 buflen = mbedtls_mpi_size( &mpi );
1562 buffer = mbedtls_calloc( 1, buflen );
1563 if( buffer == NULL )
1564 {
1565 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1566 goto exit;
1567 }
1568 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1569 if( ret != 0 )
1570 goto exit;
1571 attributes->domain_parameters = buffer;
1572 attributes->domain_parameters_size = buflen;
1573
1574exit:
1575 mbedtls_mpi_free( &mpi );
1576 if( ret != 0 )
1577 mbedtls_free( buffer );
1578 return( mbedtls_to_psa_error( ret ) );
1579}
John Durkop07cc04a2020-11-16 22:08:34 -08001580#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001581 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001582
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001583/** Retrieve all the publicly-accessible attributes of a key.
1584 */
Ronald Croncf56a0a2020-08-04 09:51:30 +02001585psa_status_t psa_get_key_attributes( mbedtls_svc_key_id_t key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001586 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001587{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001588 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001589 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001590 psa_key_slot_t *slot;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001591
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001592 psa_reset_key_attributes( attributes );
1593
Ronald Cron5c522922020-11-14 16:35:34 +01001594 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001595 if( status != PSA_SUCCESS )
1596 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001597
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001598 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001599 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1600 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1601
1602#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1603 if( psa_key_slot_is_external( slot ) )
1604 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1605#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001606
Gilles Peskine8e338702019-07-30 20:06:31 +02001607 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001608 {
John Durkop07cc04a2020-11-16 22:08:34 -08001609#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
John Durkop0e005192020-10-31 22:06:54 -07001610 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001611 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001612 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001613#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001614 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001615 * is not yet implemented.
1616 * https://github.com/ARMmbed/mbed-crypto/issues/216
1617 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001618 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001619 break;
1620#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Steven Cooremana01795d2020-07-24 22:48:15 +02001621 {
Steven Cooremana2371e52020-07-28 14:30:39 +02001622 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02001623
Steven Cooreman4fed4552020-08-03 14:46:03 +02001624 status = psa_load_rsa_representation( slot->attr.type,
1625 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02001626 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001627 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001628 if( status != PSA_SUCCESS )
1629 break;
1630
Steven Cooremana2371e52020-07-28 14:30:39 +02001631 status = psa_get_rsa_public_exponent( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02001632 attributes );
Steven Cooremana2371e52020-07-28 14:30:39 +02001633 mbedtls_rsa_free( rsa );
1634 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001635 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001636 break;
John Durkop07cc04a2020-11-16 22:08:34 -08001637#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
John Durkop9814fa22020-11-04 12:28:15 -08001638 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskineb699f072019-04-26 16:06:02 +02001639 default:
1640 /* Nothing else to do. */
1641 break;
1642 }
1643
1644 if( status != PSA_SUCCESS )
1645 psa_reset_key_attributes( attributes );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001646
Ronald Cron5c522922020-11-14 16:35:34 +01001647 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001648
Ronald Cron5c522922020-11-14 16:35:34 +01001649 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001650}
1651
Gilles Peskinec8000c02019-08-02 20:15:51 +02001652#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1653psa_status_t psa_get_key_slot_number(
1654 const psa_key_attributes_t *attributes,
1655 psa_key_slot_number_t *slot_number )
1656{
1657 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1658 {
1659 *slot_number = attributes->slot_number;
1660 return( PSA_SUCCESS );
1661 }
1662 else
1663 return( PSA_ERROR_INVALID_ARGUMENT );
1664}
1665#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1666
Steven Cooremana01795d2020-07-24 22:48:15 +02001667static psa_status_t psa_internal_export_key_buffer( const psa_key_slot_t *slot,
1668 uint8_t *data,
1669 size_t data_size,
1670 size_t *data_length )
1671{
1672 if( slot->data.key.bytes > data_size )
1673 return( PSA_ERROR_BUFFER_TOO_SMALL );
1674 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1675 memset( data + slot->data.key.bytes, 0,
1676 data_size - slot->data.key.bytes );
1677 *data_length = slot->data.key.bytes;
1678 return( PSA_SUCCESS );
1679}
1680
Gilles Peskinef603c712019-01-19 13:40:11 +01001681static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1682 uint8_t *data,
1683 size_t data_size,
1684 size_t *data_length,
1685 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001686{
Gilles Peskine5d309672019-07-12 23:47:28 +02001687#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1688 const psa_drv_se_t *drv;
1689 psa_drv_se_context_t *drv_context;
1690#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1691
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001692 *data_length = 0;
1693
Gilles Peskine8e338702019-07-30 20:06:31 +02001694 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001695 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001696
Gilles Peskinef9168942019-09-12 19:20:29 +02001697 /* Reject a zero-length output buffer now, since this can never be a
1698 * valid key representation. This way we know that data must be a valid
1699 * pointer and we can do things like memset(data, ..., data_size). */
1700 if( data_size == 0 )
1701 return( PSA_ERROR_BUFFER_TOO_SMALL );
1702
Gilles Peskine5d309672019-07-12 23:47:28 +02001703#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001704 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001705 {
1706 psa_drv_se_export_key_t method;
1707 if( drv->key_management == NULL )
1708 return( PSA_ERROR_NOT_SUPPORTED );
1709 method = ( export_public_key ?
1710 drv->key_management->p_export_public :
1711 drv->key_management->p_export );
1712 if( method == NULL )
1713 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001714 return( method( drv_context,
1715 slot->data.se.slot_number,
1716 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001717 }
1718#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1719
Gilles Peskine8e338702019-07-30 20:06:31 +02001720 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001721 {
Steven Cooremanacda8342020-07-24 23:09:52 +02001722 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001723 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001724 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1725 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Moran Peker17e36e12018-05-02 12:55:20 +03001726 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001727 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001728 {
Steven Cooreman560c28a2020-07-24 23:20:24 +02001729 /* Exporting public -> public */
1730 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1731 }
1732 else if( !export_public_key )
1733 {
1734 /* Exporting private -> private */
1735 return( psa_internal_export_key_buffer( slot, data, data_size, data_length ) );
1736 }
Steven Cooremanb9b84422020-10-14 14:39:20 +02001737
Steven Cooreman560c28a2020-07-24 23:20:24 +02001738 /* Need to export the public part of a private key,
Steven Cooremanb9b84422020-10-14 14:39:20 +02001739 * so conversion is needed. Try the accelerators first. */
1740 psa_status_t status = psa_driver_wrapper_export_public_key( slot,
1741 data,
1742 data_size,
1743 data_length );
1744
1745 if( status != PSA_ERROR_NOT_SUPPORTED ||
1746 psa_key_lifetime_is_external( slot->attr.lifetime ) )
1747 return( status );
1748
Steven Cooreman560c28a2020-07-24 23:20:24 +02001749 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
1750 {
John Durkop0e005192020-10-31 22:06:54 -07001751#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
1752 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001753 mbedtls_rsa_context *rsa = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001754 status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001755 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001756 slot->data.key.data,
1757 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001758 &rsa );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001759 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001760 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02001761
Steven Cooreman560c28a2020-07-24 23:20:24 +02001762 status = psa_export_rsa_key( PSA_KEY_TYPE_RSA_PUBLIC_KEY,
Steven Cooremana2371e52020-07-28 14:30:39 +02001763 rsa,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001764 data,
1765 data_size,
1766 data_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02001767
Steven Cooremana2371e52020-07-28 14:30:39 +02001768 mbedtls_rsa_free( rsa );
1769 mbedtls_free( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02001770
Steven Cooreman560c28a2020-07-24 23:20:24 +02001771 return( status );
Darryl Green9e2d7a02018-07-24 16:33:30 +01001772#else
Steven Cooreman560c28a2020-07-24 23:20:24 +02001773 /* We don't know how to convert a private RSA key to public. */
1774 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001775#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
1776 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine969ac722018-01-28 18:16:59 +01001777 }
1778 else
Moran Pekera998bc62018-04-16 18:16:20 +03001779 {
John Durkop6ba40d12020-11-10 08:50:04 -08001780#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) || \
1781 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY)
Steven Cooremana2371e52020-07-28 14:30:39 +02001782 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooremanb9b84422020-10-14 14:39:20 +02001783 status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02001784 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02001785 slot->data.key.data,
1786 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02001787 &ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001788 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02001789 return( status );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001790
1791 status = psa_export_ecp_key( PSA_KEY_TYPE_ECC_PUBLIC_KEY(
1792 PSA_KEY_TYPE_ECC_GET_FAMILY(
1793 slot->attr.type ) ),
Steven Cooremana2371e52020-07-28 14:30:39 +02001794 ecp,
Steven Cooreman560c28a2020-07-24 23:20:24 +02001795 data,
1796 data_size,
1797 data_length );
1798
Steven Cooremana2371e52020-07-28 14:30:39 +02001799 mbedtls_ecp_keypair_free( ecp );
1800 mbedtls_free( ecp );
Steven Cooreman560c28a2020-07-24 23:20:24 +02001801 return( status );
1802#else
1803 /* We don't know how to convert a private ECC key to public */
Moran Pekera998bc62018-04-16 18:16:20 +03001804 return( PSA_ERROR_NOT_SUPPORTED );
John Durkop9814fa22020-11-04 12:28:15 -08001805#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) ||
1806 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_PUBLIC_KEY) */
Moran Pekera998bc62018-04-16 18:16:20 +03001807 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001808 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02001809 else
1810 {
1811 /* This shouldn't happen in the reference implementation, but
1812 it is valid for a special-purpose implementation to omit
1813 support for exporting certain key types. */
1814 return( PSA_ERROR_NOT_SUPPORTED );
1815 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001816}
1817
Ronald Croncf56a0a2020-08-04 09:51:30 +02001818psa_status_t psa_export_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001819 uint8_t *data,
1820 size_t data_size,
1821 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001822{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001823 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001824 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001825 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001826
1827 /* Set the key to empty now, so that even when there are errors, we always
1828 * set data_length to a value between 0 and data_size. On error, setting
1829 * the key to empty is a good choice because an empty key representation is
1830 * unlikely to be accepted anywhere. */
1831 *data_length = 0;
1832
1833 /* Export requires the EXPORT flag. There is an exception for public keys,
Ronald Cron5c522922020-11-14 16:35:34 +01001834 * which don't require any flag, but
1835 * psa_get_and_lock_key_slot_with_policy() takes care of this.
1836 */
1837 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
1838 PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001839 if( status != PSA_SUCCESS )
1840 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001841
1842 status = psa_internal_export_key( slot, data, data_size, data_length, 0 );
Ronald Cron5c522922020-11-14 16:35:34 +01001843 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001844
Ronald Cron5c522922020-11-14 16:35:34 +01001845 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001846}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001847
Ronald Croncf56a0a2020-08-04 09:51:30 +02001848psa_status_t psa_export_public_key( mbedtls_svc_key_id_t key,
Gilles Peskine2d277862018-06-18 15:41:12 +02001849 uint8_t *data,
1850 size_t data_size,
1851 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001852{
Ronald Cronf95a2b12020-10-22 15:24:49 +02001853 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01001854 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01001855 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001856
1857 /* Set the key to empty now, so that even when there are errors, we always
1858 * set data_length to a value between 0 and data_size. On error, setting
1859 * the key to empty is a good choice because an empty key representation is
1860 * unlikely to be accepted anywhere. */
1861 *data_length = 0;
1862
1863 /* Exporting a public key doesn't require a usage flag. */
Ronald Cron5c522922020-11-14 16:35:34 +01001864 status = psa_get_and_lock_key_slot_with_policy( key, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001865 if( status != PSA_SUCCESS )
1866 return( status );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001867
1868 status = psa_internal_export_key( slot, data, data_size, data_length, 1 );
Ronald Cron5c522922020-11-14 16:35:34 +01001869 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02001870
Ronald Cron5c522922020-11-14 16:35:34 +01001871 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001872}
1873
Gilles Peskine91e8c332019-08-02 19:19:39 +02001874#if defined(static_assert)
1875static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1876 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001877static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001878 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001879static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001880 "One or more key attribute flag is listed as both internal-only and external-only" );
1881#endif
1882
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001883/** Validate that a key policy is internally well-formed.
1884 *
1885 * This function only rejects invalid policies. It does not validate the
1886 * consistency of the policy with respect to other attributes of the key
1887 * such as the key type.
1888 */
1889static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001890{
1891 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001892 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 PSA_KEY_USAGE_ENCRYPT |
1894 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001895 PSA_KEY_USAGE_SIGN_HASH |
1896 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001897 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1898 return( PSA_ERROR_INVALID_ARGUMENT );
1899
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 return( PSA_SUCCESS );
1901}
1902
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001903/** Validate the internal consistency of key attributes.
1904 *
1905 * This function only rejects invalid attribute values. If does not
1906 * validate the consistency of the attributes with any key data that may
1907 * be involved in the creation of the key.
1908 *
1909 * Call this function early in the key creation process.
1910 *
1911 * \param[in] attributes Key attributes for the new key.
1912 * \param[out] p_drv On any return, the driver for the key, if any.
1913 * NULL for a transparent key.
1914 *
1915 */
1916static psa_status_t psa_validate_key_attributes(
1917 const psa_key_attributes_t *attributes,
1918 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001919{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001920 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Ronald Crond2ed4812020-07-17 16:11:30 +02001921 psa_key_lifetime_t lifetime = psa_get_key_lifetime( attributes );
Ronald Cron65f38a32020-10-23 17:11:13 +02001922 mbedtls_svc_key_id_t key = psa_get_key_id( attributes );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001923
Ronald Cron54b90082020-10-29 15:26:43 +01001924 status = psa_validate_key_location( lifetime, p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001925 if( status != PSA_SUCCESS )
1926 return( status );
1927
Ronald Crond2ed4812020-07-17 16:11:30 +02001928 status = psa_validate_key_persistence( lifetime );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001929 if( status != PSA_SUCCESS )
1930 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001931
Ronald Cron65f38a32020-10-23 17:11:13 +02001932 if ( PSA_KEY_LIFETIME_IS_VOLATILE( lifetime ) )
1933 {
1934 if( MBEDTLS_SVC_KEY_ID_GET_KEY_ID( key ) != 0 )
1935 return( PSA_ERROR_INVALID_ARGUMENT );
1936 }
1937 else
Ronald Crond2ed4812020-07-17 16:11:30 +02001938 {
Ronald Croncbd7bea2020-11-11 14:57:44 +01001939 status = psa_validate_key_id( psa_get_key_id( attributes ), 0 );
Ronald Crond2ed4812020-07-17 16:11:30 +02001940 if( status != PSA_SUCCESS )
1941 return( status );
1942 }
1943
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001944 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001945 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001946 return( status );
1947
1948 /* Refuse to create overly large keys.
1949 * Note that this doesn't trigger on import if the attributes don't
1950 * explicitly specify a size (so psa_get_key_bits returns 0), so
1951 * psa_import_key() needs its own checks. */
1952 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1953 return( PSA_ERROR_NOT_SUPPORTED );
1954
Gilles Peskine91e8c332019-08-02 19:19:39 +02001955 /* Reject invalid flags. These should not be reachable through the API. */
1956 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1957 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1958 return( PSA_ERROR_INVALID_ARGUMENT );
1959
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001960 return( PSA_SUCCESS );
1961}
1962
Gilles Peskine4747d192019-04-17 15:05:45 +02001963/** Prepare a key slot to receive key material.
1964 *
1965 * This function allocates a key slot and sets its metadata.
1966 *
1967 * If this function fails, call psa_fail_key_creation().
1968 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001969 * This function is intended to be used as follows:
1970 * -# Call psa_start_key_creation() to allocate a key slot, prepare
Ronald Croncf56a0a2020-08-04 09:51:30 +02001971 * it with the specified attributes, and in case of a volatile key assign it
1972 * a volatile key identifier.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001973 * -# Populate the slot with the key material.
1974 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1975 * In case of failure at any step, stop the sequence and call
1976 * psa_fail_key_creation().
1977 *
Ronald Cron5c522922020-11-14 16:35:34 +01001978 * On success, the key slot is locked. It is the responsibility of the caller
1979 * to unlock the key slot when it does not access it anymore.
Ronald Cronf95a2b12020-10-22 15:24:49 +02001980 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001981 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001982 * \param[in] attributes Key attributes for the new key.
Gilles Peskine011e4282019-06-26 18:34:38 +02001983 * \param[out] p_slot On success, a pointer to the prepared slot.
1984 * \param[out] p_drv On any return, the driver for the key, if any.
1985 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001986 *
1987 * \retval #PSA_SUCCESS
1988 * The key slot is ready to receive key material.
1989 * \return If this function fails, the key slot is an invalid state.
1990 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001991 */
1992static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001993 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001994 const psa_key_attributes_t *attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001995 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001996 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001997{
1998 psa_status_t status;
Ronald Cron2a993152020-07-17 14:13:26 +02001999 psa_key_id_t volatile_key_id;
Gilles Peskine4747d192019-04-17 15:05:45 +02002000 psa_key_slot_t *slot;
2001
Gilles Peskinedf179142019-07-15 22:02:14 +02002002 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02002003 *p_drv = NULL;
2004
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002005 status = psa_validate_key_attributes( attributes, p_drv );
2006 if( status != PSA_SUCCESS )
2007 return( status );
2008
Ronald Cronc4d1b512020-07-31 11:26:37 +02002009 status = psa_get_empty_key_slot( &volatile_key_id, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02002010 if( status != PSA_SUCCESS )
2011 return( status );
2012 slot = *p_slot;
2013
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002014 /* We're storing the declared bit-size of the key. It's up to each
2015 * creation mechanism to verify that this information is correct.
2016 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02002017 * an input (generate, device) but not for those where the bit-size
Ronald Cronc4d1b512020-07-31 11:26:37 +02002018 * is optional (import, copy). In case of a volatile key, assign it the
2019 * volatile key identifier associated to the slot returned to contain its
2020 * definition. */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002021
2022 slot->attr = attributes->core;
Ronald Cronc4d1b512020-07-31 11:26:37 +02002023 if( PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
2024 {
2025#if !defined(MBEDTLS_PSA_CRYPTO_KEY_ID_ENCODES_OWNER)
2026 slot->attr.id = volatile_key_id;
2027#else
2028 slot->attr.id.key_id = volatile_key_id;
2029#endif
2030 }
Gilles Peskinec744d992019-07-30 17:26:54 +02002031
Gilles Peskine91e8c332019-08-02 19:19:39 +02002032 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02002033 * external-only flags, query `attributes`. Thanks to the check
2034 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02002035 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02002036 * may have set. */
2037 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02002038
Gilles Peskinecbaff462019-07-12 23:46:04 +02002039#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002040 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002041 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02002042 * create the key file in internal storage, create the
2043 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002044 * persistent data. This is done by starting a transaction that will
2045 * encompass these three actions.
2046 * For registering a volatile key, we just need to find an appropriate
2047 * slot number inside the SE. Since the key is designated volatile, creating
2048 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02002049 /* The first thing to do is to find a slot number for the new key.
2050 * We save the slot number in persistent storage as part of the
2051 * transaction data. It will be needed to recover if the power
2052 * fails during the key creation process, to clean up on the secure
2053 * element side after restarting. Obtaining a slot number from the
2054 * secure element driver updates its persistent state, but we do not yet
2055 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02002056 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002057 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00002058 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02002059 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02002060 &slot->data.se.slot_number );
2061 if( status != PSA_SUCCESS )
2062 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002063
2064 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02002065 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002066 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
2067 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
2068 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
2069 psa_crypto_transaction.key.id = slot->attr.id;
2070 status = psa_crypto_save_transaction( );
2071 if( status != PSA_SUCCESS )
2072 {
2073 (void) psa_crypto_stop_transaction( );
2074 return( status );
2075 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02002076 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002077 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02002078
2079 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
2080 {
2081 /* Key registration only makes sense with a secure element. */
2082 return( PSA_ERROR_INVALID_ARGUMENT );
2083 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02002084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2085
Ronald Cronc4d1b512020-07-31 11:26:37 +02002086 return( PSA_SUCCESS );
Darryl Green0c6575a2018-11-07 16:05:30 +00002087}
Gilles Peskine4747d192019-04-17 15:05:45 +02002088
2089/** Finalize the creation of a key once its key material has been set.
2090 *
2091 * This entails writing the key to persistent storage.
2092 *
2093 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002094 * See the documentation of psa_start_key_creation() for the intended use
2095 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002096 *
Ronald Cron5c522922020-11-14 16:35:34 +01002097 * If the finalization succeeds, the function unlocks the key slot (it was
2098 * locked by psa_start_key_creation()) and the key slot cannot be accessed
2099 * anymore as part of the key creation process.
Ronald Cron50972942020-11-14 11:28:25 +01002100 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002101 * \param[in,out] slot Pointer to the slot with key material.
2102 * \param[in] driver The secure element driver for the key,
2103 * or NULL for a transparent key.
Ronald Cron81709fc2020-11-14 12:10:32 +01002104 * \param[out] key On success, identifier of the key. Note that the
2105 * key identifier is also stored in the key slot.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002106 *
2107 * \retval #PSA_SUCCESS
Ronald Croncf56a0a2020-08-04 09:51:30 +02002108 * The key was successfully created.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002109 * \return If this function fails, the key slot is an invalid state.
2110 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02002111 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002112static psa_status_t psa_finish_key_creation(
2113 psa_key_slot_t *slot,
Ronald Cron81709fc2020-11-14 12:10:32 +01002114 psa_se_drv_table_entry_t *driver,
2115 mbedtls_svc_key_id_t *key)
Gilles Peskine4747d192019-04-17 15:05:45 +02002116{
2117 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02002118 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02002119 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02002120
2121#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02002122 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02002123 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02002124#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2125 if( driver != NULL )
2126 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002127 psa_se_key_data_storage_t data;
2128#if defined(static_assert)
2129 static_assert( sizeof( slot->data.se.slot_number ) ==
2130 sizeof( data.slot_number ),
2131 "Slot number size does not match psa_se_key_data_storage_t" );
Gilles Peskineb46bef22019-07-30 21:32:04 +02002132#endif
2133 memcpy( &data.slot_number, &slot->data.se.slot_number,
2134 sizeof( slot->data.se.slot_number ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02002135 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002136 (uint8_t*) &data,
2137 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002138 }
2139 else
2140#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2141 {
Steven Cooreman40120f62020-10-29 11:42:22 +01002142 /* Key material is saved in export representation in the slot, so
2143 * just pass the slot buffer for storage. */
2144 status = psa_save_persistent_key( &slot->attr,
2145 slot->data.key.data,
2146 slot->data.key.bytes );
Gilles Peskine1df83d42019-07-23 16:13:14 +02002147 }
Gilles Peskine4747d192019-04-17 15:05:45 +02002148 }
Darryl Green0c6575a2018-11-07 16:05:30 +00002149#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
2150
Gilles Peskinecbaff462019-07-12 23:46:04 +02002151#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02002152 /* Finish the transaction for a key creation. This does not
2153 * happen when registering an existing key. Detect this case
2154 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02002155 * creation of a persistent key in a secure element requires a transaction,
2156 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02002157 if( driver != NULL &&
2158 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02002159 {
2160 status = psa_save_se_persistent_data( driver );
2161 if( status != PSA_SUCCESS )
2162 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002163 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002164 return( status );
2165 }
Gilles Peskinefc762652019-07-22 19:30:34 +02002166 status = psa_crypto_stop_transaction( );
Gilles Peskinecbaff462019-07-12 23:46:04 +02002167 }
2168#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2169
Ronald Cron50972942020-11-14 11:28:25 +01002170 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01002171 {
2172 *key = slot->attr.id;
Ronald Cron5c522922020-11-14 16:35:34 +01002173 status = psa_unlock_key_slot( slot );
Ronald Cron81709fc2020-11-14 12:10:32 +01002174 if( status != PSA_SUCCESS )
2175 *key = MBEDTLS_SVC_KEY_ID_INIT;
2176 }
Ronald Cron50972942020-11-14 11:28:25 +01002177
Gilles Peskine4747d192019-04-17 15:05:45 +02002178 return( status );
2179}
2180
2181/** Abort the creation of a key.
2182 *
2183 * You may call this function after calling psa_start_key_creation(),
2184 * or after psa_finish_key_creation() fails. In other circumstances, this
2185 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02002186 * See the documentation of psa_start_key_creation() for the intended use
2187 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02002188 *
Gilles Peskine011e4282019-06-26 18:34:38 +02002189 * \param[in,out] slot Pointer to the slot with key material.
2190 * \param[in] driver The secure element driver for the key,
2191 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02002192 */
Gilles Peskine011e4282019-06-26 18:34:38 +02002193static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002194 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02002195{
Gilles Peskine011e4282019-06-26 18:34:38 +02002196 (void) driver;
2197
Gilles Peskine4747d192019-04-17 15:05:45 +02002198 if( slot == NULL )
2199 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02002200
2201#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01002202 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02002203 * element, and the failure happened later (when saving metadata
2204 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02002205 * element.
2206 * https://github.com/ARMmbed/mbed-crypto/issues/217
2207 */
Gilles Peskinefc762652019-07-22 19:30:34 +02002208
Gilles Peskined7729582019-08-05 15:55:54 +02002209 /* Abort the ongoing transaction if any (there may not be one if
2210 * the creation process failed before starting one, or if the
2211 * key creation is a registration of a key in a secure element).
2212 * Earlier functions must already have done what it takes to undo any
2213 * partial creation. All that's left is to update the transaction data
2214 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02002215 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02002216#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2217
Gilles Peskine4747d192019-04-17 15:05:45 +02002218 psa_wipe_key_slot( slot );
2219}
2220
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002221/** Validate optional attributes during key creation.
2222 *
2223 * Some key attributes are optional during key creation. If they are
2224 * specified in the attributes structure, check that they are consistent
2225 * with the data in the slot.
2226 *
2227 * This function should be called near the end of key creation, after
2228 * the slot in memory is fully populated but before saving persistent data.
2229 */
2230static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002231 const psa_key_slot_t *slot,
2232 const psa_key_attributes_t *attributes )
2233{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002234 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002235 {
Gilles Peskine8e338702019-07-30 20:06:31 +02002236 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002237 return( PSA_ERROR_INVALID_ARGUMENT );
2238 }
2239
2240 if( attributes->domain_parameters_size != 0 )
2241 {
John Durkop0e005192020-10-31 22:06:54 -07002242#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) || \
2243 defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY)
Gilles Peskine8e338702019-07-30 20:06:31 +02002244 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002245 {
Steven Cooremana2371e52020-07-28 14:30:39 +02002246 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman75b74362020-07-28 14:30:13 +02002247 mbedtls_mpi actual, required;
Steven Cooreman6d839f02020-07-30 11:36:45 +02002248 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooremana01795d2020-07-24 22:48:15 +02002249
Steven Cooreman7f391872020-07-30 14:57:44 +02002250 psa_status_t status = psa_load_rsa_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02002251 slot->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02002252 slot->data.key.data,
2253 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02002254 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02002255 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02002256 return( status );
Steven Cooreman75b74362020-07-28 14:30:13 +02002257
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002258 mbedtls_mpi_init( &actual );
2259 mbedtls_mpi_init( &required );
Steven Cooremana2371e52020-07-28 14:30:39 +02002260 ret = mbedtls_rsa_export( rsa,
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002261 NULL, NULL, NULL, NULL, &actual );
Steven Cooremana2371e52020-07-28 14:30:39 +02002262 mbedtls_rsa_free( rsa );
2263 mbedtls_free( rsa );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002264 if( ret != 0 )
2265 goto rsa_exit;
2266 ret = mbedtls_mpi_read_binary( &required,
2267 attributes->domain_parameters,
2268 attributes->domain_parameters_size );
2269 if( ret != 0 )
2270 goto rsa_exit;
2271 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
2272 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
2273 rsa_exit:
2274 mbedtls_mpi_free( &actual );
2275 mbedtls_mpi_free( &required );
2276 if( ret != 0)
2277 return( mbedtls_to_psa_error( ret ) );
2278 }
2279 else
John Durkop9814fa22020-11-04 12:28:15 -08002280#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) ||
2281 * defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_PUBLIC_KEY) */
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002282 {
2283 return( PSA_ERROR_INVALID_ARGUMENT );
2284 }
2285 }
2286
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002287 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002288 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02002289 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002290 return( PSA_ERROR_INVALID_ARGUMENT );
2291 }
2292
2293 return( PSA_SUCCESS );
2294}
2295
Gilles Peskine4747d192019-04-17 15:05:45 +02002296psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02002297 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02002298 size_t data_length,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002299 mbedtls_svc_key_id_t *key )
Gilles Peskine4747d192019-04-17 15:05:45 +02002300{
2301 psa_status_t status;
2302 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002303 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002304
Ronald Cron81709fc2020-11-14 12:10:32 +01002305 *key = MBEDTLS_SVC_KEY_ID_INIT;
2306
Gilles Peskine0f84d622019-09-12 19:03:13 +02002307 /* Reject zero-length symmetric keys (including raw data key objects).
2308 * This also rejects any key which might be encoded as an empty string,
2309 * which is never valid. */
2310 if( data_length == 0 )
2311 return( PSA_ERROR_INVALID_ARGUMENT );
2312
Gilles Peskinedf179142019-07-15 22:02:14 +02002313 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002314 &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002315 if( status != PSA_SUCCESS )
2316 goto exit;
2317
Gilles Peskine5d309672019-07-12 23:47:28 +02002318#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2319 if( driver != NULL )
2320 {
2321 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01002322 /* The driver should set the number of key bits, however in
2323 * case it doesn't, we initialize bits to an invalid value. */
2324 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02002325 if( drv->key_management == NULL ||
2326 drv->key_management->p_import == NULL )
2327 {
2328 status = PSA_ERROR_NOT_SUPPORTED;
2329 goto exit;
2330 }
2331 status = drv->key_management->p_import(
2332 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02002333 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02002334 &bits );
2335 if( status != PSA_SUCCESS )
2336 goto exit;
2337 if( bits > PSA_MAX_KEY_BITS )
2338 {
2339 status = PSA_ERROR_NOT_SUPPORTED;
2340 goto exit;
2341 }
2342 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02002343 }
2344 else
2345#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2346 {
2347 status = psa_import_key_into_slot( slot, data, data_length );
2348 if( status != PSA_SUCCESS )
2349 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02002350 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002351 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02002352 if( status != PSA_SUCCESS )
2353 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002354
Ronald Cron81709fc2020-11-14 12:10:32 +01002355 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002356exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02002357 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002358 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002359
Gilles Peskine4747d192019-04-17 15:05:45 +02002360 return( status );
2361}
2362
Gilles Peskined7729582019-08-05 15:55:54 +02002363#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2364psa_status_t mbedtls_psa_register_se_key(
2365 const psa_key_attributes_t *attributes )
2366{
2367 psa_status_t status;
2368 psa_key_slot_t *slot = NULL;
2369 psa_se_drv_table_entry_t *driver = NULL;
Ronald Croncf56a0a2020-08-04 09:51:30 +02002370 mbedtls_svc_key_id_t key = MBEDTLS_SVC_KEY_ID_INIT;
Gilles Peskined7729582019-08-05 15:55:54 +02002371
2372 /* Leaving attributes unspecified is not currently supported.
2373 * It could make sense to query the key type and size from the
2374 * secure element, but not all secure elements support this
2375 * and the driver HAL doesn't currently support it. */
2376 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
2377 return( PSA_ERROR_NOT_SUPPORTED );
2378 if( psa_get_key_bits( attributes ) == 0 )
2379 return( PSA_ERROR_NOT_SUPPORTED );
2380
2381 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
Ronald Cron81709fc2020-11-14 12:10:32 +01002382 &slot, &driver );
Gilles Peskined7729582019-08-05 15:55:54 +02002383 if( status != PSA_SUCCESS )
2384 goto exit;
2385
Ronald Cron81709fc2020-11-14 12:10:32 +01002386 status = psa_finish_key_creation( slot, driver, &key );
Gilles Peskined7729582019-08-05 15:55:54 +02002387
2388exit:
2389 if( status != PSA_SUCCESS )
Gilles Peskined7729582019-08-05 15:55:54 +02002390 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002391
Gilles Peskined7729582019-08-05 15:55:54 +02002392 /* Registration doesn't keep the key in RAM. */
Ronald Croncf56a0a2020-08-04 09:51:30 +02002393 psa_close_key( key );
Gilles Peskined7729582019-08-05 15:55:54 +02002394 return( status );
2395}
2396#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
2397
Gilles Peskinef603c712019-01-19 13:40:11 +01002398static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002399 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01002400{
Steven Cooremanf7cebd42020-10-13 20:27:40 +02002401 psa_status_t status = psa_copy_key_material_into_slot( target,
2402 source->data.key.data,
2403 source->data.key.bytes );
Gilles Peskinef603c712019-01-19 13:40:11 +01002404 if( status != PSA_SUCCESS )
Steven Cooreman398aee52020-10-13 14:35:45 +02002405 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002406
Steven Cooreman398aee52020-10-13 14:35:45 +02002407 target->attr.type = source->attr.type;
2408 target->attr.bits = source->attr.bits;
2409
2410 return( PSA_SUCCESS );
Gilles Peskinef603c712019-01-19 13:40:11 +01002411}
2412
Ronald Croncf56a0a2020-08-04 09:51:30 +02002413psa_status_t psa_copy_key( mbedtls_svc_key_id_t source_key,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002414 const psa_key_attributes_t *specified_attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02002415 mbedtls_svc_key_id_t *target_key )
Gilles Peskinef603c712019-01-19 13:40:11 +01002416{
Ronald Cronf95a2b12020-10-22 15:24:49 +02002417 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01002418 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef603c712019-01-19 13:40:11 +01002419 psa_key_slot_t *source_slot = NULL;
2420 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002421 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02002422 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01002423
Ronald Cron81709fc2020-11-14 12:10:32 +01002424 *target_key = MBEDTLS_SVC_KEY_ID_INIT;
2425
Ronald Cron5c522922020-11-14 16:35:34 +01002426 status = psa_get_and_lock_transparent_key_slot_with_policy(
2427 source_key, &source_slot, PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01002428 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002429 goto exit;
2430
Gilles Peskine1b8594a2019-07-31 17:21:46 +02002431 status = psa_validate_optional_attributes( source_slot,
2432 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002433 if( status != PSA_SUCCESS )
2434 goto exit;
2435
Gilles Peskine7e0cff92019-07-30 13:48:52 +02002436 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02002437 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002438 if( status != PSA_SUCCESS )
2439 goto exit;
2440
Ronald Cron81709fc2020-11-14 12:10:32 +01002441 status = psa_start_key_creation( PSA_KEY_CREATION_COPY, &actual_attributes,
2442 &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002443 if( status != PSA_SUCCESS )
2444 goto exit;
2445
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002446#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2447 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002448 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002449 /* Copying to a secure element is not implemented yet. */
2450 status = PSA_ERROR_NOT_SUPPORTED;
2451 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002452 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002453#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002454
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002455 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002456 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002457 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002458
Ronald Cron81709fc2020-11-14 12:10:32 +01002459 status = psa_finish_key_creation( target_slot, driver, target_key );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002460exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002461 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02002462 psa_fail_key_creation( target_slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002463
Ronald Cron5c522922020-11-14 16:35:34 +01002464 unlock_status = psa_unlock_key_slot( source_slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02002465
Ronald Cron5c522922020-11-14 16:35:34 +01002466 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002467}
2468
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002469
2470
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002471/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002472/* Message digests */
2473/****************************************************************/
2474
John Durkop07cc04a2020-11-16 22:08:34 -08002475#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
John Durkop0e005192020-10-31 22:06:54 -07002476 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) || \
2477 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) || \
John Durkop9814fa22020-11-04 12:28:15 -08002478 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002479static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002480{
2481 switch( alg )
2482 {
2483#if defined(MBEDTLS_MD2_C)
2484 case PSA_ALG_MD2:
2485 return( &mbedtls_md2_info );
2486#endif
2487#if defined(MBEDTLS_MD4_C)
2488 case PSA_ALG_MD4:
2489 return( &mbedtls_md4_info );
2490#endif
2491#if defined(MBEDTLS_MD5_C)
2492 case PSA_ALG_MD5:
2493 return( &mbedtls_md5_info );
2494#endif
2495#if defined(MBEDTLS_RIPEMD160_C)
2496 case PSA_ALG_RIPEMD160:
2497 return( &mbedtls_ripemd160_info );
2498#endif
2499#if defined(MBEDTLS_SHA1_C)
2500 case PSA_ALG_SHA_1:
2501 return( &mbedtls_sha1_info );
2502#endif
2503#if defined(MBEDTLS_SHA256_C)
2504 case PSA_ALG_SHA_224:
2505 return( &mbedtls_sha224_info );
2506 case PSA_ALG_SHA_256:
2507 return( &mbedtls_sha256_info );
2508#endif
2509#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002510#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002511 case PSA_ALG_SHA_384:
2512 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002513#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002514 case PSA_ALG_SHA_512:
2515 return( &mbedtls_sha512_info );
2516#endif
2517 default:
2518 return( NULL );
2519 }
2520}
John Durkop07cc04a2020-11-16 22:08:34 -08002521#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
John Durkop9814fa22020-11-04 12:28:15 -08002522 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) ||
2523 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) ||
2524 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskine20035e32018-02-03 22:44:14 +01002525
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002526psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2527{
2528 switch( operation->alg )
2529 {
Gilles Peskine81736312018-06-26 15:04:31 +02002530 case 0:
2531 /* The object has (apparently) been initialized but it is not
2532 * in use. It's ok to call abort on such an object, and there's
2533 * nothing to do. */
2534 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002535#if defined(MBEDTLS_MD2_C)
2536 case PSA_ALG_MD2:
2537 mbedtls_md2_free( &operation->ctx.md2 );
2538 break;
2539#endif
2540#if defined(MBEDTLS_MD4_C)
2541 case PSA_ALG_MD4:
2542 mbedtls_md4_free( &operation->ctx.md4 );
2543 break;
2544#endif
2545#if defined(MBEDTLS_MD5_C)
2546 case PSA_ALG_MD5:
2547 mbedtls_md5_free( &operation->ctx.md5 );
2548 break;
2549#endif
2550#if defined(MBEDTLS_RIPEMD160_C)
2551 case PSA_ALG_RIPEMD160:
2552 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2553 break;
2554#endif
2555#if defined(MBEDTLS_SHA1_C)
2556 case PSA_ALG_SHA_1:
2557 mbedtls_sha1_free( &operation->ctx.sha1 );
2558 break;
2559#endif
2560#if defined(MBEDTLS_SHA256_C)
2561 case PSA_ALG_SHA_224:
2562 case PSA_ALG_SHA_256:
2563 mbedtls_sha256_free( &operation->ctx.sha256 );
2564 break;
2565#endif
2566#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002567#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002568 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002569#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002570 case PSA_ALG_SHA_512:
2571 mbedtls_sha512_free( &operation->ctx.sha512 );
2572 break;
2573#endif
2574 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002575 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002576 }
2577 operation->alg = 0;
2578 return( PSA_SUCCESS );
2579}
2580
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002581psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002582 psa_algorithm_t alg )
2583{
Janos Follath24eed8d2019-11-22 13:21:35 +00002584 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002585
2586 /* A context must be freshly initialized before it can be set up. */
2587 if( operation->alg != 0 )
2588 {
2589 return( PSA_ERROR_BAD_STATE );
2590 }
2591
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002592 switch( alg )
2593 {
2594#if defined(MBEDTLS_MD2_C)
2595 case PSA_ALG_MD2:
2596 mbedtls_md2_init( &operation->ctx.md2 );
2597 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2598 break;
2599#endif
2600#if defined(MBEDTLS_MD4_C)
2601 case PSA_ALG_MD4:
2602 mbedtls_md4_init( &operation->ctx.md4 );
2603 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2604 break;
2605#endif
2606#if defined(MBEDTLS_MD5_C)
2607 case PSA_ALG_MD5:
2608 mbedtls_md5_init( &operation->ctx.md5 );
2609 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2610 break;
2611#endif
2612#if defined(MBEDTLS_RIPEMD160_C)
2613 case PSA_ALG_RIPEMD160:
2614 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2615 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2616 break;
2617#endif
2618#if defined(MBEDTLS_SHA1_C)
2619 case PSA_ALG_SHA_1:
2620 mbedtls_sha1_init( &operation->ctx.sha1 );
2621 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2622 break;
2623#endif
2624#if defined(MBEDTLS_SHA256_C)
2625 case PSA_ALG_SHA_224:
2626 mbedtls_sha256_init( &operation->ctx.sha256 );
2627 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2628 break;
2629 case PSA_ALG_SHA_256:
2630 mbedtls_sha256_init( &operation->ctx.sha256 );
2631 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2632 break;
2633#endif
2634#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002635#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002636 case PSA_ALG_SHA_384:
2637 mbedtls_sha512_init( &operation->ctx.sha512 );
2638 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2639 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002640#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002641 case PSA_ALG_SHA_512:
2642 mbedtls_sha512_init( &operation->ctx.sha512 );
2643 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2644 break;
2645#endif
2646 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002647 return( PSA_ALG_IS_HASH( alg ) ?
2648 PSA_ERROR_NOT_SUPPORTED :
2649 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002650 }
2651 if( ret == 0 )
2652 operation->alg = alg;
2653 else
2654 psa_hash_abort( operation );
2655 return( mbedtls_to_psa_error( ret ) );
2656}
2657
2658psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2659 const uint8_t *input,
2660 size_t input_length )
2661{
Janos Follath24eed8d2019-11-22 13:21:35 +00002662 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002663
2664 /* Don't require hash implementations to behave correctly on a
2665 * zero-length input, which may have an invalid pointer. */
2666 if( input_length == 0 )
2667 return( PSA_SUCCESS );
2668
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002669 switch( operation->alg )
2670 {
2671#if defined(MBEDTLS_MD2_C)
2672 case PSA_ALG_MD2:
2673 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2674 input, input_length );
2675 break;
2676#endif
2677#if defined(MBEDTLS_MD4_C)
2678 case PSA_ALG_MD4:
2679 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2680 input, input_length );
2681 break;
2682#endif
2683#if defined(MBEDTLS_MD5_C)
2684 case PSA_ALG_MD5:
2685 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2686 input, input_length );
2687 break;
2688#endif
2689#if defined(MBEDTLS_RIPEMD160_C)
2690 case PSA_ALG_RIPEMD160:
2691 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2692 input, input_length );
2693 break;
2694#endif
2695#if defined(MBEDTLS_SHA1_C)
2696 case PSA_ALG_SHA_1:
2697 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2698 input, input_length );
2699 break;
2700#endif
2701#if defined(MBEDTLS_SHA256_C)
2702 case PSA_ALG_SHA_224:
2703 case PSA_ALG_SHA_256:
2704 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2705 input, input_length );
2706 break;
2707#endif
2708#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002709#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002710 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002711#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002712 case PSA_ALG_SHA_512:
2713 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2714 input, input_length );
2715 break;
2716#endif
2717 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002718 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002719 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002720
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002721 if( ret != 0 )
2722 psa_hash_abort( operation );
2723 return( mbedtls_to_psa_error( ret ) );
2724}
2725
2726psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2727 uint8_t *hash,
2728 size_t hash_size,
2729 size_t *hash_length )
2730{
itayzafrir40835d42018-08-02 13:14:17 +03002731 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002732 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002733 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002734
2735 /* Fill the output buffer with something that isn't a valid hash
2736 * (barring an attack on the hash and deliberately-crafted input),
2737 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002738 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002739 /* If hash_size is 0 then hash may be NULL and then the
2740 * call to memset would have undefined behavior. */
2741 if( hash_size != 0 )
2742 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002743
2744 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002745 {
2746 status = PSA_ERROR_BUFFER_TOO_SMALL;
2747 goto exit;
2748 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002749
2750 switch( operation->alg )
2751 {
2752#if defined(MBEDTLS_MD2_C)
2753 case PSA_ALG_MD2:
2754 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2755 break;
2756#endif
2757#if defined(MBEDTLS_MD4_C)
2758 case PSA_ALG_MD4:
2759 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2760 break;
2761#endif
2762#if defined(MBEDTLS_MD5_C)
2763 case PSA_ALG_MD5:
2764 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2765 break;
2766#endif
2767#if defined(MBEDTLS_RIPEMD160_C)
2768 case PSA_ALG_RIPEMD160:
2769 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2770 break;
2771#endif
2772#if defined(MBEDTLS_SHA1_C)
2773 case PSA_ALG_SHA_1:
2774 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2775 break;
2776#endif
2777#if defined(MBEDTLS_SHA256_C)
2778 case PSA_ALG_SHA_224:
2779 case PSA_ALG_SHA_256:
2780 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2781 break;
2782#endif
2783#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002784#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002785 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002786#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002787 case PSA_ALG_SHA_512:
2788 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2789 break;
2790#endif
2791 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002792 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002793 }
itayzafrir40835d42018-08-02 13:14:17 +03002794 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002795
itayzafrir40835d42018-08-02 13:14:17 +03002796exit:
2797 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002798 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002799 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002800 return( psa_hash_abort( operation ) );
2801 }
2802 else
2803 {
2804 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002805 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002806 }
2807}
2808
Gilles Peskine2d277862018-06-18 15:41:12 +02002809psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2810 const uint8_t *hash,
2811 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002812{
2813 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2814 size_t actual_hash_length;
2815 psa_status_t status = psa_hash_finish( operation,
2816 actual_hash, sizeof( actual_hash ),
2817 &actual_hash_length );
2818 if( status != PSA_SUCCESS )
2819 return( status );
2820 if( actual_hash_length != hash_length )
2821 return( PSA_ERROR_INVALID_SIGNATURE );
2822 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2823 return( PSA_ERROR_INVALID_SIGNATURE );
2824 return( PSA_SUCCESS );
2825}
2826
Gilles Peskine0a749c82019-11-28 19:33:58 +01002827psa_status_t psa_hash_compute( psa_algorithm_t alg,
2828 const uint8_t *input, size_t input_length,
2829 uint8_t *hash, size_t hash_size,
2830 size_t *hash_length )
2831{
2832 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2833 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2834
2835 *hash_length = hash_size;
2836 status = psa_hash_setup( &operation, alg );
2837 if( status != PSA_SUCCESS )
2838 goto exit;
2839 status = psa_hash_update( &operation, input, input_length );
2840 if( status != PSA_SUCCESS )
2841 goto exit;
2842 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2843 if( status != PSA_SUCCESS )
2844 goto exit;
2845
2846exit:
2847 if( status == PSA_SUCCESS )
2848 status = psa_hash_abort( &operation );
2849 else
2850 psa_hash_abort( &operation );
2851 return( status );
2852}
2853
2854psa_status_t psa_hash_compare( psa_algorithm_t alg,
2855 const uint8_t *input, size_t input_length,
2856 const uint8_t *hash, size_t hash_length )
2857{
2858 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2859 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2860
2861 status = psa_hash_setup( &operation, alg );
2862 if( status != PSA_SUCCESS )
2863 goto exit;
2864 status = psa_hash_update( &operation, input, input_length );
2865 if( status != PSA_SUCCESS )
2866 goto exit;
2867 status = psa_hash_verify( &operation, hash, hash_length );
2868 if( status != PSA_SUCCESS )
2869 goto exit;
2870
2871exit:
2872 if( status == PSA_SUCCESS )
2873 status = psa_hash_abort( &operation );
2874 else
2875 psa_hash_abort( &operation );
2876 return( status );
2877}
2878
Gilles Peskineeb35d782019-01-22 17:56:16 +01002879psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2880 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002881{
2882 if( target_operation->alg != 0 )
2883 return( PSA_ERROR_BAD_STATE );
2884
2885 switch( source_operation->alg )
2886 {
2887 case 0:
2888 return( PSA_ERROR_BAD_STATE );
2889#if defined(MBEDTLS_MD2_C)
2890 case PSA_ALG_MD2:
2891 mbedtls_md2_clone( &target_operation->ctx.md2,
2892 &source_operation->ctx.md2 );
2893 break;
2894#endif
2895#if defined(MBEDTLS_MD4_C)
2896 case PSA_ALG_MD4:
2897 mbedtls_md4_clone( &target_operation->ctx.md4,
2898 &source_operation->ctx.md4 );
2899 break;
2900#endif
2901#if defined(MBEDTLS_MD5_C)
2902 case PSA_ALG_MD5:
2903 mbedtls_md5_clone( &target_operation->ctx.md5,
2904 &source_operation->ctx.md5 );
2905 break;
2906#endif
2907#if defined(MBEDTLS_RIPEMD160_C)
2908 case PSA_ALG_RIPEMD160:
2909 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2910 &source_operation->ctx.ripemd160 );
2911 break;
2912#endif
2913#if defined(MBEDTLS_SHA1_C)
2914 case PSA_ALG_SHA_1:
2915 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2916 &source_operation->ctx.sha1 );
2917 break;
2918#endif
2919#if defined(MBEDTLS_SHA256_C)
2920 case PSA_ALG_SHA_224:
2921 case PSA_ALG_SHA_256:
2922 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2923 &source_operation->ctx.sha256 );
2924 break;
2925#endif
2926#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002927#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002928 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002929#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002930 case PSA_ALG_SHA_512:
2931 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2932 &source_operation->ctx.sha512 );
2933 break;
2934#endif
2935 default:
2936 return( PSA_ERROR_NOT_SUPPORTED );
2937 }
2938
2939 target_operation->alg = source_operation->alg;
2940 return( PSA_SUCCESS );
2941}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002942
2943
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002944/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002945/* MAC */
2946/****************************************************************/
2947
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002948static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002949 psa_algorithm_t alg,
2950 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002951 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002952 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002953{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002954 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002955 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002956
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002957 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002958 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002959
Gilles Peskine8c9def32018-02-08 10:02:12 +01002960 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2961 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002962 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002963 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002964 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002965 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002966 mode = MBEDTLS_MODE_STREAM;
2967 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002968 case PSA_ALG_CTR:
2969 mode = MBEDTLS_MODE_CTR;
2970 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002971 case PSA_ALG_CFB:
2972 mode = MBEDTLS_MODE_CFB;
2973 break;
2974 case PSA_ALG_OFB:
2975 mode = MBEDTLS_MODE_OFB;
2976 break;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02002977 case PSA_ALG_ECB_NO_PADDING:
2978 mode = MBEDTLS_MODE_ECB;
2979 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002980 case PSA_ALG_CBC_NO_PADDING:
2981 mode = MBEDTLS_MODE_CBC;
2982 break;
2983 case PSA_ALG_CBC_PKCS7:
2984 mode = MBEDTLS_MODE_CBC;
2985 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002986 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002987 mode = MBEDTLS_MODE_CCM;
2988 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002989 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002990 mode = MBEDTLS_MODE_GCM;
2991 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002992 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2993 mode = MBEDTLS_MODE_CHACHAPOLY;
2994 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002995 default:
2996 return( NULL );
2997 }
2998 }
2999 else if( alg == PSA_ALG_CMAC )
3000 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003001 else
3002 return( NULL );
3003
3004 switch( key_type )
3005 {
3006 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03003007 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003008 break;
3009 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003010 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
3011 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03003013 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 else
mohammad1603f4f0d612018-06-03 15:04:51 +03003015 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003016 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
3017 * but two-key Triple-DES is functionally three-key Triple-DES
3018 * with K1=K3, so that's how we present it to mbedtls. */
3019 if( key_bits == 128 )
3020 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 break;
3022 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03003023 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003024 break;
3025 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03003026 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003027 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02003028 case PSA_KEY_TYPE_CHACHA20:
3029 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
3030 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003031 default:
3032 return( NULL );
3033 }
mohammad1603f4f0d612018-06-03 15:04:51 +03003034 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03003035 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003036
Jaeden Amero23bbb752018-06-26 14:16:54 +01003037 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
3038 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003039}
3040
John Durkop6ba40d12020-11-10 08:50:04 -08003041#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003042static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003043{
Gilles Peskine2d277862018-06-18 15:41:12 +02003044 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03003045 {
3046 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003047 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003048 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003049 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003050 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003051 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003052 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003053 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003054 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003055 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003056 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003057 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003058 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003059 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003060 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03003061 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003062 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02003063 return( 128 );
3064 default:
3065 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03003066 }
3067}
John Durkop07cc04a2020-11-16 22:08:34 -08003068#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC) */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03003069
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003070/* Initialize the MAC operation structure. Once this function has been
3071 * called, psa_mac_abort can run and will do the right thing. */
3072static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
3073 psa_algorithm_t alg )
3074{
3075 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
3076
3077 operation->alg = alg;
3078 operation->key_set = 0;
3079 operation->iv_set = 0;
3080 operation->iv_required = 0;
3081 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003082 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003083
3084#if defined(MBEDTLS_CMAC_C)
3085 if( alg == PSA_ALG_CMAC )
3086 {
3087 operation->iv_required = 0;
3088 mbedtls_cipher_init( &operation->ctx.cmac );
3089 status = PSA_SUCCESS;
3090 }
3091 else
3092#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003093#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003094 if( PSA_ALG_IS_HMAC( operation->alg ) )
3095 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02003096 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
3097 operation->ctx.hmac.hash_ctx.alg = 0;
3098 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003099 }
3100 else
John Durkopd0321952020-10-29 21:37:36 -07003101#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003102 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02003103 if( ! PSA_ALG_IS_MAC( alg ) )
3104 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003105 }
3106
3107 if( status != PSA_SUCCESS )
3108 memset( operation, 0, sizeof( *operation ) );
3109 return( status );
3110}
3111
John Durkop6ba40d12020-11-10 08:50:04 -08003112#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003113static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
3114{
Gilles Peskine3f108122018-12-07 18:14:53 +01003115 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003116 return( psa_hash_abort( &hmac->hash_ctx ) );
3117}
John Durkop6ba40d12020-11-10 08:50:04 -08003118#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003119
Gilles Peskine8c9def32018-02-08 10:02:12 +01003120psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
3121{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003122 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003123 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003124 /* The object has (apparently) been initialized but it is not
3125 * in use. It's ok to call abort on such an object, and there's
3126 * nothing to do. */
3127 return( PSA_SUCCESS );
3128 }
3129 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003130#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003131 if( operation->alg == PSA_ALG_CMAC )
3132 {
3133 mbedtls_cipher_free( &operation->ctx.cmac );
3134 }
3135 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003136#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003137#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003138 if( PSA_ALG_IS_HMAC( operation->alg ) )
3139 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003140 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003141 }
3142 else
John Durkopd0321952020-10-29 21:37:36 -07003143#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003144 {
3145 /* Sanity check (shouldn't happen: operation->alg should
3146 * always have been initialized to a valid value). */
3147 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003148 }
Moran Peker41deec42018-04-04 15:43:05 +03003149
Gilles Peskine8c9def32018-02-08 10:02:12 +01003150 operation->alg = 0;
3151 operation->key_set = 0;
3152 operation->iv_set = 0;
3153 operation->iv_required = 0;
3154 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003155 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03003156
Gilles Peskine8c9def32018-02-08 10:02:12 +01003157 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003158
3159bad_state:
3160 /* If abort is called on an uninitialized object, we can't trust
3161 * anything. Wipe the object in case it contains confidential data.
3162 * This may result in a memory leak if a pointer gets overwritten,
3163 * but it's too late to do anything about this. */
3164 memset( operation, 0, sizeof( *operation ) );
3165 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003166}
3167
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003168#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02003169static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003170 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01003171 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003172 const mbedtls_cipher_info_t *cipher_info )
3173{
Janos Follath24eed8d2019-11-22 13:21:35 +00003174 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003175
3176 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003177
3178 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
3179 if( ret != 0 )
3180 return( ret );
3181
3182 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003183 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003184 key_bits );
3185 return( ret );
3186}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02003187#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003188
John Durkop6ba40d12020-11-10 08:50:04 -08003189#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003190static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
3191 const uint8_t *key,
3192 size_t key_length,
3193 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003194{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003195 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003196 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003197 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003198 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003199 psa_status_t status;
3200
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003201 /* Sanity checks on block_size, to guarantee that there won't be a buffer
3202 * overflow below. This should never trigger if the hash algorithm
3203 * is implemented correctly. */
3204 /* The size checks against the ipad and opad buffers cannot be written
3205 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
3206 * because that triggers -Wlogical-op on GCC 7.3. */
3207 if( block_size > sizeof( ipad ) )
3208 return( PSA_ERROR_NOT_SUPPORTED );
3209 if( block_size > sizeof( hmac->opad ) )
3210 return( PSA_ERROR_NOT_SUPPORTED );
3211 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003212 return( PSA_ERROR_NOT_SUPPORTED );
3213
Gilles Peskined223b522018-06-11 18:12:58 +02003214 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003215 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01003216 status = psa_hash_compute( hash_alg, key, key_length,
3217 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003218 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02003219 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003220 }
Gilles Peskine96889972018-07-12 17:07:03 +02003221 /* A 0-length key is not commonly used in HMAC when used as a MAC,
3222 * but it is permitted. It is common when HMAC is used in HKDF, for
3223 * example. Don't call `memcpy` in the 0-length because `key` could be
3224 * an invalid pointer which would make the behavior undefined. */
3225 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003226 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003227
Gilles Peskined223b522018-06-11 18:12:58 +02003228 /* ipad contains the key followed by garbage. Xor and fill with 0x36
3229 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003230 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02003231 ipad[i] ^= 0x36;
3232 memset( ipad + key_length, 0x36, block_size - key_length );
3233
3234 /* Copy the key material from ipad to opad, flipping the requisite bits,
3235 * and filling the rest of opad with the requisite constant. */
3236 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003237 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
3238 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003239
Gilles Peskine01126fa2018-07-12 17:04:55 +02003240 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003241 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003242 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003243
Gilles Peskine01126fa2018-07-12 17:04:55 +02003244 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003245
3246cleanup:
Steven Cooreman29149862020-08-05 15:43:42 +02003247 mbedtls_platform_zeroize( ipad, sizeof( ipad ) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003248
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003249 return( status );
3250}
John Durkop6ba40d12020-11-10 08:50:04 -08003251#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003252
Gilles Peskine89167cb2018-07-08 20:12:23 +02003253static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003254 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003255 psa_algorithm_t alg,
3256 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003257{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003258 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003259 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003260 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003261 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003262 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003263 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003264 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02003265 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003266
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003267 /* A context must be freshly initialized before it can be set up. */
3268 if( operation->alg != 0 )
3269 {
3270 return( PSA_ERROR_BAD_STATE );
3271 }
3272
Gilles Peskined911eb72018-08-14 15:18:45 +02003273 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003274 if( status != PSA_SUCCESS )
3275 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003276 if( is_sign )
3277 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003278
Ronald Cron5c522922020-11-14 16:35:34 +01003279 status = psa_get_and_lock_transparent_key_slot_with_policy(
3280 key, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003281 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003282 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003283 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02003284
Gilles Peskine8c9def32018-02-08 10:02:12 +01003285#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02003286 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003287 {
3288 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02003289 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02003290 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00003291 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003292 if( cipher_info == NULL )
3293 {
3294 status = PSA_ERROR_NOT_SUPPORTED;
3295 goto exit;
3296 }
3297 operation->mac_size = cipher_info->block_size;
3298 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
3299 status = mbedtls_to_psa_error( ret );
3300 }
3301 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01003302#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003303#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskined911eb72018-08-14 15:18:45 +02003304 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02003305 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02003306 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003307 if( hash_alg == 0 )
3308 {
3309 status = PSA_ERROR_NOT_SUPPORTED;
3310 goto exit;
3311 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003312
3313 operation->mac_size = PSA_HASH_SIZE( hash_alg );
3314 /* Sanity check. This shouldn't fail on a valid configuration. */
3315 if( operation->mac_size == 0 ||
3316 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
3317 {
3318 status = PSA_ERROR_NOT_SUPPORTED;
3319 goto exit;
3320 }
3321
Gilles Peskine8e338702019-07-30 20:06:31 +02003322 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02003323 {
3324 status = PSA_ERROR_INVALID_ARGUMENT;
3325 goto exit;
3326 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02003327
Gilles Peskine01126fa2018-07-12 17:04:55 +02003328 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003329 slot->data.key.data,
3330 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02003331 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003332 }
3333 else
John Durkopd0321952020-10-29 21:37:36 -07003334#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003335 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00003336 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02003337 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003338 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003339
Gilles Peskined911eb72018-08-14 15:18:45 +02003340 if( truncated == 0 )
3341 {
3342 /* The "normal" case: untruncated algorithm. Nothing to do. */
3343 }
3344 else if( truncated < 4 )
3345 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02003346 /* A very short MAC is too short for security since it can be
3347 * brute-forced. Ancient protocols with 32-bit MACs do exist,
3348 * so we make this our minimum, even though 32 bits is still
3349 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02003350 status = PSA_ERROR_NOT_SUPPORTED;
3351 }
3352 else if( truncated > operation->mac_size )
3353 {
3354 /* It's impossible to "truncate" to a larger length. */
3355 status = PSA_ERROR_INVALID_ARGUMENT;
3356 }
3357 else
3358 operation->mac_size = truncated;
3359
Gilles Peskinefbfac682018-07-08 20:51:54 +02003360exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003361 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003362 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02003363 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003364 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003365 else
3366 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02003367 operation->key_set = 1;
3368 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02003369
Ronald Cron5c522922020-11-14 16:35:34 +01003370 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003371
Ronald Cron5c522922020-11-14 16:35:34 +01003372 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003373}
3374
Gilles Peskine89167cb2018-07-08 20:12:23 +02003375psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003376 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003377 psa_algorithm_t alg )
3378{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003379 return( psa_mac_setup( operation, key, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003380}
3381
3382psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02003383 mbedtls_svc_key_id_t key,
Gilles Peskine89167cb2018-07-08 20:12:23 +02003384 psa_algorithm_t alg )
3385{
Ronald Croncf56a0a2020-08-04 09:51:30 +02003386 return( psa_mac_setup( operation, key, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003387}
3388
Gilles Peskine8c9def32018-02-08 10:02:12 +01003389psa_status_t psa_mac_update( psa_mac_operation_t *operation,
3390 const uint8_t *input,
3391 size_t input_length )
3392{
Gilles Peskinefbfac682018-07-08 20:51:54 +02003393 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01003394 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003395 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003396 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003397 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003398 operation->has_input = 1;
3399
Gilles Peskine8c9def32018-02-08 10:02:12 +01003400#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003401 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003402 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02003403 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
3404 input, input_length );
3405 status = mbedtls_to_psa_error( ret );
3406 }
3407 else
3408#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003409#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003410 if( PSA_ALG_IS_HMAC( operation->alg ) )
3411 {
3412 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
3413 input_length );
3414 }
3415 else
John Durkopd0321952020-10-29 21:37:36 -07003416#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003417 {
3418 /* This shouldn't happen if `operation` was initialized by
3419 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003420 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03003421 }
3422
Gilles Peskinefbfac682018-07-08 20:51:54 +02003423 if( status != PSA_SUCCESS )
3424 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003425 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003426}
3427
John Durkop6ba40d12020-11-10 08:50:04 -08003428#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskine01126fa2018-07-12 17:04:55 +02003429static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
3430 uint8_t *mac,
3431 size_t mac_size )
3432{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003433 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02003434 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
3435 size_t hash_size = 0;
3436 size_t block_size = psa_get_hash_block_size( hash_alg );
3437 psa_status_t status;
3438
Gilles Peskine01126fa2018-07-12 17:04:55 +02003439 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3440 if( status != PSA_SUCCESS )
3441 return( status );
3442 /* From here on, tmp needs to be wiped. */
3443
3444 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
3445 if( status != PSA_SUCCESS )
3446 goto exit;
3447
3448 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
3449 if( status != PSA_SUCCESS )
3450 goto exit;
3451
3452 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
3453 if( status != PSA_SUCCESS )
3454 goto exit;
3455
Gilles Peskined911eb72018-08-14 15:18:45 +02003456 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
3457 if( status != PSA_SUCCESS )
3458 goto exit;
3459
3460 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003461
3462exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003463 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003464 return( status );
3465}
John Durkop6ba40d12020-11-10 08:50:04 -08003466#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskine01126fa2018-07-12 17:04:55 +02003467
mohammad16036df908f2018-04-02 08:34:15 -07003468static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003469 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003470 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003471{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003472 if( ! operation->key_set )
3473 return( PSA_ERROR_BAD_STATE );
3474 if( operation->iv_required && ! operation->iv_set )
3475 return( PSA_ERROR_BAD_STATE );
3476
Gilles Peskine8c9def32018-02-08 10:02:12 +01003477 if( mac_size < operation->mac_size )
3478 return( PSA_ERROR_BUFFER_TOO_SMALL );
3479
Gilles Peskine8c9def32018-02-08 10:02:12 +01003480#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003481 if( operation->alg == PSA_ALG_CMAC )
3482 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003483 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3484 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3485 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003486 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003487 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003488 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003489 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003490 else
3491#endif /* MBEDTLS_CMAC_C */
John Durkopd0321952020-10-29 21:37:36 -07003492#if defined(MBEDTLS_PSA_BUILTIN_ALG_HMAC)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003493 if( PSA_ALG_IS_HMAC( operation->alg ) )
3494 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003495 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003496 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003497 }
3498 else
John Durkopd0321952020-10-29 21:37:36 -07003499#endif /* MBEDTLS_PSA_BUILTIN_ALG_HMAC */
Gilles Peskinefbfac682018-07-08 20:51:54 +02003500 {
3501 /* This shouldn't happen if `operation` was initialized by
3502 * a setup function. */
3503 return( PSA_ERROR_BAD_STATE );
3504 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003505}
3506
Gilles Peskineacd4be32018-07-08 19:56:25 +02003507psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3508 uint8_t *mac,
3509 size_t mac_size,
3510 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003511{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003512 psa_status_t status;
3513
Jaeden Amero252ef282019-02-15 14:05:35 +00003514 if( operation->alg == 0 )
3515 {
3516 return( PSA_ERROR_BAD_STATE );
3517 }
3518
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003519 /* Fill the output buffer with something that isn't a valid mac
3520 * (barring an attack on the mac and deliberately-crafted input),
3521 * in case the caller doesn't check the return status properly. */
3522 *mac_length = mac_size;
3523 /* If mac_size is 0 then mac may be NULL and then the
3524 * call to memset would have undefined behavior. */
3525 if( mac_size != 0 )
3526 memset( mac, '!', mac_size );
3527
Gilles Peskine89167cb2018-07-08 20:12:23 +02003528 if( ! operation->is_sign )
3529 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003530 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003531 }
mohammad16036df908f2018-04-02 08:34:15 -07003532
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003533 status = psa_mac_finish_internal( operation, mac, mac_size );
3534
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003535 if( status == PSA_SUCCESS )
3536 {
3537 status = psa_mac_abort( operation );
3538 if( status == PSA_SUCCESS )
3539 *mac_length = operation->mac_size;
3540 else
3541 memset( mac, '!', mac_size );
3542 }
3543 else
3544 psa_mac_abort( operation );
3545 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003546}
3547
Gilles Peskineacd4be32018-07-08 19:56:25 +02003548psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3549 const uint8_t *mac,
3550 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003551{
Gilles Peskine828ed142018-06-18 23:25:51 +02003552 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003553 psa_status_t status;
3554
Jaeden Amero252ef282019-02-15 14:05:35 +00003555 if( operation->alg == 0 )
3556 {
3557 return( PSA_ERROR_BAD_STATE );
3558 }
3559
Gilles Peskine89167cb2018-07-08 20:12:23 +02003560 if( operation->is_sign )
3561 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003562 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003563 }
3564 if( operation->mac_size != mac_length )
3565 {
3566 status = PSA_ERROR_INVALID_SIGNATURE;
3567 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003568 }
mohammad16036df908f2018-04-02 08:34:15 -07003569
3570 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003571 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003572 if( status != PSA_SUCCESS )
3573 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003574
3575 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3576 status = PSA_ERROR_INVALID_SIGNATURE;
3577
3578cleanup:
3579 if( status == PSA_SUCCESS )
3580 status = psa_mac_abort( operation );
3581 else
3582 psa_mac_abort( operation );
3583
Gilles Peskine3f108122018-12-07 18:14:53 +01003584 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003585
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003586 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003587}
3588
3589
Gilles Peskine20035e32018-02-03 22:44:14 +01003590
Gilles Peskine20035e32018-02-03 22:44:14 +01003591/****************************************************************/
3592/* Asymmetric cryptography */
3593/****************************************************************/
3594
John Durkop6ba40d12020-11-10 08:50:04 -08003595#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3596 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003597/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003598 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003599static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3600 size_t hash_length,
3601 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003602{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003603 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003604 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003605 *md_alg = mbedtls_md_get_type( md_info );
3606
3607 /* The Mbed TLS RSA module uses an unsigned int for hash length
3608 * parameters. Validate that it fits so that we don't risk an
3609 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003610#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003611 if( hash_length > UINT_MAX )
3612 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003613#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003614
John Durkop0e005192020-10-31 22:06:54 -07003615#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003616 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3617 * must be correct. */
3618 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3619 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003620 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003621 if( md_info == NULL )
3622 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003623 if( mbedtls_md_get_size( md_info ) != hash_length )
3624 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003625 }
John Durkop0e005192020-10-31 22:06:54 -07003626#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003627
John Durkop0e005192020-10-31 22:06:54 -07003628#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003629 /* PSS requires a hash internally. */
3630 if( PSA_ALG_IS_RSA_PSS( alg ) )
3631 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003632 if( md_info == NULL )
3633 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003634 }
John Durkop0e005192020-10-31 22:06:54 -07003635#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003636
Gilles Peskine61b91d42018-06-08 16:09:36 +02003637 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003638}
3639
Gilles Peskine2b450e32018-06-27 15:42:46 +02003640static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3641 psa_algorithm_t alg,
3642 const uint8_t *hash,
3643 size_t hash_length,
3644 uint8_t *signature,
3645 size_t signature_size,
3646 size_t *signature_length )
3647{
3648 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003649 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003650 mbedtls_md_type_t md_alg;
3651
3652 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3653 if( status != PSA_SUCCESS )
3654 return( status );
3655
Gilles Peskine630a18a2018-06-29 17:49:35 +02003656 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003657 return( PSA_ERROR_BUFFER_TOO_SMALL );
3658
John Durkop0e005192020-10-31 22:06:54 -07003659#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003660 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3661 {
3662 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3663 MBEDTLS_MD_NONE );
3664 ret = mbedtls_rsa_pkcs1_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003665 mbedtls_psa_get_random,
3666 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003667 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003668 md_alg,
3669 (unsigned int) hash_length,
3670 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003671 signature );
3672 }
3673 else
John Durkop0e005192020-10-31 22:06:54 -07003674#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3675#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003676 if( PSA_ALG_IS_RSA_PSS( alg ) )
3677 {
3678 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3679 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003680 mbedtls_psa_get_random,
3681 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003682 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003683 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003684 (unsigned int) hash_length,
3685 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003686 signature );
3687 }
3688 else
John Durkop0e005192020-10-31 22:06:54 -07003689#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003690 {
3691 return( PSA_ERROR_INVALID_ARGUMENT );
3692 }
3693
3694 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003695 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003696 return( mbedtls_to_psa_error( ret ) );
3697}
3698
3699static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3700 psa_algorithm_t alg,
3701 const uint8_t *hash,
3702 size_t hash_length,
3703 const uint8_t *signature,
3704 size_t signature_length )
3705{
3706 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003707 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003708 mbedtls_md_type_t md_alg;
3709
3710 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3711 if( status != PSA_SUCCESS )
3712 return( status );
3713
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003714 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3715 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003716
John Durkop0e005192020-10-31 22:06:54 -07003717#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003718 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3719 {
3720 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3721 MBEDTLS_MD_NONE );
3722 ret = mbedtls_rsa_pkcs1_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003723 mbedtls_psa_get_random,
3724 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003725 MBEDTLS_RSA_PUBLIC,
3726 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003727 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003728 hash,
3729 signature );
3730 }
3731 else
John Durkop0e005192020-10-31 22:06:54 -07003732#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN */
3733#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine2b450e32018-06-27 15:42:46 +02003734 if( PSA_ALG_IS_RSA_PSS( alg ) )
3735 {
3736 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3737 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003738 mbedtls_psa_get_random,
3739 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskine2b450e32018-06-27 15:42:46 +02003740 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003741 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003742 (unsigned int) hash_length,
3743 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003744 signature );
3745 }
3746 else
John Durkop0e005192020-10-31 22:06:54 -07003747#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003748 {
3749 return( PSA_ERROR_INVALID_ARGUMENT );
3750 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003751
3752 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3753 * the rest of the signature is invalid". This has little use in
3754 * practice and PSA doesn't report this distinction. */
3755 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3756 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003757 return( mbedtls_to_psa_error( ret ) );
3758}
John Durkop6ba40d12020-11-10 08:50:04 -08003759#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3760 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003761
John Durkop6ba40d12020-11-10 08:50:04 -08003762#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3763 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003764/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3765 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3766 * (even though these functions don't modify it). */
3767static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3768 psa_algorithm_t alg,
3769 const uint8_t *hash,
3770 size_t hash_length,
3771 uint8_t *signature,
3772 size_t signature_size,
3773 size_t *signature_length )
3774{
Janos Follath24eed8d2019-11-22 13:21:35 +00003775 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003776 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003777 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003778 mbedtls_mpi_init( &r );
3779 mbedtls_mpi_init( &s );
3780
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003781 if( signature_size < 2 * curve_bytes )
3782 {
3783 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3784 goto cleanup;
3785 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003786
John Durkop0ea39e02020-10-13 19:58:20 -07003787#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003788 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3789 {
3790 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3791 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3792 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003793 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3794 &ecp->d, hash,
3795 hash_length, md_alg,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003796 mbedtls_psa_get_random,
3797 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003798 }
3799 else
John Durkop0ea39e02020-10-13 19:58:20 -07003800#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003801 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003802 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003803 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3804 hash, hash_length,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003805 mbedtls_psa_get_random,
3806 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003807 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003808
3809 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3810 signature,
3811 curve_bytes ) );
3812 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3813 signature + curve_bytes,
3814 curve_bytes ) );
3815
3816cleanup:
3817 mbedtls_mpi_free( &r );
3818 mbedtls_mpi_free( &s );
3819 if( ret == 0 )
3820 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003821 return( mbedtls_to_psa_error( ret ) );
3822}
3823
3824static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3825 const uint8_t *hash,
3826 size_t hash_length,
3827 const uint8_t *signature,
3828 size_t signature_length )
3829{
Janos Follath24eed8d2019-11-22 13:21:35 +00003830 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003831 mbedtls_mpi r, s;
3832 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3833 mbedtls_mpi_init( &r );
3834 mbedtls_mpi_init( &s );
3835
3836 if( signature_length != 2 * curve_bytes )
3837 return( PSA_ERROR_INVALID_SIGNATURE );
3838
3839 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3840 signature,
3841 curve_bytes ) );
3842 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3843 signature + curve_bytes,
3844 curve_bytes ) );
3845
Steven Cooremanacda8342020-07-24 23:09:52 +02003846 /* Check whether the public part is loaded. If not, load it. */
3847 if( mbedtls_ecp_is_zero( &ecp->Q ) )
3848 {
3849 MBEDTLS_MPI_CHK(
3850 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
Gilles Peskine30524eb2020-11-13 17:02:26 +01003851 mbedtls_psa_get_random, mbedtls_psa_random_state( &global_data.rng ) ) );
Steven Cooremanacda8342020-07-24 23:09:52 +02003852 }
3853
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003854 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3855 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003856
3857cleanup:
3858 mbedtls_mpi_free( &r );
3859 mbedtls_mpi_free( &s );
3860 return( mbedtls_to_psa_error( ret ) );
3861}
John Durkop6ba40d12020-11-10 08:50:04 -08003862#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3863 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003864
Ronald Croncf56a0a2020-08-04 09:51:30 +02003865psa_status_t psa_sign_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003866 psa_algorithm_t alg,
3867 const uint8_t *hash,
3868 size_t hash_length,
3869 uint8_t *signature,
3870 size_t signature_size,
3871 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003872{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003873 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003874 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003875 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003876
3877 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003878 /* Immediately reject a zero-length signature buffer. This guarantees
3879 * that signature must be a valid pointer. (On the other hand, the hash
3880 * buffer can in principle be empty since it doesn't actually have
3881 * to be a hash.) */
3882 if( signature_size == 0 )
3883 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003884
Ronald Cron5c522922020-11-14 16:35:34 +01003885 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
3886 PSA_KEY_USAGE_SIGN_HASH,
3887 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003888 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003889 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003890 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003891 {
3892 status = PSA_ERROR_INVALID_ARGUMENT;
3893 goto exit;
3894 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003895
Steven Cooremancd84cb42020-07-16 20:28:36 +02003896 /* Try any of the available accelerators first */
3897 status = psa_driver_wrapper_sign_hash( slot,
3898 alg,
3899 hash,
3900 hash_length,
3901 signature,
3902 signature_size,
3903 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02003904 if( status != PSA_ERROR_NOT_SUPPORTED ||
3905 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Steven Cooremancd84cb42020-07-16 20:28:36 +02003906 goto exit;
3907
Steven Cooreman7a250572020-07-17 16:43:05 +02003908 /* If the operation was not supported by any accelerator, try fallback. */
John Durkop6ba40d12020-11-10 08:50:04 -08003909#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
3910 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02003911 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003912 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003913 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02003914
Steven Cooreman4fed4552020-08-03 14:46:03 +02003915 status = psa_load_rsa_representation( slot->attr.type,
3916 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003917 slot->data.key.bytes,
Steven Cooremana01795d2020-07-24 22:48:15 +02003918 &rsa );
3919 if( status != PSA_SUCCESS )
3920 goto exit;
3921
Steven Cooremana2371e52020-07-28 14:30:39 +02003922 status = psa_rsa_sign( rsa,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003923 alg,
3924 hash, hash_length,
3925 signature, signature_size,
3926 signature_length );
Steven Cooremana01795d2020-07-24 22:48:15 +02003927
Steven Cooremana2371e52020-07-28 14:30:39 +02003928 mbedtls_rsa_free( rsa );
3929 mbedtls_free( rsa );
Gilles Peskine20035e32018-02-03 22:44:14 +01003930 }
3931 else
John Durkop6ba40d12020-11-10 08:50:04 -08003932#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
3933 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02003934 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003935 {
John Durkop9814fa22020-11-04 12:28:15 -08003936#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
3937 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003938 if(
John Durkop0ea39e02020-10-13 19:58:20 -07003939#if defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003940 PSA_ALG_IS_ECDSA( alg )
3941#else
3942 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3943#endif
3944 )
Steven Cooremanacda8342020-07-24 23:09:52 +02003945 {
Steven Cooremana2371e52020-07-28 14:30:39 +02003946 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02003947 status = psa_load_ecp_representation( slot->attr.type,
3948 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02003949 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02003950 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003951 if( status != PSA_SUCCESS )
3952 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02003953 status = psa_ecdsa_sign( ecp,
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003954 alg,
3955 hash, hash_length,
3956 signature, signature_size,
3957 signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02003958 mbedtls_ecp_keypair_free( ecp );
3959 mbedtls_free( ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02003960 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003961 else
John Durkop9814fa22020-11-04 12:28:15 -08003962#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
3963 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003964 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003965 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003966 }
itayzafrir5c753392018-05-08 11:18:38 +03003967 }
3968 else
itayzafrir5c753392018-05-08 11:18:38 +03003969 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003970 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003971 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003972
3973exit:
3974 /* Fill the unused part of the output buffer (the whole buffer on error,
3975 * the trailing part on success) with something that isn't a valid mac
3976 * (barring an attack on the mac and deliberately-crafted input),
3977 * in case the caller doesn't check the return status properly. */
3978 if( status == PSA_SUCCESS )
3979 memset( signature + *signature_length, '!',
3980 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003981 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003982 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003983 /* If signature_size is 0 then we have nothing to do. We must not call
3984 * memset because signature may be NULL in this case. */
Ronald Cronf95a2b12020-10-22 15:24:49 +02003985
Ronald Cron5c522922020-11-14 16:35:34 +01003986 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02003987
Ronald Cron5c522922020-11-14 16:35:34 +01003988 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
itayzafrir5c753392018-05-08 11:18:38 +03003989}
3990
Ronald Croncf56a0a2020-08-04 09:51:30 +02003991psa_status_t psa_verify_hash( mbedtls_svc_key_id_t key,
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003992 psa_algorithm_t alg,
3993 const uint8_t *hash,
3994 size_t hash_length,
3995 const uint8_t *signature,
3996 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003997{
Ronald Cronf95a2b12020-10-22 15:24:49 +02003998 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01003999 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004000 psa_key_slot_t *slot;
Gilles Peskine2b450e32018-06-27 15:42:46 +02004001
Ronald Cron5c522922020-11-14 16:35:34 +01004002 status = psa_get_and_lock_key_slot_with_policy( key, &slot,
4003 PSA_KEY_USAGE_VERIFY_HASH,
4004 alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004005 if( status != PSA_SUCCESS )
4006 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03004007
Steven Cooreman55ae2172020-07-17 19:46:15 +02004008 /* Try any of the available accelerators first */
4009 status = psa_driver_wrapper_verify_hash( slot,
4010 alg,
4011 hash,
4012 hash_length,
4013 signature,
4014 signature_length );
Steven Cooreman8d2bde72020-09-04 13:06:39 +02004015 if( status != PSA_ERROR_NOT_SUPPORTED ||
4016 psa_key_lifetime_is_external( slot->attr.lifetime ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004017 goto exit;
Steven Cooreman55ae2172020-07-17 19:46:15 +02004018
John Durkop6ba40d12020-11-10 08:50:04 -08004019#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) || \
4020 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS)
Gilles Peskine8e338702019-07-30 20:06:31 +02004021 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004022 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004023 mbedtls_rsa_context *rsa = NULL;
Steven Cooremana01795d2020-07-24 22:48:15 +02004024
Steven Cooreman4fed4552020-08-03 14:46:03 +02004025 status = psa_load_rsa_representation( slot->attr.type,
4026 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004027 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004028 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004029 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004030 goto exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004031
Steven Cooremana2371e52020-07-28 14:30:39 +02004032 status = psa_rsa_verify( rsa,
Steven Cooremana01795d2020-07-24 22:48:15 +02004033 alg,
4034 hash, hash_length,
4035 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004036 mbedtls_rsa_free( rsa );
4037 mbedtls_free( rsa );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004038 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004039 }
4040 else
John Durkop6ba40d12020-11-10 08:50:04 -08004041#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_SIGN) ||
4042 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PSS) */
Gilles Peskine8e338702019-07-30 20:06:31 +02004043 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03004044 {
John Durkop9814fa22020-11-04 12:28:15 -08004045#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) || \
4046 defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004047 if( PSA_ALG_IS_ECDSA( alg ) )
Steven Cooremanacda8342020-07-24 23:09:52 +02004048 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004049 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004050 status = psa_load_ecp_representation( slot->attr.type,
4051 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004052 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004053 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02004054 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004055 goto exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004056 status = psa_ecdsa_verify( ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02004057 hash, hash_length,
4058 signature, signature_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02004059 mbedtls_ecp_keypair_free( ecp );
4060 mbedtls_free( ecp );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004061 goto exit;
Steven Cooremanacda8342020-07-24 23:09:52 +02004062 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004063 else
John Durkop9814fa22020-11-04 12:28:15 -08004064#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_ECDSA) ||
4065 * defined(MBEDTLS_PSA_BUILTIN_ALG_DETERMINISTIC_ECDSA) */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004066 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004067 status = PSA_ERROR_INVALID_ARGUMENT;
4068 goto exit;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02004069 }
itayzafrir5c753392018-05-08 11:18:38 +03004070 }
Gilles Peskine20035e32018-02-03 22:44:14 +01004071 else
Gilles Peskine20035e32018-02-03 22:44:14 +01004072 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004073 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01004074 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004075
4076exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004077 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004078
Ronald Cron5c522922020-11-14 16:35:34 +01004079 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine20035e32018-02-03 22:44:14 +01004080}
4081
John Durkop0e005192020-10-31 22:06:54 -07004082#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine072ac562018-06-30 00:21:29 +02004083static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
4084 mbedtls_rsa_context *rsa )
4085{
4086 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
4087 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
4088 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
4089 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
4090}
John Durkop0e005192020-10-31 22:06:54 -07004091#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Gilles Peskine072ac562018-06-30 00:21:29 +02004092
Ronald Croncf56a0a2020-08-04 09:51:30 +02004093psa_status_t psa_asymmetric_encrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004094 psa_algorithm_t alg,
4095 const uint8_t *input,
4096 size_t input_length,
4097 const uint8_t *salt,
4098 size_t salt_length,
4099 uint8_t *output,
4100 size_t output_size,
4101 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004102{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004103 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004104 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004105 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004106
Darryl Green5cc689a2018-07-24 15:34:10 +01004107 (void) input;
4108 (void) input_length;
4109 (void) salt;
4110 (void) output;
4111 (void) output_size;
4112
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004113 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004114
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004115 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4116 return( PSA_ERROR_INVALID_ARGUMENT );
4117
Ronald Cron5c522922020-11-14 16:35:34 +01004118 status = psa_get_and_lock_transparent_key_slot_with_policy(
4119 key, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004120 if( status != PSA_SUCCESS )
4121 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004122 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
4123 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004124 {
4125 status = PSA_ERROR_INVALID_ARGUMENT;
4126 goto exit;
4127 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004128
John Durkop6ba40d12020-11-10 08:50:04 -08004129#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4130 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004131 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004132 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004133 mbedtls_rsa_context *rsa = NULL;
Steven Cooreman4fed4552020-08-03 14:46:03 +02004134 status = psa_load_rsa_representation( slot->attr.type,
4135 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004136 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004137 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004138 if( status != PSA_SUCCESS )
Steven Cooreman4fed4552020-08-03 14:46:03 +02004139 goto rsa_exit;
Steven Cooremana2371e52020-07-28 14:30:39 +02004140
4141 if( output_size < mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004142 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004143 status = PSA_ERROR_BUFFER_TOO_SMALL;
4144 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004145 }
John Durkop0e005192020-10-31 22:06:54 -07004146#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004147 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004148 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004149 status = mbedtls_to_psa_error(
4150 mbedtls_rsa_pkcs1_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004151 mbedtls_psa_get_random,
4152 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004153 MBEDTLS_RSA_PUBLIC,
4154 input_length,
4155 input,
4156 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004157 }
4158 else
John Durkop0e005192020-10-31 22:06:54 -07004159#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4160#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004161 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004162 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004163 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004164 status = mbedtls_to_psa_error(
4165 mbedtls_rsa_rsaes_oaep_encrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004166 mbedtls_psa_get_random,
4167 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004168 MBEDTLS_RSA_PUBLIC,
4169 salt, salt_length,
4170 input_length,
4171 input,
4172 output ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004173 }
4174 else
John Durkop0e005192020-10-31 22:06:54 -07004175#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004176 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004177 status = PSA_ERROR_INVALID_ARGUMENT;
4178 goto rsa_exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004179 }
Steven Cooreman4fed4552020-08-03 14:46:03 +02004180rsa_exit:
4181 if( status == PSA_SUCCESS )
Steven Cooremana2371e52020-07-28 14:30:39 +02004182 *output_length = mbedtls_rsa_get_len( rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004183
Steven Cooremana2371e52020-07-28 14:30:39 +02004184 mbedtls_rsa_free( rsa );
4185 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004186 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004187 else
John Durkop9814fa22020-11-04 12:28:15 -08004188#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
John Durkop6ba40d12020-11-10 08:50:04 -08004189 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004190 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004191 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004192 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004193
4194exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004195 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004196
Ronald Cron5c522922020-11-14 16:35:34 +01004197 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004198}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004199
Ronald Croncf56a0a2020-08-04 09:51:30 +02004200psa_status_t psa_asymmetric_decrypt( mbedtls_svc_key_id_t key,
Gilles Peskine61b91d42018-06-08 16:09:36 +02004201 psa_algorithm_t alg,
4202 const uint8_t *input,
4203 size_t input_length,
4204 const uint8_t *salt,
4205 size_t salt_length,
4206 uint8_t *output,
4207 size_t output_size,
4208 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004209{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004210 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004211 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004212 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004213
Darryl Green5cc689a2018-07-24 15:34:10 +01004214 (void) input;
4215 (void) input_length;
4216 (void) salt;
4217 (void) output;
4218 (void) output_size;
4219
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03004220 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004221
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02004222 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
4223 return( PSA_ERROR_INVALID_ARGUMENT );
4224
Ronald Cron5c522922020-11-14 16:35:34 +01004225 status = psa_get_and_lock_transparent_key_slot_with_policy(
4226 key, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004227 if( status != PSA_SUCCESS )
4228 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02004229 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004230 {
4231 status = PSA_ERROR_INVALID_ARGUMENT;
4232 goto exit;
4233 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004234
John Durkop6ba40d12020-11-10 08:50:04 -08004235#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) || \
4236 defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine8e338702019-07-30 20:06:31 +02004237 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004238 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004239 mbedtls_rsa_context *rsa = NULL;
4240 status = psa_load_rsa_representation( slot->attr.type,
4241 slot->data.key.data,
Steven Cooreman7f391872020-07-30 14:57:44 +02004242 slot->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02004243 &rsa );
Steven Cooremana01795d2020-07-24 22:48:15 +02004244 if( status != PSA_SUCCESS )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004245 goto exit;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004246
Steven Cooremana2371e52020-07-28 14:30:39 +02004247 if( input_length != mbedtls_rsa_get_len( rsa ) )
Steven Cooremana01795d2020-07-24 22:48:15 +02004248 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004249 status = PSA_ERROR_INVALID_ARGUMENT;
4250 goto rsa_exit;
Steven Cooremana01795d2020-07-24 22:48:15 +02004251 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004252
John Durkop0e005192020-10-31 22:06:54 -07004253#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT)
Gilles Peskine6afe7892018-05-31 13:16:08 +02004254 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004255 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004256 status = mbedtls_to_psa_error(
4257 mbedtls_rsa_pkcs1_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004258 mbedtls_psa_get_random,
4259 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004260 MBEDTLS_RSA_PRIVATE,
4261 output_length,
4262 input,
4263 output,
4264 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004265 }
4266 else
John Durkop0e005192020-10-31 22:06:54 -07004267#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT */
4268#if defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02004269 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004270 {
Steven Cooremana2371e52020-07-28 14:30:39 +02004271 psa_rsa_oaep_set_padding_mode( alg, rsa );
Steven Cooreman4fed4552020-08-03 14:46:03 +02004272 status = mbedtls_to_psa_error(
4273 mbedtls_rsa_rsaes_oaep_decrypt( rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01004274 mbedtls_psa_get_random,
4275 mbedtls_psa_random_state( &global_data.rng ),
Steven Cooreman4fed4552020-08-03 14:46:03 +02004276 MBEDTLS_RSA_PRIVATE,
4277 salt, salt_length,
4278 output_length,
4279 input,
4280 output,
4281 output_size ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004282 }
4283 else
John Durkop0e005192020-10-31 22:06:54 -07004284#endif /* MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004285 {
Steven Cooreman4fed4552020-08-03 14:46:03 +02004286 status = PSA_ERROR_INVALID_ARGUMENT;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004287 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03004288
Steven Cooreman4fed4552020-08-03 14:46:03 +02004289rsa_exit:
Steven Cooremana2371e52020-07-28 14:30:39 +02004290 mbedtls_rsa_free( rsa );
4291 mbedtls_free( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004292 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004293 else
John Durkop6ba40d12020-11-10 08:50:04 -08004294#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_PKCS1V15_CRYPT) ||
4295 * defined(MBEDTLS_PSA_BUILTIN_ALG_RSA_OAEP) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004296 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004297 status = PSA_ERROR_NOT_SUPPORTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03004298 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004299
4300exit:
Ronald Cron5c522922020-11-14 16:35:34 +01004301 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004302
Ronald Cron5c522922020-11-14 16:35:34 +01004303 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine5b051bc2018-05-31 13:25:48 +02004304}
Gilles Peskine20035e32018-02-03 22:44:14 +01004305
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004306
4307
mohammad1603503973b2018-03-12 15:59:30 +02004308/****************************************************************/
4309/* Symmetric cryptography */
4310/****************************************************************/
4311
Gilles Peskinee553c652018-06-04 16:22:46 +02004312static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004313 mbedtls_svc_key_id_t key,
Gilles Peskine7e928852018-06-04 16:23:10 +02004314 psa_algorithm_t alg,
4315 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02004316{
Ronald Cronf95a2b12020-10-22 15:24:49 +02004317 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01004318 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004319 int ret = 0;
Gilles Peskine2f060a82018-12-04 17:12:32 +01004320 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02004321 size_t key_bits;
4322 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004323 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
4324 PSA_KEY_USAGE_ENCRYPT :
4325 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02004326
Steven Cooremand3feccd2020-09-01 15:56:14 +02004327 /* A context must be freshly initialized before it can be set up. */
4328 if( operation->alg != 0 )
4329 return( PSA_ERROR_BAD_STATE );
4330
Steven Cooremana07b9972020-09-10 14:54:14 +02004331 /* The requested algorithm must be one that can be processed by cipher. */
4332 if( ! PSA_ALG_IS_CIPHER( alg ) )
Steven Cooremana07b9972020-09-10 14:54:14 +02004333 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004334
Steven Cooremanef8575e2020-09-11 11:44:50 +02004335 /* Fetch key material from key storage. */
Ronald Cron5c522922020-11-14 16:35:34 +01004336 status = psa_get_and_lock_key_slot_with_policy( key, &slot, usage, alg );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004337 if( status != PSA_SUCCESS )
4338 goto exit;
4339
4340 /* Initialize the operation struct members, except for alg. The alg member
4341 * is used to indicate to psa_cipher_abort that there are resources to free,
4342 * so we only set it after resources have been allocated/initialized. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004343 operation->key_set = 0;
4344 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004345 operation->mbedtls_in_use = 0;
Steven Cooremana07b9972020-09-10 14:54:14 +02004346 operation->iv_size = 0;
4347 operation->block_size = 0;
4348 if( alg == PSA_ALG_ECB_NO_PADDING )
4349 operation->iv_required = 0;
4350 else
4351 operation->iv_required = 1;
4352
Steven Cooremana07b9972020-09-10 14:54:14 +02004353 /* Try doing the operation through a driver before using software fallback. */
Steven Cooreman37941cb2020-07-28 18:49:51 +02004354 if( cipher_operation == MBEDTLS_ENCRYPT )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004355 status = psa_driver_wrapper_cipher_encrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004356 slot,
4357 alg );
4358 else
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004359 status = psa_driver_wrapper_cipher_decrypt_setup( &operation->ctx.driver,
Steven Cooreman37941cb2020-07-28 18:49:51 +02004360 slot,
4361 alg );
4362
Steven Cooremanef8575e2020-09-11 11:44:50 +02004363 if( status == PSA_SUCCESS )
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004364 {
Steven Cooremanef8575e2020-09-11 11:44:50 +02004365 /* Once the driver context is initialised, it needs to be freed using
4366 * psa_cipher_abort. Indicate this through setting alg. */
4367 operation->alg = alg;
Steven Cooreman6d81f7e2020-09-14 13:14:31 +02004368 }
Steven Cooremanef8575e2020-09-11 11:44:50 +02004369
Steven Cooremand3feccd2020-09-01 15:56:14 +02004370 if( status != PSA_ERROR_NOT_SUPPORTED ||
4371 psa_key_lifetime_is_external( slot->attr.lifetime ) )
4372 goto exit;
4373
Steven Cooremana07b9972020-09-10 14:54:14 +02004374 /* Proceed with initializing an mbed TLS cipher context if no driver is
Steven Cooremand3feccd2020-09-01 15:56:14 +02004375 * available for the given algorithm & key. */
4376 mbedtls_cipher_init( &operation->ctx.cipher );
mohammad1603503973b2018-03-12 15:59:30 +02004377
Steven Cooremana07b9972020-09-10 14:54:14 +02004378 /* Once the cipher context is initialised, it needs to be freed using
Steven Cooremanef8575e2020-09-11 11:44:50 +02004379 * psa_cipher_abort. Indicate there is something to be freed through setting
4380 * alg, and indicate the operation is being done using mbedtls crypto through
4381 * setting mbedtls_in_use. */
Steven Cooremana07b9972020-09-10 14:54:14 +02004382 operation->alg = alg;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004383 operation->mbedtls_in_use = 1;
Steven Cooremana07b9972020-09-10 14:54:14 +02004384
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004385 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskine8e338702019-07-30 20:06:31 +02004386 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02004387 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004388 {
4389 status = PSA_ERROR_NOT_SUPPORTED;
4390 goto exit;
4391 }
mohammad1603503973b2018-03-12 15:59:30 +02004392
mohammad1603503973b2018-03-12 15:59:30 +02004393 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03004394 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004395 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004396
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004397#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004398 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004399 {
4400 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02004401 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004402 memcpy( keys, slot->data.key.data, 16 );
4403 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004404 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
4405 keys,
4406 192, cipher_operation );
4407 }
4408 else
4409#endif
4410 {
4411 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004412 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01004413 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02004414 }
Moran Peker41deec42018-04-04 15:43:05 +03004415 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004416 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004417
mohammad16038481e742018-03-18 13:57:31 +02004418#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004419 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02004420 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004421 case PSA_ALG_CBC_NO_PADDING:
4422 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4423 MBEDTLS_PADDING_NONE );
4424 break;
4425 case PSA_ALG_CBC_PKCS7:
4426 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
4427 MBEDTLS_PADDING_PKCS7 );
4428 break;
4429 default:
4430 /* The algorithm doesn't involve padding. */
4431 ret = 0;
4432 break;
4433 }
4434 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004435 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02004436#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
4437
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004438 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02004439 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Steven Cooremana6033e92020-08-25 11:47:50 +02004440 if( ( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG ) != 0 &&
4441 alg != PSA_ALG_ECB_NO_PADDING )
mohammad16038481e742018-03-18 13:57:31 +02004442 {
Gilles Peskine8e338702019-07-30 20:06:31 +02004443 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02004444 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004445#if defined(MBEDTLS_CHACHA20_C)
4446 else
4447 if( alg == PSA_ALG_CHACHA20 )
4448 operation->iv_size = 12;
4449#endif
mohammad1603503973b2018-03-12 15:59:30 +02004450
Steven Cooreman7df02922020-09-09 15:28:49 +02004451 status = PSA_SUCCESS;
4452
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004453exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004454 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004455 status = mbedtls_to_psa_error( ret );
Steven Cooreman7df02922020-09-09 15:28:49 +02004456 if( status == PSA_SUCCESS )
4457 {
4458 /* Update operation flags for both driver and software implementations */
4459 operation->key_set = 1;
4460 }
4461 else
Gilles Peskine9ab61b62019-02-25 17:43:14 +01004462 psa_cipher_abort( operation );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004463
Ronald Cron5c522922020-11-14 16:35:34 +01004464 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02004465
Ronald Cron5c522922020-11-14 16:35:34 +01004466 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
mohammad1603503973b2018-03-12 15:59:30 +02004467}
4468
Gilles Peskinefe119512018-07-08 21:39:34 +02004469psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004470 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004471 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004472{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004473 return( psa_cipher_setup( operation, key, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004474}
4475
Gilles Peskinefe119512018-07-08 21:39:34 +02004476psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004477 mbedtls_svc_key_id_t key,
Gilles Peskinefe119512018-07-08 21:39:34 +02004478 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02004479{
Ronald Croncf56a0a2020-08-04 09:51:30 +02004480 return( psa_cipher_setup( operation, key, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02004481}
4482
Gilles Peskinefe119512018-07-08 21:39:34 +02004483psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004484 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004485 size_t iv_size,
4486 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004487{
itayzafrir534bd7c2018-08-02 13:56:32 +03004488 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004489 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004490 if( operation->iv_set || ! operation->iv_required )
4491 {
4492 return( PSA_ERROR_BAD_STATE );
4493 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004494
Steven Cooremanef8575e2020-09-11 11:44:50 +02004495 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004496 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004497 status = psa_driver_wrapper_cipher_generate_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004498 iv,
4499 iv_size,
4500 iv_length );
4501 goto exit;
4502 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004503
Moran Peker41deec42018-04-04 15:43:05 +03004504 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02004505 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004506 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03004507 goto exit;
4508 }
Gilles Peskine30524eb2020-11-13 17:02:26 +01004509 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
4510 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03004511 if( ret != 0 )
4512 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004513 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02004514 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02004515 }
Gilles Peskinee553c652018-06-04 16:22:46 +02004516
mohammad16038481e742018-03-18 13:57:31 +02004517 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03004518 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03004519
Moran Peker395db872018-05-31 14:07:14 +03004520exit:
Steven Cooreman7df02922020-09-09 15:28:49 +02004521 if( status == PSA_SUCCESS )
4522 operation->iv_set = 1;
4523 else
Moran Peker395db872018-05-31 14:07:14 +03004524 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004525 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004526}
4527
Gilles Peskinefe119512018-07-08 21:39:34 +02004528psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004529 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02004530 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02004531{
itayzafrir534bd7c2018-08-02 13:56:32 +03004532 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00004533 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Steven Cooreman7df02922020-09-09 15:28:49 +02004534 if( operation->iv_set || ! operation->iv_required )
4535 {
4536 return( PSA_ERROR_BAD_STATE );
4537 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004538
Steven Cooremanef8575e2020-09-11 11:44:50 +02004539 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004540 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004541 status = psa_driver_wrapper_cipher_set_iv( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004542 iv,
4543 iv_length );
4544 goto exit;
4545 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004546
Moran Pekera28258c2018-05-29 16:25:04 +03004547 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03004548 {
itayzafrir534bd7c2018-08-02 13:56:32 +03004549 status = PSA_ERROR_INVALID_ARGUMENT;
4550 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03004551 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004552 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
4553 status = mbedtls_to_psa_error( ret );
4554exit:
4555 if( status == PSA_SUCCESS )
4556 operation->iv_set = 1;
4557 else
mohammad1603503973b2018-03-12 15:59:30 +02004558 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004559 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004560}
4561
Steven Cooreman177deba2020-09-07 17:14:14 +02004562/* Process input for which the algorithm is set to ECB mode. This requires
4563 * manual processing, since the PSA API is defined as being able to process
4564 * arbitrary-length calls to psa_cipher_update() with ECB mode, but the
4565 * underlying mbedtls_cipher_update only takes full blocks. */
4566static psa_status_t psa_cipher_update_ecb_internal(
4567 mbedtls_cipher_context_t *ctx,
4568 const uint8_t *input,
4569 size_t input_length,
4570 uint8_t *output,
4571 size_t output_size,
4572 size_t *output_length )
4573{
4574 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
4575 size_t block_size = ctx->cipher_info->block_size;
4576 size_t internal_output_length = 0;
4577 *output_length = 0;
4578
4579 if( input_length == 0 )
4580 {
4581 status = PSA_SUCCESS;
4582 goto exit;
4583 }
4584
4585 if( ctx->unprocessed_len > 0 )
4586 {
4587 /* Fill up to block size, and run the block if there's a full one. */
4588 size_t bytes_to_copy = block_size - ctx->unprocessed_len;
4589
4590 if( input_length < bytes_to_copy )
4591 bytes_to_copy = input_length;
4592
4593 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4594 input, bytes_to_copy );
4595 input_length -= bytes_to_copy;
4596 input += bytes_to_copy;
4597 ctx->unprocessed_len += bytes_to_copy;
4598
4599 if( ctx->unprocessed_len == block_size )
4600 {
4601 status = mbedtls_to_psa_error(
4602 mbedtls_cipher_update( ctx,
4603 ctx->unprocessed_data,
4604 block_size,
4605 output, &internal_output_length ) );
4606
4607 if( status != PSA_SUCCESS )
4608 goto exit;
4609
4610 output += internal_output_length;
4611 output_size -= internal_output_length;
4612 *output_length += internal_output_length;
4613 ctx->unprocessed_len = 0;
4614 }
4615 }
4616
4617 while( input_length >= block_size )
4618 {
4619 /* Run all full blocks we have, one by one */
4620 status = mbedtls_to_psa_error(
4621 mbedtls_cipher_update( ctx, input,
4622 block_size,
4623 output, &internal_output_length ) );
4624
4625 if( status != PSA_SUCCESS )
4626 goto exit;
4627
4628 input_length -= block_size;
4629 input += block_size;
4630
4631 output += internal_output_length;
4632 output_size -= internal_output_length;
4633 *output_length += internal_output_length;
4634 }
4635
4636 if( input_length > 0 )
4637 {
4638 /* Save unprocessed bytes for later processing */
4639 memcpy( &( ctx->unprocessed_data[ctx->unprocessed_len] ),
4640 input, input_length );
4641 ctx->unprocessed_len += input_length;
4642 }
4643
4644 status = PSA_SUCCESS;
4645
4646exit:
4647 return( status );
4648}
4649
Gilles Peskinee553c652018-06-04 16:22:46 +02004650psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
4651 const uint8_t *input,
4652 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01004653 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02004654 size_t output_size,
4655 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004656{
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004657 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02004658 size_t expected_output_size;
Steven Cooreman7df02922020-09-09 15:28:49 +02004659 if( operation->alg == 0 )
4660 {
4661 return( PSA_ERROR_BAD_STATE );
4662 }
4663 if( operation->iv_required && ! operation->iv_set )
4664 {
4665 return( PSA_ERROR_BAD_STATE );
4666 }
Jaeden Ameroab439972019-02-15 14:12:05 +00004667
Steven Cooremanef8575e2020-09-11 11:44:50 +02004668 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004669 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004670 status = psa_driver_wrapper_cipher_update( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004671 input,
4672 input_length,
4673 output,
4674 output_size,
4675 output_length );
4676 goto exit;
4677 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004678
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004679 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02004680 {
4681 /* Take the unprocessed partial block left over from previous
4682 * update calls, if any, plus the input to this call. Remove
4683 * the last partial block, if any. You get the data that will be
4684 * output in this call. */
4685 expected_output_size =
4686 ( operation->ctx.cipher.unprocessed_len + input_length )
4687 / operation->block_size * operation->block_size;
4688 }
4689 else
4690 {
4691 expected_output_size = input_length;
4692 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004693
Gilles Peskine89d789c2018-06-04 17:17:16 +02004694 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03004695 {
4696 status = PSA_ERROR_BUFFER_TOO_SMALL;
4697 goto exit;
4698 }
mohammad160382759612018-03-12 18:16:40 +02004699
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004700 if( operation->alg == PSA_ALG_ECB_NO_PADDING )
4701 {
4702 /* mbedtls_cipher_update has an API inconsistency: it will only
4703 * process a single block at a time in ECB mode. Abstract away that
4704 * inconsistency here to match the PSA API behaviour. */
Steven Cooreman177deba2020-09-07 17:14:14 +02004705 status = psa_cipher_update_ecb_internal( &operation->ctx.cipher,
4706 input,
4707 input_length,
4708 output,
4709 output_size,
4710 output_length );
Steven Cooremanffecb7b2020-08-25 15:13:13 +02004711 }
4712 else
4713 {
4714 status = mbedtls_to_psa_error(
4715 mbedtls_cipher_update( &operation->ctx.cipher, input,
4716 input_length, output, output_length ) );
4717 }
itayzafrir534bd7c2018-08-02 13:56:32 +03004718exit:
4719 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02004720 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03004721 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004722}
4723
Gilles Peskinee553c652018-06-04 16:22:46 +02004724psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
4725 uint8_t *output,
4726 size_t output_size,
4727 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02004728{
David Saadab4ecc272019-02-14 13:48:10 +02004729 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskinee553c652018-06-04 16:22:46 +02004730 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Steven Cooreman7df02922020-09-09 15:28:49 +02004731 if( operation->alg == 0 )
4732 {
4733 return( PSA_ERROR_BAD_STATE );
4734 }
4735 if( operation->iv_required && ! operation->iv_set )
4736 {
4737 return( PSA_ERROR_BAD_STATE );
4738 }
Moran Pekerbed71a22018-04-22 20:19:20 +03004739
Steven Cooremanef8575e2020-09-11 11:44:50 +02004740 if( operation->mbedtls_in_use == 0 )
Steven Cooreman8b122252020-09-03 15:30:32 +02004741 {
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004742 status = psa_driver_wrapper_cipher_finish( &operation->ctx.driver,
Steven Cooreman8b122252020-09-03 15:30:32 +02004743 output,
4744 output_size,
4745 output_length );
Steven Cooremanef8575e2020-09-11 11:44:50 +02004746 goto exit;
Steven Cooreman8b122252020-09-03 15:30:32 +02004747 }
Steven Cooremand3feccd2020-09-01 15:56:14 +02004748
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004749 if( operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004750 {
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004751 if( operation->alg == PSA_ALG_ECB_NO_PADDING ||
Fredrik Strupef90e3012020-09-28 16:11:33 +02004752 operation->alg == PSA_ALG_CBC_NO_PADDING )
Steven Cooremana6033e92020-08-25 11:47:50 +02004753 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004754 status = PSA_ERROR_INVALID_ARGUMENT;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004755 goto exit;
Steven Cooremaned3c9ec2020-07-06 14:08:59 +02004756 }
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004757 }
4758
Steven Cooremanef8575e2020-09-11 11:44:50 +02004759 status = mbedtls_to_psa_error(
4760 mbedtls_cipher_finish( &operation->ctx.cipher,
4761 temp_output_buffer,
4762 output_length ) );
4763 if( status != PSA_SUCCESS )
4764 goto exit;
Janos Follath315b51c2018-07-09 16:04:51 +01004765
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004766 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004767 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004768 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004769 memcpy( output, temp_output_buffer, *output_length );
4770 else
Janos Follath315b51c2018-07-09 16:04:51 +01004771 status = PSA_ERROR_BUFFER_TOO_SMALL;
mohammad1603503973b2018-03-12 15:59:30 +02004772
Steven Cooremanef8575e2020-09-11 11:44:50 +02004773exit:
4774 if( operation->mbedtls_in_use == 1 )
4775 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004776
Steven Cooremanef8575e2020-09-11 11:44:50 +02004777 if( status == PSA_SUCCESS )
4778 return( psa_cipher_abort( operation ) );
4779 else
4780 {
4781 *output_length = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004782 (void) psa_cipher_abort( operation );
Janos Follath315b51c2018-07-09 16:04:51 +01004783
Steven Cooremanef8575e2020-09-11 11:44:50 +02004784 return( status );
4785 }
mohammad1603503973b2018-03-12 15:59:30 +02004786}
4787
Gilles Peskinee553c652018-06-04 16:22:46 +02004788psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4789{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004790 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004791 {
Steven Cooremana07b9972020-09-10 14:54:14 +02004792 /* The object has (apparently) been initialized but it is not (yet)
Gilles Peskine81736312018-06-26 15:04:31 +02004793 * in use. It's ok to call abort on such an object, and there's
4794 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004795 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004796 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004797
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004798 /* Sanity check (shouldn't happen: operation->alg should
4799 * always have been initialized to a valid value). */
4800 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4801 return( PSA_ERROR_BAD_STATE );
4802
Steven Cooremanef8575e2020-09-11 11:44:50 +02004803 if( operation->mbedtls_in_use == 0 )
Steven Cooremanfb81aa52020-09-09 12:01:43 +02004804 psa_driver_wrapper_cipher_abort( &operation->ctx.driver );
Steven Cooremand3feccd2020-09-01 15:56:14 +02004805 else
4806 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004807
Moran Peker41deec42018-04-04 15:43:05 +03004808 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004809 operation->key_set = 0;
4810 operation->iv_set = 0;
Steven Cooremanef8575e2020-09-11 11:44:50 +02004811 operation->mbedtls_in_use = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004812 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004813 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004814 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004815
Moran Peker395db872018-05-31 14:07:14 +03004816 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004817}
4818
Gilles Peskinea0655c32018-04-30 17:06:50 +02004819
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004820
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004821
mohammad16035955c982018-04-26 00:53:03 +03004822/****************************************************************/
4823/* AEAD */
4824/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004825
Gilles Peskineedf9a652018-08-17 18:11:56 +02004826typedef struct
4827{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004828 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004829 const mbedtls_cipher_info_t *cipher_info;
4830 union
4831 {
Ronald Cronf95a2b12020-10-22 15:24:49 +02004832 unsigned dummy; /* Make the union non-empty even with no supported algorithms. */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004833#if defined(MBEDTLS_CCM_C)
4834 mbedtls_ccm_context ccm;
4835#endif /* MBEDTLS_CCM_C */
4836#if defined(MBEDTLS_GCM_C)
4837 mbedtls_gcm_context gcm;
4838#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004839#if defined(MBEDTLS_CHACHAPOLY_C)
4840 mbedtls_chachapoly_context chachapoly;
4841#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004842 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004843 psa_algorithm_t core_alg;
4844 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004845 uint8_t tag_length;
4846} aead_operation_t;
4847
Ronald Cronf95a2b12020-10-22 15:24:49 +02004848#define AEAD_OPERATION_INIT {0, 0, {0}, 0, 0, 0}
4849
Gilles Peskine30a9e412019-01-14 18:36:12 +01004850static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004851{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004852 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004853 {
4854#if defined(MBEDTLS_CCM_C)
4855 case PSA_ALG_CCM:
4856 mbedtls_ccm_free( &operation->ctx.ccm );
4857 break;
4858#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004859#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004860 case PSA_ALG_GCM:
4861 mbedtls_gcm_free( &operation->ctx.gcm );
4862 break;
4863#endif /* MBEDTLS_GCM_C */
4864 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02004865
Ronald Cron5c522922020-11-14 16:35:34 +01004866 psa_unlock_key_slot( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004867}
4868
4869static psa_status_t psa_aead_setup( aead_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02004870 mbedtls_svc_key_id_t key,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004871 psa_key_usage_t usage,
4872 psa_algorithm_t alg )
4873{
4874 psa_status_t status;
4875 size_t key_bits;
4876 mbedtls_cipher_id_t cipher_id;
4877
Ronald Cron5c522922020-11-14 16:35:34 +01004878 status = psa_get_and_lock_transparent_key_slot_with_policy(
4879 key, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004880 if( status != PSA_SUCCESS )
4881 return( status );
4882
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004883 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004884
4885 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004886 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004887 &cipher_id );
4888 if( operation->cipher_info == NULL )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004889 {
4890 status = PSA_ERROR_NOT_SUPPORTED;
4891 goto cleanup;
4892 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004893
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004894 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004895 {
4896#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004897 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4898 operation->core_alg = PSA_ALG_CCM;
4899 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004900 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4901 * The call to mbedtls_ccm_encrypt_and_tag or
4902 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004903 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004904 {
4905 status = PSA_ERROR_INVALID_ARGUMENT;
4906 goto cleanup;
4907 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004908 mbedtls_ccm_init( &operation->ctx.ccm );
4909 status = mbedtls_to_psa_error(
4910 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004911 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004912 (unsigned int) key_bits ) );
4913 if( status != 0 )
4914 goto cleanup;
4915 break;
4916#endif /* MBEDTLS_CCM_C */
4917
4918#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004919 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4920 operation->core_alg = PSA_ALG_GCM;
4921 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004922 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4923 * The call to mbedtls_gcm_crypt_and_tag or
4924 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004925 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004926 {
4927 status = PSA_ERROR_INVALID_ARGUMENT;
4928 goto cleanup;
4929 }
Gilles Peskineedf9a652018-08-17 18:11:56 +02004930 mbedtls_gcm_init( &operation->ctx.gcm );
4931 status = mbedtls_to_psa_error(
4932 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004933 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004934 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004935 if( status != 0 )
4936 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004937 break;
4938#endif /* MBEDTLS_GCM_C */
4939
Gilles Peskine26869f22019-05-06 15:25:00 +02004940#if defined(MBEDTLS_CHACHAPOLY_C)
4941 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4942 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4943 operation->full_tag_length = 16;
4944 /* We only support the default tag length. */
4945 if( alg != PSA_ALG_CHACHA20_POLY1305 )
Ronald Cronf95a2b12020-10-22 15:24:49 +02004946 {
4947 status = PSA_ERROR_NOT_SUPPORTED;
4948 goto cleanup;
4949 }
Gilles Peskine26869f22019-05-06 15:25:00 +02004950 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4951 status = mbedtls_to_psa_error(
4952 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004953 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004954 if( status != 0 )
4955 goto cleanup;
4956 break;
4957#endif /* MBEDTLS_CHACHAPOLY_C */
4958
Gilles Peskineedf9a652018-08-17 18:11:56 +02004959 default:
Ronald Cronf95a2b12020-10-22 15:24:49 +02004960 status = PSA_ERROR_NOT_SUPPORTED;
4961 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004962 }
4963
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004964 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4965 {
4966 status = PSA_ERROR_INVALID_ARGUMENT;
4967 goto cleanup;
4968 }
4969 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004970
Gilles Peskineedf9a652018-08-17 18:11:56 +02004971 return( PSA_SUCCESS );
4972
4973cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004974 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004975 return( status );
4976}
4977
Ronald Croncf56a0a2020-08-04 09:51:30 +02004978psa_status_t psa_aead_encrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03004979 psa_algorithm_t alg,
4980 const uint8_t *nonce,
4981 size_t nonce_length,
4982 const uint8_t *additional_data,
4983 size_t additional_data_length,
4984 const uint8_t *plaintext,
4985 size_t plaintext_length,
4986 uint8_t *ciphertext,
4987 size_t ciphertext_size,
4988 size_t *ciphertext_length )
4989{
mohammad16035955c982018-04-26 00:53:03 +03004990 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02004991 aead_operation_t operation = AEAD_OPERATION_INIT;
mohammad160315223a82018-06-03 17:19:55 +03004992 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004993
mohammad1603f08a5502018-06-03 15:05:47 +03004994 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004995
Ronald Croncf56a0a2020-08-04 09:51:30 +02004996 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004997 if( status != PSA_SUCCESS )
4998 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004999
Gilles Peskineedf9a652018-08-17 18:11:56 +02005000 /* For all currently supported modes, the tag is at the end of the
5001 * ciphertext. */
5002 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
5003 {
5004 status = PSA_ERROR_BUFFER_TOO_SMALL;
5005 goto exit;
5006 }
5007 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03005008
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005009#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005010 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005011 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005012 status = mbedtls_to_psa_error(
5013 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
5014 MBEDTLS_GCM_ENCRYPT,
5015 plaintext_length,
5016 nonce, nonce_length,
5017 additional_data, additional_data_length,
5018 plaintext, ciphertext,
5019 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03005020 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005021 else
5022#endif /* MBEDTLS_GCM_C */
5023#if defined(MBEDTLS_CCM_C)
5024 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005025 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005026 status = mbedtls_to_psa_error(
5027 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
5028 plaintext_length,
5029 nonce, nonce_length,
5030 additional_data,
5031 additional_data_length,
5032 plaintext, ciphertext,
5033 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005034 }
mohammad16035c8845f2018-05-09 05:40:09 -07005035 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005036#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005037#if defined(MBEDTLS_CHACHAPOLY_C)
5038 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5039 {
5040 if( nonce_length != 12 || operation.tag_length != 16 )
5041 {
5042 status = PSA_ERROR_NOT_SUPPORTED;
5043 goto exit;
5044 }
5045 status = mbedtls_to_psa_error(
5046 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
5047 plaintext_length,
5048 nonce,
5049 additional_data,
5050 additional_data_length,
5051 plaintext,
5052 ciphertext,
5053 tag ) );
5054 }
5055 else
5056#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07005057 {
mohammad1603554faad2018-06-03 15:07:38 +03005058 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07005059 }
Gilles Peskine2d277862018-06-18 15:41:12 +02005060
Gilles Peskineedf9a652018-08-17 18:11:56 +02005061 if( status != PSA_SUCCESS && ciphertext_size != 0 )
5062 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02005063
Gilles Peskineedf9a652018-08-17 18:11:56 +02005064exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005065 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005066 if( status == PSA_SUCCESS )
5067 *ciphertext_length = plaintext_length + operation.tag_length;
5068 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005069}
5070
Gilles Peskineee652a32018-06-01 19:23:52 +02005071/* Locate the tag in a ciphertext buffer containing the encrypted data
5072 * followed by the tag. Return the length of the part preceding the tag in
5073 * *plaintext_length. This is the size of the plaintext in modes where
5074 * the encrypted data has the same size as the plaintext, such as
5075 * CCM and GCM. */
5076static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
5077 const uint8_t *ciphertext,
5078 size_t ciphertext_length,
5079 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02005080 const uint8_t **p_tag )
5081{
5082 size_t payload_length;
5083 if( tag_length > ciphertext_length )
5084 return( PSA_ERROR_INVALID_ARGUMENT );
5085 payload_length = ciphertext_length - tag_length;
5086 if( payload_length > plaintext_size )
5087 return( PSA_ERROR_BUFFER_TOO_SMALL );
5088 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02005089 return( PSA_SUCCESS );
5090}
5091
Ronald Croncf56a0a2020-08-04 09:51:30 +02005092psa_status_t psa_aead_decrypt( mbedtls_svc_key_id_t key,
mohammad16035955c982018-04-26 00:53:03 +03005093 psa_algorithm_t alg,
5094 const uint8_t *nonce,
5095 size_t nonce_length,
5096 const uint8_t *additional_data,
5097 size_t additional_data_length,
5098 const uint8_t *ciphertext,
5099 size_t ciphertext_length,
5100 uint8_t *plaintext,
5101 size_t plaintext_size,
5102 size_t *plaintext_length )
5103{
mohammad16035955c982018-04-26 00:53:03 +03005104 psa_status_t status;
Ronald Cronf95a2b12020-10-22 15:24:49 +02005105 aead_operation_t operation = AEAD_OPERATION_INIT;
Gilles Peskineedf9a652018-08-17 18:11:56 +02005106 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02005107
Gilles Peskineee652a32018-06-01 19:23:52 +02005108 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03005109
Ronald Croncf56a0a2020-08-04 09:51:30 +02005110 status = psa_aead_setup( &operation, key, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005111 if( status != PSA_SUCCESS )
5112 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005113
Gilles Peskinef7e7b012019-05-06 15:27:16 +02005114 status = psa_aead_unpadded_locate_tag( operation.tag_length,
5115 ciphertext, ciphertext_length,
5116 plaintext_size, &tag );
5117 if( status != PSA_SUCCESS )
5118 goto exit;
5119
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005120#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02005121 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03005122 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005123 status = mbedtls_to_psa_error(
5124 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
5125 ciphertext_length - operation.tag_length,
5126 nonce, nonce_length,
5127 additional_data,
5128 additional_data_length,
5129 tag, operation.tag_length,
5130 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03005131 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005132 else
5133#endif /* MBEDTLS_GCM_C */
5134#if defined(MBEDTLS_CCM_C)
5135 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03005136 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02005137 status = mbedtls_to_psa_error(
5138 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
5139 ciphertext_length - operation.tag_length,
5140 nonce, nonce_length,
5141 additional_data,
5142 additional_data_length,
5143 ciphertext, plaintext,
5144 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03005145 }
mohammad160339574652018-06-01 04:39:53 -07005146 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01005147#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02005148#if defined(MBEDTLS_CHACHAPOLY_C)
5149 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
5150 {
5151 if( nonce_length != 12 || operation.tag_length != 16 )
5152 {
5153 status = PSA_ERROR_NOT_SUPPORTED;
5154 goto exit;
5155 }
5156 status = mbedtls_to_psa_error(
5157 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
5158 ciphertext_length - operation.tag_length,
5159 nonce,
5160 additional_data,
5161 additional_data_length,
5162 tag,
5163 ciphertext,
5164 plaintext ) );
5165 }
5166 else
5167#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07005168 {
mohammad1603554faad2018-06-03 15:07:38 +03005169 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07005170 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02005171
Gilles Peskineedf9a652018-08-17 18:11:56 +02005172 if( status != PSA_SUCCESS && plaintext_size != 0 )
5173 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03005174
Gilles Peskineedf9a652018-08-17 18:11:56 +02005175exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01005176 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02005177 if( status == PSA_SUCCESS )
5178 *plaintext_length = ciphertext_length - operation.tag_length;
5179 return( status );
mohammad16035955c982018-04-26 00:53:03 +03005180}
5181
Gilles Peskinea0655c32018-04-30 17:06:50 +02005182
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02005183
Gilles Peskine20035e32018-02-03 22:44:14 +01005184/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02005185/* Generators */
5186/****************************************************************/
5187
John Durkop07cc04a2020-11-16 22:08:34 -08005188#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF) || \
5189 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5190 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5191#define AT_LEAST_ONE_BUILTIN_KDF
5192#endif
5193
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194#define HKDF_STATE_INIT 0 /* no input yet */
5195#define HKDF_STATE_STARTED 1 /* got salt */
5196#define HKDF_STATE_KEYED 2 /* got key */
5197#define HKDF_STATE_OUTPUT 3 /* output started */
5198
Gilles Peskinecbe66502019-05-16 16:59:18 +02005199static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005201{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005202 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
5203 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005204 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005205 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005206}
5207
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005208psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005209{
5210 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005212 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005213 {
5214 /* The object has (apparently) been initialized but it is not
5215 * in use. It's ok to call abort on such an object, and there's
5216 * nothing to do. */
5217 }
5218 else
John Durkopd0321952020-10-29 21:37:36 -07005219#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005220 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005221 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005222 mbedtls_free( operation->ctx.hkdf.info );
5223 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005224 }
John Durkop07cc04a2020-11-16 22:08:34 -08005225 else
5226#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5227#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5228 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5229 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005230 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005231 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005232 {
Janos Follath6a1d2622019-06-11 10:37:28 +01005233 if( operation->ctx.tls12_prf.seed != NULL )
5234 {
5235 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
5236 operation->ctx.tls12_prf.seed_length );
5237 mbedtls_free( operation->ctx.tls12_prf.seed );
5238 }
5239
5240 if( operation->ctx.tls12_prf.label != NULL )
5241 {
5242 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
5243 operation->ctx.tls12_prf.label_length );
5244 mbedtls_free( operation->ctx.tls12_prf.label );
5245 }
5246
5247 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
5248
5249 /* We leave the fields Ai and output_block to be erased safely by the
5250 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005251 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005252 else
John Durkop07cc04a2020-11-16 22:08:34 -08005253#endif /* defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5254 * defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS) */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005255 {
5256 status = PSA_ERROR_BAD_STATE;
5257 }
Janos Follath083036a2019-06-11 10:22:26 +01005258 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005259 return( status );
5260}
5261
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005262psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02005263 size_t *capacity)
5264{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005265 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005266 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005267 /* This is a blank key derivation operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005268 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005269 }
5270
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005271 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005272 return( PSA_SUCCESS );
5273}
5274
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005275psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005276 size_t capacity )
5277{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005278 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005279 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005280 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005281 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005282 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005283 return( PSA_SUCCESS );
5284}
5285
John Durkopd0321952020-10-29 21:37:36 -07005286#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005287/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02005288 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02005289static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005290 psa_algorithm_t hash_alg,
5291 uint8_t *output,
5292 size_t output_length )
5293{
5294 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5295 psa_status_t status;
5296
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005297 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
5298 return( PSA_ERROR_BAD_STATE );
5299 hkdf->state = HKDF_STATE_OUTPUT;
5300
Gilles Peskinebef7f142018-07-12 17:22:21 +02005301 while( output_length != 0 )
5302 {
5303 /* Copy what remains of the current block */
5304 uint8_t n = hash_length - hkdf->offset_in_block;
5305 if( n > output_length )
5306 n = (uint8_t) output_length;
5307 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
5308 output += n;
5309 output_length -= n;
5310 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02005311 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005312 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02005313 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005314 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005315 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02005316 * object was corrupted or if this function is called directly
5317 * inside the library. */
5318 if( hkdf->block_number == 0xff )
5319 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02005320
5321 /* We need a new block */
5322 ++hkdf->block_number;
5323 hkdf->offset_in_block = 0;
5324 status = psa_hmac_setup_internal( &hkdf->hmac,
5325 hkdf->prk, hash_length,
5326 hash_alg );
5327 if( status != PSA_SUCCESS )
5328 return( status );
5329 if( hkdf->block_number != 1 )
5330 {
5331 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5332 hkdf->output_block,
5333 hash_length );
5334 if( status != PSA_SUCCESS )
5335 return( status );
5336 }
5337 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5338 hkdf->info,
5339 hkdf->info_length );
5340 if( status != PSA_SUCCESS )
5341 return( status );
5342 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5343 &hkdf->block_number, 1 );
5344 if( status != PSA_SUCCESS )
5345 return( status );
5346 status = psa_hmac_finish_internal( &hkdf->hmac,
5347 hkdf->output_block,
5348 sizeof( hkdf->output_block ) );
5349 if( status != PSA_SUCCESS )
5350 return( status );
5351 }
5352
5353 return( PSA_SUCCESS );
5354}
John Durkop07cc04a2020-11-16 22:08:34 -08005355#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005356
John Durkop07cc04a2020-11-16 22:08:34 -08005357#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5358 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follath7742fee2019-06-17 12:58:10 +01005359static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
5360 psa_tls12_prf_key_derivation_t *tls12_prf,
5361 psa_algorithm_t alg )
5362{
5363 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
5364 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01005365 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
5366 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005367
Janos Follath7742fee2019-06-17 12:58:10 +01005368 /* We can't be wanting more output after block 0xff, otherwise
5369 * the capacity check in psa_key_derivation_output_bytes() would have
5370 * prevented this call. It could happen only if the operation
5371 * object was corrupted or if this function is called directly
5372 * inside the library. */
5373 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01005374 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01005375
5376 /* We need a new block */
5377 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01005378 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01005379
5380 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
5381 *
5382 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
5383 *
5384 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
5385 * HMAC_hash(secret, A(2) + seed) +
5386 * HMAC_hash(secret, A(3) + seed) + ...
5387 *
5388 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01005389 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01005390 *
Janos Follathea29bfb2019-06-19 12:21:20 +01005391 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01005392 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01005393 * is currently extracted as `output_block` and where i is
5394 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01005395 */
5396
Janos Follathea29bfb2019-06-19 12:21:20 +01005397 /* Save the hash context before using it, to preserve the hash state with
5398 * only the inner padding in it. We need this, because inner padding depends
5399 * on the key (secret in the RFC's terminology). */
5400 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
5401 if( status != PSA_SUCCESS )
5402 goto cleanup;
5403
5404 /* Calculate A(i) where i = tls12_prf->block_number. */
5405 if( tls12_prf->block_number == 1 )
5406 {
5407 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
5408 * the variable seed and in this instance means it in the context of the
5409 * P_hash function, where seed = label + seed.) */
5410 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5411 tls12_prf->label, tls12_prf->label_length );
5412 if( status != PSA_SUCCESS )
5413 goto cleanup;
5414 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5415 tls12_prf->seed, tls12_prf->seed_length );
5416 if( status != PSA_SUCCESS )
5417 goto cleanup;
5418 }
5419 else
5420 {
5421 /* A(i) = HMAC_hash(secret, A(i-1)) */
5422 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5423 tls12_prf->Ai, hash_length );
5424 if( status != PSA_SUCCESS )
5425 goto cleanup;
5426 }
5427
5428 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5429 tls12_prf->Ai, hash_length );
5430 if( status != PSA_SUCCESS )
5431 goto cleanup;
5432 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5433 if( status != PSA_SUCCESS )
5434 goto cleanup;
5435
5436 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
5437 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5438 tls12_prf->Ai, hash_length );
5439 if( status != PSA_SUCCESS )
5440 goto cleanup;
5441 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5442 tls12_prf->label, tls12_prf->label_length );
5443 if( status != PSA_SUCCESS )
5444 goto cleanup;
5445 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
5446 tls12_prf->seed, tls12_prf->seed_length );
5447 if( status != PSA_SUCCESS )
5448 goto cleanup;
5449 status = psa_hmac_finish_internal( &tls12_prf->hmac,
5450 tls12_prf->output_block, hash_length );
5451 if( status != PSA_SUCCESS )
5452 goto cleanup;
5453 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
5454 if( status != PSA_SUCCESS )
5455 goto cleanup;
5456
Janos Follath7742fee2019-06-17 12:58:10 +01005457
5458cleanup:
5459
Janos Follathea29bfb2019-06-19 12:21:20 +01005460 cleanup_status = psa_hash_abort( &backup );
5461 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
5462 status = cleanup_status;
5463
5464 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01005465}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005466
Janos Follath844eb0e2019-06-19 12:10:49 +01005467static psa_status_t psa_key_derivation_tls12_prf_read(
5468 psa_tls12_prf_key_derivation_t *tls12_prf,
5469 psa_algorithm_t alg,
5470 uint8_t *output,
5471 size_t output_length )
5472{
5473 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
5474 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
5475 psa_status_t status;
5476 uint8_t offset, length;
5477
5478 while( output_length != 0 )
5479 {
5480 /* Check if we have fully processed the current block. */
5481 if( tls12_prf->left_in_block == 0 )
5482 {
5483 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
5484 alg );
5485 if( status != PSA_SUCCESS )
5486 return( status );
5487
5488 continue;
5489 }
5490
5491 if( tls12_prf->left_in_block > output_length )
5492 length = (uint8_t) output_length;
5493 else
5494 length = tls12_prf->left_in_block;
5495
5496 offset = hash_length - tls12_prf->left_in_block;
5497 memcpy( output, tls12_prf->output_block + offset, length );
5498 output += length;
5499 output_length -= length;
5500 tls12_prf->left_in_block -= length;
5501 }
5502
5503 return( PSA_SUCCESS );
5504}
John Durkop07cc04a2020-11-16 22:08:34 -08005505#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5506 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskinebef7f142018-07-12 17:22:21 +02005507
Janos Follath6c6c8fc2019-06-17 12:38:20 +01005508psa_status_t psa_key_derivation_output_bytes(
5509 psa_key_derivation_operation_t *operation,
5510 uint8_t *output,
5511 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005512{
5513 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005514 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005515
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005516 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005517 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005518 /* This is a blank operation. */
Steven Cooreman29149862020-08-05 15:43:42 +02005519 return( PSA_ERROR_BAD_STATE );
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005520 }
5521
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005522 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005523 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005524 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005525 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005526 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02005527 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005528 goto exit;
5529 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005530 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005531 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005532 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005533 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02005534 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
5535 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005536 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02005537 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02005538 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005539 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005540 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005541
John Durkopd0321952020-10-29 21:37:36 -07005542#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005543 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02005544 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005545 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005546 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02005547 output, output_length );
5548 }
Janos Follathadbec812019-06-14 11:05:39 +01005549 else
John Durkop07cc04a2020-11-16 22:08:34 -08005550#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
5551#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5552 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathadbec812019-06-14 11:05:39 +01005553 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
John Durkop07cc04a2020-11-16 22:08:34 -08005554 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005555 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005556 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005557 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01005558 output_length );
5559 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02005560 else
John Durkop07cc04a2020-11-16 22:08:34 -08005561#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF ||
5562 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineeab56e42018-07-12 17:12:33 +02005563 {
5564 return( PSA_ERROR_BAD_STATE );
5565 }
5566
5567exit:
5568 if( status != PSA_SUCCESS )
5569 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00005570 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005571 * This allows us to differentiate between exhausted operations and
5572 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
5573 * operations. */
5574 psa_algorithm_t alg = operation->alg;
5575 psa_key_derivation_abort( operation );
5576 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02005577 memset( output, '!', output_length );
5578 }
5579 return( status );
5580}
5581
Gilles Peskine08542d82018-07-19 17:05:42 +02005582#if defined(MBEDTLS_DES_C)
5583static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
5584{
5585 if( data_size >= 8 )
5586 mbedtls_des_key_set_parity( data );
5587 if( data_size >= 16 )
5588 mbedtls_des_key_set_parity( data + 8 );
5589 if( data_size >= 24 )
5590 mbedtls_des_key_set_parity( data + 16 );
5591}
5592#endif /* MBEDTLS_DES_C */
5593
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005594static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005595 psa_key_slot_t *slot,
5596 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005597 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005598{
5599 uint8_t *data = NULL;
5600 size_t bytes = PSA_BITS_TO_BYTES( bits );
5601 psa_status_t status;
5602
Gilles Peskine8e338702019-07-30 20:06:31 +02005603 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02005604 return( PSA_ERROR_INVALID_ARGUMENT );
5605 if( bits % 8 != 0 )
5606 return( PSA_ERROR_INVALID_ARGUMENT );
5607 data = mbedtls_calloc( 1, bytes );
5608 if( data == NULL )
5609 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5610
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005611 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005612 if( status != PSA_SUCCESS )
5613 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02005614#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02005615 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02005616 psa_des_set_key_parity( data, bytes );
5617#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005618 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02005619
5620exit:
5621 mbedtls_free( data );
5622 return( status );
5623}
5624
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005625psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005626 psa_key_derivation_operation_t *operation,
Ronald Croncf56a0a2020-08-04 09:51:30 +02005627 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005628{
5629 psa_status_t status;
5630 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005631 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02005632
Ronald Cron81709fc2020-11-14 12:10:32 +01005633 *key = MBEDTLS_SVC_KEY_ID_INIT;
5634
Gilles Peskine0f84d622019-09-12 19:03:13 +02005635 /* Reject any attempt to create a zero-length key so that we don't
5636 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5637 if( psa_get_key_bits( attributes ) == 0 )
5638 return( PSA_ERROR_INVALID_ARGUMENT );
5639
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005640 if( ! operation->can_output_key )
5641 return( PSA_ERROR_NOT_PERMITTED );
5642
Ronald Cron81709fc2020-11-14 12:10:32 +01005643 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE, attributes,
5644 &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005645#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5646 if( driver != NULL )
5647 {
5648 /* Deriving a key in a secure element is not implemented yet. */
5649 status = PSA_ERROR_NOT_SUPPORTED;
5650 }
5651#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005652 if( status == PSA_SUCCESS )
5653 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01005654 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005655 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005656 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005657 }
5658 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01005659 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005660 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005661 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02005662
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005663 return( status );
5664}
5665
Gilles Peskineeab56e42018-07-12 17:12:33 +02005666
5667
5668/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02005669/* Key derivation */
5670/****************************************************************/
5671
John Durkop07cc04a2020-11-16 22:08:34 -08005672#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine969c5d62019-01-16 15:53:06 +01005673static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005674 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005675 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005676{
John Durkop07cc04a2020-11-16 22:08:34 -08005677 int is_kdf_alg_supported;
5678
Janos Follath5fe19732019-06-20 15:09:30 +01005679 /* Make sure that operation->ctx is properly zero-initialised. (Macro
5680 * initialisers for this union leave some bytes unspecified.) */
5681 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
5682
Gilles Peskine969c5d62019-01-16 15:53:06 +01005683 /* Make sure that kdf_alg is a supported key derivation algorithm. */
John Durkopd0321952020-10-29 21:37:36 -07005684#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
John Durkop07cc04a2020-11-16 22:08:34 -08005685 if( PSA_ALG_IS_HKDF( kdf_alg ) )
5686 is_kdf_alg_supported = 1;
5687 else
5688#endif
5689#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
5690 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
5691 is_kdf_alg_supported = 1;
5692 else
5693#endif
5694#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5695 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5696 is_kdf_alg_supported = 1;
5697 else
5698#endif
5699 is_kdf_alg_supported = 0;
5700
5701 if( is_kdf_alg_supported )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005702 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01005703 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005704 size_t hash_size = PSA_HASH_SIZE( hash_alg );
5705 if( hash_size == 0 )
5706 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005707 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
5708 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02005709 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005710 {
5711 return( PSA_ERROR_NOT_SUPPORTED );
5712 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005713 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005714 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005715 }
John Durkop07cc04a2020-11-16 22:08:34 -08005716
5717 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005718}
John Durkop07cc04a2020-11-16 22:08:34 -08005719#endif /* AT_LEAST_ONE_BUILTIN_KDF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01005720
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005721psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005722 psa_algorithm_t alg )
5723{
5724 psa_status_t status;
5725
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005726 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005727 return( PSA_ERROR_BAD_STATE );
5728
Gilles Peskine6843c292019-01-18 16:44:49 +01005729 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
5730 return( PSA_ERROR_INVALID_ARGUMENT );
John Durkop07cc04a2020-11-16 22:08:34 -08005731#ifdef AT_LEAST_ONE_BUILTIN_KDF
Gilles Peskine6843c292019-01-18 16:44:49 +01005732 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01005733 {
5734 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005735 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005736 }
5737 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
5738 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005739 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005740 }
John Durkop07cc04a2020-11-16 22:08:34 -08005741#endif
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005742 else
5743 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005744
5745 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005746 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01005747 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005748}
5749
John Durkopd0321952020-10-29 21:37:36 -07005750#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskinecbe66502019-05-16 16:59:18 +02005751static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005752 psa_algorithm_t hash_alg,
5753 psa_key_derivation_step_t step,
5754 const uint8_t *data,
5755 size_t data_length )
5756{
5757 psa_status_t status;
5758 switch( step )
5759 {
Gilles Peskine03410b52019-05-16 16:05:19 +02005760 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02005761 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005762 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005763 status = psa_hmac_setup_internal( &hkdf->hmac,
5764 data, data_length,
5765 hash_alg );
5766 if( status != PSA_SUCCESS )
5767 return( status );
5768 hkdf->state = HKDF_STATE_STARTED;
5769 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005770 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005771 /* If no salt was provided, use an empty salt. */
5772 if( hkdf->state == HKDF_STATE_INIT )
5773 {
5774 status = psa_hmac_setup_internal( &hkdf->hmac,
5775 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02005776 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005777 if( status != PSA_SUCCESS )
5778 return( status );
5779 hkdf->state = HKDF_STATE_STARTED;
5780 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02005781 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005782 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02005783 status = psa_hash_update( &hkdf->hmac.hash_ctx,
5784 data, data_length );
5785 if( status != PSA_SUCCESS )
5786 return( status );
5787 status = psa_hmac_finish_internal( &hkdf->hmac,
5788 hkdf->prk,
5789 sizeof( hkdf->prk ) );
5790 if( status != PSA_SUCCESS )
5791 return( status );
5792 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
5793 hkdf->block_number = 0;
5794 hkdf->state = HKDF_STATE_KEYED;
5795 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02005796 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005797 if( hkdf->state == HKDF_STATE_OUTPUT )
5798 return( PSA_ERROR_BAD_STATE );
5799 if( hkdf->info_set )
5800 return( PSA_ERROR_BAD_STATE );
5801 hkdf->info_length = data_length;
5802 if( data_length != 0 )
5803 {
5804 hkdf->info = mbedtls_calloc( 1, data_length );
5805 if( hkdf->info == NULL )
5806 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5807 memcpy( hkdf->info, data, data_length );
5808 }
5809 hkdf->info_set = 1;
5810 return( PSA_SUCCESS );
5811 default:
5812 return( PSA_ERROR_INVALID_ARGUMENT );
5813 }
5814}
John Durkop07cc04a2020-11-16 22:08:34 -08005815#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
Janos Follathb03233e2019-06-11 15:30:30 +01005816
John Durkop07cc04a2020-11-16 22:08:34 -08005817#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) || \
5818 defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
Janos Follathf08e2652019-06-13 09:05:41 +01005819static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5820 const uint8_t *data,
5821 size_t data_length )
5822{
5823 if( prf->state != TLS12_PRF_STATE_INIT )
5824 return( PSA_ERROR_BAD_STATE );
5825
Janos Follathd6dce9f2019-07-04 09:11:38 +01005826 if( data_length != 0 )
5827 {
5828 prf->seed = mbedtls_calloc( 1, data_length );
5829 if( prf->seed == NULL )
5830 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005831
Janos Follathd6dce9f2019-07-04 09:11:38 +01005832 memcpy( prf->seed, data, data_length );
5833 prf->seed_length = data_length;
5834 }
Janos Follathf08e2652019-06-13 09:05:41 +01005835
5836 prf->state = TLS12_PRF_STATE_SEED_SET;
5837
5838 return( PSA_SUCCESS );
5839}
5840
Janos Follath81550542019-06-13 14:26:34 +01005841static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5842 psa_algorithm_t hash_alg,
5843 const uint8_t *data,
5844 size_t data_length )
5845{
5846 psa_status_t status;
5847 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5848 return( PSA_ERROR_BAD_STATE );
5849
5850 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5851 if( status != PSA_SUCCESS )
5852 return( status );
5853
5854 prf->state = TLS12_PRF_STATE_KEY_SET;
5855
5856 return( PSA_SUCCESS );
5857}
5858
Janos Follath63028dd2019-06-13 09:15:47 +01005859static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5860 const uint8_t *data,
5861 size_t data_length )
5862{
5863 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5864 return( PSA_ERROR_BAD_STATE );
5865
Janos Follathd6dce9f2019-07-04 09:11:38 +01005866 if( data_length != 0 )
5867 {
5868 prf->label = mbedtls_calloc( 1, data_length );
5869 if( prf->label == NULL )
5870 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005871
Janos Follathd6dce9f2019-07-04 09:11:38 +01005872 memcpy( prf->label, data, data_length );
5873 prf->label_length = data_length;
5874 }
Janos Follath63028dd2019-06-13 09:15:47 +01005875
5876 prf->state = TLS12_PRF_STATE_LABEL_SET;
5877
5878 return( PSA_SUCCESS );
5879}
5880
Janos Follathb03233e2019-06-11 15:30:30 +01005881static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5882 psa_algorithm_t hash_alg,
5883 psa_key_derivation_step_t step,
5884 const uint8_t *data,
5885 size_t data_length )
5886{
Janos Follathb03233e2019-06-11 15:30:30 +01005887 switch( step )
5888 {
Janos Follathf08e2652019-06-13 09:05:41 +01005889 case PSA_KEY_DERIVATION_INPUT_SEED:
5890 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005891 case PSA_KEY_DERIVATION_INPUT_SECRET:
5892 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005893 case PSA_KEY_DERIVATION_INPUT_LABEL:
5894 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005895 default:
5896 return( PSA_ERROR_INVALID_ARGUMENT );
5897 }
5898}
John Durkop07cc04a2020-11-16 22:08:34 -08005899#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF) ||
5900 * MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
5901
5902#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
5903static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5904 psa_tls12_prf_key_derivation_t *prf,
5905 psa_algorithm_t hash_alg,
5906 const uint8_t *data,
5907 size_t data_length )
5908{
5909 psa_status_t status;
5910 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5911 uint8_t *cur = pms;
5912
5913 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5914 return( PSA_ERROR_INVALID_ARGUMENT );
5915
5916 /* Quoting RFC 4279, Section 2:
5917 *
5918 * The premaster secret is formed as follows: if the PSK is N octets
5919 * long, concatenate a uint16 with the value N, N zero octets, a second
5920 * uint16 with the value N, and the PSK itself.
5921 */
5922
5923 *cur++ = ( data_length >> 8 ) & 0xff;
5924 *cur++ = ( data_length >> 0 ) & 0xff;
5925 memset( cur, 0, data_length );
5926 cur += data_length;
5927 *cur++ = pms[0];
5928 *cur++ = pms[1];
5929 memcpy( cur, data, data_length );
5930 cur += data_length;
5931
5932 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
5933
5934 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5935 return( status );
5936}
Janos Follath6660f0e2019-06-17 08:44:03 +01005937
5938static psa_status_t psa_tls12_prf_psk_to_ms_input(
5939 psa_tls12_prf_key_derivation_t *prf,
5940 psa_algorithm_t hash_alg,
5941 psa_key_derivation_step_t step,
5942 const uint8_t *data,
5943 size_t data_length )
5944{
5945 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005946 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005947 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5948 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005949 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005950
5951 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5952}
John Durkop07cc04a2020-11-16 22:08:34 -08005953#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005954
Gilles Peskineb8965192019-09-24 16:21:10 +02005955/** Check whether the given key type is acceptable for the given
5956 * input step of a key derivation.
5957 *
5958 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5959 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5960 * Both secret and non-secret inputs can alternatively have the type
5961 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5962 * that the input was passed as a buffer rather than via a key object.
5963 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005964static int psa_key_derivation_check_input_type(
5965 psa_key_derivation_step_t step,
5966 psa_key_type_t key_type )
5967{
5968 switch( step )
5969 {
5970 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005971 if( key_type == PSA_KEY_TYPE_DERIVE )
5972 return( PSA_SUCCESS );
5973 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005974 return( PSA_SUCCESS );
5975 break;
5976 case PSA_KEY_DERIVATION_INPUT_LABEL:
5977 case PSA_KEY_DERIVATION_INPUT_SALT:
5978 case PSA_KEY_DERIVATION_INPUT_INFO:
5979 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005980 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5981 return( PSA_SUCCESS );
5982 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005983 return( PSA_SUCCESS );
5984 break;
5985 }
5986 return( PSA_ERROR_INVALID_ARGUMENT );
5987}
5988
Janos Follathb80a94e2019-06-12 15:54:46 +01005989static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005990 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005991 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005992 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005993 const uint8_t *data,
5994 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005995{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005996 psa_status_t status;
5997 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5998
5999 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02006000 if( status != PSA_SUCCESS )
6001 goto exit;
6002
John Durkopd0321952020-10-29 21:37:36 -07006003#if defined(MBEDTLS_PSA_BUILTIN_ALG_HKDF)
Gilles Peskine969c5d62019-01-16 15:53:06 +01006004 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006005 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006006 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006007 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006008 step, data, data_length );
6009 }
John Durkop07cc04a2020-11-16 22:08:34 -08006010 else
6011#endif /* MBEDTLS_PSA_BUILTIN_ALG_HKDF */
6012#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF)
6013 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006014 {
Janos Follathb03233e2019-06-11 15:30:30 +01006015 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
6016 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6017 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01006018 }
John Durkop07cc04a2020-11-16 22:08:34 -08006019 else
6020#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PRF */
6021#if defined(MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS)
6022 if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Janos Follath6660f0e2019-06-17 08:44:03 +01006023 {
6024 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
6025 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
6026 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006027 }
6028 else
John Durkop07cc04a2020-11-16 22:08:34 -08006029#endif /* MBEDTLS_PSA_BUILTIN_ALG_TLS12_PSK_TO_MS */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006030 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006031 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006032 return( PSA_ERROR_BAD_STATE );
6033 }
6034
Gilles Peskine224b0d62019-09-23 18:13:17 +02006035exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006036 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006037 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006038 return( status );
6039}
6040
Janos Follath51f4a0f2019-06-14 11:35:55 +01006041psa_status_t psa_key_derivation_input_bytes(
6042 psa_key_derivation_operation_t *operation,
6043 psa_key_derivation_step_t step,
6044 const uint8_t *data,
6045 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006046{
Gilles Peskineb8965192019-09-24 16:21:10 +02006047 return( psa_key_derivation_input_internal( operation, step,
6048 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01006049 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01006050}
6051
Janos Follath51f4a0f2019-06-14 11:35:55 +01006052psa_status_t psa_key_derivation_input_key(
6053 psa_key_derivation_operation_t *operation,
6054 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006055 mbedtls_svc_key_id_t key )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006056{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006057 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006058 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006059 psa_key_slot_t *slot;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006060
Ronald Cron5c522922020-11-14 16:35:34 +01006061 status = psa_get_and_lock_transparent_key_slot_with_policy(
6062 key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006063 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02006064 {
6065 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006066 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02006067 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02006068
6069 /* Passing a key object as a SECRET input unlocks the permission
6070 * to output to a key object. */
6071 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6072 operation->can_output_key = 1;
6073
Ronald Cronf95a2b12020-10-22 15:24:49 +02006074 status = psa_key_derivation_input_internal( operation,
6075 step, slot->attr.type,
6076 slot->data.key.data,
6077 slot->data.key.bytes );
6078
Ronald Cron5c522922020-11-14 16:35:34 +01006079 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006080
Ronald Cron5c522922020-11-14 16:35:34 +01006081 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01006082}
Gilles Peskineea0fb492018-07-12 17:17:20 +02006083
6084
6085
6086/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02006087/* Key agreement */
6088/****************************************************************/
6089
John Durkop6ba40d12020-11-10 08:50:04 -08006090#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006091static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
6092 size_t peer_key_length,
6093 const mbedtls_ecp_keypair *our_key,
6094 uint8_t *shared_secret,
6095 size_t shared_secret_size,
6096 size_t *shared_secret_length )
6097{
Steven Cooremand4867872020-08-05 16:31:39 +02006098 mbedtls_ecp_keypair *their_key = NULL;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006099 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00006100 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006101 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01006102 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006103 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006104
Steven Cooreman4fed4552020-08-03 14:46:03 +02006105 status = psa_load_ecp_representation( PSA_KEY_TYPE_ECC_PUBLIC_KEY(curve),
6106 peer_key,
Steven Cooreman7f391872020-07-30 14:57:44 +02006107 peer_key_length,
Steven Cooreman7f391872020-07-30 14:57:44 +02006108 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00006109 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006110 goto exit;
6111
Jaeden Amero97271b32019-01-10 19:38:51 +00006112 status = mbedtls_to_psa_error(
Steven Cooremana2371e52020-07-28 14:30:39 +02006113 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
Jaeden Amero97271b32019-01-10 19:38:51 +00006114 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006115 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00006116 status = mbedtls_to_psa_error(
6117 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
6118 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006119 goto exit;
6120
Jaeden Amero97271b32019-01-10 19:38:51 +00006121 status = mbedtls_to_psa_error(
6122 mbedtls_ecdh_calc_secret( &ecdh,
6123 shared_secret_length,
6124 shared_secret, shared_secret_size,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006125 mbedtls_psa_get_random,
6126 mbedtls_psa_random_state( &global_data.rng ) ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01006127 if( status != PSA_SUCCESS )
6128 goto exit;
6129 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
6130 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006131
6132exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01006133 if( status != PSA_SUCCESS )
6134 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006135 mbedtls_ecdh_free( &ecdh );
Steven Cooremana2371e52020-07-28 14:30:39 +02006136 mbedtls_ecp_keypair_free( their_key );
Steven Cooreman6d839f02020-07-30 11:36:45 +02006137 mbedtls_free( their_key );
Steven Cooremana2371e52020-07-28 14:30:39 +02006138
Jaeden Amero97271b32019-01-10 19:38:51 +00006139 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006140}
John Durkop6ba40d12020-11-10 08:50:04 -08006141#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006142
Gilles Peskine01d718c2018-09-18 12:01:02 +02006143#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
6144
Gilles Peskine0216fe12019-04-11 21:23:21 +02006145static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
6146 psa_key_slot_t *private_key,
6147 const uint8_t *peer_key,
6148 size_t peer_key_length,
6149 uint8_t *shared_secret,
6150 size_t shared_secret_size,
6151 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006152{
Gilles Peskine0216fe12019-04-11 21:23:21 +02006153 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006154 {
John Durkop6ba40d12020-11-10 08:50:04 -08006155#if defined(MBEDTLS_PSA_BUILTIN_ALG_ECDH)
Gilles Peskine0216fe12019-04-11 21:23:21 +02006156 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02006157 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02006158 return( PSA_ERROR_INVALID_ARGUMENT );
Steven Cooremana2371e52020-07-28 14:30:39 +02006159 mbedtls_ecp_keypair *ecp = NULL;
Steven Cooreman7f391872020-07-30 14:57:44 +02006160 psa_status_t status = psa_load_ecp_representation(
Steven Cooreman4fed4552020-08-03 14:46:03 +02006161 private_key->attr.type,
Steven Cooreman7f391872020-07-30 14:57:44 +02006162 private_key->data.key.data,
6163 private_key->data.key.bytes,
Steven Cooreman7f391872020-07-30 14:57:44 +02006164 &ecp );
Steven Cooremanacda8342020-07-24 23:09:52 +02006165 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006166 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006167 status = psa_key_agreement_ecdh( peer_key, peer_key_length,
Steven Cooremana2371e52020-07-28 14:30:39 +02006168 ecp,
Steven Cooremanacda8342020-07-24 23:09:52 +02006169 shared_secret, shared_secret_size,
6170 shared_secret_length );
Steven Cooremana2371e52020-07-28 14:30:39 +02006171 mbedtls_ecp_keypair_free( ecp );
6172 mbedtls_free( ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006173 return( status );
John Durkop6ba40d12020-11-10 08:50:04 -08006174#endif /* MBEDTLS_PSA_BUILTIN_ALG_ECDH */
Gilles Peskine01d718c2018-09-18 12:01:02 +02006175 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01006176 (void) private_key;
6177 (void) peer_key;
6178 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006179 (void) shared_secret;
6180 (void) shared_secret_size;
6181 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02006182 return( PSA_ERROR_NOT_SUPPORTED );
6183 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02006184}
6185
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02006186/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02006187 * to potentially free embedded data structures and wipe confidential data.
6188 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006189static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006190 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02006191 psa_key_slot_t *private_key,
6192 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006193 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02006194{
6195 psa_status_t status;
6196 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
6197 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006198 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02006199
6200 /* Step 1: run the secret agreement algorithm to generate the shared
6201 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02006202 status = psa_key_agreement_raw_internal( ka_alg,
6203 private_key,
6204 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03006205 shared_secret,
6206 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02006207 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02006208 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006209 goto exit;
6210
Gilles Peskineb0b255c2018-07-06 17:01:38 +02006211 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02006212 * the shared secret. A shared secret is permitted wherever a key
6213 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01006214 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02006215 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01006216 shared_secret,
6217 shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006218exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01006219 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006220 return( status );
6221}
6222
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006223psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006224 psa_key_derivation_step_t step,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006225 mbedtls_svc_key_id_t private_key,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02006226 const uint8_t *peer_key,
6227 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006228{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006229 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006230 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2f060a82018-12-04 17:12:32 +01006231 psa_key_slot_t *slot;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006232
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006233 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02006234 return( PSA_ERROR_INVALID_ARGUMENT );
Ronald Cron5c522922020-11-14 16:35:34 +01006235 status = psa_get_and_lock_transparent_key_slot_with_policy(
6236 private_key, &slot, PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02006237 if( status != PSA_SUCCESS )
6238 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006239 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01006240 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01006241 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01006242 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02006243 psa_key_derivation_abort( operation );
Steven Cooremanfa5e6312020-10-15 17:07:12 +02006244 else
6245 {
6246 /* If a private key has been added as SECRET, we allow the derived
6247 * key material to be used as a key in PSA Crypto. */
6248 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
6249 operation->can_output_key = 1;
6250 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006251
Ronald Cron5c522922020-11-14 16:35:34 +01006252 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006253
Ronald Cron5c522922020-11-14 16:35:34 +01006254 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02006255}
6256
Gilles Peskinebe697d82019-05-16 18:00:41 +02006257psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006258 mbedtls_svc_key_id_t private_key,
Gilles Peskinebe697d82019-05-16 18:00:41 +02006259 const uint8_t *peer_key,
6260 size_t peer_key_length,
6261 uint8_t *output,
6262 size_t output_size,
6263 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02006264{
Ronald Cronf95a2b12020-10-22 15:24:49 +02006265 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cron5c522922020-11-14 16:35:34 +01006266 psa_status_t unlock_status = PSA_ERROR_CORRUPTION_DETECTED;
Ronald Cronf95a2b12020-10-22 15:24:49 +02006267 psa_key_slot_t *slot = NULL;
Gilles Peskine0216fe12019-04-11 21:23:21 +02006268
6269 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
6270 {
6271 status = PSA_ERROR_INVALID_ARGUMENT;
6272 goto exit;
6273 }
Ronald Cron5c522922020-11-14 16:35:34 +01006274 status = psa_get_and_lock_transparent_key_slot_with_policy(
6275 private_key, &slot, PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006276 if( status != PSA_SUCCESS )
6277 goto exit;
6278
6279 status = psa_key_agreement_raw_internal( alg, slot,
6280 peer_key, peer_key_length,
6281 output, output_size,
6282 output_length );
6283
6284exit:
6285 if( status != PSA_SUCCESS )
6286 {
6287 /* If an error happens and is not handled properly, the output
6288 * may be used as a key to protect sensitive data. Arrange for such
6289 * a key to be random, which is likely to result in decryption or
6290 * verification errors. This is better than filling the buffer with
6291 * some constant data such as zeros, which would result in the data
6292 * being protected with a reproducible, easily knowable key.
6293 */
6294 psa_generate_random( output, output_size );
6295 *output_length = output_size;
6296 }
Ronald Cronf95a2b12020-10-22 15:24:49 +02006297
Ronald Cron5c522922020-11-14 16:35:34 +01006298 unlock_status = psa_unlock_key_slot( slot );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006299
Ronald Cron5c522922020-11-14 16:35:34 +01006300 return( ( status == PSA_SUCCESS ) ? unlock_status : status );
Gilles Peskine0216fe12019-04-11 21:23:21 +02006301}
Gilles Peskine86a440b2018-11-12 18:39:40 +01006302
6303
Gilles Peskine30524eb2020-11-13 17:02:26 +01006304
Gilles Peskine86a440b2018-11-12 18:39:40 +01006305/****************************************************************/
6306/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02006307/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02006308
Gilles Peskine30524eb2020-11-13 17:02:26 +01006309/** Initialize the PSA random generator.
6310 */
6311static void mbedtls_psa_random_init( mbedtls_psa_random_context_t *rng )
6312{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006313#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6314 memset( rng, 0, sizeof( *rng ) );
6315#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6316
Gilles Peskine30524eb2020-11-13 17:02:26 +01006317 /* Set default configuration if
6318 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
6319 if( rng->entropy_init == NULL )
6320 rng->entropy_init = mbedtls_entropy_init;
6321 if( rng->entropy_free == NULL )
6322 rng->entropy_free = mbedtls_entropy_free;
6323
6324 rng->entropy_init( &rng->entropy );
6325#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
6326 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
6327 /* The PSA entropy injection feature depends on using NV seed as an entropy
6328 * source. Add NV seed as an entropy source for PSA entropy injection. */
6329 mbedtls_entropy_add_source( &rng->entropy,
6330 mbedtls_nv_seed_poll, NULL,
6331 MBEDTLS_ENTROPY_BLOCK_SIZE,
6332 MBEDTLS_ENTROPY_SOURCE_STRONG );
6333#endif
6334
6335 mbedtls_psa_drbg_init( mbedtls_psa_random_state( rng ) );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006336#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006337}
6338
6339/** Deinitialize the PSA random generator.
6340 */
6341static void mbedtls_psa_random_free( mbedtls_psa_random_context_t *rng )
6342{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006343#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6344 memset( rng, 0, sizeof( *rng ) );
6345#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006346 mbedtls_psa_drbg_free( mbedtls_psa_random_state( rng ) );
6347 rng->entropy_free( &rng->entropy );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006348#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006349}
6350
6351/** Seed the PSA random generator.
6352 */
6353static psa_status_t mbedtls_psa_random_seed( mbedtls_psa_random_context_t *rng )
6354{
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006355#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6356 /* Do nothing: the external RNG seeds itself. */
6357 (void) rng;
6358 return( PSA_SUCCESS );
6359#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006360 const unsigned char drbg_seed[] = "PSA";
6361 int ret = mbedtls_psa_drbg_seed( rng, drbg_seed, sizeof( drbg_seed ) - 1 );
6362 return mbedtls_to_psa_error( ret );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006363#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskine30524eb2020-11-13 17:02:26 +01006364}
6365
Gilles Peskinee59236f2018-01-27 23:32:46 +01006366psa_status_t psa_generate_random( uint8_t *output,
6367 size_t output_size )
6368{
Gilles Peskinee59236f2018-01-27 23:32:46 +01006369 GUARD_MODULE_INITIALIZED;
6370
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006371#if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
6372
6373 size_t output_length = 0;
6374 psa_status_t status = mbedtls_psa_external_get_random( &global_data.rng,
6375 output, output_size,
6376 &output_length );
6377 if( status != PSA_SUCCESS )
6378 return( status );
6379 /* Breaking up a request into smaller chunks is currently not supported
6380 * for the extrernal RNG interface. */
6381 if( output_length != output_size )
6382 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
6383 return( PSA_SUCCESS );
6384
6385#else /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
6386
6387 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
6388
Gilles Peskine30524eb2020-11-13 17:02:26 +01006389 while( output_size > MBEDTLS_PSA_RANDOM_MAX_REQUEST )
Gilles Peskinef181eca2019-08-07 13:49:00 +02006390 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006391 ret = mbedtls_psa_get_random(
6392 mbedtls_psa_random_state( &global_data.rng ),
6393 output, MBEDTLS_PSA_RANDOM_MAX_REQUEST );
Gilles Peskinef181eca2019-08-07 13:49:00 +02006394 if( ret != 0 )
6395 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006396 output += MBEDTLS_PSA_RANDOM_MAX_REQUEST;
6397 output_size -= MBEDTLS_PSA_RANDOM_MAX_REQUEST;
Gilles Peskinef181eca2019-08-07 13:49:00 +02006398 }
6399
Gilles Peskine30524eb2020-11-13 17:02:26 +01006400 ret = mbedtls_psa_get_random( mbedtls_psa_random_state( &global_data.rng ),
6401 output, output_size );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006402 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006403#endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006404}
6405
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006406#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
6407#include "mbedtls/entropy_poll.h"
6408
Gilles Peskine7228da22019-07-15 11:06:15 +02006409psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006410 size_t seed_size )
6411{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006412 if( global_data.initialized )
6413 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02006414
6415 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
6416 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
6417 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
6418 return( PSA_ERROR_INVALID_ARGUMENT );
6419
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006420 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006421}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01006422#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02006423
John Durkop07cc04a2020-11-16 22:08:34 -08006424#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinee56e8782019-04-26 17:34:02 +02006425static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
6426 size_t domain_parameters_size,
6427 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006428{
Gilles Peskinee56e8782019-04-26 17:34:02 +02006429 size_t i;
6430 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006431
Gilles Peskinee56e8782019-04-26 17:34:02 +02006432 if( domain_parameters_size == 0 )
6433 {
6434 *exponent = 65537;
6435 return( PSA_SUCCESS );
6436 }
6437
6438 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
6439 * support values that fit in a 32-bit integer, which is larger than
6440 * int on just about every platform anyway. */
6441 if( domain_parameters_size > sizeof( acc ) )
6442 return( PSA_ERROR_NOT_SUPPORTED );
6443 for( i = 0; i < domain_parameters_size; i++ )
6444 acc = ( acc << 8 ) | domain_parameters[i];
6445 if( acc > INT_MAX )
6446 return( PSA_ERROR_NOT_SUPPORTED );
6447 *exponent = acc;
6448 return( PSA_SUCCESS );
6449}
John Durkop07cc04a2020-11-16 22:08:34 -08006450#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee56e8782019-04-26 17:34:02 +02006451
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006452static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02006453 psa_key_slot_t *slot, size_t bits,
6454 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006455{
Gilles Peskine8e338702019-07-30 20:06:31 +02006456 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006457
Gilles Peskinee56e8782019-04-26 17:34:02 +02006458 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006459 return( PSA_ERROR_INVALID_ARGUMENT );
6460
Gilles Peskinee59236f2018-01-27 23:32:46 +01006461 if( key_type_is_raw_bytes( type ) )
6462 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006463 psa_status_t status;
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006464
6465 status = validate_unstructured_key_bit_size( slot->attr.type, bits );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006466 if( status != PSA_SUCCESS )
6467 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006468
6469 /* Allocate memory for the key */
Steven Cooreman75b74362020-07-28 14:30:13 +02006470 status = psa_allocate_buffer_to_slot( slot, PSA_BITS_TO_BYTES( bits ) );
6471 if( status != PSA_SUCCESS )
Steven Cooreman29149862020-08-05 15:43:42 +02006472 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006473
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006474 status = psa_generate_random( slot->data.key.data,
6475 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006476 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006477 return( status );
Steven Cooreman81be2fa2020-07-24 22:04:59 +02006478
6479 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006480#if defined(MBEDTLS_DES_C)
6481 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02006482 psa_des_set_key_parity( slot->data.key.data,
6483 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006484#endif /* MBEDTLS_DES_C */
6485 }
6486 else
6487
John Durkop07cc04a2020-11-16 22:08:34 -08006488#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006489 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006490 {
Steven Cooremana01795d2020-07-24 22:48:15 +02006491 mbedtls_rsa_context rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00006492 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006493 int exponent;
6494 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006495 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
6496 return( PSA_ERROR_NOT_SUPPORTED );
6497 /* Accept only byte-aligned keys, for the same reasons as
6498 * in psa_import_rsa_key(). */
6499 if( bits % 8 != 0 )
6500 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02006501 status = psa_read_rsa_exponent( domain_parameters,
6502 domain_parameters_size,
6503 &exponent );
6504 if( status != PSA_SUCCESS )
6505 return( status );
Steven Cooremana01795d2020-07-24 22:48:15 +02006506 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
6507 ret = mbedtls_rsa_gen_key( &rsa,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006508 mbedtls_psa_get_random,
6509 mbedtls_psa_random_state( &global_data.rng ),
Gilles Peskinee59236f2018-01-27 23:32:46 +01006510 (unsigned int) bits,
6511 exponent );
6512 if( ret != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006513 return( mbedtls_to_psa_error( ret ) );
Steven Cooremana01795d2020-07-24 22:48:15 +02006514
6515 /* Make sure to always have an export representation available */
6516 size_t bytes = PSA_KEY_EXPORT_RSA_KEY_PAIR_MAX_SIZE( bits );
6517
Steven Cooreman75b74362020-07-28 14:30:13 +02006518 status = psa_allocate_buffer_to_slot( slot, bytes );
6519 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006520 {
6521 mbedtls_rsa_free( &rsa );
Steven Cooreman29149862020-08-05 15:43:42 +02006522 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006523 }
Steven Cooremana01795d2020-07-24 22:48:15 +02006524
6525 status = psa_export_rsa_key( type,
6526 &rsa,
6527 slot->data.key.data,
6528 bytes,
6529 &slot->data.key.bytes );
6530 mbedtls_rsa_free( &rsa );
6531 if( status != PSA_SUCCESS )
Steven Cooremana01795d2020-07-24 22:48:15 +02006532 psa_remove_key_data_from_memory( slot );
Steven Cooreman560c28a2020-07-24 23:20:24 +02006533 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006534 }
6535 else
John Durkop07cc04a2020-11-16 22:08:34 -08006536#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_RSA_KEY_PAIR) */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006537
John Durkop9814fa22020-11-04 12:28:15 -08006538#if defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02006539 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006540 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01006541 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01006542 mbedtls_ecp_group_id grp_id =
6543 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006544 const mbedtls_ecp_curve_info *curve_info =
6545 mbedtls_ecp_curve_info_from_grp_id( grp_id );
Steven Cooremanacda8342020-07-24 23:09:52 +02006546 mbedtls_ecp_keypair ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00006547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02006548 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006549 return( PSA_ERROR_NOT_SUPPORTED );
6550 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
6551 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooremanacda8342020-07-24 23:09:52 +02006552 mbedtls_ecp_keypair_init( &ecp );
6553 ret = mbedtls_ecp_gen_key( grp_id, &ecp,
Gilles Peskine30524eb2020-11-13 17:02:26 +01006554 mbedtls_psa_get_random,
6555 mbedtls_psa_random_state( &global_data.rng ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006556 if( ret != 0 )
6557 {
Steven Cooremanacda8342020-07-24 23:09:52 +02006558 mbedtls_ecp_keypair_free( &ecp );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006559 return( mbedtls_to_psa_error( ret ) );
6560 }
Steven Cooremanacda8342020-07-24 23:09:52 +02006561
6562
6563 /* Make sure to always have an export representation available */
6564 size_t bytes = PSA_BITS_TO_BYTES( bits );
Steven Cooreman75b74362020-07-28 14:30:13 +02006565 psa_status_t status = psa_allocate_buffer_to_slot( slot, bytes );
6566 if( status != PSA_SUCCESS )
Steven Cooremanacda8342020-07-24 23:09:52 +02006567 {
6568 mbedtls_ecp_keypair_free( &ecp );
Steven Cooreman29149862020-08-05 15:43:42 +02006569 return( status );
Steven Cooremanacda8342020-07-24 23:09:52 +02006570 }
Steven Cooreman75b74362020-07-28 14:30:13 +02006571
6572 status = mbedtls_to_psa_error(
Steven Cooremanacda8342020-07-24 23:09:52 +02006573 mbedtls_ecp_write_key( &ecp, slot->data.key.data, bytes ) );
6574
6575 mbedtls_ecp_keypair_free( &ecp );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006576 if( status != PSA_SUCCESS ) {
6577 memset( slot->data.key.data, 0, bytes );
Steven Cooremanacda8342020-07-24 23:09:52 +02006578 psa_remove_key_data_from_memory( slot );
Steven Cooremanb7f6dea2020-08-05 16:07:20 +02006579 }
Steven Cooreman560c28a2020-07-24 23:20:24 +02006580 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006581 }
6582 else
John Durkop9814fa22020-11-04 12:28:15 -08006583#endif /* defined(MBEDTLS_PSA_BUILTIN_KEY_TYPE_ECC_KEY_PAIR) */
Steven Cooreman29149862020-08-05 15:43:42 +02006584 {
Gilles Peskinee59236f2018-01-27 23:32:46 +01006585 return( PSA_ERROR_NOT_SUPPORTED );
Steven Cooreman29149862020-08-05 15:43:42 +02006586 }
Gilles Peskinee59236f2018-01-27 23:32:46 +01006587
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006588 return( PSA_SUCCESS );
6589}
Darryl Green0c6575a2018-11-07 16:05:30 +00006590
Gilles Peskine35ef36b2019-05-16 19:42:05 +02006591psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Ronald Croncf56a0a2020-08-04 09:51:30 +02006592 mbedtls_svc_key_id_t *key )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006593{
6594 psa_status_t status;
6595 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02006596 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02006597
Ronald Cron81709fc2020-11-14 12:10:32 +01006598 *key = MBEDTLS_SVC_KEY_ID_INIT;
6599
Gilles Peskine0f84d622019-09-12 19:03:13 +02006600 /* Reject any attempt to create a zero-length key so that we don't
6601 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
6602 if( psa_get_key_bits( attributes ) == 0 )
6603 return( PSA_ERROR_INVALID_ARGUMENT );
6604
Ronald Cron81709fc2020-11-14 12:10:32 +01006605 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE, attributes,
6606 &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02006607 if( status != PSA_SUCCESS )
6608 goto exit;
6609
Steven Cooreman2a1664c2020-07-20 15:33:08 +02006610 status = psa_driver_wrapper_generate_key( attributes,
6611 slot );
6612 if( status != PSA_ERROR_NOT_SUPPORTED ||
6613 psa_key_lifetime_is_external( attributes->core.lifetime ) )
6614 goto exit;
6615
6616 status = psa_generate_key_internal(
6617 slot, attributes->core.bits,
6618 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskine11792082019-08-06 18:36:36 +02006619
6620exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006621 if( status == PSA_SUCCESS )
Ronald Cron81709fc2020-11-14 12:10:32 +01006622 status = psa_finish_key_creation( slot, driver, key );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02006623 if( status != PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02006624 psa_fail_key_creation( slot, driver );
Ronald Cronf95a2b12020-10-22 15:24:49 +02006625
Darryl Green0c6575a2018-11-07 16:05:30 +00006626 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006627}
6628
6629
Gilles Peskinee59236f2018-01-27 23:32:46 +01006630
6631/****************************************************************/
6632/* Module setup */
6633/****************************************************************/
6634
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006635#if !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
Gilles Peskine5e769522018-11-20 21:59:56 +01006636psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
6637 void (* entropy_init )( mbedtls_entropy_context *ctx ),
6638 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
6639{
6640 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6641 return( PSA_ERROR_BAD_STATE );
Gilles Peskine30524eb2020-11-13 17:02:26 +01006642 global_data.rng.entropy_init = entropy_init;
6643 global_data.rng.entropy_free = entropy_free;
Gilles Peskine5e769522018-11-20 21:59:56 +01006644 return( PSA_SUCCESS );
6645}
Gilles Peskine4fc21fd2020-11-13 18:47:18 +01006646#endif /* !defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG) */
Gilles Peskine5e769522018-11-20 21:59:56 +01006647
Gilles Peskinee59236f2018-01-27 23:32:46 +01006648void mbedtls_psa_crypto_free( void )
6649{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006650 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006651 if( global_data.rng_state != RNG_NOT_INITIALIZED )
6652 {
Gilles Peskine30524eb2020-11-13 17:02:26 +01006653 mbedtls_psa_random_free( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006654 }
6655 /* Wipe all remaining data, including configuration.
6656 * In particular, this sets all state indicator to the value
6657 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01006658 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006659#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02006660 /* Unregister all secure element drivers, so that we restart from
6661 * a pristine state. */
6662 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02006663#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006664}
6665
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006666#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
6667/** Recover a transaction that was interrupted by a power failure.
6668 *
6669 * This function is called during initialization, before psa_crypto_init()
6670 * returns. If this function returns a failure status, the initialization
6671 * fails.
6672 */
6673static psa_status_t psa_crypto_recover_transaction(
6674 const psa_crypto_transaction_t *transaction )
6675{
6676 switch( transaction->unknown.type )
6677 {
6678 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
6679 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01006680 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02006681 * is implemented.
6682 * https://github.com/ARMmbed/mbed-crypto/issues/218
6683 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006684 default:
6685 /* We found an unsupported transaction in the storage.
6686 * We don't know what state the storage is in. Give up. */
6687 return( PSA_ERROR_STORAGE_FAILURE );
6688 }
6689}
6690#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
6691
Gilles Peskinee59236f2018-01-27 23:32:46 +01006692psa_status_t psa_crypto_init( void )
6693{
Gilles Peskine66fb1262018-12-10 16:29:04 +01006694 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006695
Gilles Peskinec6b69072018-11-20 21:42:52 +01006696 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006697 if( global_data.initialized != 0 )
6698 return( PSA_SUCCESS );
6699
Gilles Peskine30524eb2020-11-13 17:02:26 +01006700 /* Initialize and seed the random generator. */
6701 mbedtls_psa_random_init( &global_data.rng );
Gilles Peskinec6b69072018-11-20 21:42:52 +01006702 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine30524eb2020-11-13 17:02:26 +01006703 status = mbedtls_psa_random_seed( &global_data.rng );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006704 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006705 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006706 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01006707
Gilles Peskine66fb1262018-12-10 16:29:04 +01006708 status = psa_initialize_key_slots( );
6709 if( status != PSA_SUCCESS )
6710 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01006711
Gilles Peskined9348f22019-10-01 15:22:29 +02006712#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
6713 status = psa_init_all_se_drivers( );
6714 if( status != PSA_SUCCESS )
6715 goto exit;
6716#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
6717
Gilles Peskine4b734222019-07-24 15:56:31 +02006718#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02006719 status = psa_crypto_load_transaction( );
6720 if( status == PSA_SUCCESS )
6721 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02006722 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
6723 if( status != PSA_SUCCESS )
6724 goto exit;
6725 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02006726 }
6727 else if( status == PSA_ERROR_DOES_NOT_EXIST )
6728 {
6729 /* There's no transaction to complete. It's all good. */
6730 status = PSA_SUCCESS;
6731 }
Gilles Peskine4b734222019-07-24 15:56:31 +02006732#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02006733
Gilles Peskinec6b69072018-11-20 21:42:52 +01006734 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01006735 global_data.initialized = 1;
6736
6737exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01006738 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01006739 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01006740 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01006741}
6742
6743#endif /* MBEDTLS_PSA_CRYPTO_C */