blob: 54980730c99cfba1472231678599b4b5d2b2733a [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/*
5 * Copyright (C) 2018, ARM Limited, All Rights Reserved
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.
19 *
20 * This file is part of mbed TLS (https://tls.mbed.org)
21 */
22
Gilles Peskinedb09ef62020-06-03 01:43:33 +020023#include "common.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010024
25#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030026
itayzafrir7723ab12019-02-14 10:28:02 +020027#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010028#include "psa/crypto.h"
29
Gilles Peskine039b90c2018-12-07 18:24:41 +010030#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010031#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020032#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020033#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020034#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010035#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010036/* Include internal declarations that are useful for implementing persistently
37 * stored keys. */
38#include "psa_crypto_storage.h"
39
Gilles Peskineb46bef22019-07-30 21:32:04 +020040#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010041#include <stdlib.h>
42#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010043#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020044#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#define mbedtls_calloc calloc
46#define mbedtls_free free
47#endif
48
Gilles Peskinea5905292018-02-07 20:59:33 +010049#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020050#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000051#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020052#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/blowfish.h"
54#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020055#include "mbedtls/chacha20.h"
56#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/cipher.h"
58#include "mbedtls/ccm.h"
59#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010060#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020062#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010063#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/error.h"
66#include "mbedtls/gcm.h"
67#include "mbedtls/md2.h"
68#include "mbedtls/md4.h"
69#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010070#include "mbedtls/md.h"
71#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010072#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010073#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010074#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000075#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010076#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010078#include "mbedtls/sha1.h"
79#include "mbedtls/sha256.h"
80#include "mbedtls/sha512.h"
81#include "mbedtls/xtea.h"
82
Gilles Peskine996deb12018-08-01 15:45:45 +020083#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
84
Gilles Peskine9ef733f2018-02-07 21:05:37 +010085/* constant-time buffer comparison */
86static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
87{
88 size_t i;
89 unsigned char diff = 0;
90
91 for( i = 0; i < n; i++ )
92 diff |= a[i] ^ b[i];
93
94 return( diff );
95}
96
97
98
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010099/****************************************************************/
100/* Global data, support functions and library management */
101/****************************************************************/
102
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200103static int key_type_is_raw_bytes( psa_key_type_t type )
104{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200105 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106}
107
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100108/* Values for psa_global_data_t::rng_state */
109#define RNG_NOT_INITIALIZED 0
110#define RNG_INITIALIZED 1
111#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100112
Gilles Peskine2d277862018-06-18 15:41:12 +0200113typedef struct
114{
Gilles Peskine5e769522018-11-20 21:59:56 +0100115 void (* entropy_init )( mbedtls_entropy_context *ctx );
116 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100117 mbedtls_entropy_context entropy;
118 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100119 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100120 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121} psa_global_data_t;
122
123static psa_global_data_t global_data;
124
itayzafrir0adf0fc2018-09-06 16:24:41 +0300125#define GUARD_MODULE_INITIALIZED \
126 if( global_data.initialized == 0 ) \
127 return( PSA_ERROR_BAD_STATE );
128
Gilles Peskinee59236f2018-01-27 23:32:46 +0100129static psa_status_t mbedtls_to_psa_error( int ret )
130{
Gilles Peskinea5905292018-02-07 20:59:33 +0100131 /* If there's both a high-level code and low-level code, dispatch on
132 * the high-level code. */
133 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100134 {
135 case 0:
136 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100137
138 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
139 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
140 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
141 return( PSA_ERROR_NOT_SUPPORTED );
142 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
143 return( PSA_ERROR_HARDWARE_FAILURE );
144
145 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
Gilles Peskine9a944802018-06-21 09:35:35 +0200148 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
149 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
150 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
151 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
152 case MBEDTLS_ERR_ASN1_INVALID_DATA:
153 return( PSA_ERROR_INVALID_ARGUMENT );
154 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
155 return( PSA_ERROR_INSUFFICIENT_MEMORY );
156 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
157 return( PSA_ERROR_BUFFER_TOO_SMALL );
158
Jaeden Amero93e21112019-02-20 13:57:28 +0000159#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000160 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000161#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
162 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
163#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100164 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
165 return( PSA_ERROR_NOT_SUPPORTED );
166 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
167 return( PSA_ERROR_HARDWARE_FAILURE );
168
Jaeden Amero93e21112019-02-20 13:57:28 +0000169#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000170 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000171#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
172 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
173#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100174 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
175 return( PSA_ERROR_NOT_SUPPORTED );
176 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
177 return( PSA_ERROR_HARDWARE_FAILURE );
178
179 case MBEDTLS_ERR_CCM_BAD_INPUT:
180 return( PSA_ERROR_INVALID_ARGUMENT );
181 case MBEDTLS_ERR_CCM_AUTH_FAILED:
182 return( PSA_ERROR_INVALID_SIGNATURE );
183 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
184 return( PSA_ERROR_HARDWARE_FAILURE );
185
Gilles Peskine26869f22019-05-06 15:25:00 +0200186 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
187 return( PSA_ERROR_INVALID_ARGUMENT );
188
189 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
190 return( PSA_ERROR_BAD_STATE );
191 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
192 return( PSA_ERROR_INVALID_SIGNATURE );
193
Gilles Peskinea5905292018-02-07 20:59:33 +0100194 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
195 return( PSA_ERROR_NOT_SUPPORTED );
196 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
197 return( PSA_ERROR_INVALID_ARGUMENT );
198 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
199 return( PSA_ERROR_INSUFFICIENT_MEMORY );
200 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
201 return( PSA_ERROR_INVALID_PADDING );
202 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
203 return( PSA_ERROR_BAD_STATE );
204 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
205 return( PSA_ERROR_INVALID_SIGNATURE );
206 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200207 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100208 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
209 return( PSA_ERROR_HARDWARE_FAILURE );
210
211 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
215 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
216 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
217 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
218 return( PSA_ERROR_NOT_SUPPORTED );
219 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
220 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
221
222 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
223 return( PSA_ERROR_NOT_SUPPORTED );
224 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
225 return( PSA_ERROR_HARDWARE_FAILURE );
226
Gilles Peskinee59236f2018-01-27 23:32:46 +0100227 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
228 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
229 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
230 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100231
232 case MBEDTLS_ERR_GCM_AUTH_FAILED:
233 return( PSA_ERROR_INVALID_SIGNATURE );
234 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200235 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100236 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
237 return( PSA_ERROR_HARDWARE_FAILURE );
238
239 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
240 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
241 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
242 return( PSA_ERROR_HARDWARE_FAILURE );
243
244 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
245 return( PSA_ERROR_NOT_SUPPORTED );
246 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
247 return( PSA_ERROR_INVALID_ARGUMENT );
248 case MBEDTLS_ERR_MD_ALLOC_FAILED:
249 return( PSA_ERROR_INSUFFICIENT_MEMORY );
250 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
251 return( PSA_ERROR_STORAGE_FAILURE );
252 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
253 return( PSA_ERROR_HARDWARE_FAILURE );
254
Gilles Peskinef76aa772018-10-29 19:24:33 +0100255 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
256 return( PSA_ERROR_STORAGE_FAILURE );
257 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
258 return( PSA_ERROR_INVALID_ARGUMENT );
259 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
260 return( PSA_ERROR_INVALID_ARGUMENT );
261 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
262 return( PSA_ERROR_BUFFER_TOO_SMALL );
263 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
266 return( PSA_ERROR_INVALID_ARGUMENT );
267 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
270 return( PSA_ERROR_INSUFFICIENT_MEMORY );
271
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100272 case MBEDTLS_ERR_PK_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
275 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
276 return( PSA_ERROR_INVALID_ARGUMENT );
277 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100278 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100279 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
280 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
281 return( PSA_ERROR_INVALID_ARGUMENT );
282 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
283 return( PSA_ERROR_NOT_SUPPORTED );
284 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
285 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
286 return( PSA_ERROR_NOT_PERMITTED );
287 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
288 return( PSA_ERROR_INVALID_ARGUMENT );
289 case MBEDTLS_ERR_PK_INVALID_ALG:
290 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
291 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
292 return( PSA_ERROR_NOT_SUPPORTED );
293 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
294 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100295 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
296 return( PSA_ERROR_HARDWARE_FAILURE );
297
Gilles Peskineff2d2002019-05-06 15:26:23 +0200298 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
301 return( PSA_ERROR_NOT_SUPPORTED );
302
Gilles Peskinea5905292018-02-07 20:59:33 +0100303 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
304 return( PSA_ERROR_HARDWARE_FAILURE );
305
306 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
307 return( PSA_ERROR_INVALID_ARGUMENT );
308 case MBEDTLS_ERR_RSA_INVALID_PADDING:
309 return( PSA_ERROR_INVALID_PADDING );
310 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
311 return( PSA_ERROR_HARDWARE_FAILURE );
312 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
313 return( PSA_ERROR_INVALID_ARGUMENT );
314 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
315 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200316 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100317 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
318 return( PSA_ERROR_INVALID_SIGNATURE );
319 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
320 return( PSA_ERROR_BUFFER_TOO_SMALL );
321 case MBEDTLS_ERR_RSA_RNG_FAILED:
322 return( PSA_ERROR_INSUFFICIENT_MEMORY );
323 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
324 return( PSA_ERROR_NOT_SUPPORTED );
325 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
326 return( PSA_ERROR_HARDWARE_FAILURE );
327
328 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
329 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
330 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
331 return( PSA_ERROR_HARDWARE_FAILURE );
332
333 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
334 return( PSA_ERROR_INVALID_ARGUMENT );
335 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
336 return( PSA_ERROR_HARDWARE_FAILURE );
337
itayzafrir5c753392018-05-08 11:18:38 +0300338 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300339 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300340 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300341 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
342 return( PSA_ERROR_BUFFER_TOO_SMALL );
343 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
344 return( PSA_ERROR_NOT_SUPPORTED );
345 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
346 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
347 return( PSA_ERROR_INVALID_SIGNATURE );
348 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
349 return( PSA_ERROR_INSUFFICIENT_MEMORY );
350 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
351 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000352 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
353 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300354
Gilles Peskinee59236f2018-01-27 23:32:46 +0100355 default:
David Saadab4ecc272019-02-14 13:48:10 +0200356 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100357 }
358}
359
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200360
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200361
362
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100363/****************************************************************/
364/* Key management */
365/****************************************************************/
366
Gilles Peskine73167e12019-07-12 23:44:37 +0200367#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
368static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
369{
Gilles Peskine8e338702019-07-30 20:06:31 +0200370 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200371}
372#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
373
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100374#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100375mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100376 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200377{
378 switch( curve )
379 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100380 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100381 switch( byte_length )
382 {
383 case PSA_BITS_TO_BYTES( 192 ):
384 return( MBEDTLS_ECP_DP_SECP192R1 );
385 case PSA_BITS_TO_BYTES( 224 ):
386 return( MBEDTLS_ECP_DP_SECP224R1 );
387 case PSA_BITS_TO_BYTES( 256 ):
388 return( MBEDTLS_ECP_DP_SECP256R1 );
389 case PSA_BITS_TO_BYTES( 384 ):
390 return( MBEDTLS_ECP_DP_SECP384R1 );
391 case PSA_BITS_TO_BYTES( 521 ):
392 return( MBEDTLS_ECP_DP_SECP521R1 );
393 default:
394 return( MBEDTLS_ECP_DP_NONE );
395 }
396 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100397
Paul Elliott8ff510a2020-06-02 17:19:28 +0100398 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100399 switch( byte_length )
400 {
401 case PSA_BITS_TO_BYTES( 256 ):
402 return( MBEDTLS_ECP_DP_BP256R1 );
403 case PSA_BITS_TO_BYTES( 384 ):
404 return( MBEDTLS_ECP_DP_BP384R1 );
405 case PSA_BITS_TO_BYTES( 512 ):
406 return( MBEDTLS_ECP_DP_BP512R1 );
407 default:
408 return( MBEDTLS_ECP_DP_NONE );
409 }
410 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100411
Paul Elliott8ff510a2020-06-02 17:19:28 +0100412 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100413 switch( byte_length )
414 {
415 case PSA_BITS_TO_BYTES( 255 ):
416 return( MBEDTLS_ECP_DP_CURVE25519 );
417 case PSA_BITS_TO_BYTES( 448 ):
418 return( MBEDTLS_ECP_DP_CURVE448 );
419 default:
420 return( MBEDTLS_ECP_DP_NONE );
421 }
422 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100423
Paul Elliott8ff510a2020-06-02 17:19:28 +0100424 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100425 switch( byte_length )
426 {
427 case PSA_BITS_TO_BYTES( 192 ):
428 return( MBEDTLS_ECP_DP_SECP192K1 );
429 case PSA_BITS_TO_BYTES( 224 ):
430 return( MBEDTLS_ECP_DP_SECP224K1 );
431 case PSA_BITS_TO_BYTES( 256 ):
432 return( MBEDTLS_ECP_DP_SECP256K1 );
433 default:
434 return( MBEDTLS_ECP_DP_NONE );
435 }
436 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100437
Gilles Peskine12313cd2018-06-20 00:20:32 +0200438 default:
439 return( MBEDTLS_ECP_DP_NONE );
440 }
441}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100442#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200443
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200444static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
445 size_t bits,
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200446 struct key_data *key )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200447{
448 /* Check that the bit size is acceptable for the key type */
449 switch( type )
450 {
451 case PSA_KEY_TYPE_RAW_DATA:
452#if defined(MBEDTLS_MD_C)
453 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200454#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200455 case PSA_KEY_TYPE_DERIVE:
456 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200457#if defined(MBEDTLS_AES_C)
458 case PSA_KEY_TYPE_AES:
459 if( bits != 128 && bits != 192 && bits != 256 )
460 return( PSA_ERROR_INVALID_ARGUMENT );
461 break;
462#endif
463#if defined(MBEDTLS_CAMELLIA_C)
464 case PSA_KEY_TYPE_CAMELLIA:
465 if( bits != 128 && bits != 192 && bits != 256 )
466 return( PSA_ERROR_INVALID_ARGUMENT );
467 break;
468#endif
469#if defined(MBEDTLS_DES_C)
470 case PSA_KEY_TYPE_DES:
471 if( bits != 64 && bits != 128 && bits != 192 )
472 return( PSA_ERROR_INVALID_ARGUMENT );
473 break;
474#endif
475#if defined(MBEDTLS_ARC4_C)
476 case PSA_KEY_TYPE_ARC4:
477 if( bits < 8 || bits > 2048 )
478 return( PSA_ERROR_INVALID_ARGUMENT );
479 break;
480#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200481#if defined(MBEDTLS_CHACHA20_C)
482 case PSA_KEY_TYPE_CHACHA20:
483 if( bits != 256 )
484 return( PSA_ERROR_INVALID_ARGUMENT );
485 break;
486#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200487 default:
488 return( PSA_ERROR_NOT_SUPPORTED );
489 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200490 if( bits % 8 != 0 )
491 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492
493 /* Allocate memory for the key */
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200494 key->bytes = PSA_BITS_TO_BYTES( bits );
495 key->data = mbedtls_calloc( 1, key->bytes );
496 if( key->data == NULL )
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200498 key->bytes = 0;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200499 return( PSA_ERROR_INSUFFICIENT_MEMORY );
500 }
501 return( PSA_SUCCESS );
502}
503
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200504#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100505/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
506 * that are not a multiple of 8) well. For example, there is only
507 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
508 * way to return the exact bit size of a key.
509 * To keep things simple, reject non-byte-aligned key sizes. */
510static psa_status_t psa_check_rsa_key_byte_aligned(
511 const mbedtls_rsa_context *rsa )
512{
513 mbedtls_mpi n;
514 psa_status_t status;
515 mbedtls_mpi_init( &n );
516 status = mbedtls_to_psa_error(
517 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
518 if( status == PSA_SUCCESS )
519 {
520 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
521 status = PSA_ERROR_NOT_SUPPORTED;
522 }
523 mbedtls_mpi_free( &n );
524 return( status );
525}
526
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000527static psa_status_t psa_import_rsa_key( psa_key_type_t type,
528 const uint8_t *data,
529 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200530 mbedtls_rsa_context **p_rsa )
531{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532 psa_status_t status;
533 mbedtls_pk_context pk;
534 mbedtls_rsa_context *rsa;
535 size_t bits;
536
537 mbedtls_pk_init( &pk );
538
539 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200540 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000541 status = mbedtls_to_psa_error(
542 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200543 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000544 status = mbedtls_to_psa_error(
545 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
546 if( status != PSA_SUCCESS )
547 goto exit;
548
549 /* We have something that the pkparse module recognizes. If it is a
550 * valid RSA key, store it. */
551 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200552 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000553 status = PSA_ERROR_INVALID_ARGUMENT;
554 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200555 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000556
557 rsa = mbedtls_pk_rsa( pk );
558 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
559 * supports non-byte-aligned key sizes, but not well. For example,
560 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
561 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
562 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
563 {
564 status = PSA_ERROR_NOT_SUPPORTED;
565 goto exit;
566 }
567 status = psa_check_rsa_key_byte_aligned( rsa );
568
569exit:
570 /* Free the content of the pk object only on error. */
571 if( status != PSA_SUCCESS )
572 {
573 mbedtls_pk_free( &pk );
574 return( status );
575 }
576
577 /* On success, store the content of the object in the RSA context. */
578 *p_rsa = rsa;
579
580 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200581}
582#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
583
Jaeden Ameroccdce902019-01-10 11:42:27 +0000584#if defined(MBEDTLS_ECP_C)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100585static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100586 size_t data_length,
587 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100588 mbedtls_ecp_keypair **p_ecp )
589{
590 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
591 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
592 if( *p_ecp == NULL )
593 return( PSA_ERROR_INSUFFICIENT_MEMORY );
594 mbedtls_ecp_keypair_init( *p_ecp );
595
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100596 if( is_public )
597 {
598 /* A public key is represented as:
599 * - The byte 0x04;
600 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
601 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
602 * So its data length is 2m+1 where n is the key size in bits.
603 */
604 if( ( data_length & 1 ) == 0 )
605 return( PSA_ERROR_INVALID_ARGUMENT );
606 data_length = data_length / 2;
607 }
608
Gilles Peskine4cd32772019-12-02 20:49:42 +0100609 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100610 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
611 if( grp_id == MBEDTLS_ECP_DP_NONE )
612 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100613 return( mbedtls_to_psa_error(
614 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
615}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616
617/* Import a public key given as the uncompressed representation defined by SEC1
618 * 2.3.3 as the content of an ECPoint. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100619static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000620 const uint8_t *data,
621 size_t data_length,
622 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200623{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200624 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000625 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000626
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100627 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000628 if( status != PSA_SUCCESS )
629 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100630
Jaeden Ameroccdce902019-01-10 11:42:27 +0000631 /* Load the public value. */
632 status = mbedtls_to_psa_error(
633 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
634 data, data_length ) );
635 if( status != PSA_SUCCESS )
636 goto exit;
637
638 /* Check that the point is on the curve. */
639 status = mbedtls_to_psa_error(
640 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
641 if( status != PSA_SUCCESS )
642 goto exit;
643
644 *p_ecp = ecp;
645 return( PSA_SUCCESS );
646
647exit:
648 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200649 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000650 mbedtls_ecp_keypair_free( ecp );
651 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200652 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000653 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200654}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200655
Gilles Peskinef76aa772018-10-29 19:24:33 +0100656/* Import a private key given as a byte string which is the private value
657 * in big-endian order. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100658static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve,
Gilles Peskinef76aa772018-10-29 19:24:33 +0100659 const uint8_t *data,
660 size_t data_length,
661 mbedtls_ecp_keypair **p_ecp )
662{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200663 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100664 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100665
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100666 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100667 if( status != PSA_SUCCESS )
668 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100669
Steven Cooremane3fd3922020-06-11 16:50:36 +0200670 /* Load and validate the secret key */
Gilles Peskinef76aa772018-10-29 19:24:33 +0100671 status = mbedtls_to_psa_error(
Steven Cooremane3fd3922020-06-11 16:50:36 +0200672 mbedtls_ecp_read_key( ecp->grp.id, ecp, data, data_length ) );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100673 if( status != PSA_SUCCESS )
674 goto exit;
Steven Cooremane3fd3922020-06-11 16:50:36 +0200675
Gilles Peskinef76aa772018-10-29 19:24:33 +0100676 /* Calculate the public key from the private key. */
677 status = mbedtls_to_psa_error(
678 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
679 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
680 if( status != PSA_SUCCESS )
681 goto exit;
682
683 *p_ecp = ecp;
684 return( PSA_SUCCESS );
685
686exit:
687 if( ecp != NULL )
688 {
689 mbedtls_ecp_keypair_free( ecp );
690 mbedtls_free( ecp );
691 }
692 return( status );
693}
694#endif /* defined(MBEDTLS_ECP_C) */
695
Gilles Peskineb46bef22019-07-30 21:32:04 +0200696
697/** Return the size of the key in the given slot, in bits.
698 *
699 * \param[in] slot A key slot.
700 *
701 * \return The key size in bits, read from the metadata in the slot.
702 */
703static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
704{
705 return( slot->attr.bits );
706}
707
708/** Calculate the size of the key in the given slot, in bits.
709 *
710 * \param[in] slot A key slot containing a transparent key.
711 *
712 * \return The key size in bits, calculated from the key data.
713 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200714static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200715{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200716 size_t bits = 0; /* return 0 on an empty slot */
717
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718 if( key_type_is_raw_bytes( slot->attr.type ) )
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200719 bits = PSA_BYTES_TO_BITS( slot->data.key.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200720#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200721 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
722 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723#endif /* defined(MBEDTLS_RSA_C) */
724#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200725 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
726 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200728
729 /* We know that the size fits in psa_key_bits_t thanks to checks
730 * when the key was created. */
731 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200732}
733
Gilles Peskine8e338702019-07-30 20:06:31 +0200734/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100735 * previously. This function assumes that the slot does not contain
736 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100737psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
738 const uint8_t *data,
739 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100740{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200741 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742
Gilles Peskine8e338702019-07-30 20:06:31 +0200743 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100744 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200745 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200746 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100747 if( data_length > SIZE_MAX / 8 )
748 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200749 /* Enforce a size limit, and in particular ensure that the bit
750 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200751 if( bit_size > PSA_MAX_KEY_BITS )
752 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200753 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200754 &slot->data.key );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200755 if( status != PSA_SUCCESS )
756 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200757 if( data_length != 0 )
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200758 memcpy( slot->data.key.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100759 }
760 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100761#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200762 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100763 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100764 status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100765 data, data_length,
766 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100767 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200768 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000770 status = psa_import_ec_public_key(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100771 PSA_KEY_TYPE_ECC_GET_FAMILY( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000772 data, data_length,
773 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 }
775 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100776#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000777#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200778 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100779 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200780 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000781 data, data_length,
782 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100783 }
784 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000785#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100786 {
787 return( PSA_ERROR_NOT_SUPPORTED );
788 }
Darryl Green940d72c2018-07-13 13:18:51 +0100789
Gilles Peskineb46bef22019-07-30 21:32:04 +0200790 if( status == PSA_SUCCESS )
791 {
792 /* Write the actual key size to the slot.
793 * psa_start_key_creation() wrote the size declared by the
794 * caller, which may be 0 (meaning unspecified) or wrong. */
795 slot->attr.bits = psa_calculate_key_bits( slot );
796 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100797 return( status );
798}
799
Gilles Peskinef603c712019-01-19 13:40:11 +0100800/** Calculate the intersection of two algorithm usage policies.
801 *
802 * Return 0 (which allows no operation) on incompatibility.
803 */
804static psa_algorithm_t psa_key_policy_algorithm_intersection(
805 psa_algorithm_t alg1,
806 psa_algorithm_t alg2 )
807{
Gilles Peskine549ea862019-05-22 11:45:59 +0200808 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100809 if( alg1 == alg2 )
810 return( alg1 );
811 /* If the policies are from the same hash-and-sign family, check
812 * if one is a wildcard. If so the other has the specific algorithm. */
813 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
814 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
815 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
816 {
817 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
818 return( alg2 );
819 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
820 return( alg1 );
821 }
822 /* If the policies are incompatible, allow nothing. */
823 return( 0 );
824}
825
Gilles Peskined6f371b2019-05-10 19:33:38 +0200826static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
827 psa_algorithm_t requested_alg )
828{
Gilles Peskine549ea862019-05-22 11:45:59 +0200829 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200830 if( requested_alg == policy_alg )
831 return( 1 );
832 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200833 * and requested_alg is the same hash-and-sign family with any hash,
834 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200835 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
836 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
837 {
838 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
839 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
840 }
841 /* If it isn't permitted, it's forbidden. */
842 return( 0 );
843}
844
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100845/** Test whether a policy permits an algorithm.
846 *
847 * The caller must test usage flags separately.
848 */
849static int psa_key_policy_permits( const psa_key_policy_t *policy,
850 psa_algorithm_t alg )
851{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200852 return( psa_key_algorithm_permits( policy->alg, alg ) ||
853 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100854}
855
Gilles Peskinef603c712019-01-19 13:40:11 +0100856/** Restrict a key policy based on a constraint.
857 *
858 * \param[in,out] policy The policy to restrict.
859 * \param[in] constraint The policy constraint to apply.
860 *
861 * \retval #PSA_SUCCESS
862 * \c *policy contains the intersection of the original value of
863 * \c *policy and \c *constraint.
864 * \retval #PSA_ERROR_INVALID_ARGUMENT
865 * \c *policy and \c *constraint are incompatible.
866 * \c *policy is unchanged.
867 */
868static psa_status_t psa_restrict_key_policy(
869 psa_key_policy_t *policy,
870 const psa_key_policy_t *constraint )
871{
872 psa_algorithm_t intersection_alg =
873 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200874 psa_algorithm_t intersection_alg2 =
875 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100876 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
877 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200878 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
879 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100880 policy->usage &= constraint->usage;
881 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100883 return( PSA_SUCCESS );
884}
885
Darryl Green06fd18d2018-07-16 11:21:11 +0100886/** Retrieve a slot which must contain a key. The key must have allow all the
887 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
888 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100889static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100890 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100891 psa_key_usage_t usage,
892 psa_algorithm_t alg )
893{
894 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100895 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100896
897 *p_slot = NULL;
898
Gilles Peskinec5487a82018-12-03 18:08:14 +0100899 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100900 if( status != PSA_SUCCESS )
901 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100902
903 /* Enforce that usage policy for the key slot contains all the flags
904 * required by the usage parameter. There is one exception: public
905 * keys can always be exported, so we treat public key objects as
906 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200907 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100908 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200909 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100910 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100911
912 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200913 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100914 return( PSA_ERROR_NOT_PERMITTED );
915
916 *p_slot = slot;
917 return( PSA_SUCCESS );
918}
Darryl Green940d72c2018-07-13 13:18:51 +0100919
Gilles Peskine28f8f302019-07-24 13:30:31 +0200920/** Retrieve a slot which must contain a transparent key.
921 *
922 * A transparent key is a key for which the key material is directly
923 * available, as opposed to a key in a secure element.
924 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200925 * This is a temporary function to use instead of psa_get_key_from_slot()
926 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200927 */
928#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
929static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
930 psa_key_slot_t **p_slot,
931 psa_key_usage_t usage,
932 psa_algorithm_t alg )
933{
934 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
935 if( status != PSA_SUCCESS )
936 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200937 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200938 {
939 *p_slot = NULL;
940 return( PSA_ERROR_NOT_SUPPORTED );
941 }
942 return( PSA_SUCCESS );
943}
944#else /* MBEDTLS_PSA_CRYPTO_SE_C */
945/* With no secure element support, all keys are transparent. */
946#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
947 psa_get_key_from_slot( handle, p_slot, usage, alg )
948#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
949
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100950/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100951static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000952{
Gilles Peskine73167e12019-07-12 23:44:37 +0200953#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
954 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000955 {
956 /* No key material to clean. */
957 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200958 else
959#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200960 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000961 {
962 /* No key material to clean. */
963 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200964 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000965 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +0200966 mbedtls_free( slot->data.key.data );
Darryl Green40225ba2018-11-15 14:48:15 +0000967 }
968 else
969#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200970 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000971 {
972 mbedtls_rsa_free( slot->data.rsa );
973 mbedtls_free( slot->data.rsa );
974 }
975 else
976#endif /* defined(MBEDTLS_RSA_C) */
977#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200978 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000979 {
980 mbedtls_ecp_keypair_free( slot->data.ecp );
981 mbedtls_free( slot->data.ecp );
982 }
983 else
984#endif /* defined(MBEDTLS_ECP_C) */
985 {
986 /* Shouldn't happen: the key type is not any type that we
987 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200988 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000989 }
990
991 return( PSA_SUCCESS );
992}
993
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100994/** Completely wipe a slot in memory, including its policy.
995 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100996psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100997{
998 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200999 /* Multipart operations may still be using the key. This is safe
1000 * because all multipart operation objects are independent from
1001 * the key slot: if they need to access the key after the setup
1002 * phase, they have a copy of the key. Note that this means that
1003 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001004 /* At this point, key material and other type-specific content has
1005 * been wiped. Clear remaining metadata. We can call memset and not
1006 * zeroize because the metadata is not particularly sensitive. */
1007 memset( slot, 0, sizeof( *slot ) );
1008 return( status );
1009}
1010
Gilles Peskinec5487a82018-12-03 18:08:14 +01001011psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001012{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001013 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001014 psa_status_t status; /* status of the last operation */
1015 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001016#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1017 psa_se_drv_table_entry_t *driver;
1018#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019
Gilles Peskine1841cf42019-10-08 15:48:25 +02001020 if( handle == 0 )
1021 return( PSA_SUCCESS );
1022
Gilles Peskinec5487a82018-12-03 18:08:14 +01001023 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001024 if( status != PSA_SUCCESS )
1025 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001026
1027#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001028 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001029 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001030 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001031 /* For a key in a secure element, we need to do three things:
1032 * remove the key file in internal storage, destroy the
1033 * key inside the secure element, and update the driver's
1034 * persistent data. Start a transaction that will encompass these
1035 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001036 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001037 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001039 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001040 status = psa_crypto_save_transaction( );
1041 if( status != PSA_SUCCESS )
1042 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001043 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001044 /* We should still try to destroy the key in the secure
1045 * element and the key metadata in storage. This is especially
1046 * important if the error is that the storage is full.
1047 * But how to do it exactly without risking an inconsistent
1048 * state after a reset?
1049 * https://github.com/ARMmbed/mbed-crypto/issues/215
1050 */
1051 overall_status = status;
1052 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001053 }
1054
Gilles Peskine354f7672019-07-12 23:46:38 +02001055 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001056 if( overall_status == PSA_SUCCESS )
1057 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001058 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001059#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1060
Darryl Greend49a4992018-06-18 17:27:26 +01001061#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001062 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001063 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 status = psa_destroy_persistent_key( slot->attr.id );
1065 if( overall_status == PSA_SUCCESS )
1066 overall_status = status;
1067
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001068 /* TODO: other slots may have a copy of the same key. We should
1069 * invalidate them.
1070 * https://github.com/ARMmbed/mbed-crypto/issues/214
1071 */
Darryl Greend49a4992018-06-18 17:27:26 +01001072 }
1073#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001074
Gilles Peskinefc762652019-07-22 19:30:34 +02001075#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1076 if( driver != NULL )
1077 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001078 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001079 if( overall_status == PSA_SUCCESS )
1080 overall_status = status;
1081 status = psa_crypto_stop_transaction( );
1082 if( overall_status == PSA_SUCCESS )
1083 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001084 }
1085#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1086
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1088exit:
1089#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001090 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001091 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1092 if( overall_status == PSA_SUCCESS )
1093 overall_status = status;
1094 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001095}
1096
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001097void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001098{
Gilles Peskineb699f072019-04-26 16:06:02 +02001099 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001100 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001101}
1102
Gilles Peskineb699f072019-04-26 16:06:02 +02001103psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1104 psa_key_type_t type,
1105 const uint8_t *data,
1106 size_t data_length )
1107{
1108 uint8_t *copy = NULL;
1109
1110 if( data_length != 0 )
1111 {
1112 copy = mbedtls_calloc( 1, data_length );
1113 if( copy == NULL )
1114 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1115 memcpy( copy, data, data_length );
1116 }
1117 /* After this point, this function is guaranteed to succeed, so it
1118 * can start modifying `*attributes`. */
1119
1120 if( attributes->domain_parameters != NULL )
1121 {
1122 mbedtls_free( attributes->domain_parameters );
1123 attributes->domain_parameters = NULL;
1124 attributes->domain_parameters_size = 0;
1125 }
1126
1127 attributes->domain_parameters = copy;
1128 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001129 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001130 return( PSA_SUCCESS );
1131}
1132
1133psa_status_t psa_get_key_domain_parameters(
1134 const psa_key_attributes_t *attributes,
1135 uint8_t *data, size_t data_size, size_t *data_length )
1136{
1137 if( attributes->domain_parameters_size > data_size )
1138 return( PSA_ERROR_BUFFER_TOO_SMALL );
1139 *data_length = attributes->domain_parameters_size;
1140 if( attributes->domain_parameters_size != 0 )
1141 memcpy( data, attributes->domain_parameters,
1142 attributes->domain_parameters_size );
1143 return( PSA_SUCCESS );
1144}
1145
1146#if defined(MBEDTLS_RSA_C)
1147static psa_status_t psa_get_rsa_public_exponent(
1148 const mbedtls_rsa_context *rsa,
1149 psa_key_attributes_t *attributes )
1150{
1151 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001152 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001153 uint8_t *buffer = NULL;
1154 size_t buflen;
1155 mbedtls_mpi_init( &mpi );
1156
1157 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1158 if( ret != 0 )
1159 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001160 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1161 {
1162 /* It's the default value, which is reported as an empty string,
1163 * so there's nothing to do. */
1164 goto exit;
1165 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001166
1167 buflen = mbedtls_mpi_size( &mpi );
1168 buffer = mbedtls_calloc( 1, buflen );
1169 if( buffer == NULL )
1170 {
1171 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1172 goto exit;
1173 }
1174 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1175 if( ret != 0 )
1176 goto exit;
1177 attributes->domain_parameters = buffer;
1178 attributes->domain_parameters_size = buflen;
1179
1180exit:
1181 mbedtls_mpi_free( &mpi );
1182 if( ret != 0 )
1183 mbedtls_free( buffer );
1184 return( mbedtls_to_psa_error( ret ) );
1185}
1186#endif /* MBEDTLS_RSA_C */
1187
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001188/** Retrieve all the publicly-accessible attributes of a key.
1189 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001190psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1191 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001192{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001193 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001194 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001195
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001196 psa_reset_key_attributes( attributes );
1197
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001198 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001199 if( status != PSA_SUCCESS )
1200 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001201
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001202 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001203 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1204 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1205
1206#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1207 if( psa_key_slot_is_external( slot ) )
1208 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1209#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001210
Gilles Peskine8e338702019-07-30 20:06:31 +02001211 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001212 {
1213#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001214 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001215 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001216#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001217 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001218 * is not yet implemented.
1219 * https://github.com/ARMmbed/mbed-crypto/issues/216
1220 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001221 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001222 break;
1223#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001224 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1225 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001226#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001227 default:
1228 /* Nothing else to do. */
1229 break;
1230 }
1231
1232 if( status != PSA_SUCCESS )
1233 psa_reset_key_attributes( attributes );
1234 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001235}
1236
Gilles Peskinec8000c02019-08-02 20:15:51 +02001237#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1238psa_status_t psa_get_key_slot_number(
1239 const psa_key_attributes_t *attributes,
1240 psa_key_slot_number_t *slot_number )
1241{
1242 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1243 {
1244 *slot_number = attributes->slot_number;
1245 return( PSA_SUCCESS );
1246 }
1247 else
1248 return( PSA_ERROR_INVALID_ARGUMENT );
1249}
1250#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1251
Jaeden Ameroccdce902019-01-10 11:42:27 +00001252#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001253static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1254 unsigned char *buf, size_t size )
1255{
Janos Follath24eed8d2019-11-22 13:21:35 +00001256 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001257 unsigned char *c;
1258 size_t len = 0;
1259
1260 c = buf + size;
1261
1262 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1263
1264 return( (int) len );
1265}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001266#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001267
Gilles Peskinef603c712019-01-19 13:40:11 +01001268static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1269 uint8_t *data,
1270 size_t data_size,
1271 size_t *data_length,
1272 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001273{
Gilles Peskine5d309672019-07-12 23:47:28 +02001274#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1275 const psa_drv_se_t *drv;
1276 psa_drv_se_context_t *drv_context;
1277#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1278
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001279 *data_length = 0;
1280
Gilles Peskine8e338702019-07-30 20:06:31 +02001281 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001282 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001283
Gilles Peskinef9168942019-09-12 19:20:29 +02001284 /* Reject a zero-length output buffer now, since this can never be a
1285 * valid key representation. This way we know that data must be a valid
1286 * pointer and we can do things like memset(data, ..., data_size). */
1287 if( data_size == 0 )
1288 return( PSA_ERROR_BUFFER_TOO_SMALL );
1289
Gilles Peskine5d309672019-07-12 23:47:28 +02001290#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001291 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001292 {
1293 psa_drv_se_export_key_t method;
1294 if( drv->key_management == NULL )
1295 return( PSA_ERROR_NOT_SUPPORTED );
1296 method = ( export_public_key ?
1297 drv->key_management->p_export_public :
1298 drv->key_management->p_export );
1299 if( method == NULL )
1300 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001301 return( method( drv_context,
1302 slot->data.se.slot_number,
1303 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001304 }
1305#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1306
Gilles Peskine8e338702019-07-30 20:06:31 +02001307 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001308 {
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001309 if( slot->data.key.bytes > data_size )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001310 return( PSA_ERROR_BUFFER_TOO_SMALL );
Steven Cooreman71fd80d2020-07-07 21:12:27 +02001311 memcpy( data, slot->data.key.data, slot->data.key.bytes );
1312 memset( data + slot->data.key.bytes, 0,
1313 data_size - slot->data.key.bytes );
1314 *data_length = slot->data.key.bytes;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001315 return( PSA_SUCCESS );
1316 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001317#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001318 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001319 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001320 psa_status_t status;
1321
Gilles Peskineb46bef22019-07-30 21:32:04 +02001322 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001323 if( bytes > data_size )
1324 return( PSA_ERROR_BUFFER_TOO_SMALL );
1325 status = mbedtls_to_psa_error(
Steven Cooreman0024df62020-07-13 10:59:40 +02001326 mbedtls_ecp_write_key( slot->data.ecp,
1327 data, bytes ) );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001328 if( status != PSA_SUCCESS )
1329 return( status );
1330 memset( data + bytes, 0, data_size - bytes );
1331 *data_length = bytes;
1332 return( PSA_SUCCESS );
1333 }
1334#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001335 else
Moran Peker17e36e12018-05-02 12:55:20 +03001336 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001337#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001338 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1339 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001340 {
Moran Pekera998bc62018-04-16 18:16:20 +03001341 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001342 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001343 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001344 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001345#if defined(MBEDTLS_RSA_C)
1346 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001347 pk.pk_info = &mbedtls_rsa_info;
1348 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#else
1350 return( PSA_ERROR_NOT_SUPPORTED );
1351#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001352 }
1353 else
1354 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001355#if defined(MBEDTLS_ECP_C)
1356 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001357 pk.pk_info = &mbedtls_eckey_info;
1358 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001359#else
1360 return( PSA_ERROR_NOT_SUPPORTED );
1361#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001362 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001363 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001364 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001365 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001366 }
Moran Peker17e36e12018-05-02 12:55:20 +03001367 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001368 {
Moran Peker17e36e12018-05-02 12:55:20 +03001369 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001370 }
Moran Peker60364322018-04-29 11:34:58 +03001371 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001372 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001373 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001374 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001375 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001376 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1377 * Move the data to the beginning and erase remaining data
1378 * at the original location. */
1379 if( 2 * (size_t) ret <= data_size )
1380 {
1381 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 }
1384 else if( (size_t) ret < data_size )
1385 {
1386 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001387 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001388 }
Moran Pekera998bc62018-04-16 18:16:20 +03001389 *data_length = ret;
1390 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001391 }
1392 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001393#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001394 {
1395 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001396 it is valid for a special-purpose implementation to omit
1397 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001398 return( PSA_ERROR_NOT_SUPPORTED );
1399 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001400 }
1401}
1402
Gilles Peskinec5487a82018-12-03 18:08:14 +01001403psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001404 uint8_t *data,
1405 size_t data_size,
1406 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001407{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001408 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001409 psa_status_t status;
1410
1411 /* Set the key to empty now, so that even when there are errors, we always
1412 * set data_length to a value between 0 and data_size. On error, setting
1413 * the key to empty is a good choice because an empty key representation is
1414 * unlikely to be accepted anywhere. */
1415 *data_length = 0;
1416
1417 /* Export requires the EXPORT flag. There is an exception for public keys,
1418 * which don't require any flag, but psa_get_key_from_slot takes
1419 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001420 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001421 if( status != PSA_SUCCESS )
1422 return( status );
1423 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001424 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001425}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001426
Gilles Peskinec5487a82018-12-03 18:08:14 +01001427psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001428 uint8_t *data,
1429 size_t data_size,
1430 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001431{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001432 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001433 psa_status_t status;
1434
1435 /* Set the key to empty now, so that even when there are errors, we always
1436 * set data_length to a value between 0 and data_size. On error, setting
1437 * the key to empty is a good choice because an empty key representation is
1438 * unlikely to be accepted anywhere. */
1439 *data_length = 0;
1440
1441 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001442 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001443 if( status != PSA_SUCCESS )
1444 return( status );
1445 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001446 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001447}
1448
Gilles Peskine91e8c332019-08-02 19:19:39 +02001449#if defined(static_assert)
1450static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1451 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001452static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001453 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001454static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001455 "One or more key attribute flag is listed as both internal-only and external-only" );
1456#endif
1457
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001458/** Validate that a key policy is internally well-formed.
1459 *
1460 * This function only rejects invalid policies. It does not validate the
1461 * consistency of the policy with respect to other attributes of the key
1462 * such as the key type.
1463 */
1464static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001465{
1466 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001467 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001468 PSA_KEY_USAGE_ENCRYPT |
1469 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001470 PSA_KEY_USAGE_SIGN_HASH |
1471 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001472 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1473 return( PSA_ERROR_INVALID_ARGUMENT );
1474
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 return( PSA_SUCCESS );
1476}
1477
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001478/** Validate the internal consistency of key attributes.
1479 *
1480 * This function only rejects invalid attribute values. If does not
1481 * validate the consistency of the attributes with any key data that may
1482 * be involved in the creation of the key.
1483 *
1484 * Call this function early in the key creation process.
1485 *
1486 * \param[in] attributes Key attributes for the new key.
1487 * \param[out] p_drv On any return, the driver for the key, if any.
1488 * NULL for a transparent key.
1489 *
1490 */
1491static psa_status_t psa_validate_key_attributes(
1492 const psa_key_attributes_t *attributes,
1493 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001494{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001495 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001496
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001497 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1498 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001499 if( status != PSA_SUCCESS )
1500 return( status );
1501
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001502 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1503 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001504 if( status != PSA_SUCCESS )
1505 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506
1507 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001508 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001509 return( status );
1510
1511 /* Refuse to create overly large keys.
1512 * Note that this doesn't trigger on import if the attributes don't
1513 * explicitly specify a size (so psa_get_key_bits returns 0), so
1514 * psa_import_key() needs its own checks. */
1515 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1516 return( PSA_ERROR_NOT_SUPPORTED );
1517
Gilles Peskine91e8c332019-08-02 19:19:39 +02001518 /* Reject invalid flags. These should not be reachable through the API. */
1519 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1520 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1521 return( PSA_ERROR_INVALID_ARGUMENT );
1522
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001523 return( PSA_SUCCESS );
1524}
1525
Gilles Peskine4747d192019-04-17 15:05:45 +02001526/** Prepare a key slot to receive key material.
1527 *
1528 * This function allocates a key slot and sets its metadata.
1529 *
1530 * If this function fails, call psa_fail_key_creation().
1531 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001532 * This function is intended to be used as follows:
1533 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1534 * it with the specified attributes, and assign it a handle.
1535 * -# Populate the slot with the key material.
1536 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1537 * In case of failure at any step, stop the sequence and call
1538 * psa_fail_key_creation().
1539 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001540 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001541 * \param[in] attributes Key attributes for the new key.
1542 * \param[out] handle On success, a handle for the allocated slot.
1543 * \param[out] p_slot On success, a pointer to the prepared slot.
1544 * \param[out] p_drv On any return, the driver for the key, if any.
1545 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001546 *
1547 * \retval #PSA_SUCCESS
1548 * The key slot is ready to receive key material.
1549 * \return If this function fails, the key slot is an invalid state.
1550 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001551 */
1552static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001553 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001554 const psa_key_attributes_t *attributes,
1555 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001556 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001557 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001558{
1559 psa_status_t status;
1560 psa_key_slot_t *slot;
1561
Gilles Peskinedf179142019-07-15 22:02:14 +02001562 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001563 *p_drv = NULL;
1564
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001565 status = psa_validate_key_attributes( attributes, p_drv );
1566 if( status != PSA_SUCCESS )
1567 return( status );
1568
Gilles Peskineedbed562019-08-07 18:19:59 +02001569 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001570 if( status != PSA_SUCCESS )
1571 return( status );
1572 slot = *p_slot;
1573
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001574 /* We're storing the declared bit-size of the key. It's up to each
1575 * creation mechanism to verify that this information is correct.
1576 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001577 * an input (generate, device) but not for those where the bit-size
1578 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001579
1580 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001581
Gilles Peskine91e8c332019-08-02 19:19:39 +02001582 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001583 * external-only flags, query `attributes`. Thanks to the check
1584 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001585 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001586 * may have set. */
1587 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001588
Gilles Peskinecbaff462019-07-12 23:46:04 +02001589#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001590 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001591 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001592 * create the key file in internal storage, create the
1593 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001594 * persistent data. This is done by starting a transaction that will
1595 * encompass these three actions.
1596 * For registering a volatile key, we just need to find an appropriate
1597 * slot number inside the SE. Since the key is designated volatile, creating
1598 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001599 /* The first thing to do is to find a slot number for the new key.
1600 * We save the slot number in persistent storage as part of the
1601 * transaction data. It will be needed to recover if the power
1602 * fails during the key creation process, to clean up on the secure
1603 * element side after restarting. Obtaining a slot number from the
1604 * secure element driver updates its persistent state, but we do not yet
1605 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001606 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001607 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001608 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001609 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001610 &slot->data.se.slot_number );
1611 if( status != PSA_SUCCESS )
1612 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001613
1614 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001615 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001616 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1617 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1618 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1619 psa_crypto_transaction.key.id = slot->attr.id;
1620 status = psa_crypto_save_transaction( );
1621 if( status != PSA_SUCCESS )
1622 {
1623 (void) psa_crypto_stop_transaction( );
1624 return( status );
1625 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001626 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001627 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001628
1629 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1630 {
1631 /* Key registration only makes sense with a secure element. */
1632 return( PSA_ERROR_INVALID_ARGUMENT );
1633 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001634#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1635
Darryl Green0c6575a2018-11-07 16:05:30 +00001636 return( status );
1637}
Gilles Peskine4747d192019-04-17 15:05:45 +02001638
1639/** Finalize the creation of a key once its key material has been set.
1640 *
1641 * This entails writing the key to persistent storage.
1642 *
1643 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001644 * See the documentation of psa_start_key_creation() for the intended use
1645 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001646 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001647 * \param[in,out] slot Pointer to the slot with key material.
1648 * \param[in] driver The secure element driver for the key,
1649 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001650 *
1651 * \retval #PSA_SUCCESS
1652 * The key was successfully created. The handle is now valid.
1653 * \return If this function fails, the key slot is an invalid state.
1654 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001655 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001656static psa_status_t psa_finish_key_creation(
1657 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001658 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001659{
1660 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001661 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001662 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001663
1664#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001665 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001666 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001667#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1668 if( driver != NULL )
1669 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001670 psa_se_key_data_storage_t data;
1671#if defined(static_assert)
1672 static_assert( sizeof( slot->data.se.slot_number ) ==
1673 sizeof( data.slot_number ),
1674 "Slot number size does not match psa_se_key_data_storage_t" );
1675 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1676 "Bit-size size does not match psa_se_key_data_storage_t" );
1677#endif
1678 memcpy( &data.slot_number, &slot->data.se.slot_number,
1679 sizeof( slot->data.se.slot_number ) );
1680 memcpy( &data.bits, &slot->attr.bits,
1681 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001682 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001683 (uint8_t*) &data,
1684 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001685 }
1686 else
1687#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1688 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001689 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001690 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001691 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001692 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1693 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001694 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001695 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1696 status = psa_internal_export_key( slot,
1697 buffer, buffer_size, &length,
1698 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001699 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001700 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001701 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001702
Gilles Peskinef9168942019-09-12 19:20:29 +02001703 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001704 mbedtls_free( buffer );
1705 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001706 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001707#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1708
Gilles Peskinecbaff462019-07-12 23:46:04 +02001709#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001710 /* Finish the transaction for a key creation. This does not
1711 * happen when registering an existing key. Detect this case
1712 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001713 * creation of a persistent key in a secure element requires a transaction,
1714 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001715 if( driver != NULL &&
1716 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001717 {
1718 status = psa_save_se_persistent_data( driver );
1719 if( status != PSA_SUCCESS )
1720 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001721 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001722 return( status );
1723 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001724 status = psa_crypto_stop_transaction( );
1725 if( status != PSA_SUCCESS )
1726 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001727 }
1728#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1729
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 return( status );
1731}
1732
1733/** Abort the creation of a key.
1734 *
1735 * You may call this function after calling psa_start_key_creation(),
1736 * or after psa_finish_key_creation() fails. In other circumstances, this
1737 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001738 * See the documentation of psa_start_key_creation() for the intended use
1739 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001740 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * \param[in,out] slot Pointer to the slot with key material.
1742 * \param[in] driver The secure element driver for the key,
1743 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001744 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001745static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001746 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001747{
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 (void) driver;
1749
Gilles Peskine4747d192019-04-17 15:05:45 +02001750 if( slot == NULL )
1751 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001752
1753#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001754 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 * element, and the failure happened later (when saving metadata
1756 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001757 * element.
1758 * https://github.com/ARMmbed/mbed-crypto/issues/217
1759 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001760
Gilles Peskined7729582019-08-05 15:55:54 +02001761 /* Abort the ongoing transaction if any (there may not be one if
1762 * the creation process failed before starting one, or if the
1763 * key creation is a registration of a key in a secure element).
1764 * Earlier functions must already have done what it takes to undo any
1765 * partial creation. All that's left is to update the transaction data
1766 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001767 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001768#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1769
Gilles Peskine4747d192019-04-17 15:05:45 +02001770 psa_wipe_key_slot( slot );
1771}
1772
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001773/** Validate optional attributes during key creation.
1774 *
1775 * Some key attributes are optional during key creation. If they are
1776 * specified in the attributes structure, check that they are consistent
1777 * with the data in the slot.
1778 *
1779 * This function should be called near the end of key creation, after
1780 * the slot in memory is fully populated but before saving persistent data.
1781 */
1782static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001783 const psa_key_slot_t *slot,
1784 const psa_key_attributes_t *attributes )
1785{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001786 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001787 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001788 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001789 return( PSA_ERROR_INVALID_ARGUMENT );
1790 }
1791
1792 if( attributes->domain_parameters_size != 0 )
1793 {
1794#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001795 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796 {
1797 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001798 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001799 mbedtls_mpi_init( &actual );
1800 mbedtls_mpi_init( &required );
1801 ret = mbedtls_rsa_export( slot->data.rsa,
1802 NULL, NULL, NULL, NULL, &actual );
1803 if( ret != 0 )
1804 goto rsa_exit;
1805 ret = mbedtls_mpi_read_binary( &required,
1806 attributes->domain_parameters,
1807 attributes->domain_parameters_size );
1808 if( ret != 0 )
1809 goto rsa_exit;
1810 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1811 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1812 rsa_exit:
1813 mbedtls_mpi_free( &actual );
1814 mbedtls_mpi_free( &required );
1815 if( ret != 0)
1816 return( mbedtls_to_psa_error( ret ) );
1817 }
1818 else
1819#endif
1820 {
1821 return( PSA_ERROR_INVALID_ARGUMENT );
1822 }
1823 }
1824
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001825 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001826 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001827 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828 return( PSA_ERROR_INVALID_ARGUMENT );
1829 }
1830
1831 return( PSA_SUCCESS );
1832}
1833
Gilles Peskine4747d192019-04-17 15:05:45 +02001834psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001835 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001836 size_t data_length,
1837 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001838{
1839 psa_status_t status;
1840 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001841 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001842
Gilles Peskine0f84d622019-09-12 19:03:13 +02001843 /* Reject zero-length symmetric keys (including raw data key objects).
1844 * This also rejects any key which might be encoded as an empty string,
1845 * which is never valid. */
1846 if( data_length == 0 )
1847 return( PSA_ERROR_INVALID_ARGUMENT );
1848
Gilles Peskinedf179142019-07-15 22:02:14 +02001849 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1850 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001851 if( status != PSA_SUCCESS )
1852 goto exit;
1853
Gilles Peskine5d309672019-07-12 23:47:28 +02001854#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1855 if( driver != NULL )
1856 {
1857 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001858 /* The driver should set the number of key bits, however in
1859 * case it doesn't, we initialize bits to an invalid value. */
1860 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001861 if( drv->key_management == NULL ||
1862 drv->key_management->p_import == NULL )
1863 {
1864 status = PSA_ERROR_NOT_SUPPORTED;
1865 goto exit;
1866 }
1867 status = drv->key_management->p_import(
1868 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001869 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001870 &bits );
1871 if( status != PSA_SUCCESS )
1872 goto exit;
1873 if( bits > PSA_MAX_KEY_BITS )
1874 {
1875 status = PSA_ERROR_NOT_SUPPORTED;
1876 goto exit;
1877 }
1878 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001879 }
1880 else
1881#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1882 {
1883 status = psa_import_key_into_slot( slot, data, data_length );
1884 if( status != PSA_SUCCESS )
1885 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001886 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001887 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001888 if( status != PSA_SUCCESS )
1889 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001890
Gilles Peskine011e4282019-06-26 18:34:38 +02001891 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001892exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001893 if( status != PSA_SUCCESS )
1894 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001895 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001896 *handle = 0;
1897 }
1898 return( status );
1899}
1900
Gilles Peskined7729582019-08-05 15:55:54 +02001901#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1902psa_status_t mbedtls_psa_register_se_key(
1903 const psa_key_attributes_t *attributes )
1904{
1905 psa_status_t status;
1906 psa_key_slot_t *slot = NULL;
1907 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001908 psa_key_handle_t handle = 0;
1909
1910 /* Leaving attributes unspecified is not currently supported.
1911 * It could make sense to query the key type and size from the
1912 * secure element, but not all secure elements support this
1913 * and the driver HAL doesn't currently support it. */
1914 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1915 return( PSA_ERROR_NOT_SUPPORTED );
1916 if( psa_get_key_bits( attributes ) == 0 )
1917 return( PSA_ERROR_NOT_SUPPORTED );
1918
1919 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1920 &handle, &slot, &driver );
1921 if( status != PSA_SUCCESS )
1922 goto exit;
1923
Gilles Peskined7729582019-08-05 15:55:54 +02001924 status = psa_finish_key_creation( slot, driver );
1925
1926exit:
1927 if( status != PSA_SUCCESS )
1928 {
1929 psa_fail_key_creation( slot, driver );
1930 }
1931 /* Registration doesn't keep the key in RAM. */
1932 psa_close_key( handle );
1933 return( status );
1934}
1935#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1936
Gilles Peskinef603c712019-01-19 13:40:11 +01001937static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001938 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001939{
1940 psa_status_t status;
1941 uint8_t *buffer = NULL;
1942 size_t buffer_size = 0;
1943 size_t length;
1944
Gilles Peskine8e338702019-07-30 20:06:31 +02001945 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001946 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001947 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001948 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001949 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001950 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1951 if( status != PSA_SUCCESS )
1952 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001953 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001954 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001955
1956exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001957 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001958 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001959 return( status );
1960}
1961
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001962psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1963 const psa_key_attributes_t *specified_attributes,
1964 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001965{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001966 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001967 psa_key_slot_t *source_slot = NULL;
1968 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001970 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001971
Gilles Peskine28f8f302019-07-24 13:30:31 +02001972 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001973 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001974 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001975 goto exit;
1976
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001977 status = psa_validate_optional_attributes( source_slot,
1978 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001979 if( status != PSA_SUCCESS )
1980 goto exit;
1981
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001982 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001983 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001984 if( status != PSA_SUCCESS )
1985 goto exit;
1986
Gilles Peskinedf179142019-07-15 22:02:14 +02001987 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1988 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001989 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001990 if( status != PSA_SUCCESS )
1991 goto exit;
1992
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001993#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1994 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001995 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001996 /* Copying to a secure element is not implemented yet. */
1997 status = PSA_ERROR_NOT_SUPPORTED;
1998 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001999 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002000#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002001
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002002 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002003 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002004 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002005
Gilles Peskine011e4282019-06-26 18:34:38 +02002006 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002008 if( status != PSA_SUCCESS )
2009 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002010 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002011 *target_handle = 0;
2012 }
2013 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002014}
2015
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002016
2017
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002018/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002019/* Message digests */
2020/****************************************************************/
2021
Gilles Peskineb16841e2019-10-10 20:36:12 +02002022#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002023static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002024{
2025 switch( alg )
2026 {
2027#if defined(MBEDTLS_MD2_C)
2028 case PSA_ALG_MD2:
2029 return( &mbedtls_md2_info );
2030#endif
2031#if defined(MBEDTLS_MD4_C)
2032 case PSA_ALG_MD4:
2033 return( &mbedtls_md4_info );
2034#endif
2035#if defined(MBEDTLS_MD5_C)
2036 case PSA_ALG_MD5:
2037 return( &mbedtls_md5_info );
2038#endif
2039#if defined(MBEDTLS_RIPEMD160_C)
2040 case PSA_ALG_RIPEMD160:
2041 return( &mbedtls_ripemd160_info );
2042#endif
2043#if defined(MBEDTLS_SHA1_C)
2044 case PSA_ALG_SHA_1:
2045 return( &mbedtls_sha1_info );
2046#endif
2047#if defined(MBEDTLS_SHA256_C)
2048 case PSA_ALG_SHA_224:
2049 return( &mbedtls_sha224_info );
2050 case PSA_ALG_SHA_256:
2051 return( &mbedtls_sha256_info );
2052#endif
2053#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002054#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002055 case PSA_ALG_SHA_384:
2056 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002057#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002058 case PSA_ALG_SHA_512:
2059 return( &mbedtls_sha512_info );
2060#endif
2061 default:
2062 return( NULL );
2063 }
2064}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002065#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002066
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002067psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2068{
2069 switch( operation->alg )
2070 {
Gilles Peskine81736312018-06-26 15:04:31 +02002071 case 0:
2072 /* The object has (apparently) been initialized but it is not
2073 * in use. It's ok to call abort on such an object, and there's
2074 * nothing to do. */
2075 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002076#if defined(MBEDTLS_MD2_C)
2077 case PSA_ALG_MD2:
2078 mbedtls_md2_free( &operation->ctx.md2 );
2079 break;
2080#endif
2081#if defined(MBEDTLS_MD4_C)
2082 case PSA_ALG_MD4:
2083 mbedtls_md4_free( &operation->ctx.md4 );
2084 break;
2085#endif
2086#if defined(MBEDTLS_MD5_C)
2087 case PSA_ALG_MD5:
2088 mbedtls_md5_free( &operation->ctx.md5 );
2089 break;
2090#endif
2091#if defined(MBEDTLS_RIPEMD160_C)
2092 case PSA_ALG_RIPEMD160:
2093 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2094 break;
2095#endif
2096#if defined(MBEDTLS_SHA1_C)
2097 case PSA_ALG_SHA_1:
2098 mbedtls_sha1_free( &operation->ctx.sha1 );
2099 break;
2100#endif
2101#if defined(MBEDTLS_SHA256_C)
2102 case PSA_ALG_SHA_224:
2103 case PSA_ALG_SHA_256:
2104 mbedtls_sha256_free( &operation->ctx.sha256 );
2105 break;
2106#endif
2107#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002108#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002110#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002111 case PSA_ALG_SHA_512:
2112 mbedtls_sha512_free( &operation->ctx.sha512 );
2113 break;
2114#endif
2115 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002116 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002117 }
2118 operation->alg = 0;
2119 return( PSA_SUCCESS );
2120}
2121
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002122psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002123 psa_algorithm_t alg )
2124{
Janos Follath24eed8d2019-11-22 13:21:35 +00002125 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002126
2127 /* A context must be freshly initialized before it can be set up. */
2128 if( operation->alg != 0 )
2129 {
2130 return( PSA_ERROR_BAD_STATE );
2131 }
2132
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002133 switch( alg )
2134 {
2135#if defined(MBEDTLS_MD2_C)
2136 case PSA_ALG_MD2:
2137 mbedtls_md2_init( &operation->ctx.md2 );
2138 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2139 break;
2140#endif
2141#if defined(MBEDTLS_MD4_C)
2142 case PSA_ALG_MD4:
2143 mbedtls_md4_init( &operation->ctx.md4 );
2144 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2145 break;
2146#endif
2147#if defined(MBEDTLS_MD5_C)
2148 case PSA_ALG_MD5:
2149 mbedtls_md5_init( &operation->ctx.md5 );
2150 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2151 break;
2152#endif
2153#if defined(MBEDTLS_RIPEMD160_C)
2154 case PSA_ALG_RIPEMD160:
2155 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2156 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2157 break;
2158#endif
2159#if defined(MBEDTLS_SHA1_C)
2160 case PSA_ALG_SHA_1:
2161 mbedtls_sha1_init( &operation->ctx.sha1 );
2162 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2163 break;
2164#endif
2165#if defined(MBEDTLS_SHA256_C)
2166 case PSA_ALG_SHA_224:
2167 mbedtls_sha256_init( &operation->ctx.sha256 );
2168 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2169 break;
2170 case PSA_ALG_SHA_256:
2171 mbedtls_sha256_init( &operation->ctx.sha256 );
2172 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2173 break;
2174#endif
2175#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002176#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 case PSA_ALG_SHA_384:
2178 mbedtls_sha512_init( &operation->ctx.sha512 );
2179 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2180 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002181#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002182 case PSA_ALG_SHA_512:
2183 mbedtls_sha512_init( &operation->ctx.sha512 );
2184 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2185 break;
2186#endif
2187 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002188 return( PSA_ALG_IS_HASH( alg ) ?
2189 PSA_ERROR_NOT_SUPPORTED :
2190 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002191 }
2192 if( ret == 0 )
2193 operation->alg = alg;
2194 else
2195 psa_hash_abort( operation );
2196 return( mbedtls_to_psa_error( ret ) );
2197}
2198
2199psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2200 const uint8_t *input,
2201 size_t input_length )
2202{
Janos Follath24eed8d2019-11-22 13:21:35 +00002203 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002204
2205 /* Don't require hash implementations to behave correctly on a
2206 * zero-length input, which may have an invalid pointer. */
2207 if( input_length == 0 )
2208 return( PSA_SUCCESS );
2209
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002210 switch( operation->alg )
2211 {
2212#if defined(MBEDTLS_MD2_C)
2213 case PSA_ALG_MD2:
2214 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2215 input, input_length );
2216 break;
2217#endif
2218#if defined(MBEDTLS_MD4_C)
2219 case PSA_ALG_MD4:
2220 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2221 input, input_length );
2222 break;
2223#endif
2224#if defined(MBEDTLS_MD5_C)
2225 case PSA_ALG_MD5:
2226 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2227 input, input_length );
2228 break;
2229#endif
2230#if defined(MBEDTLS_RIPEMD160_C)
2231 case PSA_ALG_RIPEMD160:
2232 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2233 input, input_length );
2234 break;
2235#endif
2236#if defined(MBEDTLS_SHA1_C)
2237 case PSA_ALG_SHA_1:
2238 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2239 input, input_length );
2240 break;
2241#endif
2242#if defined(MBEDTLS_SHA256_C)
2243 case PSA_ALG_SHA_224:
2244 case PSA_ALG_SHA_256:
2245 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2246 input, input_length );
2247 break;
2248#endif
2249#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002250#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002251 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002252#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002253 case PSA_ALG_SHA_512:
2254 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2255 input, input_length );
2256 break;
2257#endif
2258 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002259 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002261
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002262 if( ret != 0 )
2263 psa_hash_abort( operation );
2264 return( mbedtls_to_psa_error( ret ) );
2265}
2266
2267psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2268 uint8_t *hash,
2269 size_t hash_size,
2270 size_t *hash_length )
2271{
itayzafrir40835d42018-08-02 13:14:17 +03002272 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002273 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002274 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002275
2276 /* Fill the output buffer with something that isn't a valid hash
2277 * (barring an attack on the hash and deliberately-crafted input),
2278 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002279 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002280 /* If hash_size is 0 then hash may be NULL and then the
2281 * call to memset would have undefined behavior. */
2282 if( hash_size != 0 )
2283 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002284
2285 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002286 {
2287 status = PSA_ERROR_BUFFER_TOO_SMALL;
2288 goto exit;
2289 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002290
2291 switch( operation->alg )
2292 {
2293#if defined(MBEDTLS_MD2_C)
2294 case PSA_ALG_MD2:
2295 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2296 break;
2297#endif
2298#if defined(MBEDTLS_MD4_C)
2299 case PSA_ALG_MD4:
2300 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2301 break;
2302#endif
2303#if defined(MBEDTLS_MD5_C)
2304 case PSA_ALG_MD5:
2305 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2306 break;
2307#endif
2308#if defined(MBEDTLS_RIPEMD160_C)
2309 case PSA_ALG_RIPEMD160:
2310 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2311 break;
2312#endif
2313#if defined(MBEDTLS_SHA1_C)
2314 case PSA_ALG_SHA_1:
2315 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2316 break;
2317#endif
2318#if defined(MBEDTLS_SHA256_C)
2319 case PSA_ALG_SHA_224:
2320 case PSA_ALG_SHA_256:
2321 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2322 break;
2323#endif
2324#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002325#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002326 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002327#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002328 case PSA_ALG_SHA_512:
2329 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2330 break;
2331#endif
2332 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002333 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002334 }
itayzafrir40835d42018-08-02 13:14:17 +03002335 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002336
itayzafrir40835d42018-08-02 13:14:17 +03002337exit:
2338 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002340 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 return( psa_hash_abort( operation ) );
2342 }
2343 else
2344 {
2345 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002346 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002347 }
2348}
2349
Gilles Peskine2d277862018-06-18 15:41:12 +02002350psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2351 const uint8_t *hash,
2352 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002353{
2354 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2355 size_t actual_hash_length;
2356 psa_status_t status = psa_hash_finish( operation,
2357 actual_hash, sizeof( actual_hash ),
2358 &actual_hash_length );
2359 if( status != PSA_SUCCESS )
2360 return( status );
2361 if( actual_hash_length != hash_length )
2362 return( PSA_ERROR_INVALID_SIGNATURE );
2363 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2364 return( PSA_ERROR_INVALID_SIGNATURE );
2365 return( PSA_SUCCESS );
2366}
2367
Gilles Peskine0a749c82019-11-28 19:33:58 +01002368psa_status_t psa_hash_compute( psa_algorithm_t alg,
2369 const uint8_t *input, size_t input_length,
2370 uint8_t *hash, size_t hash_size,
2371 size_t *hash_length )
2372{
2373 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2374 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2375
2376 *hash_length = hash_size;
2377 status = psa_hash_setup( &operation, alg );
2378 if( status != PSA_SUCCESS )
2379 goto exit;
2380 status = psa_hash_update( &operation, input, input_length );
2381 if( status != PSA_SUCCESS )
2382 goto exit;
2383 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2384 if( status != PSA_SUCCESS )
2385 goto exit;
2386
2387exit:
2388 if( status == PSA_SUCCESS )
2389 status = psa_hash_abort( &operation );
2390 else
2391 psa_hash_abort( &operation );
2392 return( status );
2393}
2394
2395psa_status_t psa_hash_compare( psa_algorithm_t alg,
2396 const uint8_t *input, size_t input_length,
2397 const uint8_t *hash, size_t hash_length )
2398{
2399 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2400 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2401
2402 status = psa_hash_setup( &operation, alg );
2403 if( status != PSA_SUCCESS )
2404 goto exit;
2405 status = psa_hash_update( &operation, input, input_length );
2406 if( status != PSA_SUCCESS )
2407 goto exit;
2408 status = psa_hash_verify( &operation, hash, hash_length );
2409 if( status != PSA_SUCCESS )
2410 goto exit;
2411
2412exit:
2413 if( status == PSA_SUCCESS )
2414 status = psa_hash_abort( &operation );
2415 else
2416 psa_hash_abort( &operation );
2417 return( status );
2418}
2419
Gilles Peskineeb35d782019-01-22 17:56:16 +01002420psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2421 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002422{
2423 if( target_operation->alg != 0 )
2424 return( PSA_ERROR_BAD_STATE );
2425
2426 switch( source_operation->alg )
2427 {
2428 case 0:
2429 return( PSA_ERROR_BAD_STATE );
2430#if defined(MBEDTLS_MD2_C)
2431 case PSA_ALG_MD2:
2432 mbedtls_md2_clone( &target_operation->ctx.md2,
2433 &source_operation->ctx.md2 );
2434 break;
2435#endif
2436#if defined(MBEDTLS_MD4_C)
2437 case PSA_ALG_MD4:
2438 mbedtls_md4_clone( &target_operation->ctx.md4,
2439 &source_operation->ctx.md4 );
2440 break;
2441#endif
2442#if defined(MBEDTLS_MD5_C)
2443 case PSA_ALG_MD5:
2444 mbedtls_md5_clone( &target_operation->ctx.md5,
2445 &source_operation->ctx.md5 );
2446 break;
2447#endif
2448#if defined(MBEDTLS_RIPEMD160_C)
2449 case PSA_ALG_RIPEMD160:
2450 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2451 &source_operation->ctx.ripemd160 );
2452 break;
2453#endif
2454#if defined(MBEDTLS_SHA1_C)
2455 case PSA_ALG_SHA_1:
2456 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2457 &source_operation->ctx.sha1 );
2458 break;
2459#endif
2460#if defined(MBEDTLS_SHA256_C)
2461 case PSA_ALG_SHA_224:
2462 case PSA_ALG_SHA_256:
2463 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2464 &source_operation->ctx.sha256 );
2465 break;
2466#endif
2467#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002468#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002469 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002470#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002471 case PSA_ALG_SHA_512:
2472 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2473 &source_operation->ctx.sha512 );
2474 break;
2475#endif
2476 default:
2477 return( PSA_ERROR_NOT_SUPPORTED );
2478 }
2479
2480 target_operation->alg = source_operation->alg;
2481 return( PSA_SUCCESS );
2482}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002483
2484
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002485/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002486/* MAC */
2487/****************************************************************/
2488
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002489static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 psa_algorithm_t alg,
2491 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002492 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002493 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002496 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002498 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002499 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002500
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2502 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002503 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002505 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002506 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002507 mode = MBEDTLS_MODE_STREAM;
2508 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002509 case PSA_ALG_CTR:
2510 mode = MBEDTLS_MODE_CTR;
2511 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002512 case PSA_ALG_CFB:
2513 mode = MBEDTLS_MODE_CFB;
2514 break;
2515 case PSA_ALG_OFB:
2516 mode = MBEDTLS_MODE_OFB;
2517 break;
2518 case PSA_ALG_CBC_NO_PADDING:
2519 mode = MBEDTLS_MODE_CBC;
2520 break;
2521 case PSA_ALG_CBC_PKCS7:
2522 mode = MBEDTLS_MODE_CBC;
2523 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002524 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525 mode = MBEDTLS_MODE_CCM;
2526 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002527 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002528 mode = MBEDTLS_MODE_GCM;
2529 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002530 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2531 mode = MBEDTLS_MODE_CHACHAPOLY;
2532 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002533 default:
2534 return( NULL );
2535 }
2536 }
2537 else if( alg == PSA_ALG_CMAC )
2538 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002539 else
2540 return( NULL );
2541
2542 switch( key_type )
2543 {
2544 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002545 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 break;
2547 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002548 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2549 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002550 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002551 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002552 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002553 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002554 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2555 * but two-key Triple-DES is functionally three-key Triple-DES
2556 * with K1=K3, so that's how we present it to mbedtls. */
2557 if( key_bits == 128 )
2558 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 break;
2560 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002561 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002562 break;
2563 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002564 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002565 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002566 case PSA_KEY_TYPE_CHACHA20:
2567 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2568 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 default:
2570 return( NULL );
2571 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002572 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002573 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002574
Jaeden Amero23bbb752018-06-26 14:16:54 +01002575 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2576 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002577}
2578
Gilles Peskinea05219c2018-11-16 16:02:56 +01002579#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002580static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002581{
Gilles Peskine2d277862018-06-18 15:41:12 +02002582 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002583 {
2584 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002587 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002589 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002591 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002592 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002593 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002594 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002595 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002596 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002597 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002598 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002599 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002600 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002601 return( 128 );
2602 default:
2603 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002604 }
2605}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002606#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002607
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002608/* Initialize the MAC operation structure. Once this function has been
2609 * called, psa_mac_abort can run and will do the right thing. */
2610static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2611 psa_algorithm_t alg )
2612{
2613 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2614
2615 operation->alg = alg;
2616 operation->key_set = 0;
2617 operation->iv_set = 0;
2618 operation->iv_required = 0;
2619 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002620 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002621
2622#if defined(MBEDTLS_CMAC_C)
2623 if( alg == PSA_ALG_CMAC )
2624 {
2625 operation->iv_required = 0;
2626 mbedtls_cipher_init( &operation->ctx.cmac );
2627 status = PSA_SUCCESS;
2628 }
2629 else
2630#endif /* MBEDTLS_CMAC_C */
2631#if defined(MBEDTLS_MD_C)
2632 if( PSA_ALG_IS_HMAC( operation->alg ) )
2633 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002634 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2635 operation->ctx.hmac.hash_ctx.alg = 0;
2636 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002637 }
2638 else
2639#endif /* MBEDTLS_MD_C */
2640 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002641 if( ! PSA_ALG_IS_MAC( alg ) )
2642 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002643 }
2644
2645 if( status != PSA_SUCCESS )
2646 memset( operation, 0, sizeof( *operation ) );
2647 return( status );
2648}
2649
Gilles Peskine01126fa2018-07-12 17:04:55 +02002650#if defined(MBEDTLS_MD_C)
2651static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2652{
Gilles Peskine3f108122018-12-07 18:14:53 +01002653 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002654 return( psa_hash_abort( &hmac->hash_ctx ) );
2655}
2656#endif /* MBEDTLS_MD_C */
2657
Gilles Peskine8c9def32018-02-08 10:02:12 +01002658psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2659{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002660 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002662 /* The object has (apparently) been initialized but it is not
2663 * in use. It's ok to call abort on such an object, and there's
2664 * nothing to do. */
2665 return( PSA_SUCCESS );
2666 }
2667 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002669 if( operation->alg == PSA_ALG_CMAC )
2670 {
2671 mbedtls_cipher_free( &operation->ctx.cmac );
2672 }
2673 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 if( PSA_ALG_IS_HMAC( operation->alg ) )
2677 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002678 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002679 }
2680 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682 {
2683 /* Sanity check (shouldn't happen: operation->alg should
2684 * always have been initialized to a valid value). */
2685 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002686 }
Moran Peker41deec42018-04-04 15:43:05 +03002687
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688 operation->alg = 0;
2689 operation->key_set = 0;
2690 operation->iv_set = 0;
2691 operation->iv_required = 0;
2692 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002693 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002694
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002696
2697bad_state:
2698 /* If abort is called on an uninitialized object, we can't trust
2699 * anything. Wipe the object in case it contains confidential data.
2700 * This may result in a memory leak if a pointer gets overwritten,
2701 * but it's too late to do anything about this. */
2702 memset( operation, 0, sizeof( *operation ) );
2703 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002704}
2705
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002706#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002707static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002708 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002709 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002710 const mbedtls_cipher_info_t *cipher_info )
2711{
Janos Follath24eed8d2019-11-22 13:21:35 +00002712 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002713
2714 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715
2716 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2717 if( ret != 0 )
2718 return( ret );
2719
2720 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002721 slot->data.key.data,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722 key_bits );
2723 return( ret );
2724}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002725#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002726
Gilles Peskine248051a2018-06-20 16:09:38 +02002727#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002728static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2729 const uint8_t *key,
2730 size_t key_length,
2731 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002732{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002733 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002734 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002735 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002736 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002737 psa_status_t status;
2738
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002739 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2740 * overflow below. This should never trigger if the hash algorithm
2741 * is implemented correctly. */
2742 /* The size checks against the ipad and opad buffers cannot be written
2743 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2744 * because that triggers -Wlogical-op on GCC 7.3. */
2745 if( block_size > sizeof( ipad ) )
2746 return( PSA_ERROR_NOT_SUPPORTED );
2747 if( block_size > sizeof( hmac->opad ) )
2748 return( PSA_ERROR_NOT_SUPPORTED );
2749 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002750 return( PSA_ERROR_NOT_SUPPORTED );
2751
Gilles Peskined223b522018-06-11 18:12:58 +02002752 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002753 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002754 status = psa_hash_compute( hash_alg, key, key_length,
2755 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002756 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002757 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002758 }
Gilles Peskine96889972018-07-12 17:07:03 +02002759 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2760 * but it is permitted. It is common when HMAC is used in HKDF, for
2761 * example. Don't call `memcpy` in the 0-length because `key` could be
2762 * an invalid pointer which would make the behavior undefined. */
2763 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002764 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765
Gilles Peskined223b522018-06-11 18:12:58 +02002766 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2767 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002768 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002769 ipad[i] ^= 0x36;
2770 memset( ipad + key_length, 0x36, block_size - key_length );
2771
2772 /* Copy the key material from ipad to opad, flipping the requisite bits,
2773 * and filling the rest of opad with the requisite constant. */
2774 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002775 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2776 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002777
Gilles Peskine01126fa2018-07-12 17:04:55 +02002778 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002780 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002781
Gilles Peskine01126fa2018-07-12 17:04:55 +02002782 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002783
2784cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002785 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002786
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002787 return( status );
2788}
Gilles Peskine248051a2018-06-20 16:09:38 +02002789#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002790
Gilles Peskine89167cb2018-07-08 20:12:23 +02002791static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002792 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002793 psa_algorithm_t alg,
2794 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002796 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002797 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002798 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002799 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002800 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002801 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002802 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002804 /* A context must be freshly initialized before it can be set up. */
2805 if( operation->alg != 0 )
2806 {
2807 return( PSA_ERROR_BAD_STATE );
2808 }
2809
Gilles Peskined911eb72018-08-14 15:18:45 +02002810 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002811 if( status != PSA_SUCCESS )
2812 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002813 if( is_sign )
2814 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002815
Gilles Peskine28f8f302019-07-24 13:30:31 +02002816 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002817 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002818 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002819 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002820
Gilles Peskine8c9def32018-02-08 10:02:12 +01002821#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002822 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002823 {
2824 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002825 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002826 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002827 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002828 if( cipher_info == NULL )
2829 {
2830 status = PSA_ERROR_NOT_SUPPORTED;
2831 goto exit;
2832 }
2833 operation->mac_size = cipher_info->block_size;
2834 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2835 status = mbedtls_to_psa_error( ret );
2836 }
2837 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002839#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002840 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002841 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002842 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002843 if( hash_alg == 0 )
2844 {
2845 status = PSA_ERROR_NOT_SUPPORTED;
2846 goto exit;
2847 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002848
2849 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2850 /* Sanity check. This shouldn't fail on a valid configuration. */
2851 if( operation->mac_size == 0 ||
2852 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2853 {
2854 status = PSA_ERROR_NOT_SUPPORTED;
2855 goto exit;
2856 }
2857
Gilles Peskine8e338702019-07-30 20:06:31 +02002858 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002859 {
2860 status = PSA_ERROR_INVALID_ARGUMENT;
2861 goto exit;
2862 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002863
Gilles Peskine01126fa2018-07-12 17:04:55 +02002864 status = psa_hmac_setup_internal( &operation->ctx.hmac,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02002865 slot->data.key.data,
2866 slot->data.key.bytes,
Gilles Peskine01126fa2018-07-12 17:04:55 +02002867 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002868 }
2869 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002870#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002871 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002872 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002873 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002874 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002875
Gilles Peskined911eb72018-08-14 15:18:45 +02002876 if( truncated == 0 )
2877 {
2878 /* The "normal" case: untruncated algorithm. Nothing to do. */
2879 }
2880 else if( truncated < 4 )
2881 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002882 /* A very short MAC is too short for security since it can be
2883 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2884 * so we make this our minimum, even though 32 bits is still
2885 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002886 status = PSA_ERROR_NOT_SUPPORTED;
2887 }
2888 else if( truncated > operation->mac_size )
2889 {
2890 /* It's impossible to "truncate" to a larger length. */
2891 status = PSA_ERROR_INVALID_ARGUMENT;
2892 }
2893 else
2894 operation->mac_size = truncated;
2895
Gilles Peskinefbfac682018-07-08 20:51:54 +02002896exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002897 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002899 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002900 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002901 else
2902 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002903 operation->key_set = 1;
2904 }
2905 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002906}
2907
Gilles Peskine89167cb2018-07-08 20:12:23 +02002908psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002909 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002910 psa_algorithm_t alg )
2911{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002912 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002913}
2914
2915psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002916 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002917 psa_algorithm_t alg )
2918{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920}
2921
Gilles Peskine8c9def32018-02-08 10:02:12 +01002922psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2923 const uint8_t *input,
2924 size_t input_length )
2925{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002926 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002927 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002928 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002930 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002931 operation->has_input = 1;
2932
Gilles Peskine8c9def32018-02-08 10:02:12 +01002933#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002934 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002935 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002936 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2937 input, input_length );
2938 status = mbedtls_to_psa_error( ret );
2939 }
2940 else
2941#endif /* MBEDTLS_CMAC_C */
2942#if defined(MBEDTLS_MD_C)
2943 if( PSA_ALG_IS_HMAC( operation->alg ) )
2944 {
2945 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2946 input_length );
2947 }
2948 else
2949#endif /* MBEDTLS_MD_C */
2950 {
2951 /* This shouldn't happen if `operation` was initialized by
2952 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002953 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002954 }
2955
Gilles Peskinefbfac682018-07-08 20:51:54 +02002956 if( status != PSA_SUCCESS )
2957 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002958 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002959}
2960
Gilles Peskine01126fa2018-07-12 17:04:55 +02002961#if defined(MBEDTLS_MD_C)
2962static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2963 uint8_t *mac,
2964 size_t mac_size )
2965{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002966 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002967 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2968 size_t hash_size = 0;
2969 size_t block_size = psa_get_hash_block_size( hash_alg );
2970 psa_status_t status;
2971
Gilles Peskine01126fa2018-07-12 17:04:55 +02002972 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2973 if( status != PSA_SUCCESS )
2974 return( status );
2975 /* From here on, tmp needs to be wiped. */
2976
2977 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2978 if( status != PSA_SUCCESS )
2979 goto exit;
2980
2981 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
2985 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2986 if( status != PSA_SUCCESS )
2987 goto exit;
2988
Gilles Peskined911eb72018-08-14 15:18:45 +02002989 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2990 if( status != PSA_SUCCESS )
2991 goto exit;
2992
2993 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002994
2995exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002996 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002997 return( status );
2998}
2999#endif /* MBEDTLS_MD_C */
3000
mohammad16036df908f2018-04-02 08:34:15 -07003001static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003002 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003003 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003004{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003005 if( ! operation->key_set )
3006 return( PSA_ERROR_BAD_STATE );
3007 if( operation->iv_required && ! operation->iv_set )
3008 return( PSA_ERROR_BAD_STATE );
3009
Gilles Peskine8c9def32018-02-08 10:02:12 +01003010 if( mac_size < operation->mac_size )
3011 return( PSA_ERROR_BUFFER_TOO_SMALL );
3012
Gilles Peskine8c9def32018-02-08 10:02:12 +01003013#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003014 if( operation->alg == PSA_ALG_CMAC )
3015 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003016 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3017 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3018 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003019 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003020 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003022 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003023 else
3024#endif /* MBEDTLS_CMAC_C */
3025#if defined(MBEDTLS_MD_C)
3026 if( PSA_ALG_IS_HMAC( operation->alg ) )
3027 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003028 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003029 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030 }
3031 else
3032#endif /* MBEDTLS_MD_C */
3033 {
3034 /* This shouldn't happen if `operation` was initialized by
3035 * a setup function. */
3036 return( PSA_ERROR_BAD_STATE );
3037 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003038}
3039
Gilles Peskineacd4be32018-07-08 19:56:25 +02003040psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3041 uint8_t *mac,
3042 size_t mac_size,
3043 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003044{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003045 psa_status_t status;
3046
Jaeden Amero252ef282019-02-15 14:05:35 +00003047 if( operation->alg == 0 )
3048 {
3049 return( PSA_ERROR_BAD_STATE );
3050 }
3051
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003052 /* Fill the output buffer with something that isn't a valid mac
3053 * (barring an attack on the mac and deliberately-crafted input),
3054 * in case the caller doesn't check the return status properly. */
3055 *mac_length = mac_size;
3056 /* If mac_size is 0 then mac may be NULL and then the
3057 * call to memset would have undefined behavior. */
3058 if( mac_size != 0 )
3059 memset( mac, '!', mac_size );
3060
Gilles Peskine89167cb2018-07-08 20:12:23 +02003061 if( ! operation->is_sign )
3062 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003063 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003064 }
mohammad16036df908f2018-04-02 08:34:15 -07003065
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003066 status = psa_mac_finish_internal( operation, mac, mac_size );
3067
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003068 if( status == PSA_SUCCESS )
3069 {
3070 status = psa_mac_abort( operation );
3071 if( status == PSA_SUCCESS )
3072 *mac_length = operation->mac_size;
3073 else
3074 memset( mac, '!', mac_size );
3075 }
3076 else
3077 psa_mac_abort( operation );
3078 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003079}
3080
Gilles Peskineacd4be32018-07-08 19:56:25 +02003081psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3082 const uint8_t *mac,
3083 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003084{
Gilles Peskine828ed142018-06-18 23:25:51 +02003085 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003086 psa_status_t status;
3087
Jaeden Amero252ef282019-02-15 14:05:35 +00003088 if( operation->alg == 0 )
3089 {
3090 return( PSA_ERROR_BAD_STATE );
3091 }
3092
Gilles Peskine89167cb2018-07-08 20:12:23 +02003093 if( operation->is_sign )
3094 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003095 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003096 }
3097 if( operation->mac_size != mac_length )
3098 {
3099 status = PSA_ERROR_INVALID_SIGNATURE;
3100 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003101 }
mohammad16036df908f2018-04-02 08:34:15 -07003102
3103 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003104 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003105 if( status != PSA_SUCCESS )
3106 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003107
3108 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3109 status = PSA_ERROR_INVALID_SIGNATURE;
3110
3111cleanup:
3112 if( status == PSA_SUCCESS )
3113 status = psa_mac_abort( operation );
3114 else
3115 psa_mac_abort( operation );
3116
Gilles Peskine3f108122018-12-07 18:14:53 +01003117 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003118
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003119 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003120}
3121
3122
Gilles Peskine20035e32018-02-03 22:44:14 +01003123
Gilles Peskine20035e32018-02-03 22:44:14 +01003124/****************************************************************/
3125/* Asymmetric cryptography */
3126/****************************************************************/
3127
Gilles Peskine2b450e32018-06-27 15:42:46 +02003128#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003129/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003130 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003131static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3132 size_t hash_length,
3133 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003134{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003135 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003136 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003137 *md_alg = mbedtls_md_get_type( md_info );
3138
3139 /* The Mbed TLS RSA module uses an unsigned int for hash length
3140 * parameters. Validate that it fits so that we don't risk an
3141 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003142#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003143 if( hash_length > UINT_MAX )
3144 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003145#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003146
3147#if defined(MBEDTLS_PKCS1_V15)
3148 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3149 * must be correct. */
3150 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3151 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003152 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003153 if( md_info == NULL )
3154 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003155 if( mbedtls_md_get_size( md_info ) != hash_length )
3156 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003157 }
3158#endif /* MBEDTLS_PKCS1_V15 */
3159
3160#if defined(MBEDTLS_PKCS1_V21)
3161 /* PSS requires a hash internally. */
3162 if( PSA_ALG_IS_RSA_PSS( alg ) )
3163 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003164 if( md_info == NULL )
3165 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003166 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003167#endif /* MBEDTLS_PKCS1_V21 */
3168
Gilles Peskine61b91d42018-06-08 16:09:36 +02003169 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003170}
3171
Gilles Peskine2b450e32018-06-27 15:42:46 +02003172static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3173 psa_algorithm_t alg,
3174 const uint8_t *hash,
3175 size_t hash_length,
3176 uint8_t *signature,
3177 size_t signature_size,
3178 size_t *signature_length )
3179{
3180 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003181 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003182 mbedtls_md_type_t md_alg;
3183
3184 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3185 if( status != PSA_SUCCESS )
3186 return( status );
3187
Gilles Peskine630a18a2018-06-29 17:49:35 +02003188 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003189 return( PSA_ERROR_BUFFER_TOO_SMALL );
3190
3191#if defined(MBEDTLS_PKCS1_V15)
3192 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3193 {
3194 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3195 MBEDTLS_MD_NONE );
3196 ret = mbedtls_rsa_pkcs1_sign( rsa,
3197 mbedtls_ctr_drbg_random,
3198 &global_data.ctr_drbg,
3199 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003200 md_alg,
3201 (unsigned int) hash_length,
3202 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003203 signature );
3204 }
3205 else
3206#endif /* MBEDTLS_PKCS1_V15 */
3207#if defined(MBEDTLS_PKCS1_V21)
3208 if( PSA_ALG_IS_RSA_PSS( alg ) )
3209 {
3210 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3211 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3212 mbedtls_ctr_drbg_random,
3213 &global_data.ctr_drbg,
3214 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003215 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003216 (unsigned int) hash_length,
3217 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003218 signature );
3219 }
3220 else
3221#endif /* MBEDTLS_PKCS1_V21 */
3222 {
3223 return( PSA_ERROR_INVALID_ARGUMENT );
3224 }
3225
3226 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003227 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003228 return( mbedtls_to_psa_error( ret ) );
3229}
3230
3231static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3232 psa_algorithm_t alg,
3233 const uint8_t *hash,
3234 size_t hash_length,
3235 const uint8_t *signature,
3236 size_t signature_length )
3237{
3238 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003239 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003240 mbedtls_md_type_t md_alg;
3241
3242 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3243 if( status != PSA_SUCCESS )
3244 return( status );
3245
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003246 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3247 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003248
3249#if defined(MBEDTLS_PKCS1_V15)
3250 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3251 {
3252 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3253 MBEDTLS_MD_NONE );
3254 ret = mbedtls_rsa_pkcs1_verify( rsa,
3255 mbedtls_ctr_drbg_random,
3256 &global_data.ctr_drbg,
3257 MBEDTLS_RSA_PUBLIC,
3258 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003259 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003260 hash,
3261 signature );
3262 }
3263 else
3264#endif /* MBEDTLS_PKCS1_V15 */
3265#if defined(MBEDTLS_PKCS1_V21)
3266 if( PSA_ALG_IS_RSA_PSS( alg ) )
3267 {
3268 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3269 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3270 mbedtls_ctr_drbg_random,
3271 &global_data.ctr_drbg,
3272 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003273 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003274 (unsigned int) hash_length,
3275 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003276 signature );
3277 }
3278 else
3279#endif /* MBEDTLS_PKCS1_V21 */
3280 {
3281 return( PSA_ERROR_INVALID_ARGUMENT );
3282 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003283
3284 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3285 * the rest of the signature is invalid". This has little use in
3286 * practice and PSA doesn't report this distinction. */
3287 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3288 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003289 return( mbedtls_to_psa_error( ret ) );
3290}
3291#endif /* MBEDTLS_RSA_C */
3292
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003293#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003294/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3295 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3296 * (even though these functions don't modify it). */
3297static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3298 psa_algorithm_t alg,
3299 const uint8_t *hash,
3300 size_t hash_length,
3301 uint8_t *signature,
3302 size_t signature_size,
3303 size_t *signature_length )
3304{
Janos Follath24eed8d2019-11-22 13:21:35 +00003305 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003306 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003307 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003308 mbedtls_mpi_init( &r );
3309 mbedtls_mpi_init( &s );
3310
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003311 if( signature_size < 2 * curve_bytes )
3312 {
3313 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3314 goto cleanup;
3315 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003316
Gilles Peskinea05219c2018-11-16 16:02:56 +01003317#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003318 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3319 {
3320 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3321 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3322 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003323 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3324 &ecp->d, hash,
3325 hash_length, md_alg,
3326 mbedtls_ctr_drbg_random,
3327 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003328 }
3329 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003330#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003331 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003332 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003333 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3334 hash, hash_length,
3335 mbedtls_ctr_drbg_random,
3336 &global_data.ctr_drbg ) );
3337 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003338
3339 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3340 signature,
3341 curve_bytes ) );
3342 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3343 signature + curve_bytes,
3344 curve_bytes ) );
3345
3346cleanup:
3347 mbedtls_mpi_free( &r );
3348 mbedtls_mpi_free( &s );
3349 if( ret == 0 )
3350 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003351 return( mbedtls_to_psa_error( ret ) );
3352}
3353
3354static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3355 const uint8_t *hash,
3356 size_t hash_length,
3357 const uint8_t *signature,
3358 size_t signature_length )
3359{
Janos Follath24eed8d2019-11-22 13:21:35 +00003360 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003361 mbedtls_mpi r, s;
3362 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3363 mbedtls_mpi_init( &r );
3364 mbedtls_mpi_init( &s );
3365
3366 if( signature_length != 2 * curve_bytes )
3367 return( PSA_ERROR_INVALID_SIGNATURE );
3368
3369 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3370 signature,
3371 curve_bytes ) );
3372 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3373 signature + curve_bytes,
3374 curve_bytes ) );
3375
3376 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3377 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003378
3379cleanup:
3380 mbedtls_mpi_free( &r );
3381 mbedtls_mpi_free( &s );
3382 return( mbedtls_to_psa_error( ret ) );
3383}
3384#endif /* MBEDTLS_ECDSA_C */
3385
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003386psa_status_t psa_sign_hash( psa_key_handle_t handle,
3387 psa_algorithm_t alg,
3388 const uint8_t *hash,
3389 size_t hash_length,
3390 uint8_t *signature,
3391 size_t signature_size,
3392 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003393{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003394 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003395 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003396#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3397 const psa_drv_se_t *drv;
3398 psa_drv_se_context_t *drv_context;
3399#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003400
3401 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003402 /* Immediately reject a zero-length signature buffer. This guarantees
3403 * that signature must be a valid pointer. (On the other hand, the hash
3404 * buffer can in principle be empty since it doesn't actually have
3405 * to be a hash.) */
3406 if( signature_size == 0 )
3407 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003408
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003409 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003410 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003411 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003412 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003413 {
3414 status = PSA_ERROR_INVALID_ARGUMENT;
3415 goto exit;
3416 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003417
Gilles Peskineedc64242019-08-07 21:05:07 +02003418#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3419 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3420 {
3421 if( drv->asymmetric == NULL ||
3422 drv->asymmetric->p_sign == NULL )
3423 {
3424 status = PSA_ERROR_NOT_SUPPORTED;
3425 goto exit;
3426 }
3427 status = drv->asymmetric->p_sign( drv_context,
3428 slot->data.se.slot_number,
3429 alg,
3430 hash, hash_length,
3431 signature, signature_size,
3432 signature_length );
3433 }
3434 else
3435#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003436#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003437 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003438 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003439 status = psa_rsa_sign( slot->data.rsa,
3440 alg,
3441 hash, hash_length,
3442 signature, signature_size,
3443 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003444 }
3445 else
3446#endif /* defined(MBEDTLS_RSA_C) */
3447#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003448 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003449 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003450#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003451 if(
3452#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3453 PSA_ALG_IS_ECDSA( alg )
3454#else
3455 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3456#endif
3457 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003458 status = psa_ecdsa_sign( slot->data.ecp,
3459 alg,
3460 hash, hash_length,
3461 signature, signature_size,
3462 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003463 else
3464#endif /* defined(MBEDTLS_ECDSA_C) */
3465 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003466 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003467 }
itayzafrir5c753392018-05-08 11:18:38 +03003468 }
3469 else
3470#endif /* defined(MBEDTLS_ECP_C) */
3471 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003472 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003473 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003474
3475exit:
3476 /* Fill the unused part of the output buffer (the whole buffer on error,
3477 * the trailing part on success) with something that isn't a valid mac
3478 * (barring an attack on the mac and deliberately-crafted input),
3479 * in case the caller doesn't check the return status properly. */
3480 if( status == PSA_SUCCESS )
3481 memset( signature + *signature_length, '!',
3482 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003483 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003484 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003485 /* If signature_size is 0 then we have nothing to do. We must not call
3486 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003487 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003488}
3489
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003490psa_status_t psa_verify_hash( psa_key_handle_t handle,
3491 psa_algorithm_t alg,
3492 const uint8_t *hash,
3493 size_t hash_length,
3494 const uint8_t *signature,
3495 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003496{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003497 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003498 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003499#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3500 const psa_drv_se_t *drv;
3501 psa_drv_se_context_t *drv_context;
3502#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003503
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003504 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003505 if( status != PSA_SUCCESS )
3506 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003507
Gilles Peskineedc64242019-08-07 21:05:07 +02003508#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3509 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3510 {
3511 if( drv->asymmetric == NULL ||
3512 drv->asymmetric->p_verify == NULL )
3513 return( PSA_ERROR_NOT_SUPPORTED );
3514 return( drv->asymmetric->p_verify( drv_context,
3515 slot->data.se.slot_number,
3516 alg,
3517 hash, hash_length,
3518 signature, signature_length ) );
3519 }
3520 else
3521#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003522#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003523 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003524 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003525 return( psa_rsa_verify( slot->data.rsa,
3526 alg,
3527 hash, hash_length,
3528 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003529 }
3530 else
3531#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003532#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003533 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003534 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003535#if defined(MBEDTLS_ECDSA_C)
3536 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003537 return( psa_ecdsa_verify( slot->data.ecp,
3538 hash, hash_length,
3539 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003540 else
3541#endif /* defined(MBEDTLS_ECDSA_C) */
3542 {
3543 return( PSA_ERROR_INVALID_ARGUMENT );
3544 }
itayzafrir5c753392018-05-08 11:18:38 +03003545 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003546 else
3547#endif /* defined(MBEDTLS_ECP_C) */
3548 {
3549 return( PSA_ERROR_NOT_SUPPORTED );
3550 }
3551}
3552
Gilles Peskine072ac562018-06-30 00:21:29 +02003553#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3554static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3555 mbedtls_rsa_context *rsa )
3556{
3557 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3558 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3559 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3560 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3561}
3562#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3563
Gilles Peskinec5487a82018-12-03 18:08:14 +01003564psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003565 psa_algorithm_t alg,
3566 const uint8_t *input,
3567 size_t input_length,
3568 const uint8_t *salt,
3569 size_t salt_length,
3570 uint8_t *output,
3571 size_t output_size,
3572 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003573{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003574 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003575 psa_status_t status;
3576
Darryl Green5cc689a2018-07-24 15:34:10 +01003577 (void) input;
3578 (void) input_length;
3579 (void) salt;
3580 (void) output;
3581 (void) output_size;
3582
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003583 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003585 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3586 return( PSA_ERROR_INVALID_ARGUMENT );
3587
Gilles Peskine28f8f302019-07-24 13:30:31 +02003588 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003589 if( status != PSA_SUCCESS )
3590 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003591 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3592 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594
3595#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003596 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003597 {
3598 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003599 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003600 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003601 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003603 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003605 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3606 mbedtls_ctr_drbg_random,
3607 &global_data.ctr_drbg,
3608 MBEDTLS_RSA_PUBLIC,
3609 input_length,
3610 input,
3611 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003612 }
3613 else
3614#endif /* MBEDTLS_PKCS1_V15 */
3615#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003616 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003618 psa_rsa_oaep_set_padding_mode( alg, rsa );
3619 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3620 mbedtls_ctr_drbg_random,
3621 &global_data.ctr_drbg,
3622 MBEDTLS_RSA_PUBLIC,
3623 salt, salt_length,
3624 input_length,
3625 input,
3626 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627 }
3628 else
3629#endif /* MBEDTLS_PKCS1_V21 */
3630 {
3631 return( PSA_ERROR_INVALID_ARGUMENT );
3632 }
3633 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003634 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003635 return( mbedtls_to_psa_error( ret ) );
3636 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003637 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003638#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003639 {
3640 return( PSA_ERROR_NOT_SUPPORTED );
3641 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003642}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643
Gilles Peskinec5487a82018-12-03 18:08:14 +01003644psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003645 psa_algorithm_t alg,
3646 const uint8_t *input,
3647 size_t input_length,
3648 const uint8_t *salt,
3649 size_t salt_length,
3650 uint8_t *output,
3651 size_t output_size,
3652 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003653{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003654 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003655 psa_status_t status;
3656
Darryl Green5cc689a2018-07-24 15:34:10 +01003657 (void) input;
3658 (void) input_length;
3659 (void) salt;
3660 (void) output;
3661 (void) output_size;
3662
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003663 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003664
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003665 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3666 return( PSA_ERROR_INVALID_ARGUMENT );
3667
Gilles Peskine28f8f302019-07-24 13:30:31 +02003668 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003669 if( status != PSA_SUCCESS )
3670 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003671 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003672 return( PSA_ERROR_INVALID_ARGUMENT );
3673
3674#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003675 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003676 {
3677 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003678 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679
Gilles Peskine630a18a2018-06-29 17:49:35 +02003680 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003681 return( PSA_ERROR_INVALID_ARGUMENT );
3682
3683#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003684 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003685 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003686 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3687 mbedtls_ctr_drbg_random,
3688 &global_data.ctr_drbg,
3689 MBEDTLS_RSA_PRIVATE,
3690 output_length,
3691 input,
3692 output,
3693 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003694 }
3695 else
3696#endif /* MBEDTLS_PKCS1_V15 */
3697#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003698 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003699 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003700 psa_rsa_oaep_set_padding_mode( alg, rsa );
3701 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3702 mbedtls_ctr_drbg_random,
3703 &global_data.ctr_drbg,
3704 MBEDTLS_RSA_PRIVATE,
3705 salt, salt_length,
3706 output_length,
3707 input,
3708 output,
3709 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003710 }
3711 else
3712#endif /* MBEDTLS_PKCS1_V21 */
3713 {
3714 return( PSA_ERROR_INVALID_ARGUMENT );
3715 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003716
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 return( mbedtls_to_psa_error( ret ) );
3718 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003719 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003720#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003721 {
3722 return( PSA_ERROR_NOT_SUPPORTED );
3723 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003724}
Gilles Peskine20035e32018-02-03 22:44:14 +01003725
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003726
3727
mohammad1603503973b2018-03-12 15:59:30 +02003728/****************************************************************/
3729/* Symmetric cryptography */
3730/****************************************************************/
3731
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003732/* Initialize the cipher operation structure. Once this function has been
3733 * called, psa_cipher_abort can run and will do the right thing. */
3734static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3735 psa_algorithm_t alg )
3736{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003737 if( ! PSA_ALG_IS_CIPHER( alg ) )
3738 {
3739 memset( operation, 0, sizeof( *operation ) );
3740 return( PSA_ERROR_INVALID_ARGUMENT );
3741 }
3742
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003743 operation->alg = alg;
3744 operation->key_set = 0;
3745 operation->iv_set = 0;
3746 operation->iv_required = 1;
3747 operation->iv_size = 0;
3748 operation->block_size = 0;
3749 mbedtls_cipher_init( &operation->ctx.cipher );
3750 return( PSA_SUCCESS );
3751}
3752
Gilles Peskinee553c652018-06-04 16:22:46 +02003753static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003754 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003755 psa_algorithm_t alg,
3756 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003757{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003758 int ret = 0;
3759 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003760 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003761 size_t key_bits;
3762 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003763 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3764 PSA_KEY_USAGE_ENCRYPT :
3765 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003766
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003767 /* A context must be freshly initialized before it can be set up. */
3768 if( operation->alg != 0 )
3769 {
3770 return( PSA_ERROR_BAD_STATE );
3771 }
3772
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003773 status = psa_cipher_init( operation, alg );
3774 if( status != PSA_SUCCESS )
3775 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003776
Gilles Peskine28f8f302019-07-24 13:30:31 +02003777 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003778 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003779 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003780 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003781
Gilles Peskine8e338702019-07-30 20:06:31 +02003782 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003783 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003784 {
3785 status = PSA_ERROR_NOT_SUPPORTED;
3786 goto exit;
3787 }
mohammad1603503973b2018-03-12 15:59:30 +02003788
mohammad1603503973b2018-03-12 15:59:30 +02003789 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003790 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003791 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003792
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003793#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003794 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003795 {
3796 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003797 uint8_t keys[24];
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003798 memcpy( keys, slot->data.key.data, 16 );
3799 memcpy( keys + 16, slot->data.key.data, 8 );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003800 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3801 keys,
3802 192, cipher_operation );
3803 }
3804 else
3805#endif
3806 {
3807 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02003808 slot->data.key.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003809 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003810 }
Moran Peker41deec42018-04-04 15:43:05 +03003811 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003812 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003813
mohammad16038481e742018-03-18 13:57:31 +02003814#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003815 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003816 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003817 case PSA_ALG_CBC_NO_PADDING:
3818 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3819 MBEDTLS_PADDING_NONE );
3820 break;
3821 case PSA_ALG_CBC_PKCS7:
3822 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3823 MBEDTLS_PADDING_PKCS7 );
3824 break;
3825 default:
3826 /* The algorithm doesn't involve padding. */
3827 ret = 0;
3828 break;
3829 }
3830 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003831 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003832#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3833
mohammad1603503973b2018-03-12 15:59:30 +02003834 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003835 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003836 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003837 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003838 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003839 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003840 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003841#if defined(MBEDTLS_CHACHA20_C)
3842 else
3843 if( alg == PSA_ALG_CHACHA20 )
3844 operation->iv_size = 12;
3845#endif
mohammad1603503973b2018-03-12 15:59:30 +02003846
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003847exit:
3848 if( status == 0 )
3849 status = mbedtls_to_psa_error( ret );
3850 if( status != 0 )
3851 psa_cipher_abort( operation );
3852 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003853}
3854
Gilles Peskinefe119512018-07-08 21:39:34 +02003855psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003856 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003857 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003858{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003859 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003860}
3861
Gilles Peskinefe119512018-07-08 21:39:34 +02003862psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003863 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003864 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003865{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003867}
3868
Gilles Peskinefe119512018-07-08 21:39:34 +02003869psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003870 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003871 size_t iv_size,
3872 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003873{
itayzafrir534bd7c2018-08-02 13:56:32 +03003874 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003875 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003876 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003877 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003878 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003879 }
Moran Peker41deec42018-04-04 15:43:05 +03003880 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003881 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003882 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003883 goto exit;
3884 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003885 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3886 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003887 if( ret != 0 )
3888 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003890 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003891 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003892
mohammad16038481e742018-03-18 13:57:31 +02003893 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003894 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003895
Moran Peker395db872018-05-31 14:07:14 +03003896exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003897 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003898 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003900}
3901
Gilles Peskinefe119512018-07-08 21:39:34 +02003902psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003903 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003904 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003905{
itayzafrir534bd7c2018-08-02 13:56:32 +03003906 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003907 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003908 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003909 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003910 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003911 }
Moran Pekera28258c2018-05-29 16:25:04 +03003912 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003913 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 status = PSA_ERROR_INVALID_ARGUMENT;
3915 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003916 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003917 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3918 status = mbedtls_to_psa_error( ret );
3919exit:
3920 if( status == PSA_SUCCESS )
3921 operation->iv_set = 1;
3922 else
mohammad1603503973b2018-03-12 15:59:30 +02003923 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003925}
3926
Gilles Peskinee553c652018-06-04 16:22:46 +02003927psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3928 const uint8_t *input,
3929 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003930 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003931 size_t output_size,
3932 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003933{
itayzafrir534bd7c2018-08-02 13:56:32 +03003934 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003935 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003936 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003937
3938 if( operation->alg == 0 )
3939 {
3940 return( PSA_ERROR_BAD_STATE );
3941 }
3942
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003943 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003944 {
3945 /* Take the unprocessed partial block left over from previous
3946 * update calls, if any, plus the input to this call. Remove
3947 * the last partial block, if any. You get the data that will be
3948 * output in this call. */
3949 expected_output_size =
3950 ( operation->ctx.cipher.unprocessed_len + input_length )
3951 / operation->block_size * operation->block_size;
3952 }
3953 else
3954 {
3955 expected_output_size = input_length;
3956 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003957
Gilles Peskine89d789c2018-06-04 17:17:16 +02003958 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003959 {
3960 status = PSA_ERROR_BUFFER_TOO_SMALL;
3961 goto exit;
3962 }
mohammad160382759612018-03-12 18:16:40 +02003963
mohammad1603503973b2018-03-12 15:59:30 +02003964 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003965 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003966 status = mbedtls_to_psa_error( ret );
3967exit:
3968 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003969 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003970 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003971}
3972
Gilles Peskinee553c652018-06-04 16:22:46 +02003973psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3974 uint8_t *output,
3975 size_t output_size,
3976 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003977{
David Saadab4ecc272019-02-14 13:48:10 +02003978 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003979 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003980 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003981
mohammad1603503973b2018-03-12 15:59:30 +02003982 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003983 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003984 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003985 }
3986 if( operation->iv_required && ! operation->iv_set )
3987 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003988 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003989 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003990
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003991 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003992 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3993 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003994 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003995 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003996 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003997 }
3998
Janos Follath315b51c2018-07-09 16:04:51 +01003999 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4000 temp_output_buffer,
4001 output_length );
4002 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004003 {
Janos Follath315b51c2018-07-09 16:04:51 +01004004 status = mbedtls_to_psa_error( cipher_ret );
4005 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004006 }
Janos Follath315b51c2018-07-09 16:04:51 +01004007
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004008 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004009 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004010 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004011 memcpy( output, temp_output_buffer, *output_length );
4012 else
4013 {
Janos Follath315b51c2018-07-09 16:04:51 +01004014 status = PSA_ERROR_BUFFER_TOO_SMALL;
4015 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004016 }
mohammad1603503973b2018-03-12 15:59:30 +02004017
Gilles Peskine3f108122018-12-07 18:14:53 +01004018 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004019 status = psa_cipher_abort( operation );
4020
4021 return( status );
4022
4023error:
4024
4025 *output_length = 0;
4026
Gilles Peskine3f108122018-12-07 18:14:53 +01004027 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004028 (void) psa_cipher_abort( operation );
4029
4030 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004031}
4032
Gilles Peskinee553c652018-06-04 16:22:46 +02004033psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4034{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004035 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004036 {
4037 /* The object has (apparently) been initialized but it is not
4038 * in use. It's ok to call abort on such an object, and there's
4039 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004040 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004041 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004042
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004043 /* Sanity check (shouldn't happen: operation->alg should
4044 * always have been initialized to a valid value). */
4045 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4046 return( PSA_ERROR_BAD_STATE );
4047
mohammad1603503973b2018-03-12 15:59:30 +02004048 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004049
Moran Peker41deec42018-04-04 15:43:05 +03004050 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004051 operation->key_set = 0;
4052 operation->iv_set = 0;
4053 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004054 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004055 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004056
Moran Peker395db872018-05-31 14:07:14 +03004057 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004058}
4059
Gilles Peskinea0655c32018-04-30 17:06:50 +02004060
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004061
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004062
mohammad16035955c982018-04-26 00:53:03 +03004063/****************************************************************/
4064/* AEAD */
4065/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004066
Gilles Peskineedf9a652018-08-17 18:11:56 +02004067typedef struct
4068{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004069 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004070 const mbedtls_cipher_info_t *cipher_info;
4071 union
4072 {
4073#if defined(MBEDTLS_CCM_C)
4074 mbedtls_ccm_context ccm;
4075#endif /* MBEDTLS_CCM_C */
4076#if defined(MBEDTLS_GCM_C)
4077 mbedtls_gcm_context gcm;
4078#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004079#if defined(MBEDTLS_CHACHAPOLY_C)
4080 mbedtls_chachapoly_context chachapoly;
4081#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004082 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004083 psa_algorithm_t core_alg;
4084 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004085 uint8_t tag_length;
4086} aead_operation_t;
4087
Gilles Peskine30a9e412019-01-14 18:36:12 +01004088static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004090 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004091 {
4092#if defined(MBEDTLS_CCM_C)
4093 case PSA_ALG_CCM:
4094 mbedtls_ccm_free( &operation->ctx.ccm );
4095 break;
4096#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004097#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004098 case PSA_ALG_GCM:
4099 mbedtls_gcm_free( &operation->ctx.gcm );
4100 break;
4101#endif /* MBEDTLS_GCM_C */
4102 }
4103}
4104
4105static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004106 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004107 psa_key_usage_t usage,
4108 psa_algorithm_t alg )
4109{
4110 psa_status_t status;
4111 size_t key_bits;
4112 mbedtls_cipher_id_t cipher_id;
4113
Gilles Peskine28f8f302019-07-24 13:30:31 +02004114 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004115 if( status != PSA_SUCCESS )
4116 return( status );
4117
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004118 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004119
4120 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004121 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 &cipher_id );
4123 if( operation->cipher_info == NULL )
4124 return( PSA_ERROR_NOT_SUPPORTED );
4125
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004126 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004127 {
4128#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004129 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4130 operation->core_alg = PSA_ALG_CCM;
4131 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004132 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4133 * The call to mbedtls_ccm_encrypt_and_tag or
4134 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004135 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004136 return( PSA_ERROR_INVALID_ARGUMENT );
4137 mbedtls_ccm_init( &operation->ctx.ccm );
4138 status = mbedtls_to_psa_error(
4139 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004140 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004141 (unsigned int) key_bits ) );
4142 if( status != 0 )
4143 goto cleanup;
4144 break;
4145#endif /* MBEDTLS_CCM_C */
4146
4147#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004148 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4149 operation->core_alg = PSA_ALG_GCM;
4150 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004151 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4152 * The call to mbedtls_gcm_crypt_and_tag or
4153 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004154 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004155 return( PSA_ERROR_INVALID_ARGUMENT );
4156 mbedtls_gcm_init( &operation->ctx.gcm );
4157 status = mbedtls_to_psa_error(
4158 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004159 operation->slot->data.key.data,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004160 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004161 if( status != 0 )
4162 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004163 break;
4164#endif /* MBEDTLS_GCM_C */
4165
Gilles Peskine26869f22019-05-06 15:25:00 +02004166#if defined(MBEDTLS_CHACHAPOLY_C)
4167 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4168 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4169 operation->full_tag_length = 16;
4170 /* We only support the default tag length. */
4171 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4172 return( PSA_ERROR_NOT_SUPPORTED );
4173 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4174 status = mbedtls_to_psa_error(
4175 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02004176 operation->slot->data.key.data ) );
Gilles Peskine26869f22019-05-06 15:25:00 +02004177 if( status != 0 )
4178 goto cleanup;
4179 break;
4180#endif /* MBEDTLS_CHACHAPOLY_C */
4181
Gilles Peskineedf9a652018-08-17 18:11:56 +02004182 default:
4183 return( PSA_ERROR_NOT_SUPPORTED );
4184 }
4185
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004186 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4187 {
4188 status = PSA_ERROR_INVALID_ARGUMENT;
4189 goto cleanup;
4190 }
4191 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004192
Gilles Peskineedf9a652018-08-17 18:11:56 +02004193 return( PSA_SUCCESS );
4194
4195cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004196 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004197 return( status );
4198}
4199
Gilles Peskinec5487a82018-12-03 18:08:14 +01004200psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004201 psa_algorithm_t alg,
4202 const uint8_t *nonce,
4203 size_t nonce_length,
4204 const uint8_t *additional_data,
4205 size_t additional_data_length,
4206 const uint8_t *plaintext,
4207 size_t plaintext_length,
4208 uint8_t *ciphertext,
4209 size_t ciphertext_size,
4210 size_t *ciphertext_length )
4211{
mohammad16035955c982018-04-26 00:53:03 +03004212 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004213 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004214 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004215
mohammad1603f08a5502018-06-03 15:05:47 +03004216 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004217
Gilles Peskinec5487a82018-12-03 18:08:14 +01004218 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004219 if( status != PSA_SUCCESS )
4220 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004221
Gilles Peskineedf9a652018-08-17 18:11:56 +02004222 /* For all currently supported modes, the tag is at the end of the
4223 * ciphertext. */
4224 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4225 {
4226 status = PSA_ERROR_BUFFER_TOO_SMALL;
4227 goto exit;
4228 }
4229 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004230
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004231#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004232 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004233 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004234 status = mbedtls_to_psa_error(
4235 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4236 MBEDTLS_GCM_ENCRYPT,
4237 plaintext_length,
4238 nonce, nonce_length,
4239 additional_data, additional_data_length,
4240 plaintext, ciphertext,
4241 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004242 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004243 else
4244#endif /* MBEDTLS_GCM_C */
4245#if defined(MBEDTLS_CCM_C)
4246 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004247 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004248 status = mbedtls_to_psa_error(
4249 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4250 plaintext_length,
4251 nonce, nonce_length,
4252 additional_data,
4253 additional_data_length,
4254 plaintext, ciphertext,
4255 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004256 }
mohammad16035c8845f2018-05-09 05:40:09 -07004257 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004258#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004259#if defined(MBEDTLS_CHACHAPOLY_C)
4260 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4261 {
4262 if( nonce_length != 12 || operation.tag_length != 16 )
4263 {
4264 status = PSA_ERROR_NOT_SUPPORTED;
4265 goto exit;
4266 }
4267 status = mbedtls_to_psa_error(
4268 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4269 plaintext_length,
4270 nonce,
4271 additional_data,
4272 additional_data_length,
4273 plaintext,
4274 ciphertext,
4275 tag ) );
4276 }
4277 else
4278#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004279 {
mohammad1603554faad2018-06-03 15:07:38 +03004280 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004281 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004282
Gilles Peskineedf9a652018-08-17 18:11:56 +02004283 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4284 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004285
Gilles Peskineedf9a652018-08-17 18:11:56 +02004286exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004287 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004288 if( status == PSA_SUCCESS )
4289 *ciphertext_length = plaintext_length + operation.tag_length;
4290 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004291}
4292
Gilles Peskineee652a32018-06-01 19:23:52 +02004293/* Locate the tag in a ciphertext buffer containing the encrypted data
4294 * followed by the tag. Return the length of the part preceding the tag in
4295 * *plaintext_length. This is the size of the plaintext in modes where
4296 * the encrypted data has the same size as the plaintext, such as
4297 * CCM and GCM. */
4298static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4299 const uint8_t *ciphertext,
4300 size_t ciphertext_length,
4301 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004302 const uint8_t **p_tag )
4303{
4304 size_t payload_length;
4305 if( tag_length > ciphertext_length )
4306 return( PSA_ERROR_INVALID_ARGUMENT );
4307 payload_length = ciphertext_length - tag_length;
4308 if( payload_length > plaintext_size )
4309 return( PSA_ERROR_BUFFER_TOO_SMALL );
4310 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004311 return( PSA_SUCCESS );
4312}
4313
Gilles Peskinec5487a82018-12-03 18:08:14 +01004314psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004315 psa_algorithm_t alg,
4316 const uint8_t *nonce,
4317 size_t nonce_length,
4318 const uint8_t *additional_data,
4319 size_t additional_data_length,
4320 const uint8_t *ciphertext,
4321 size_t ciphertext_length,
4322 uint8_t *plaintext,
4323 size_t plaintext_size,
4324 size_t *plaintext_length )
4325{
mohammad16035955c982018-04-26 00:53:03 +03004326 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004327 aead_operation_t operation;
4328 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004329
Gilles Peskineee652a32018-06-01 19:23:52 +02004330 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004331
Gilles Peskinec5487a82018-12-03 18:08:14 +01004332 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004333 if( status != PSA_SUCCESS )
4334 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004335
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004336 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4337 ciphertext, ciphertext_length,
4338 plaintext_size, &tag );
4339 if( status != PSA_SUCCESS )
4340 goto exit;
4341
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004342#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004343 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004344 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004345 status = mbedtls_to_psa_error(
4346 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4347 ciphertext_length - operation.tag_length,
4348 nonce, nonce_length,
4349 additional_data,
4350 additional_data_length,
4351 tag, operation.tag_length,
4352 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004353 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004354 else
4355#endif /* MBEDTLS_GCM_C */
4356#if defined(MBEDTLS_CCM_C)
4357 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004358 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004359 status = mbedtls_to_psa_error(
4360 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4361 ciphertext_length - operation.tag_length,
4362 nonce, nonce_length,
4363 additional_data,
4364 additional_data_length,
4365 ciphertext, plaintext,
4366 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004367 }
mohammad160339574652018-06-01 04:39:53 -07004368 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004369#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004370#if defined(MBEDTLS_CHACHAPOLY_C)
4371 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4372 {
4373 if( nonce_length != 12 || operation.tag_length != 16 )
4374 {
4375 status = PSA_ERROR_NOT_SUPPORTED;
4376 goto exit;
4377 }
4378 status = mbedtls_to_psa_error(
4379 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4380 ciphertext_length - operation.tag_length,
4381 nonce,
4382 additional_data,
4383 additional_data_length,
4384 tag,
4385 ciphertext,
4386 plaintext ) );
4387 }
4388 else
4389#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004390 {
mohammad1603554faad2018-06-03 15:07:38 +03004391 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004392 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004393
Gilles Peskineedf9a652018-08-17 18:11:56 +02004394 if( status != PSA_SUCCESS && plaintext_size != 0 )
4395 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004396
Gilles Peskineedf9a652018-08-17 18:11:56 +02004397exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004398 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004399 if( status == PSA_SUCCESS )
4400 *plaintext_length = ciphertext_length - operation.tag_length;
4401 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004402}
4403
Gilles Peskinea0655c32018-04-30 17:06:50 +02004404
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004405
Gilles Peskine20035e32018-02-03 22:44:14 +01004406/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004407/* Generators */
4408/****************************************************************/
4409
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004410#define HKDF_STATE_INIT 0 /* no input yet */
4411#define HKDF_STATE_STARTED 1 /* got salt */
4412#define HKDF_STATE_KEYED 2 /* got key */
4413#define HKDF_STATE_OUTPUT 3 /* output started */
4414
Gilles Peskinecbe66502019-05-16 16:59:18 +02004415static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004416 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004417{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4419 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004420 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004421 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004422}
4423
4424
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004426{
4427 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004429 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004430 {
4431 /* The object has (apparently) been initialized but it is not
4432 * in use. It's ok to call abort on such an object, and there's
4433 * nothing to do. */
4434 }
4435 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004436#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004437 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004438 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004439 mbedtls_free( operation->ctx.hkdf.info );
4440 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004441 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004442 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004443 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004444 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004445 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004446 if( operation->ctx.tls12_prf.seed != NULL )
4447 {
4448 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4449 operation->ctx.tls12_prf.seed_length );
4450 mbedtls_free( operation->ctx.tls12_prf.seed );
4451 }
4452
4453 if( operation->ctx.tls12_prf.label != NULL )
4454 {
4455 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4456 operation->ctx.tls12_prf.label_length );
4457 mbedtls_free( operation->ctx.tls12_prf.label );
4458 }
4459
4460 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4461
4462 /* We leave the fields Ai and output_block to be erased safely by the
4463 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004464 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004465 else
4466#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004467 {
4468 status = PSA_ERROR_BAD_STATE;
4469 }
Janos Follath083036a2019-06-11 10:22:26 +01004470 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004471 return( status );
4472}
4473
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004474psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004475 size_t *capacity)
4476{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004478 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004479 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004480 return PSA_ERROR_BAD_STATE;
4481 }
4482
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004483 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004484 return( PSA_SUCCESS );
4485}
4486
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004487psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004488 size_t capacity )
4489{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004491 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004492 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004493 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004495 return( PSA_SUCCESS );
4496}
4497
Gilles Peskinebef7f142018-07-12 17:22:21 +02004498#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004500 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004501static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004502 psa_algorithm_t hash_alg,
4503 uint8_t *output,
4504 size_t output_length )
4505{
4506 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4507 psa_status_t status;
4508
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004509 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4510 return( PSA_ERROR_BAD_STATE );
4511 hkdf->state = HKDF_STATE_OUTPUT;
4512
Gilles Peskinebef7f142018-07-12 17:22:21 +02004513 while( output_length != 0 )
4514 {
4515 /* Copy what remains of the current block */
4516 uint8_t n = hash_length - hkdf->offset_in_block;
4517 if( n > output_length )
4518 n = (uint8_t) output_length;
4519 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4520 output += n;
4521 output_length -= n;
4522 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004523 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004524 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004525 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004526 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004527 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004528 * object was corrupted or if this function is called directly
4529 * inside the library. */
4530 if( hkdf->block_number == 0xff )
4531 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004532
4533 /* We need a new block */
4534 ++hkdf->block_number;
4535 hkdf->offset_in_block = 0;
4536 status = psa_hmac_setup_internal( &hkdf->hmac,
4537 hkdf->prk, hash_length,
4538 hash_alg );
4539 if( status != PSA_SUCCESS )
4540 return( status );
4541 if( hkdf->block_number != 1 )
4542 {
4543 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4544 hkdf->output_block,
4545 hash_length );
4546 if( status != PSA_SUCCESS )
4547 return( status );
4548 }
4549 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4550 hkdf->info,
4551 hkdf->info_length );
4552 if( status != PSA_SUCCESS )
4553 return( status );
4554 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4555 &hkdf->block_number, 1 );
4556 if( status != PSA_SUCCESS )
4557 return( status );
4558 status = psa_hmac_finish_internal( &hkdf->hmac,
4559 hkdf->output_block,
4560 sizeof( hkdf->output_block ) );
4561 if( status != PSA_SUCCESS )
4562 return( status );
4563 }
4564
4565 return( PSA_SUCCESS );
4566}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004567
Janos Follath7742fee2019-06-17 12:58:10 +01004568static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4569 psa_tls12_prf_key_derivation_t *tls12_prf,
4570 psa_algorithm_t alg )
4571{
4572 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4573 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004574 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4575 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004576
Janos Follath7742fee2019-06-17 12:58:10 +01004577 /* We can't be wanting more output after block 0xff, otherwise
4578 * the capacity check in psa_key_derivation_output_bytes() would have
4579 * prevented this call. It could happen only if the operation
4580 * object was corrupted or if this function is called directly
4581 * inside the library. */
4582 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004583 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004584
4585 /* We need a new block */
4586 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004587 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004588
4589 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4590 *
4591 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4592 *
4593 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4594 * HMAC_hash(secret, A(2) + seed) +
4595 * HMAC_hash(secret, A(3) + seed) + ...
4596 *
4597 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004598 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004599 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004600 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004601 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004602 * is currently extracted as `output_block` and where i is
4603 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004604 */
4605
Janos Follathea29bfb2019-06-19 12:21:20 +01004606 /* Save the hash context before using it, to preserve the hash state with
4607 * only the inner padding in it. We need this, because inner padding depends
4608 * on the key (secret in the RFC's terminology). */
4609 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4610 if( status != PSA_SUCCESS )
4611 goto cleanup;
4612
4613 /* Calculate A(i) where i = tls12_prf->block_number. */
4614 if( tls12_prf->block_number == 1 )
4615 {
4616 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4617 * the variable seed and in this instance means it in the context of the
4618 * P_hash function, where seed = label + seed.) */
4619 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4620 tls12_prf->label, tls12_prf->label_length );
4621 if( status != PSA_SUCCESS )
4622 goto cleanup;
4623 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4624 tls12_prf->seed, tls12_prf->seed_length );
4625 if( status != PSA_SUCCESS )
4626 goto cleanup;
4627 }
4628 else
4629 {
4630 /* A(i) = HMAC_hash(secret, A(i-1)) */
4631 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4632 tls12_prf->Ai, hash_length );
4633 if( status != PSA_SUCCESS )
4634 goto cleanup;
4635 }
4636
4637 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4638 tls12_prf->Ai, hash_length );
4639 if( status != PSA_SUCCESS )
4640 goto cleanup;
4641 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4642 if( status != PSA_SUCCESS )
4643 goto cleanup;
4644
4645 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4646 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4647 tls12_prf->Ai, hash_length );
4648 if( status != PSA_SUCCESS )
4649 goto cleanup;
4650 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4651 tls12_prf->label, tls12_prf->label_length );
4652 if( status != PSA_SUCCESS )
4653 goto cleanup;
4654 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4655 tls12_prf->seed, tls12_prf->seed_length );
4656 if( status != PSA_SUCCESS )
4657 goto cleanup;
4658 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4659 tls12_prf->output_block, hash_length );
4660 if( status != PSA_SUCCESS )
4661 goto cleanup;
4662 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4663 if( status != PSA_SUCCESS )
4664 goto cleanup;
4665
Janos Follath7742fee2019-06-17 12:58:10 +01004666
4667cleanup:
4668
Janos Follathea29bfb2019-06-19 12:21:20 +01004669 cleanup_status = psa_hash_abort( &backup );
4670 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4671 status = cleanup_status;
4672
4673 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004674}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004675
Janos Follath844eb0e2019-06-19 12:10:49 +01004676static psa_status_t psa_key_derivation_tls12_prf_read(
4677 psa_tls12_prf_key_derivation_t *tls12_prf,
4678 psa_algorithm_t alg,
4679 uint8_t *output,
4680 size_t output_length )
4681{
4682 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4683 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4684 psa_status_t status;
4685 uint8_t offset, length;
4686
4687 while( output_length != 0 )
4688 {
4689 /* Check if we have fully processed the current block. */
4690 if( tls12_prf->left_in_block == 0 )
4691 {
4692 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4693 alg );
4694 if( status != PSA_SUCCESS )
4695 return( status );
4696
4697 continue;
4698 }
4699
4700 if( tls12_prf->left_in_block > output_length )
4701 length = (uint8_t) output_length;
4702 else
4703 length = tls12_prf->left_in_block;
4704
4705 offset = hash_length - tls12_prf->left_in_block;
4706 memcpy( output, tls12_prf->output_block + offset, length );
4707 output += length;
4708 output_length -= length;
4709 tls12_prf->left_in_block -= length;
4710 }
4711
4712 return( PSA_SUCCESS );
4713}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004714#endif /* MBEDTLS_MD_C */
4715
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004716psa_status_t psa_key_derivation_output_bytes(
4717 psa_key_derivation_operation_t *operation,
4718 uint8_t *output,
4719 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004720{
4721 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004725 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004726 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004727 return PSA_ERROR_BAD_STATE;
4728 }
4729
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004731 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004732 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004733 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004735 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004736 goto exit;
4737 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004740 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004741 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004742 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4743 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004745 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004746 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004747 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004748 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749
Gilles Peskinebef7f142018-07-12 17:22:21 +02004750#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004751 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004752 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004753 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004754 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755 output, output_length );
4756 }
Janos Follathadbec812019-06-14 11:05:39 +01004757 else
Janos Follathadbec812019-06-14 11:05:39 +01004758 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004759 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004760 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004762 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004763 output_length );
4764 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004765 else
4766#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004767 {
4768 return( PSA_ERROR_BAD_STATE );
4769 }
4770
4771exit:
4772 if( status != PSA_SUCCESS )
4773 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004774 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004775 * This allows us to differentiate between exhausted operations and
4776 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4777 * operations. */
4778 psa_algorithm_t alg = operation->alg;
4779 psa_key_derivation_abort( operation );
4780 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004781 memset( output, '!', output_length );
4782 }
4783 return( status );
4784}
4785
Gilles Peskine08542d82018-07-19 17:05:42 +02004786#if defined(MBEDTLS_DES_C)
4787static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4788{
4789 if( data_size >= 8 )
4790 mbedtls_des_key_set_parity( data );
4791 if( data_size >= 16 )
4792 mbedtls_des_key_set_parity( data + 8 );
4793 if( data_size >= 24 )
4794 mbedtls_des_key_set_parity( data + 16 );
4795}
4796#endif /* MBEDTLS_DES_C */
4797
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004798static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004799 psa_key_slot_t *slot,
4800 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004801 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004802{
4803 uint8_t *data = NULL;
4804 size_t bytes = PSA_BITS_TO_BYTES( bits );
4805 psa_status_t status;
4806
Gilles Peskine8e338702019-07-30 20:06:31 +02004807 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004808 return( PSA_ERROR_INVALID_ARGUMENT );
4809 if( bits % 8 != 0 )
4810 return( PSA_ERROR_INVALID_ARGUMENT );
4811 data = mbedtls_calloc( 1, bytes );
4812 if( data == NULL )
4813 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4814
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004815 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004816 if( status != PSA_SUCCESS )
4817 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004818#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004819 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004820 psa_des_set_key_parity( data, bytes );
4821#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004823
4824exit:
4825 mbedtls_free( data );
4826 return( status );
4827}
4828
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004829psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004830 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004831 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004832{
4833 psa_status_t status;
4834 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004835 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004836
4837 /* Reject any attempt to create a zero-length key so that we don't
4838 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4839 if( psa_get_key_bits( attributes ) == 0 )
4840 return( PSA_ERROR_INVALID_ARGUMENT );
4841
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004842 if( ! operation->can_output_key )
4843 return( PSA_ERROR_NOT_PERMITTED );
4844
Gilles Peskinedf179142019-07-15 22:02:14 +02004845 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4846 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004847#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4848 if( driver != NULL )
4849 {
4850 /* Deriving a key in a secure element is not implemented yet. */
4851 status = PSA_ERROR_NOT_SUPPORTED;
4852 }
4853#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004854 if( status == PSA_SUCCESS )
4855 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004856 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004857 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004858 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004859 }
4860 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004861 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004862 if( status != PSA_SUCCESS )
4863 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004864 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004865 *handle = 0;
4866 }
4867 return( status );
4868}
4869
Gilles Peskineeab56e42018-07-12 17:12:33 +02004870
4871
4872/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004873/* Key derivation */
4874/****************************************************************/
4875
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004877 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004878 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879{
Janos Follath5fe19732019-06-20 15:09:30 +01004880 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4881 * initialisers for this union leave some bytes unspecified.) */
4882 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4883
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004885#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004886 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4887 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4888 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004889 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004890 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004891 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4892 if( hash_size == 0 )
4893 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4895 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004896 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004897 {
4898 return( PSA_ERROR_NOT_SUPPORTED );
4899 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004900 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004902 }
4903#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004905 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004906}
4907
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004908psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 psa_algorithm_t alg )
4910{
4911 psa_status_t status;
4912
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004913 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004914 return( PSA_ERROR_BAD_STATE );
4915
Gilles Peskine6843c292019-01-18 16:44:49 +01004916 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4917 return( PSA_ERROR_INVALID_ARGUMENT );
4918 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919 {
4920 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 }
4923 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4924 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004925 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927 else
4928 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929
4930 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004931 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004932 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004933}
4934
4935#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004936static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937 psa_algorithm_t hash_alg,
4938 psa_key_derivation_step_t step,
4939 const uint8_t *data,
4940 size_t data_length )
4941{
4942 psa_status_t status;
4943 switch( step )
4944 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004945 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004946 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004947 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004948 status = psa_hmac_setup_internal( &hkdf->hmac,
4949 data, data_length,
4950 hash_alg );
4951 if( status != PSA_SUCCESS )
4952 return( status );
4953 hkdf->state = HKDF_STATE_STARTED;
4954 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004955 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004956 /* If no salt was provided, use an empty salt. */
4957 if( hkdf->state == HKDF_STATE_INIT )
4958 {
4959 status = psa_hmac_setup_internal( &hkdf->hmac,
4960 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004961 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004962 if( status != PSA_SUCCESS )
4963 return( status );
4964 hkdf->state = HKDF_STATE_STARTED;
4965 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004966 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004967 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004968 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4969 data, data_length );
4970 if( status != PSA_SUCCESS )
4971 return( status );
4972 status = psa_hmac_finish_internal( &hkdf->hmac,
4973 hkdf->prk,
4974 sizeof( hkdf->prk ) );
4975 if( status != PSA_SUCCESS )
4976 return( status );
4977 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4978 hkdf->block_number = 0;
4979 hkdf->state = HKDF_STATE_KEYED;
4980 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004981 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004982 if( hkdf->state == HKDF_STATE_OUTPUT )
4983 return( PSA_ERROR_BAD_STATE );
4984 if( hkdf->info_set )
4985 return( PSA_ERROR_BAD_STATE );
4986 hkdf->info_length = data_length;
4987 if( data_length != 0 )
4988 {
4989 hkdf->info = mbedtls_calloc( 1, data_length );
4990 if( hkdf->info == NULL )
4991 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4992 memcpy( hkdf->info, data, data_length );
4993 }
4994 hkdf->info_set = 1;
4995 return( PSA_SUCCESS );
4996 default:
4997 return( PSA_ERROR_INVALID_ARGUMENT );
4998 }
4999}
Janos Follathb03233e2019-06-11 15:30:30 +01005000
Janos Follathf08e2652019-06-13 09:05:41 +01005001static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5002 const uint8_t *data,
5003 size_t data_length )
5004{
5005 if( prf->state != TLS12_PRF_STATE_INIT )
5006 return( PSA_ERROR_BAD_STATE );
5007
Janos Follathd6dce9f2019-07-04 09:11:38 +01005008 if( data_length != 0 )
5009 {
5010 prf->seed = mbedtls_calloc( 1, data_length );
5011 if( prf->seed == NULL )
5012 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005013
Janos Follathd6dce9f2019-07-04 09:11:38 +01005014 memcpy( prf->seed, data, data_length );
5015 prf->seed_length = data_length;
5016 }
Janos Follathf08e2652019-06-13 09:05:41 +01005017
5018 prf->state = TLS12_PRF_STATE_SEED_SET;
5019
5020 return( PSA_SUCCESS );
5021}
5022
Janos Follath81550542019-06-13 14:26:34 +01005023static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5024 psa_algorithm_t hash_alg,
5025 const uint8_t *data,
5026 size_t data_length )
5027{
5028 psa_status_t status;
5029 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5030 return( PSA_ERROR_BAD_STATE );
5031
5032 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5033 if( status != PSA_SUCCESS )
5034 return( status );
5035
5036 prf->state = TLS12_PRF_STATE_KEY_SET;
5037
5038 return( PSA_SUCCESS );
5039}
5040
Janos Follath6660f0e2019-06-17 08:44:03 +01005041static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5042 psa_tls12_prf_key_derivation_t *prf,
5043 psa_algorithm_t hash_alg,
5044 const uint8_t *data,
5045 size_t data_length )
5046{
5047 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005048 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5049 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005050
5051 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5052 return( PSA_ERROR_INVALID_ARGUMENT );
5053
5054 /* Quoting RFC 4279, Section 2:
5055 *
5056 * The premaster secret is formed as follows: if the PSK is N octets
5057 * long, concatenate a uint16 with the value N, N zero octets, a second
5058 * uint16 with the value N, and the PSK itself.
5059 */
5060
Janos Follath40e13932019-06-26 13:22:29 +01005061 *cur++ = ( data_length >> 8 ) & 0xff;
5062 *cur++ = ( data_length >> 0 ) & 0xff;
5063 memset( cur, 0, data_length );
5064 cur += data_length;
5065 *cur++ = pms[0];
5066 *cur++ = pms[1];
5067 memcpy( cur, data, data_length );
5068 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005069
Janos Follath40e13932019-06-26 13:22:29 +01005070 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005071
5072 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5073 return( status );
5074}
5075
Janos Follath63028dd2019-06-13 09:15:47 +01005076static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5077 const uint8_t *data,
5078 size_t data_length )
5079{
5080 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5081 return( PSA_ERROR_BAD_STATE );
5082
Janos Follathd6dce9f2019-07-04 09:11:38 +01005083 if( data_length != 0 )
5084 {
5085 prf->label = mbedtls_calloc( 1, data_length );
5086 if( prf->label == NULL )
5087 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005088
Janos Follathd6dce9f2019-07-04 09:11:38 +01005089 memcpy( prf->label, data, data_length );
5090 prf->label_length = data_length;
5091 }
Janos Follath63028dd2019-06-13 09:15:47 +01005092
5093 prf->state = TLS12_PRF_STATE_LABEL_SET;
5094
5095 return( PSA_SUCCESS );
5096}
5097
Janos Follathb03233e2019-06-11 15:30:30 +01005098static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5099 psa_algorithm_t hash_alg,
5100 psa_key_derivation_step_t step,
5101 const uint8_t *data,
5102 size_t data_length )
5103{
Janos Follathb03233e2019-06-11 15:30:30 +01005104 switch( step )
5105 {
Janos Follathf08e2652019-06-13 09:05:41 +01005106 case PSA_KEY_DERIVATION_INPUT_SEED:
5107 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005108 case PSA_KEY_DERIVATION_INPUT_SECRET:
5109 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005110 case PSA_KEY_DERIVATION_INPUT_LABEL:
5111 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005112 default:
5113 return( PSA_ERROR_INVALID_ARGUMENT );
5114 }
5115}
Janos Follath6660f0e2019-06-17 08:44:03 +01005116
5117static psa_status_t psa_tls12_prf_psk_to_ms_input(
5118 psa_tls12_prf_key_derivation_t *prf,
5119 psa_algorithm_t hash_alg,
5120 psa_key_derivation_step_t step,
5121 const uint8_t *data,
5122 size_t data_length )
5123{
5124 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005125 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005126 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5127 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005128 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005129
5130 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5131}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005132#endif /* MBEDTLS_MD_C */
5133
Gilles Peskineb8965192019-09-24 16:21:10 +02005134/** Check whether the given key type is acceptable for the given
5135 * input step of a key derivation.
5136 *
5137 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5138 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5139 * Both secret and non-secret inputs can alternatively have the type
5140 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5141 * that the input was passed as a buffer rather than via a key object.
5142 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005143static int psa_key_derivation_check_input_type(
5144 psa_key_derivation_step_t step,
5145 psa_key_type_t key_type )
5146{
5147 switch( step )
5148 {
5149 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005150 if( key_type == PSA_KEY_TYPE_DERIVE )
5151 return( PSA_SUCCESS );
5152 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005153 return( PSA_SUCCESS );
5154 break;
5155 case PSA_KEY_DERIVATION_INPUT_LABEL:
5156 case PSA_KEY_DERIVATION_INPUT_SALT:
5157 case PSA_KEY_DERIVATION_INPUT_INFO:
5158 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005159 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5160 return( PSA_SUCCESS );
5161 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005162 return( PSA_SUCCESS );
5163 break;
5164 }
5165 return( PSA_ERROR_INVALID_ARGUMENT );
5166}
5167
Janos Follathb80a94e2019-06-12 15:54:46 +01005168static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005169 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005170 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005171 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005172 const uint8_t *data,
5173 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005175 psa_status_t status;
5176 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5177
5178 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005179 if( status != PSA_SUCCESS )
5180 goto exit;
5181
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005182#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005183 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005184 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005185 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005186 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005187 step, data, data_length );
5188 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005189 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 {
Janos Follathb03233e2019-06-11 15:30:30 +01005191 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5192 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5193 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005194 }
5195 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5196 {
5197 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5198 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5199 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005200 }
5201 else
5202#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005203 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005204 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005205 return( PSA_ERROR_BAD_STATE );
5206 }
5207
Gilles Peskine224b0d62019-09-23 18:13:17 +02005208exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005209 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005210 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005211 return( status );
5212}
5213
Janos Follath51f4a0f2019-06-14 11:35:55 +01005214psa_status_t psa_key_derivation_input_bytes(
5215 psa_key_derivation_operation_t *operation,
5216 psa_key_derivation_step_t step,
5217 const uint8_t *data,
5218 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005219{
Gilles Peskineb8965192019-09-24 16:21:10 +02005220 return( psa_key_derivation_input_internal( operation, step,
5221 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005222 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005223}
5224
Janos Follath51f4a0f2019-06-14 11:35:55 +01005225psa_status_t psa_key_derivation_input_key(
5226 psa_key_derivation_operation_t *operation,
5227 psa_key_derivation_step_t step,
5228 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229{
5230 psa_key_slot_t *slot;
5231 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005232
Gilles Peskine28f8f302019-07-24 13:30:31 +02005233 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005234 PSA_KEY_USAGE_DERIVE,
5235 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005237 {
5238 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005239 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005240 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005241
5242 /* Passing a key object as a SECRET input unlocks the permission
5243 * to output to a key object. */
5244 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5245 operation->can_output_key = 1;
5246
Janos Follathb80a94e2019-06-12 15:54:46 +01005247 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005248 step, slot->attr.type,
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005249 slot->data.key.data,
5250 slot->data.key.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005251}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005252
5253
5254
5255/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005256/* Key agreement */
5257/****************************************************************/
5258
Gilles Peskinea05219c2018-11-16 16:02:56 +01005259#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005260static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5261 size_t peer_key_length,
5262 const mbedtls_ecp_keypair *our_key,
5263 uint8_t *shared_secret,
5264 size_t shared_secret_size,
5265 size_t *shared_secret_length )
5266{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005267 mbedtls_ecp_keypair *their_key = NULL;
5268 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005269 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005270 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005271 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005272 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005273
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005274 status = psa_import_ec_public_key( curve,
5275 peer_key, peer_key_length,
5276 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005277 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005278 goto exit;
5279
Jaeden Amero97271b32019-01-10 19:38:51 +00005280 status = mbedtls_to_psa_error(
5281 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5282 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005283 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005284 status = mbedtls_to_psa_error(
5285 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5286 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005287 goto exit;
5288
Jaeden Amero97271b32019-01-10 19:38:51 +00005289 status = mbedtls_to_psa_error(
5290 mbedtls_ecdh_calc_secret( &ecdh,
5291 shared_secret_length,
5292 shared_secret, shared_secret_size,
5293 mbedtls_ctr_drbg_random,
5294 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005295 if( status != PSA_SUCCESS )
5296 goto exit;
5297 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5298 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005299
5300exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005301 if( status != PSA_SUCCESS )
5302 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005303 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005304 mbedtls_ecp_keypair_free( their_key );
5305 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005306 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005307}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005308#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005309
Gilles Peskine01d718c2018-09-18 12:01:02 +02005310#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5311
Gilles Peskine0216fe12019-04-11 21:23:21 +02005312static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5313 psa_key_slot_t *private_key,
5314 const uint8_t *peer_key,
5315 size_t peer_key_length,
5316 uint8_t *shared_secret,
5317 size_t shared_secret_size,
5318 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005319{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005320 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005321 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005322#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005323 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005324 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005325 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005326 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5327 private_key->data.ecp,
5328 shared_secret, shared_secret_size,
5329 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005330#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005331 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005332 (void) private_key;
5333 (void) peer_key;
5334 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005335 (void) shared_secret;
5336 (void) shared_secret_size;
5337 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338 return( PSA_ERROR_NOT_SUPPORTED );
5339 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005340}
5341
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005342/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005343 * to potentially free embedded data structures and wipe confidential data.
5344 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005345static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005346 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005347 psa_key_slot_t *private_key,
5348 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005349 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350{
5351 psa_status_t status;
5352 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5353 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005354 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005355
5356 /* Step 1: run the secret agreement algorithm to generate the shared
5357 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005358 status = psa_key_agreement_raw_internal( ka_alg,
5359 private_key,
5360 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005361 shared_secret,
5362 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005363 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005364 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005365 goto exit;
5366
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005367 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005368 * the shared secret. A shared secret is permitted wherever a key
5369 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005370 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005371 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005372 shared_secret,
5373 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005374
Gilles Peskine12313cd2018-06-20 00:20:32 +02005375exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005376 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005377 return( status );
5378}
5379
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005380psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005381 psa_key_derivation_step_t step,
5382 psa_key_handle_t private_key,
5383 const uint8_t *peer_key,
5384 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005385{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005386 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005387 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005388 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005389 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005390 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005391 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392 if( status != PSA_SUCCESS )
5393 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005394 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005395 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005396 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005397 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005398 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005399 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005400}
5401
Gilles Peskinebe697d82019-05-16 18:00:41 +02005402psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5403 psa_key_handle_t private_key,
5404 const uint8_t *peer_key,
5405 size_t peer_key_length,
5406 uint8_t *output,
5407 size_t output_size,
5408 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005409{
5410 psa_key_slot_t *slot;
5411 psa_status_t status;
5412
5413 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5414 {
5415 status = PSA_ERROR_INVALID_ARGUMENT;
5416 goto exit;
5417 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005418 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005419 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005420 if( status != PSA_SUCCESS )
5421 goto exit;
5422
5423 status = psa_key_agreement_raw_internal( alg, slot,
5424 peer_key, peer_key_length,
5425 output, output_size,
5426 output_length );
5427
5428exit:
5429 if( status != PSA_SUCCESS )
5430 {
5431 /* If an error happens and is not handled properly, the output
5432 * may be used as a key to protect sensitive data. Arrange for such
5433 * a key to be random, which is likely to result in decryption or
5434 * verification errors. This is better than filling the buffer with
5435 * some constant data such as zeros, which would result in the data
5436 * being protected with a reproducible, easily knowable key.
5437 */
5438 psa_generate_random( output, output_size );
5439 *output_length = output_size;
5440 }
5441 return( status );
5442}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005443
5444
5445/****************************************************************/
5446/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005447/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005448
Gilles Peskine4c317f42018-07-12 01:24:09 +02005449psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005450 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005451{
Janos Follath24eed8d2019-11-22 13:21:35 +00005452 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005453 GUARD_MODULE_INITIALIZED;
5454
Gilles Peskinef181eca2019-08-07 13:49:00 +02005455 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5456 {
5457 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5458 output,
5459 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5460 if( ret != 0 )
5461 return( mbedtls_to_psa_error( ret ) );
5462 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5463 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5464 }
5465
Gilles Peskinee59236f2018-01-27 23:32:46 +01005466 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5467 return( mbedtls_to_psa_error( ret ) );
5468}
5469
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005470#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5471#include "mbedtls/entropy_poll.h"
5472
Gilles Peskine7228da22019-07-15 11:06:15 +02005473psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005474 size_t seed_size )
5475{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005476 if( global_data.initialized )
5477 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005478
5479 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5480 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5481 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5482 return( PSA_ERROR_INVALID_ARGUMENT );
5483
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005484 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005485}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005486#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005487
Gilles Peskinee56e8782019-04-26 17:34:02 +02005488#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5489static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5490 size_t domain_parameters_size,
5491 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005492{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005493 size_t i;
5494 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005495
Gilles Peskinee56e8782019-04-26 17:34:02 +02005496 if( domain_parameters_size == 0 )
5497 {
5498 *exponent = 65537;
5499 return( PSA_SUCCESS );
5500 }
5501
5502 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5503 * support values that fit in a 32-bit integer, which is larger than
5504 * int on just about every platform anyway. */
5505 if( domain_parameters_size > sizeof( acc ) )
5506 return( PSA_ERROR_NOT_SUPPORTED );
5507 for( i = 0; i < domain_parameters_size; i++ )
5508 acc = ( acc << 8 ) | domain_parameters[i];
5509 if( acc > INT_MAX )
5510 return( PSA_ERROR_NOT_SUPPORTED );
5511 *exponent = acc;
5512 return( PSA_SUCCESS );
5513}
5514#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5515
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005516static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005517 psa_key_slot_t *slot, size_t bits,
5518 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005519{
Gilles Peskine8e338702019-07-30 20:06:31 +02005520 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005521
Gilles Peskinee56e8782019-04-26 17:34:02 +02005522 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005523 return( PSA_ERROR_INVALID_ARGUMENT );
5524
Gilles Peskinee59236f2018-01-27 23:32:46 +01005525 if( key_type_is_raw_bytes( type ) )
5526 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005527 psa_status_t status;
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005528 status = prepare_raw_data_slot( type, bits, &slot->data.key );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005529 if( status != PSA_SUCCESS )
5530 return( status );
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005531 status = psa_generate_random( slot->data.key.data,
5532 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535#if defined(MBEDTLS_DES_C)
5536 if( type == PSA_KEY_TYPE_DES )
Steven Cooreman71fd80d2020-07-07 21:12:27 +02005537 psa_des_set_key_parity( slot->data.key.data,
5538 slot->data.key.bytes );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005539#endif /* MBEDTLS_DES_C */
5540 }
5541 else
5542
5543#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005544 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005545 {
5546 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005547 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005548 int exponent;
5549 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005550 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5551 return( PSA_ERROR_NOT_SUPPORTED );
5552 /* Accept only byte-aligned keys, for the same reasons as
5553 * in psa_import_rsa_key(). */
5554 if( bits % 8 != 0 )
5555 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005556 status = psa_read_rsa_exponent( domain_parameters,
5557 domain_parameters_size,
5558 &exponent );
5559 if( status != PSA_SUCCESS )
5560 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005561 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5562 if( rsa == NULL )
5563 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5564 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5565 ret = mbedtls_rsa_gen_key( rsa,
5566 mbedtls_ctr_drbg_random,
5567 &global_data.ctr_drbg,
5568 (unsigned int) bits,
5569 exponent );
5570 if( ret != 0 )
5571 {
5572 mbedtls_rsa_free( rsa );
5573 mbedtls_free( rsa );
5574 return( mbedtls_to_psa_error( ret ) );
5575 }
5576 slot->data.rsa = rsa;
5577 }
5578 else
5579#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5580
5581#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005582 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005583 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005584 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005585 mbedtls_ecp_group_id grp_id =
5586 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005587 const mbedtls_ecp_curve_info *curve_info =
5588 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5589 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005590 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005591 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005592 return( PSA_ERROR_NOT_SUPPORTED );
5593 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5594 return( PSA_ERROR_NOT_SUPPORTED );
5595 if( curve_info->bit_size != bits )
5596 return( PSA_ERROR_INVALID_ARGUMENT );
5597 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5598 if( ecp == NULL )
5599 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5600 mbedtls_ecp_keypair_init( ecp );
5601 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5602 mbedtls_ctr_drbg_random,
5603 &global_data.ctr_drbg );
5604 if( ret != 0 )
5605 {
5606 mbedtls_ecp_keypair_free( ecp );
5607 mbedtls_free( ecp );
5608 return( mbedtls_to_psa_error( ret ) );
5609 }
5610 slot->data.ecp = ecp;
5611 }
5612 else
5613#endif /* MBEDTLS_ECP_C */
5614
5615 return( PSA_ERROR_NOT_SUPPORTED );
5616
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005617 return( PSA_SUCCESS );
5618}
Darryl Green0c6575a2018-11-07 16:05:30 +00005619
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005620psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005621 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005622{
5623 psa_status_t status;
5624 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005625 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005626
Gilles Peskine0f84d622019-09-12 19:03:13 +02005627 /* Reject any attempt to create a zero-length key so that we don't
5628 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5629 if( psa_get_key_bits( attributes ) == 0 )
5630 return( PSA_ERROR_INVALID_ARGUMENT );
5631
Gilles Peskinedf179142019-07-15 22:02:14 +02005632 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5633 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005634 if( status != PSA_SUCCESS )
5635 goto exit;
5636
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005637#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5638 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005639 {
Gilles Peskine11792082019-08-06 18:36:36 +02005640 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5641 size_t pubkey_length = 0; /* We don't support this feature yet */
5642 if( drv->key_management == NULL ||
5643 drv->key_management->p_generate == NULL )
5644 {
5645 status = PSA_ERROR_NOT_SUPPORTED;
5646 goto exit;
5647 }
5648 status = drv->key_management->p_generate(
5649 psa_get_se_driver_context( driver ),
5650 slot->data.se.slot_number, attributes,
5651 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005652 }
Gilles Peskine11792082019-08-06 18:36:36 +02005653 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005654#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005655 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005656 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005657 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005658 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005659 }
Gilles Peskine11792082019-08-06 18:36:36 +02005660
5661exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005663 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005664 if( status != PSA_SUCCESS )
5665 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005666 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005667 *handle = 0;
5668 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005669 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005670}
5671
5672
Gilles Peskinee59236f2018-01-27 23:32:46 +01005673
5674/****************************************************************/
5675/* Module setup */
5676/****************************************************************/
5677
Gilles Peskine5e769522018-11-20 21:59:56 +01005678psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5679 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5680 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5681{
5682 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5683 return( PSA_ERROR_BAD_STATE );
5684 global_data.entropy_init = entropy_init;
5685 global_data.entropy_free = entropy_free;
5686 return( PSA_SUCCESS );
5687}
5688
Gilles Peskinee59236f2018-01-27 23:32:46 +01005689void mbedtls_psa_crypto_free( void )
5690{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005691 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005692 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5693 {
5694 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005695 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005696 }
5697 /* Wipe all remaining data, including configuration.
5698 * In particular, this sets all state indicator to the value
5699 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005700 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005701#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005702 /* Unregister all secure element drivers, so that we restart from
5703 * a pristine state. */
5704 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005705#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005706}
5707
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005708#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5709/** Recover a transaction that was interrupted by a power failure.
5710 *
5711 * This function is called during initialization, before psa_crypto_init()
5712 * returns. If this function returns a failure status, the initialization
5713 * fails.
5714 */
5715static psa_status_t psa_crypto_recover_transaction(
5716 const psa_crypto_transaction_t *transaction )
5717{
5718 switch( transaction->unknown.type )
5719 {
5720 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5721 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005722 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005723 * is implemented.
5724 * https://github.com/ARMmbed/mbed-crypto/issues/218
5725 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005726 default:
5727 /* We found an unsupported transaction in the storage.
5728 * We don't know what state the storage is in. Give up. */
5729 return( PSA_ERROR_STORAGE_FAILURE );
5730 }
5731}
5732#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5733
Gilles Peskinee59236f2018-01-27 23:32:46 +01005734psa_status_t psa_crypto_init( void )
5735{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005736 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005737 const unsigned char drbg_seed[] = "PSA";
5738
Gilles Peskinec6b69072018-11-20 21:42:52 +01005739 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005740 if( global_data.initialized != 0 )
5741 return( PSA_SUCCESS );
5742
Gilles Peskine5e769522018-11-20 21:59:56 +01005743 /* Set default configuration if
5744 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5745 if( global_data.entropy_init == NULL )
5746 global_data.entropy_init = mbedtls_entropy_init;
5747 if( global_data.entropy_free == NULL )
5748 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005749
Gilles Peskinec6b69072018-11-20 21:42:52 +01005750 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005751 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005752#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5753 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5754 /* The PSA entropy injection feature depends on using NV seed as an entropy
5755 * source. Add NV seed as an entropy source for PSA entropy injection. */
5756 mbedtls_entropy_add_source( &global_data.entropy,
5757 mbedtls_nv_seed_poll, NULL,
5758 MBEDTLS_ENTROPY_BLOCK_SIZE,
5759 MBEDTLS_ENTROPY_SOURCE_STRONG );
5760#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005761 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005762 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005763 status = mbedtls_to_psa_error(
5764 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5765 mbedtls_entropy_func,
5766 &global_data.entropy,
5767 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5768 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005769 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005770 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005771
Gilles Peskine66fb1262018-12-10 16:29:04 +01005772 status = psa_initialize_key_slots( );
5773 if( status != PSA_SUCCESS )
5774 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005775
Gilles Peskined9348f22019-10-01 15:22:29 +02005776#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5777 status = psa_init_all_se_drivers( );
5778 if( status != PSA_SUCCESS )
5779 goto exit;
5780#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5781
Gilles Peskine4b734222019-07-24 15:56:31 +02005782#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005783 status = psa_crypto_load_transaction( );
5784 if( status == PSA_SUCCESS )
5785 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005786 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5787 if( status != PSA_SUCCESS )
5788 goto exit;
5789 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005790 }
5791 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5792 {
5793 /* There's no transaction to complete. It's all good. */
5794 status = PSA_SUCCESS;
5795 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005796#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005797
Gilles Peskinec6b69072018-11-20 21:42:52 +01005798 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005799 global_data.initialized = 1;
5800
5801exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005802 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005803 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005804 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005805}
5806
5807#endif /* MBEDTLS_PSA_CRYPTO_C */