blob: 3dc3b867348f59cf9f6b5f045ad7967cbc9af38f [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
23#if !defined(MBEDTLS_CONFIG_FILE)
24#include "mbedtls/config.h"
25#else
26#include MBEDTLS_CONFIG_FILE
27#endif
28
29#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030030
itayzafrir7723ab12019-02-14 10:28:02 +020031#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010032#include "psa/crypto.h"
33
Gilles Peskine039b90c2018-12-07 18:24:41 +010034#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010035#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020036#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020037#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020038#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010039#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010040/* Include internal declarations that are useful for implementing persistently
41 * stored keys. */
42#include "psa_crypto_storage.h"
43
Gilles Peskineb46bef22019-07-30 21:32:04 +020044#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010045#include <stdlib.h>
46#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010047#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020048#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010049#define mbedtls_calloc calloc
50#define mbedtls_free free
51#endif
52
Gilles Peskinea5905292018-02-07 20:59:33 +010053#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020054#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000055#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020056#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010057#include "mbedtls/blowfish.h"
58#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020059#include "mbedtls/chacha20.h"
60#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010061#include "mbedtls/cipher.h"
62#include "mbedtls/ccm.h"
63#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010064#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010065#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020066#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010067#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010068#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010069#include "mbedtls/error.h"
70#include "mbedtls/gcm.h"
71#include "mbedtls/md2.h"
72#include "mbedtls/md4.h"
73#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010074#include "mbedtls/md.h"
75#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010076#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010077#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010078#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000079#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010080#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010081#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010082#include "mbedtls/sha1.h"
83#include "mbedtls/sha256.h"
84#include "mbedtls/sha512.h"
85#include "mbedtls/xtea.h"
86
Gilles Peskine996deb12018-08-01 15:45:45 +020087#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
88
Gilles Peskine9ef733f2018-02-07 21:05:37 +010089/* constant-time buffer comparison */
90static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
91{
92 size_t i;
93 unsigned char diff = 0;
94
95 for( i = 0; i < n; i++ )
96 diff |= a[i] ^ b[i];
97
98 return( diff );
99}
100
101
102
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100103/****************************************************************/
104/* Global data, support functions and library management */
105/****************************************************************/
106
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200107static int key_type_is_raw_bytes( psa_key_type_t type )
108{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200109 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200110}
111
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100112/* Values for psa_global_data_t::rng_state */
113#define RNG_NOT_INITIALIZED 0
114#define RNG_INITIALIZED 1
115#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100116
Gilles Peskine2d277862018-06-18 15:41:12 +0200117typedef struct
118{
Gilles Peskine5e769522018-11-20 21:59:56 +0100119 void (* entropy_init )( mbedtls_entropy_context *ctx );
120 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100121 mbedtls_entropy_context entropy;
122 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100123 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100124 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100125} psa_global_data_t;
126
127static psa_global_data_t global_data;
128
itayzafrir0adf0fc2018-09-06 16:24:41 +0300129#define GUARD_MODULE_INITIALIZED \
130 if( global_data.initialized == 0 ) \
131 return( PSA_ERROR_BAD_STATE );
132
Gilles Peskinee59236f2018-01-27 23:32:46 +0100133static psa_status_t mbedtls_to_psa_error( int ret )
134{
Gilles Peskinea5905292018-02-07 20:59:33 +0100135 /* If there's both a high-level code and low-level code, dispatch on
136 * the high-level code. */
137 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100138 {
139 case 0:
140 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100141
142 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
143 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
144 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
145 return( PSA_ERROR_NOT_SUPPORTED );
146 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
147 return( PSA_ERROR_HARDWARE_FAILURE );
148
149 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
150 return( PSA_ERROR_HARDWARE_FAILURE );
151
Gilles Peskine9a944802018-06-21 09:35:35 +0200152 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
153 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
154 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
155 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
156 case MBEDTLS_ERR_ASN1_INVALID_DATA:
157 return( PSA_ERROR_INVALID_ARGUMENT );
158 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
159 return( PSA_ERROR_INSUFFICIENT_MEMORY );
160 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
161 return( PSA_ERROR_BUFFER_TOO_SMALL );
162
Jaeden Amero93e21112019-02-20 13:57:28 +0000163#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000164 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000165#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
166 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
167#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100168 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
169 return( PSA_ERROR_NOT_SUPPORTED );
170 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
171 return( PSA_ERROR_HARDWARE_FAILURE );
172
Jaeden Amero93e21112019-02-20 13:57:28 +0000173#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000174 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000175#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
176 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
177#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100178 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
179 return( PSA_ERROR_NOT_SUPPORTED );
180 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
181 return( PSA_ERROR_HARDWARE_FAILURE );
182
183 case MBEDTLS_ERR_CCM_BAD_INPUT:
184 return( PSA_ERROR_INVALID_ARGUMENT );
185 case MBEDTLS_ERR_CCM_AUTH_FAILED:
186 return( PSA_ERROR_INVALID_SIGNATURE );
187 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
188 return( PSA_ERROR_HARDWARE_FAILURE );
189
Gilles Peskine26869f22019-05-06 15:25:00 +0200190 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
191 return( PSA_ERROR_INVALID_ARGUMENT );
192
193 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
194 return( PSA_ERROR_BAD_STATE );
195 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
196 return( PSA_ERROR_INVALID_SIGNATURE );
197
Gilles Peskinea5905292018-02-07 20:59:33 +0100198 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
199 return( PSA_ERROR_NOT_SUPPORTED );
200 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
201 return( PSA_ERROR_INVALID_ARGUMENT );
202 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
203 return( PSA_ERROR_INSUFFICIENT_MEMORY );
204 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
205 return( PSA_ERROR_INVALID_PADDING );
206 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
207 return( PSA_ERROR_BAD_STATE );
208 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
209 return( PSA_ERROR_INVALID_SIGNATURE );
210 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200211 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100212 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
213 return( PSA_ERROR_HARDWARE_FAILURE );
214
215 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
216 return( PSA_ERROR_HARDWARE_FAILURE );
217
218 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
219 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
220 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
221 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
222 return( PSA_ERROR_NOT_SUPPORTED );
223 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
224 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
225
226 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
227 return( PSA_ERROR_NOT_SUPPORTED );
228 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
229 return( PSA_ERROR_HARDWARE_FAILURE );
230
Gilles Peskinee59236f2018-01-27 23:32:46 +0100231 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
232 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
233 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
234 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100235
236 case MBEDTLS_ERR_GCM_AUTH_FAILED:
237 return( PSA_ERROR_INVALID_SIGNATURE );
238 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200239 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100240 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
241 return( PSA_ERROR_HARDWARE_FAILURE );
242
243 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
245 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
246 return( PSA_ERROR_HARDWARE_FAILURE );
247
248 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
249 return( PSA_ERROR_NOT_SUPPORTED );
250 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
251 return( PSA_ERROR_INVALID_ARGUMENT );
252 case MBEDTLS_ERR_MD_ALLOC_FAILED:
253 return( PSA_ERROR_INSUFFICIENT_MEMORY );
254 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
255 return( PSA_ERROR_STORAGE_FAILURE );
256 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
257 return( PSA_ERROR_HARDWARE_FAILURE );
258
Gilles Peskinef76aa772018-10-29 19:24:33 +0100259 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
260 return( PSA_ERROR_STORAGE_FAILURE );
261 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
262 return( PSA_ERROR_INVALID_ARGUMENT );
263 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
264 return( PSA_ERROR_INVALID_ARGUMENT );
265 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
266 return( PSA_ERROR_BUFFER_TOO_SMALL );
267 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
268 return( PSA_ERROR_INVALID_ARGUMENT );
269 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
270 return( PSA_ERROR_INVALID_ARGUMENT );
271 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
272 return( PSA_ERROR_INVALID_ARGUMENT );
273 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
274 return( PSA_ERROR_INSUFFICIENT_MEMORY );
275
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100276 case MBEDTLS_ERR_PK_ALLOC_FAILED:
277 return( PSA_ERROR_INSUFFICIENT_MEMORY );
278 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
279 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
280 return( PSA_ERROR_INVALID_ARGUMENT );
281 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100282 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100283 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
284 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
285 return( PSA_ERROR_INVALID_ARGUMENT );
286 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
287 return( PSA_ERROR_NOT_SUPPORTED );
288 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
289 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
290 return( PSA_ERROR_NOT_PERMITTED );
291 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
292 return( PSA_ERROR_INVALID_ARGUMENT );
293 case MBEDTLS_ERR_PK_INVALID_ALG:
294 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
295 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
296 return( PSA_ERROR_NOT_SUPPORTED );
297 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
298 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100299 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
300 return( PSA_ERROR_HARDWARE_FAILURE );
301
Gilles Peskineff2d2002019-05-06 15:26:23 +0200302 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
303 return( PSA_ERROR_HARDWARE_FAILURE );
304 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
305 return( PSA_ERROR_NOT_SUPPORTED );
306
Gilles Peskinea5905292018-02-07 20:59:33 +0100307 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
308 return( PSA_ERROR_HARDWARE_FAILURE );
309
310 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
311 return( PSA_ERROR_INVALID_ARGUMENT );
312 case MBEDTLS_ERR_RSA_INVALID_PADDING:
313 return( PSA_ERROR_INVALID_PADDING );
314 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
315 return( PSA_ERROR_HARDWARE_FAILURE );
316 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
317 return( PSA_ERROR_INVALID_ARGUMENT );
318 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
319 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200320 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100321 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
322 return( PSA_ERROR_INVALID_SIGNATURE );
323 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
324 return( PSA_ERROR_BUFFER_TOO_SMALL );
325 case MBEDTLS_ERR_RSA_RNG_FAILED:
326 return( PSA_ERROR_INSUFFICIENT_MEMORY );
327 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
328 return( PSA_ERROR_NOT_SUPPORTED );
329 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
330 return( PSA_ERROR_HARDWARE_FAILURE );
331
332 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
334 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
335 return( PSA_ERROR_HARDWARE_FAILURE );
336
337 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
338 return( PSA_ERROR_INVALID_ARGUMENT );
339 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
340 return( PSA_ERROR_HARDWARE_FAILURE );
341
itayzafrir5c753392018-05-08 11:18:38 +0300342 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300343 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300344 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300345 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
346 return( PSA_ERROR_BUFFER_TOO_SMALL );
347 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
348 return( PSA_ERROR_NOT_SUPPORTED );
349 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
350 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
351 return( PSA_ERROR_INVALID_SIGNATURE );
352 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
353 return( PSA_ERROR_INSUFFICIENT_MEMORY );
354 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
355 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000356 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
357 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300358
Gilles Peskinee59236f2018-01-27 23:32:46 +0100359 default:
David Saadab4ecc272019-02-14 13:48:10 +0200360 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100361 }
362}
363
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200364
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200365
366
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100367/****************************************************************/
368/* Key management */
369/****************************************************************/
370
Gilles Peskine73167e12019-07-12 23:44:37 +0200371#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
372static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
373{
Gilles Peskine8e338702019-07-30 20:06:31 +0200374 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200375}
376#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
377
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100378#if defined(MBEDTLS_ECP_C)
Gilles Peskine5055b232019-12-12 17:49:31 +0100379mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve,
380 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200381{
382 switch( curve )
383 {
Gilles Peskine228abc52019-12-03 17:24:19 +0100384 case PSA_ECC_CURVE_SECP_R1:
385 switch( byte_length )
386 {
387 case PSA_BITS_TO_BYTES( 192 ):
388 return( MBEDTLS_ECP_DP_SECP192R1 );
389 case PSA_BITS_TO_BYTES( 224 ):
390 return( MBEDTLS_ECP_DP_SECP224R1 );
391 case PSA_BITS_TO_BYTES( 256 ):
392 return( MBEDTLS_ECP_DP_SECP256R1 );
393 case PSA_BITS_TO_BYTES( 384 ):
394 return( MBEDTLS_ECP_DP_SECP384R1 );
395 case PSA_BITS_TO_BYTES( 521 ):
396 return( MBEDTLS_ECP_DP_SECP521R1 );
397 default:
398 return( MBEDTLS_ECP_DP_NONE );
399 }
400 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100401
402 case PSA_ECC_CURVE_BRAINPOOL_P_R1:
403 switch( byte_length )
404 {
405 case PSA_BITS_TO_BYTES( 256 ):
406 return( MBEDTLS_ECP_DP_BP256R1 );
407 case PSA_BITS_TO_BYTES( 384 ):
408 return( MBEDTLS_ECP_DP_BP384R1 );
409 case PSA_BITS_TO_BYTES( 512 ):
410 return( MBEDTLS_ECP_DP_BP512R1 );
411 default:
412 return( MBEDTLS_ECP_DP_NONE );
413 }
414 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100415
416 case PSA_ECC_CURVE_MONTGOMERY:
417 switch( byte_length )
418 {
419 case PSA_BITS_TO_BYTES( 255 ):
420 return( MBEDTLS_ECP_DP_CURVE25519 );
421 case PSA_BITS_TO_BYTES( 448 ):
422 return( MBEDTLS_ECP_DP_CURVE448 );
423 default:
424 return( MBEDTLS_ECP_DP_NONE );
425 }
426 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100427
428 case PSA_ECC_CURVE_SECP_K1:
429 switch( byte_length )
430 {
431 case PSA_BITS_TO_BYTES( 192 ):
432 return( MBEDTLS_ECP_DP_SECP192K1 );
433 case PSA_BITS_TO_BYTES( 224 ):
434 return( MBEDTLS_ECP_DP_SECP224K1 );
435 case PSA_BITS_TO_BYTES( 256 ):
436 return( MBEDTLS_ECP_DP_SECP256K1 );
437 default:
438 return( MBEDTLS_ECP_DP_NONE );
439 }
440 break;
Gilles Peskine228abc52019-12-03 17:24:19 +0100441
Gilles Peskine12313cd2018-06-20 00:20:32 +0200442 default:
443 return( MBEDTLS_ECP_DP_NONE );
444 }
445}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100446#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200447
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200448static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
449 size_t bits,
450 struct raw_data *raw )
451{
452 /* Check that the bit size is acceptable for the key type */
453 switch( type )
454 {
455 case PSA_KEY_TYPE_RAW_DATA:
456#if defined(MBEDTLS_MD_C)
457 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200458#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200459 case PSA_KEY_TYPE_DERIVE:
460 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200461#if defined(MBEDTLS_AES_C)
462 case PSA_KEY_TYPE_AES:
463 if( bits != 128 && bits != 192 && bits != 256 )
464 return( PSA_ERROR_INVALID_ARGUMENT );
465 break;
466#endif
467#if defined(MBEDTLS_CAMELLIA_C)
468 case PSA_KEY_TYPE_CAMELLIA:
469 if( bits != 128 && bits != 192 && bits != 256 )
470 return( PSA_ERROR_INVALID_ARGUMENT );
471 break;
472#endif
473#if defined(MBEDTLS_DES_C)
474 case PSA_KEY_TYPE_DES:
475 if( bits != 64 && bits != 128 && bits != 192 )
476 return( PSA_ERROR_INVALID_ARGUMENT );
477 break;
478#endif
479#if defined(MBEDTLS_ARC4_C)
480 case PSA_KEY_TYPE_ARC4:
481 if( bits < 8 || bits > 2048 )
482 return( PSA_ERROR_INVALID_ARGUMENT );
483 break;
484#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200485#if defined(MBEDTLS_CHACHA20_C)
486 case PSA_KEY_TYPE_CHACHA20:
487 if( bits != 256 )
488 return( PSA_ERROR_INVALID_ARGUMENT );
489 break;
490#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200491 default:
492 return( PSA_ERROR_NOT_SUPPORTED );
493 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200494 if( bits % 8 != 0 )
495 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200496
497 /* Allocate memory for the key */
498 raw->bytes = PSA_BITS_TO_BYTES( bits );
499 raw->data = mbedtls_calloc( 1, raw->bytes );
500 if( raw->data == NULL )
501 {
502 raw->bytes = 0;
503 return( PSA_ERROR_INSUFFICIENT_MEMORY );
504 }
505 return( PSA_SUCCESS );
506}
507
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200508#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100509/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
510 * that are not a multiple of 8) well. For example, there is only
511 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
512 * way to return the exact bit size of a key.
513 * To keep things simple, reject non-byte-aligned key sizes. */
514static psa_status_t psa_check_rsa_key_byte_aligned(
515 const mbedtls_rsa_context *rsa )
516{
517 mbedtls_mpi n;
518 psa_status_t status;
519 mbedtls_mpi_init( &n );
520 status = mbedtls_to_psa_error(
521 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
522 if( status == PSA_SUCCESS )
523 {
524 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
525 status = PSA_ERROR_NOT_SUPPORTED;
526 }
527 mbedtls_mpi_free( &n );
528 return( status );
529}
530
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000531static psa_status_t psa_import_rsa_key( psa_key_type_t type,
532 const uint8_t *data,
533 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200534 mbedtls_rsa_context **p_rsa )
535{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000536 psa_status_t status;
537 mbedtls_pk_context pk;
538 mbedtls_rsa_context *rsa;
539 size_t bits;
540
541 mbedtls_pk_init( &pk );
542
543 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200544 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000545 status = mbedtls_to_psa_error(
546 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200547 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000548 status = mbedtls_to_psa_error(
549 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
550 if( status != PSA_SUCCESS )
551 goto exit;
552
553 /* We have something that the pkparse module recognizes. If it is a
554 * valid RSA key, store it. */
555 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200556 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000557 status = PSA_ERROR_INVALID_ARGUMENT;
558 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200559 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000560
561 rsa = mbedtls_pk_rsa( pk );
562 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
563 * supports non-byte-aligned key sizes, but not well. For example,
564 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
565 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
566 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
567 {
568 status = PSA_ERROR_NOT_SUPPORTED;
569 goto exit;
570 }
571 status = psa_check_rsa_key_byte_aligned( rsa );
572
573exit:
574 /* Free the content of the pk object only on error. */
575 if( status != PSA_SUCCESS )
576 {
577 mbedtls_pk_free( &pk );
578 return( status );
579 }
580
581 /* On success, store the content of the object in the RSA context. */
582 *p_rsa = rsa;
583
584 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200585}
586#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
587
Jaeden Ameroccdce902019-01-10 11:42:27 +0000588#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100589static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100590 size_t data_length,
591 int is_public,
Gilles Peskine4cd32772019-12-02 20:49:42 +0100592 mbedtls_ecp_keypair **p_ecp )
593{
594 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
595 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
596 if( *p_ecp == NULL )
597 return( PSA_ERROR_INSUFFICIENT_MEMORY );
598 mbedtls_ecp_keypair_init( *p_ecp );
599
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100600 if( is_public )
601 {
602 /* A public key is represented as:
603 * - The byte 0x04;
604 * - `x_P` as a `ceiling(m/8)`-byte string, big-endian;
605 * - `y_P` as a `ceiling(m/8)`-byte string, big-endian.
606 * So its data length is 2m+1 where n is the key size in bits.
607 */
608 if( ( data_length & 1 ) == 0 )
609 return( PSA_ERROR_INVALID_ARGUMENT );
610 data_length = data_length / 2;
611 }
612
Gilles Peskine4cd32772019-12-02 20:49:42 +0100613 /* Load the group. */
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100614 grp_id = mbedtls_ecc_group_of_psa( curve, data_length );
615 if( grp_id == MBEDTLS_ECP_DP_NONE )
616 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine4cd32772019-12-02 20:49:42 +0100617 return( mbedtls_to_psa_error(
618 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
619}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000620
621/* Import a public key given as the uncompressed representation defined by SEC1
622 * 2.3.3 as the content of an ECPoint. */
623static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
624 const uint8_t *data,
625 size_t data_length,
626 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200627{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200628 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000629 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000630
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100631 status = psa_prepare_import_ec_key( curve, data_length, 1, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000632 if( status != PSA_SUCCESS )
633 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100634
Jaeden Ameroccdce902019-01-10 11:42:27 +0000635 /* Load the public value. */
636 status = mbedtls_to_psa_error(
637 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
638 data, data_length ) );
639 if( status != PSA_SUCCESS )
640 goto exit;
641
642 /* Check that the point is on the curve. */
643 status = mbedtls_to_psa_error(
644 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
645 if( status != PSA_SUCCESS )
646 goto exit;
647
648 *p_ecp = ecp;
649 return( PSA_SUCCESS );
650
651exit:
652 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200653 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000654 mbedtls_ecp_keypair_free( ecp );
655 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200656 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000657 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200658}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200659
Gilles Peskinef76aa772018-10-29 19:24:33 +0100660/* Import a private key given as a byte string which is the private value
661 * in big-endian order. */
662static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
663 const uint8_t *data,
664 size_t data_length,
665 mbedtls_ecp_keypair **p_ecp )
666{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200667 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100668 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100669
Gilles Peskine4295e8b2019-12-02 21:39:10 +0100670 status = psa_prepare_import_ec_key( curve, data_length, 0, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100671 if( status != PSA_SUCCESS )
672 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100673
Gilles Peskinef76aa772018-10-29 19:24:33 +0100674 /* Load the secret value. */
675 status = mbedtls_to_psa_error(
676 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
677 if( status != PSA_SUCCESS )
678 goto exit;
679 /* Validate the private key. */
680 status = mbedtls_to_psa_error(
681 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
682 if( status != PSA_SUCCESS )
683 goto exit;
684 /* Calculate the public key from the private key. */
685 status = mbedtls_to_psa_error(
686 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
687 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
688 if( status != PSA_SUCCESS )
689 goto exit;
690
691 *p_ecp = ecp;
692 return( PSA_SUCCESS );
693
694exit:
695 if( ecp != NULL )
696 {
697 mbedtls_ecp_keypair_free( ecp );
698 mbedtls_free( ecp );
699 }
700 return( status );
701}
702#endif /* defined(MBEDTLS_ECP_C) */
703
Gilles Peskineb46bef22019-07-30 21:32:04 +0200704
705/** Return the size of the key in the given slot, in bits.
706 *
707 * \param[in] slot A key slot.
708 *
709 * \return The key size in bits, read from the metadata in the slot.
710 */
711static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
712{
713 return( slot->attr.bits );
714}
715
716/** Calculate the size of the key in the given slot, in bits.
717 *
718 * \param[in] slot A key slot containing a transparent key.
719 *
720 * \return The key size in bits, calculated from the key data.
721 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200722static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200723{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200724 size_t bits = 0; /* return 0 on an empty slot */
725
Gilles Peskineb46bef22019-07-30 21:32:04 +0200726 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200727 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200728#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200729 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
730 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200731#endif /* defined(MBEDTLS_RSA_C) */
732#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200733 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
734 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200735#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200736
737 /* We know that the size fits in psa_key_bits_t thanks to checks
738 * when the key was created. */
739 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200740}
741
Gilles Peskine8e338702019-07-30 20:06:31 +0200742/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100743 * previously. This function assumes that the slot does not contain
744 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100745psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
746 const uint8_t *data,
747 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100748{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200749 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100750
Gilles Peskine8e338702019-07-30 20:06:31 +0200751 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100752 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200753 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200754 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100755 if( data_length > SIZE_MAX / 8 )
756 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200757 /* Enforce a size limit, and in particular ensure that the bit
758 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200759 if( bit_size > PSA_MAX_KEY_BITS )
760 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200761 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200762 &slot->data.raw );
763 if( status != PSA_SUCCESS )
764 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200765 if( data_length != 0 )
766 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100767 }
768 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100769#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200770 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100771 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200772 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100773 data, data_length,
774 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100775 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200776 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100777 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000778 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200779 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780 data, data_length,
781 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100782 }
783 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100784#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000785#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200786 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100787 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200788 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000789 data, data_length,
790 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100791 }
792 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000793#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100794 {
795 return( PSA_ERROR_NOT_SUPPORTED );
796 }
Darryl Green940d72c2018-07-13 13:18:51 +0100797
Gilles Peskineb46bef22019-07-30 21:32:04 +0200798 if( status == PSA_SUCCESS )
799 {
800 /* Write the actual key size to the slot.
801 * psa_start_key_creation() wrote the size declared by the
802 * caller, which may be 0 (meaning unspecified) or wrong. */
803 slot->attr.bits = psa_calculate_key_bits( slot );
804 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100805 return( status );
806}
807
Gilles Peskinef603c712019-01-19 13:40:11 +0100808/** Calculate the intersection of two algorithm usage policies.
809 *
810 * Return 0 (which allows no operation) on incompatibility.
811 */
812static psa_algorithm_t psa_key_policy_algorithm_intersection(
813 psa_algorithm_t alg1,
814 psa_algorithm_t alg2 )
815{
Gilles Peskine549ea862019-05-22 11:45:59 +0200816 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100817 if( alg1 == alg2 )
818 return( alg1 );
819 /* If the policies are from the same hash-and-sign family, check
820 * if one is a wildcard. If so the other has the specific algorithm. */
821 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
822 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
823 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
824 {
825 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
826 return( alg2 );
827 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
828 return( alg1 );
829 }
830 /* If the policies are incompatible, allow nothing. */
831 return( 0 );
832}
833
Gilles Peskined6f371b2019-05-10 19:33:38 +0200834static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
835 psa_algorithm_t requested_alg )
836{
Gilles Peskine549ea862019-05-22 11:45:59 +0200837 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200838 if( requested_alg == policy_alg )
839 return( 1 );
840 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200841 * and requested_alg is the same hash-and-sign family with any hash,
842 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200843 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
844 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
845 {
846 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
847 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
848 }
849 /* If it isn't permitted, it's forbidden. */
850 return( 0 );
851}
852
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100853/** Test whether a policy permits an algorithm.
854 *
855 * The caller must test usage flags separately.
856 */
857static int psa_key_policy_permits( const psa_key_policy_t *policy,
858 psa_algorithm_t alg )
859{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200860 return( psa_key_algorithm_permits( policy->alg, alg ) ||
861 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100862}
863
Gilles Peskinef603c712019-01-19 13:40:11 +0100864/** Restrict a key policy based on a constraint.
865 *
866 * \param[in,out] policy The policy to restrict.
867 * \param[in] constraint The policy constraint to apply.
868 *
869 * \retval #PSA_SUCCESS
870 * \c *policy contains the intersection of the original value of
871 * \c *policy and \c *constraint.
872 * \retval #PSA_ERROR_INVALID_ARGUMENT
873 * \c *policy and \c *constraint are incompatible.
874 * \c *policy is unchanged.
875 */
876static psa_status_t psa_restrict_key_policy(
877 psa_key_policy_t *policy,
878 const psa_key_policy_t *constraint )
879{
880 psa_algorithm_t intersection_alg =
881 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200882 psa_algorithm_t intersection_alg2 =
883 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100884 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
885 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200886 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
887 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100888 policy->usage &= constraint->usage;
889 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200890 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100891 return( PSA_SUCCESS );
892}
893
Darryl Green06fd18d2018-07-16 11:21:11 +0100894/** Retrieve a slot which must contain a key. The key must have allow all the
895 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
896 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100897static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100898 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100899 psa_key_usage_t usage,
900 psa_algorithm_t alg )
901{
902 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100903 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100904
905 *p_slot = NULL;
906
Gilles Peskinec5487a82018-12-03 18:08:14 +0100907 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100908 if( status != PSA_SUCCESS )
909 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100910
911 /* Enforce that usage policy for the key slot contains all the flags
912 * required by the usage parameter. There is one exception: public
913 * keys can always be exported, so we treat public key objects as
914 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200915 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100916 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200917 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100918 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100919
920 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200921 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100922 return( PSA_ERROR_NOT_PERMITTED );
923
924 *p_slot = slot;
925 return( PSA_SUCCESS );
926}
Darryl Green940d72c2018-07-13 13:18:51 +0100927
Gilles Peskine28f8f302019-07-24 13:30:31 +0200928/** Retrieve a slot which must contain a transparent key.
929 *
930 * A transparent key is a key for which the key material is directly
931 * available, as opposed to a key in a secure element.
932 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200933 * This is a temporary function to use instead of psa_get_key_from_slot()
934 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200935 */
936#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
937static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
938 psa_key_slot_t **p_slot,
939 psa_key_usage_t usage,
940 psa_algorithm_t alg )
941{
942 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
943 if( status != PSA_SUCCESS )
944 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200945 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200946 {
947 *p_slot = NULL;
948 return( PSA_ERROR_NOT_SUPPORTED );
949 }
950 return( PSA_SUCCESS );
951}
952#else /* MBEDTLS_PSA_CRYPTO_SE_C */
953/* With no secure element support, all keys are transparent. */
954#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
955 psa_get_key_from_slot( handle, p_slot, usage, alg )
956#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
957
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100958/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100959static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000960{
Gilles Peskine73167e12019-07-12 23:44:37 +0200961#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
962 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000963 {
964 /* No key material to clean. */
965 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200966 else
967#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200968 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000969 {
970 /* No key material to clean. */
971 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200972 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000973 {
974 mbedtls_free( slot->data.raw.data );
975 }
976 else
977#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200978 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000979 {
980 mbedtls_rsa_free( slot->data.rsa );
981 mbedtls_free( slot->data.rsa );
982 }
983 else
984#endif /* defined(MBEDTLS_RSA_C) */
985#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200986 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000987 {
988 mbedtls_ecp_keypair_free( slot->data.ecp );
989 mbedtls_free( slot->data.ecp );
990 }
991 else
992#endif /* defined(MBEDTLS_ECP_C) */
993 {
994 /* Shouldn't happen: the key type is not any type that we
995 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200996 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000997 }
998
999 return( PSA_SUCCESS );
1000}
1001
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001002/** Completely wipe a slot in memory, including its policy.
1003 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +01001004psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001005{
1006 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +02001007 /* Multipart operations may still be using the key. This is safe
1008 * because all multipart operation objects are independent from
1009 * the key slot: if they need to access the key after the setup
1010 * phase, they have a copy of the key. Note that this means that
1011 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001012 /* At this point, key material and other type-specific content has
1013 * been wiped. Clear remaining metadata. We can call memset and not
1014 * zeroize because the metadata is not particularly sensitive. */
1015 memset( slot, 0, sizeof( *slot ) );
1016 return( status );
1017}
1018
Gilles Peskinec5487a82018-12-03 18:08:14 +01001019psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001020{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001021 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001022 psa_status_t status; /* status of the last operation */
1023 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001024#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1025 psa_se_drv_table_entry_t *driver;
1026#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001027
Gilles Peskine1841cf42019-10-08 15:48:25 +02001028 if( handle == 0 )
1029 return( PSA_SUCCESS );
1030
Gilles Peskinec5487a82018-12-03 18:08:14 +01001031 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001032 if( status != PSA_SUCCESS )
1033 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001034
1035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001036 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001037 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001038 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001039 /* For a key in a secure element, we need to do three things:
1040 * remove the key file in internal storage, destroy the
1041 * key inside the secure element, and update the driver's
1042 * persistent data. Start a transaction that will encompass these
1043 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001044 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001045 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001046 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001047 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001048 status = psa_crypto_save_transaction( );
1049 if( status != PSA_SUCCESS )
1050 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001051 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001052 /* We should still try to destroy the key in the secure
1053 * element and the key metadata in storage. This is especially
1054 * important if the error is that the storage is full.
1055 * But how to do it exactly without risking an inconsistent
1056 * state after a reset?
1057 * https://github.com/ARMmbed/mbed-crypto/issues/215
1058 */
1059 overall_status = status;
1060 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001061 }
1062
Gilles Peskine354f7672019-07-12 23:46:38 +02001063 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001064 if( overall_status == PSA_SUCCESS )
1065 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001066 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001067#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1068
Darryl Greend49a4992018-06-18 17:27:26 +01001069#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001070 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001071 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001072 status = psa_destroy_persistent_key( slot->attr.id );
1073 if( overall_status == PSA_SUCCESS )
1074 overall_status = status;
1075
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001076 /* TODO: other slots may have a copy of the same key. We should
1077 * invalidate them.
1078 * https://github.com/ARMmbed/mbed-crypto/issues/214
1079 */
Darryl Greend49a4992018-06-18 17:27:26 +01001080 }
1081#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001082
Gilles Peskinefc762652019-07-22 19:30:34 +02001083#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1084 if( driver != NULL )
1085 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001086 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001087 if( overall_status == PSA_SUCCESS )
1088 overall_status = status;
1089 status = psa_crypto_stop_transaction( );
1090 if( overall_status == PSA_SUCCESS )
1091 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001092 }
1093#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1094
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001095#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1096exit:
1097#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001098 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001099 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1100 if( overall_status == PSA_SUCCESS )
1101 overall_status = status;
1102 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001103}
1104
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001105void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001106{
Gilles Peskineb699f072019-04-26 16:06:02 +02001107 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001108 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001109}
1110
Gilles Peskineb699f072019-04-26 16:06:02 +02001111psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1112 psa_key_type_t type,
1113 const uint8_t *data,
1114 size_t data_length )
1115{
1116 uint8_t *copy = NULL;
1117
1118 if( data_length != 0 )
1119 {
1120 copy = mbedtls_calloc( 1, data_length );
1121 if( copy == NULL )
1122 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1123 memcpy( copy, data, data_length );
1124 }
1125 /* After this point, this function is guaranteed to succeed, so it
1126 * can start modifying `*attributes`. */
1127
1128 if( attributes->domain_parameters != NULL )
1129 {
1130 mbedtls_free( attributes->domain_parameters );
1131 attributes->domain_parameters = NULL;
1132 attributes->domain_parameters_size = 0;
1133 }
1134
1135 attributes->domain_parameters = copy;
1136 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001137 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001138 return( PSA_SUCCESS );
1139}
1140
1141psa_status_t psa_get_key_domain_parameters(
1142 const psa_key_attributes_t *attributes,
1143 uint8_t *data, size_t data_size, size_t *data_length )
1144{
1145 if( attributes->domain_parameters_size > data_size )
1146 return( PSA_ERROR_BUFFER_TOO_SMALL );
1147 *data_length = attributes->domain_parameters_size;
1148 if( attributes->domain_parameters_size != 0 )
1149 memcpy( data, attributes->domain_parameters,
1150 attributes->domain_parameters_size );
1151 return( PSA_SUCCESS );
1152}
1153
1154#if defined(MBEDTLS_RSA_C)
1155static psa_status_t psa_get_rsa_public_exponent(
1156 const mbedtls_rsa_context *rsa,
1157 psa_key_attributes_t *attributes )
1158{
1159 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001160 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001161 uint8_t *buffer = NULL;
1162 size_t buflen;
1163 mbedtls_mpi_init( &mpi );
1164
1165 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1166 if( ret != 0 )
1167 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001168 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1169 {
1170 /* It's the default value, which is reported as an empty string,
1171 * so there's nothing to do. */
1172 goto exit;
1173 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001174
1175 buflen = mbedtls_mpi_size( &mpi );
1176 buffer = mbedtls_calloc( 1, buflen );
1177 if( buffer == NULL )
1178 {
1179 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1180 goto exit;
1181 }
1182 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1183 if( ret != 0 )
1184 goto exit;
1185 attributes->domain_parameters = buffer;
1186 attributes->domain_parameters_size = buflen;
1187
1188exit:
1189 mbedtls_mpi_free( &mpi );
1190 if( ret != 0 )
1191 mbedtls_free( buffer );
1192 return( mbedtls_to_psa_error( ret ) );
1193}
1194#endif /* MBEDTLS_RSA_C */
1195
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001196/** Retrieve all the publicly-accessible attributes of a key.
1197 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001198psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1199 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001200{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001201 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001202 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001203
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001204 psa_reset_key_attributes( attributes );
1205
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001206 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001207 if( status != PSA_SUCCESS )
1208 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001209
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001210 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001211 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1212 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1213
1214#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1215 if( psa_key_slot_is_external( slot ) )
1216 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1217#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001218
Gilles Peskine8e338702019-07-30 20:06:31 +02001219 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001220 {
1221#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001222 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001223 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001224#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001225 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001226 * is not yet implemented.
1227 * https://github.com/ARMmbed/mbed-crypto/issues/216
1228 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001229 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001230 break;
1231#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001232 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1233 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001234#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001235 default:
1236 /* Nothing else to do. */
1237 break;
1238 }
1239
1240 if( status != PSA_SUCCESS )
1241 psa_reset_key_attributes( attributes );
1242 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001243}
1244
Gilles Peskinec8000c02019-08-02 20:15:51 +02001245#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1246psa_status_t psa_get_key_slot_number(
1247 const psa_key_attributes_t *attributes,
1248 psa_key_slot_number_t *slot_number )
1249{
1250 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1251 {
1252 *slot_number = attributes->slot_number;
1253 return( PSA_SUCCESS );
1254 }
1255 else
1256 return( PSA_ERROR_INVALID_ARGUMENT );
1257}
1258#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1259
Jaeden Ameroccdce902019-01-10 11:42:27 +00001260#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001261static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1262 unsigned char *buf, size_t size )
1263{
Janos Follath24eed8d2019-11-22 13:21:35 +00001264 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001265 unsigned char *c;
1266 size_t len = 0;
1267
1268 c = buf + size;
1269
1270 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1271
1272 return( (int) len );
1273}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001274#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001275
Gilles Peskinef603c712019-01-19 13:40:11 +01001276static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1277 uint8_t *data,
1278 size_t data_size,
1279 size_t *data_length,
1280 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001281{
Gilles Peskine5d309672019-07-12 23:47:28 +02001282#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1283 const psa_drv_se_t *drv;
1284 psa_drv_se_context_t *drv_context;
1285#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1286
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001287 *data_length = 0;
1288
Gilles Peskine8e338702019-07-30 20:06:31 +02001289 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001290 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001291
Gilles Peskinef9168942019-09-12 19:20:29 +02001292 /* Reject a zero-length output buffer now, since this can never be a
1293 * valid key representation. This way we know that data must be a valid
1294 * pointer and we can do things like memset(data, ..., data_size). */
1295 if( data_size == 0 )
1296 return( PSA_ERROR_BUFFER_TOO_SMALL );
1297
Gilles Peskine5d309672019-07-12 23:47:28 +02001298#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001299 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001300 {
1301 psa_drv_se_export_key_t method;
1302 if( drv->key_management == NULL )
1303 return( PSA_ERROR_NOT_SUPPORTED );
1304 method = ( export_public_key ?
1305 drv->key_management->p_export_public :
1306 drv->key_management->p_export );
1307 if( method == NULL )
1308 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001309 return( method( drv_context,
1310 slot->data.se.slot_number,
1311 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001312 }
1313#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1314
Gilles Peskine8e338702019-07-30 20:06:31 +02001315 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001316 {
1317 if( slot->data.raw.bytes > data_size )
1318 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001319 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1320 memset( data + slot->data.raw.bytes, 0,
1321 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001322 *data_length = slot->data.raw.bytes;
1323 return( PSA_SUCCESS );
1324 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001325#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001326 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001327 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001328 psa_status_t status;
1329
Gilles Peskineb46bef22019-07-30 21:32:04 +02001330 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001331 if( bytes > data_size )
1332 return( PSA_ERROR_BUFFER_TOO_SMALL );
1333 status = mbedtls_to_psa_error(
1334 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1335 if( status != PSA_SUCCESS )
1336 return( status );
1337 memset( data + bytes, 0, data_size - bytes );
1338 *data_length = bytes;
1339 return( PSA_SUCCESS );
1340 }
1341#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001342 else
Moran Peker17e36e12018-05-02 12:55:20 +03001343 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001344#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001345 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1346 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001347 {
Moran Pekera998bc62018-04-16 18:16:20 +03001348 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001349 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001350 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001351 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001352#if defined(MBEDTLS_RSA_C)
1353 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001354 pk.pk_info = &mbedtls_rsa_info;
1355 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001356#else
1357 return( PSA_ERROR_NOT_SUPPORTED );
1358#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001359 }
1360 else
1361 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001362#if defined(MBEDTLS_ECP_C)
1363 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001364 pk.pk_info = &mbedtls_eckey_info;
1365 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001366#else
1367 return( PSA_ERROR_NOT_SUPPORTED );
1368#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001369 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001370 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001371 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001372 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001373 }
Moran Peker17e36e12018-05-02 12:55:20 +03001374 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001375 {
Moran Peker17e36e12018-05-02 12:55:20 +03001376 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001377 }
Moran Peker60364322018-04-29 11:34:58 +03001378 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001379 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001380 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001381 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001382 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001383 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1384 * Move the data to the beginning and erase remaining data
1385 * at the original location. */
1386 if( 2 * (size_t) ret <= data_size )
1387 {
1388 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001389 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001390 }
1391 else if( (size_t) ret < data_size )
1392 {
1393 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001394 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001395 }
Moran Pekera998bc62018-04-16 18:16:20 +03001396 *data_length = ret;
1397 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001398 }
1399 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001400#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001401 {
1402 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001403 it is valid for a special-purpose implementation to omit
1404 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001405 return( PSA_ERROR_NOT_SUPPORTED );
1406 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001407 }
1408}
1409
Gilles Peskinec5487a82018-12-03 18:08:14 +01001410psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001411 uint8_t *data,
1412 size_t data_size,
1413 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001414{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001415 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001416 psa_status_t status;
1417
1418 /* Set the key to empty now, so that even when there are errors, we always
1419 * set data_length to a value between 0 and data_size. On error, setting
1420 * the key to empty is a good choice because an empty key representation is
1421 * unlikely to be accepted anywhere. */
1422 *data_length = 0;
1423
1424 /* Export requires the EXPORT flag. There is an exception for public keys,
1425 * which don't require any flag, but psa_get_key_from_slot takes
1426 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001427 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001428 if( status != PSA_SUCCESS )
1429 return( status );
1430 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001431 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001432}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001433
Gilles Peskinec5487a82018-12-03 18:08:14 +01001434psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001435 uint8_t *data,
1436 size_t data_size,
1437 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001438{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001439 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001440 psa_status_t status;
1441
1442 /* Set the key to empty now, so that even when there are errors, we always
1443 * set data_length to a value between 0 and data_size. On error, setting
1444 * the key to empty is a good choice because an empty key representation is
1445 * unlikely to be accepted anywhere. */
1446 *data_length = 0;
1447
1448 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001449 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001450 if( status != PSA_SUCCESS )
1451 return( status );
1452 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001453 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001454}
1455
Gilles Peskine91e8c332019-08-02 19:19:39 +02001456#if defined(static_assert)
1457static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1458 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001459static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001460 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001461static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001462 "One or more key attribute flag is listed as both internal-only and external-only" );
1463#endif
1464
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001465/** Validate that a key policy is internally well-formed.
1466 *
1467 * This function only rejects invalid policies. It does not validate the
1468 * consistency of the policy with respect to other attributes of the key
1469 * such as the key type.
1470 */
1471static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001472{
1473 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001474 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001475 PSA_KEY_USAGE_ENCRYPT |
1476 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001477 PSA_KEY_USAGE_SIGN_HASH |
1478 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001479 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1480 return( PSA_ERROR_INVALID_ARGUMENT );
1481
Gilles Peskine4747d192019-04-17 15:05:45 +02001482 return( PSA_SUCCESS );
1483}
1484
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001485/** Validate the internal consistency of key attributes.
1486 *
1487 * This function only rejects invalid attribute values. If does not
1488 * validate the consistency of the attributes with any key data that may
1489 * be involved in the creation of the key.
1490 *
1491 * Call this function early in the key creation process.
1492 *
1493 * \param[in] attributes Key attributes for the new key.
1494 * \param[out] p_drv On any return, the driver for the key, if any.
1495 * NULL for a transparent key.
1496 *
1497 */
1498static psa_status_t psa_validate_key_attributes(
1499 const psa_key_attributes_t *attributes,
1500 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001501{
1502 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503
1504 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001505 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001506 status = psa_validate_persistent_key_parameters(
1507 attributes->core.lifetime, attributes->core.id,
1508 p_drv, 1 );
1509 if( status != PSA_SUCCESS )
1510 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001511 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001512
1513 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001514 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001515 return( status );
1516
1517 /* Refuse to create overly large keys.
1518 * Note that this doesn't trigger on import if the attributes don't
1519 * explicitly specify a size (so psa_get_key_bits returns 0), so
1520 * psa_import_key() needs its own checks. */
1521 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1522 return( PSA_ERROR_NOT_SUPPORTED );
1523
Gilles Peskine91e8c332019-08-02 19:19:39 +02001524 /* Reject invalid flags. These should not be reachable through the API. */
1525 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1526 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1527 return( PSA_ERROR_INVALID_ARGUMENT );
1528
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001529 return( PSA_SUCCESS );
1530}
1531
Gilles Peskine4747d192019-04-17 15:05:45 +02001532/** Prepare a key slot to receive key material.
1533 *
1534 * This function allocates a key slot and sets its metadata.
1535 *
1536 * If this function fails, call psa_fail_key_creation().
1537 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001538 * This function is intended to be used as follows:
1539 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1540 * it with the specified attributes, and assign it a handle.
1541 * -# Populate the slot with the key material.
1542 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1543 * In case of failure at any step, stop the sequence and call
1544 * psa_fail_key_creation().
1545 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001547 * \param[in] attributes Key attributes for the new key.
1548 * \param[out] handle On success, a handle for the allocated slot.
1549 * \param[out] p_slot On success, a pointer to the prepared slot.
1550 * \param[out] p_drv On any return, the driver for the key, if any.
1551 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001552 *
1553 * \retval #PSA_SUCCESS
1554 * The key slot is ready to receive key material.
1555 * \return If this function fails, the key slot is an invalid state.
1556 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001557 */
1558static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001559 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001560 const psa_key_attributes_t *attributes,
1561 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001562 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001563 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001564{
1565 psa_status_t status;
1566 psa_key_slot_t *slot;
1567
Gilles Peskinedf179142019-07-15 22:02:14 +02001568 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001569 *p_drv = NULL;
1570
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001571 status = psa_validate_key_attributes( attributes, p_drv );
1572 if( status != PSA_SUCCESS )
1573 return( status );
1574
Gilles Peskineedbed562019-08-07 18:19:59 +02001575 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001576 if( status != PSA_SUCCESS )
1577 return( status );
1578 slot = *p_slot;
1579
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001580 /* We're storing the declared bit-size of the key. It's up to each
1581 * creation mechanism to verify that this information is correct.
1582 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001583 * an input (generate, device) but not for those where the bit-size
1584 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001585
1586 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001587
Gilles Peskine91e8c332019-08-02 19:19:39 +02001588 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001589 * external-only flags, query `attributes`. Thanks to the check
1590 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001591 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001592 * may have set. */
1593 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001594
Gilles Peskinecbaff462019-07-12 23:46:04 +02001595#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001596 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001597 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001598 * create the key file in internal storage, create the
1599 * key inside the secure element, and update the driver's
1600 * persistent data. Start a transaction that will encompass these
1601 * three actions. */
1602 /* The first thing to do is to find a slot number for the new key.
1603 * We save the slot number in persistent storage as part of the
1604 * transaction data. It will be needed to recover if the power
1605 * fails during the key creation process, to clean up on the secure
1606 * element side after restarting. Obtaining a slot number from the
1607 * secure element driver updates its persistent state, but we do not yet
1608 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001609 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001610 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001611 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001612 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001613 &slot->data.se.slot_number );
1614 if( status != PSA_SUCCESS )
1615 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001616 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001617 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001618 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001619 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001620 status = psa_crypto_save_transaction( );
1621 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001622 {
1623 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001624 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001625 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001626 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001627
1628 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1629 {
1630 /* Key registration only makes sense with a secure element. */
1631 return( PSA_ERROR_INVALID_ARGUMENT );
1632 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001633#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1634
Darryl Green0c6575a2018-11-07 16:05:30 +00001635 return( status );
1636}
Gilles Peskine4747d192019-04-17 15:05:45 +02001637
1638/** Finalize the creation of a key once its key material has been set.
1639 *
1640 * This entails writing the key to persistent storage.
1641 *
1642 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001643 * See the documentation of psa_start_key_creation() for the intended use
1644 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001645 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001646 * \param[in,out] slot Pointer to the slot with key material.
1647 * \param[in] driver The secure element driver for the key,
1648 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001649 *
1650 * \retval #PSA_SUCCESS
1651 * The key was successfully created. The handle is now valid.
1652 * \return If this function fails, the key slot is an invalid state.
1653 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001654 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001655static psa_status_t psa_finish_key_creation(
1656 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001657 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001658{
1659 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001660 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001661 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001662
1663#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001664 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001665 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001666#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1667 if( driver != NULL )
1668 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001669 psa_se_key_data_storage_t data;
1670#if defined(static_assert)
1671 static_assert( sizeof( slot->data.se.slot_number ) ==
1672 sizeof( data.slot_number ),
1673 "Slot number size does not match psa_se_key_data_storage_t" );
1674 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1675 "Bit-size size does not match psa_se_key_data_storage_t" );
1676#endif
1677 memcpy( &data.slot_number, &slot->data.se.slot_number,
1678 sizeof( slot->data.se.slot_number ) );
1679 memcpy( &data.bits, &slot->attr.bits,
1680 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001681 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001682 (uint8_t*) &data,
1683 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001684 }
1685 else
1686#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1687 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001688 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001689 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001690 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001691 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1692 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001693 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001694 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1695 status = psa_internal_export_key( slot,
1696 buffer, buffer_size, &length,
1697 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001698 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001699 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001700 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001701
Gilles Peskinef9168942019-09-12 19:20:29 +02001702 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001703 mbedtls_free( buffer );
1704 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001705 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001706#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1707
Gilles Peskinecbaff462019-07-12 23:46:04 +02001708#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001709 /* Finish the transaction for a key creation. This does not
1710 * happen when registering an existing key. Detect this case
1711 * by checking whether a transaction is in progress (actual
1712 * creation of a key in a secure element requires a transaction,
1713 * but registration doesn't use one). */
1714 if( driver != NULL &&
1715 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716 {
1717 status = psa_save_se_persistent_data( driver );
1718 if( status != PSA_SUCCESS )
1719 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001720 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001721 return( status );
1722 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001723 status = psa_crypto_stop_transaction( );
1724 if( status != PSA_SUCCESS )
1725 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001726 }
1727#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1728
Gilles Peskine4747d192019-04-17 15:05:45 +02001729 return( status );
1730}
1731
1732/** Abort the creation of a key.
1733 *
1734 * You may call this function after calling psa_start_key_creation(),
1735 * or after psa_finish_key_creation() fails. In other circumstances, this
1736 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001737 * See the documentation of psa_start_key_creation() for the intended use
1738 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001739 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001740 * \param[in,out] slot Pointer to the slot with key material.
1741 * \param[in] driver The secure element driver for the key,
1742 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001743 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001744static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001745 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001746{
Gilles Peskine011e4282019-06-26 18:34:38 +02001747 (void) driver;
1748
Gilles Peskine4747d192019-04-17 15:05:45 +02001749 if( slot == NULL )
1750 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001751
1752#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001753 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001754 * element, and the failure happened later (when saving metadata
1755 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001756 * element.
1757 * https://github.com/ARMmbed/mbed-crypto/issues/217
1758 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001759
Gilles Peskined7729582019-08-05 15:55:54 +02001760 /* Abort the ongoing transaction if any (there may not be one if
1761 * the creation process failed before starting one, or if the
1762 * key creation is a registration of a key in a secure element).
1763 * Earlier functions must already have done what it takes to undo any
1764 * partial creation. All that's left is to update the transaction data
1765 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001766 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001767#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1768
Gilles Peskine4747d192019-04-17 15:05:45 +02001769 psa_wipe_key_slot( slot );
1770}
1771
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001772/** Validate optional attributes during key creation.
1773 *
1774 * Some key attributes are optional during key creation. If they are
1775 * specified in the attributes structure, check that they are consistent
1776 * with the data in the slot.
1777 *
1778 * This function should be called near the end of key creation, after
1779 * the slot in memory is fully populated but before saving persistent data.
1780 */
1781static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 const psa_key_slot_t *slot,
1783 const psa_key_attributes_t *attributes )
1784{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001785 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001786 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001787 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001788 return( PSA_ERROR_INVALID_ARGUMENT );
1789 }
1790
1791 if( attributes->domain_parameters_size != 0 )
1792 {
1793#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001794 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001795 {
1796 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001797 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001798 mbedtls_mpi_init( &actual );
1799 mbedtls_mpi_init( &required );
1800 ret = mbedtls_rsa_export( slot->data.rsa,
1801 NULL, NULL, NULL, NULL, &actual );
1802 if( ret != 0 )
1803 goto rsa_exit;
1804 ret = mbedtls_mpi_read_binary( &required,
1805 attributes->domain_parameters,
1806 attributes->domain_parameters_size );
1807 if( ret != 0 )
1808 goto rsa_exit;
1809 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1810 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1811 rsa_exit:
1812 mbedtls_mpi_free( &actual );
1813 mbedtls_mpi_free( &required );
1814 if( ret != 0)
1815 return( mbedtls_to_psa_error( ret ) );
1816 }
1817 else
1818#endif
1819 {
1820 return( PSA_ERROR_INVALID_ARGUMENT );
1821 }
1822 }
1823
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001824 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001825 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001826 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001827 return( PSA_ERROR_INVALID_ARGUMENT );
1828 }
1829
1830 return( PSA_SUCCESS );
1831}
1832
Gilles Peskine4747d192019-04-17 15:05:45 +02001833psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001834 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001835 size_t data_length,
1836 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001837{
1838 psa_status_t status;
1839 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001840 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001841
Gilles Peskine0f84d622019-09-12 19:03:13 +02001842 /* Reject zero-length symmetric keys (including raw data key objects).
1843 * This also rejects any key which might be encoded as an empty string,
1844 * which is never valid. */
1845 if( data_length == 0 )
1846 return( PSA_ERROR_INVALID_ARGUMENT );
1847
Gilles Peskinedf179142019-07-15 22:02:14 +02001848 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1849 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001850 if( status != PSA_SUCCESS )
1851 goto exit;
1852
Gilles Peskine5d309672019-07-12 23:47:28 +02001853#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1854 if( driver != NULL )
1855 {
1856 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001857 /* The driver should set the number of key bits, however in
1858 * case it doesn't, we initialize bits to an invalid value. */
1859 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001860 if( drv->key_management == NULL ||
1861 drv->key_management->p_import == NULL )
1862 {
1863 status = PSA_ERROR_NOT_SUPPORTED;
1864 goto exit;
1865 }
1866 status = drv->key_management->p_import(
1867 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001868 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001869 &bits );
1870 if( status != PSA_SUCCESS )
1871 goto exit;
1872 if( bits > PSA_MAX_KEY_BITS )
1873 {
1874 status = PSA_ERROR_NOT_SUPPORTED;
1875 goto exit;
1876 }
1877 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001878 }
1879 else
1880#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1881 {
1882 status = psa_import_key_into_slot( slot, data, data_length );
1883 if( status != PSA_SUCCESS )
1884 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001885 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001886 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001887 if( status != PSA_SUCCESS )
1888 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001889
Gilles Peskine011e4282019-06-26 18:34:38 +02001890 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001891exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001892 if( status != PSA_SUCCESS )
1893 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001894 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001895 *handle = 0;
1896 }
1897 return( status );
1898}
1899
Gilles Peskined7729582019-08-05 15:55:54 +02001900#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1901psa_status_t mbedtls_psa_register_se_key(
1902 const psa_key_attributes_t *attributes )
1903{
1904 psa_status_t status;
1905 psa_key_slot_t *slot = NULL;
1906 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001907 psa_key_handle_t handle = 0;
1908
1909 /* Leaving attributes unspecified is not currently supported.
1910 * It could make sense to query the key type and size from the
1911 * secure element, but not all secure elements support this
1912 * and the driver HAL doesn't currently support it. */
1913 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1914 return( PSA_ERROR_NOT_SUPPORTED );
1915 if( psa_get_key_bits( attributes ) == 0 )
1916 return( PSA_ERROR_NOT_SUPPORTED );
1917
1918 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1919 &handle, &slot, &driver );
1920 if( status != PSA_SUCCESS )
1921 goto exit;
1922
Gilles Peskined7729582019-08-05 15:55:54 +02001923 status = psa_finish_key_creation( slot, driver );
1924
1925exit:
1926 if( status != PSA_SUCCESS )
1927 {
1928 psa_fail_key_creation( slot, driver );
1929 }
1930 /* Registration doesn't keep the key in RAM. */
1931 psa_close_key( handle );
1932 return( status );
1933}
1934#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1935
Gilles Peskinef603c712019-01-19 13:40:11 +01001936static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001937 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001938{
1939 psa_status_t status;
1940 uint8_t *buffer = NULL;
1941 size_t buffer_size = 0;
1942 size_t length;
1943
Gilles Peskine8e338702019-07-30 20:06:31 +02001944 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001945 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001946 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001947 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001948 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001949 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1950 if( status != PSA_SUCCESS )
1951 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001952 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001953 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001954
1955exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001956 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001957 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001958 return( status );
1959}
1960
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001961psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1962 const psa_key_attributes_t *specified_attributes,
1963 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001964{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001965 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001966 psa_key_slot_t *source_slot = NULL;
1967 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001968 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001969 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001970
Gilles Peskine28f8f302019-07-24 13:30:31 +02001971 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001972 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001973 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001974 goto exit;
1975
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001976 status = psa_validate_optional_attributes( source_slot,
1977 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001978 if( status != PSA_SUCCESS )
1979 goto exit;
1980
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001981 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001982 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001983 if( status != PSA_SUCCESS )
1984 goto exit;
1985
Gilles Peskinedf179142019-07-15 22:02:14 +02001986 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1987 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001988 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001989 if( status != PSA_SUCCESS )
1990 goto exit;
1991
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001992#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1993 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001994 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001995 /* Copying to a secure element is not implemented yet. */
1996 status = PSA_ERROR_NOT_SUPPORTED;
1997 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001998 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001999#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002000
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002001 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002002 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002003 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002004
Gilles Peskine011e4282019-06-26 18:34:38 +02002005 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002006exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002007 if( status != PSA_SUCCESS )
2008 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002009 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002010 *target_handle = 0;
2011 }
2012 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002013}
2014
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002015
2016
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002017/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002018/* Message digests */
2019/****************************************************************/
2020
Gilles Peskineb16841e2019-10-10 20:36:12 +02002021#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002022static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002023{
2024 switch( alg )
2025 {
2026#if defined(MBEDTLS_MD2_C)
2027 case PSA_ALG_MD2:
2028 return( &mbedtls_md2_info );
2029#endif
2030#if defined(MBEDTLS_MD4_C)
2031 case PSA_ALG_MD4:
2032 return( &mbedtls_md4_info );
2033#endif
2034#if defined(MBEDTLS_MD5_C)
2035 case PSA_ALG_MD5:
2036 return( &mbedtls_md5_info );
2037#endif
2038#if defined(MBEDTLS_RIPEMD160_C)
2039 case PSA_ALG_RIPEMD160:
2040 return( &mbedtls_ripemd160_info );
2041#endif
2042#if defined(MBEDTLS_SHA1_C)
2043 case PSA_ALG_SHA_1:
2044 return( &mbedtls_sha1_info );
2045#endif
2046#if defined(MBEDTLS_SHA256_C)
2047 case PSA_ALG_SHA_224:
2048 return( &mbedtls_sha224_info );
2049 case PSA_ALG_SHA_256:
2050 return( &mbedtls_sha256_info );
2051#endif
2052#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002053#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002054 case PSA_ALG_SHA_384:
2055 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002056#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002057 case PSA_ALG_SHA_512:
2058 return( &mbedtls_sha512_info );
2059#endif
2060 default:
2061 return( NULL );
2062 }
2063}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002064#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002065
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002066psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2067{
2068 switch( operation->alg )
2069 {
Gilles Peskine81736312018-06-26 15:04:31 +02002070 case 0:
2071 /* The object has (apparently) been initialized but it is not
2072 * in use. It's ok to call abort on such an object, and there's
2073 * nothing to do. */
2074 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002075#if defined(MBEDTLS_MD2_C)
2076 case PSA_ALG_MD2:
2077 mbedtls_md2_free( &operation->ctx.md2 );
2078 break;
2079#endif
2080#if defined(MBEDTLS_MD4_C)
2081 case PSA_ALG_MD4:
2082 mbedtls_md4_free( &operation->ctx.md4 );
2083 break;
2084#endif
2085#if defined(MBEDTLS_MD5_C)
2086 case PSA_ALG_MD5:
2087 mbedtls_md5_free( &operation->ctx.md5 );
2088 break;
2089#endif
2090#if defined(MBEDTLS_RIPEMD160_C)
2091 case PSA_ALG_RIPEMD160:
2092 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2093 break;
2094#endif
2095#if defined(MBEDTLS_SHA1_C)
2096 case PSA_ALG_SHA_1:
2097 mbedtls_sha1_free( &operation->ctx.sha1 );
2098 break;
2099#endif
2100#if defined(MBEDTLS_SHA256_C)
2101 case PSA_ALG_SHA_224:
2102 case PSA_ALG_SHA_256:
2103 mbedtls_sha256_free( &operation->ctx.sha256 );
2104 break;
2105#endif
2106#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002107#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002108 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002109#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002110 case PSA_ALG_SHA_512:
2111 mbedtls_sha512_free( &operation->ctx.sha512 );
2112 break;
2113#endif
2114 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002115 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 }
2117 operation->alg = 0;
2118 return( PSA_SUCCESS );
2119}
2120
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002121psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002122 psa_algorithm_t alg )
2123{
Janos Follath24eed8d2019-11-22 13:21:35 +00002124 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002125
2126 /* A context must be freshly initialized before it can be set up. */
2127 if( operation->alg != 0 )
2128 {
2129 return( PSA_ERROR_BAD_STATE );
2130 }
2131
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002132 switch( alg )
2133 {
2134#if defined(MBEDTLS_MD2_C)
2135 case PSA_ALG_MD2:
2136 mbedtls_md2_init( &operation->ctx.md2 );
2137 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2138 break;
2139#endif
2140#if defined(MBEDTLS_MD4_C)
2141 case PSA_ALG_MD4:
2142 mbedtls_md4_init( &operation->ctx.md4 );
2143 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2144 break;
2145#endif
2146#if defined(MBEDTLS_MD5_C)
2147 case PSA_ALG_MD5:
2148 mbedtls_md5_init( &operation->ctx.md5 );
2149 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2150 break;
2151#endif
2152#if defined(MBEDTLS_RIPEMD160_C)
2153 case PSA_ALG_RIPEMD160:
2154 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2155 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2156 break;
2157#endif
2158#if defined(MBEDTLS_SHA1_C)
2159 case PSA_ALG_SHA_1:
2160 mbedtls_sha1_init( &operation->ctx.sha1 );
2161 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2162 break;
2163#endif
2164#if defined(MBEDTLS_SHA256_C)
2165 case PSA_ALG_SHA_224:
2166 mbedtls_sha256_init( &operation->ctx.sha256 );
2167 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2168 break;
2169 case PSA_ALG_SHA_256:
2170 mbedtls_sha256_init( &operation->ctx.sha256 );
2171 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2172 break;
2173#endif
2174#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002175#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002176 case PSA_ALG_SHA_384:
2177 mbedtls_sha512_init( &operation->ctx.sha512 );
2178 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2179 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002180#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002181 case PSA_ALG_SHA_512:
2182 mbedtls_sha512_init( &operation->ctx.sha512 );
2183 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2184 break;
2185#endif
2186 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002187 return( PSA_ALG_IS_HASH( alg ) ?
2188 PSA_ERROR_NOT_SUPPORTED :
2189 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002190 }
2191 if( ret == 0 )
2192 operation->alg = alg;
2193 else
2194 psa_hash_abort( operation );
2195 return( mbedtls_to_psa_error( ret ) );
2196}
2197
2198psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2199 const uint8_t *input,
2200 size_t input_length )
2201{
Janos Follath24eed8d2019-11-22 13:21:35 +00002202 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002203
2204 /* Don't require hash implementations to behave correctly on a
2205 * zero-length input, which may have an invalid pointer. */
2206 if( input_length == 0 )
2207 return( PSA_SUCCESS );
2208
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002209 switch( operation->alg )
2210 {
2211#if defined(MBEDTLS_MD2_C)
2212 case PSA_ALG_MD2:
2213 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2214 input, input_length );
2215 break;
2216#endif
2217#if defined(MBEDTLS_MD4_C)
2218 case PSA_ALG_MD4:
2219 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2220 input, input_length );
2221 break;
2222#endif
2223#if defined(MBEDTLS_MD5_C)
2224 case PSA_ALG_MD5:
2225 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2226 input, input_length );
2227 break;
2228#endif
2229#if defined(MBEDTLS_RIPEMD160_C)
2230 case PSA_ALG_RIPEMD160:
2231 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_SHA1_C)
2236 case PSA_ALG_SHA_1:
2237 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2238 input, input_length );
2239 break;
2240#endif
2241#if defined(MBEDTLS_SHA256_C)
2242 case PSA_ALG_SHA_224:
2243 case PSA_ALG_SHA_256:
2244 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2245 input, input_length );
2246 break;
2247#endif
2248#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002249#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002250 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002251#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002252 case PSA_ALG_SHA_512:
2253 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2254 input, input_length );
2255 break;
2256#endif
2257 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002258 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002259 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002260
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261 if( ret != 0 )
2262 psa_hash_abort( operation );
2263 return( mbedtls_to_psa_error( ret ) );
2264}
2265
2266psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2267 uint8_t *hash,
2268 size_t hash_size,
2269 size_t *hash_length )
2270{
itayzafrir40835d42018-08-02 13:14:17 +03002271 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002272 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002273 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002274
2275 /* Fill the output buffer with something that isn't a valid hash
2276 * (barring an attack on the hash and deliberately-crafted input),
2277 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002278 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002279 /* If hash_size is 0 then hash may be NULL and then the
2280 * call to memset would have undefined behavior. */
2281 if( hash_size != 0 )
2282 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002283
2284 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002285 {
2286 status = PSA_ERROR_BUFFER_TOO_SMALL;
2287 goto exit;
2288 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002289
2290 switch( operation->alg )
2291 {
2292#if defined(MBEDTLS_MD2_C)
2293 case PSA_ALG_MD2:
2294 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2295 break;
2296#endif
2297#if defined(MBEDTLS_MD4_C)
2298 case PSA_ALG_MD4:
2299 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2300 break;
2301#endif
2302#if defined(MBEDTLS_MD5_C)
2303 case PSA_ALG_MD5:
2304 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2305 break;
2306#endif
2307#if defined(MBEDTLS_RIPEMD160_C)
2308 case PSA_ALG_RIPEMD160:
2309 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2310 break;
2311#endif
2312#if defined(MBEDTLS_SHA1_C)
2313 case PSA_ALG_SHA_1:
2314 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2315 break;
2316#endif
2317#if defined(MBEDTLS_SHA256_C)
2318 case PSA_ALG_SHA_224:
2319 case PSA_ALG_SHA_256:
2320 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2321 break;
2322#endif
2323#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002324#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002326#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 case PSA_ALG_SHA_512:
2328 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2329 break;
2330#endif
2331 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002332 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 }
itayzafrir40835d42018-08-02 13:14:17 +03002334 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335
itayzafrir40835d42018-08-02 13:14:17 +03002336exit:
2337 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002338 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002339 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002340 return( psa_hash_abort( operation ) );
2341 }
2342 else
2343 {
2344 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002345 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 }
2347}
2348
Gilles Peskine2d277862018-06-18 15:41:12 +02002349psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2350 const uint8_t *hash,
2351 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002352{
2353 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2354 size_t actual_hash_length;
2355 psa_status_t status = psa_hash_finish( operation,
2356 actual_hash, sizeof( actual_hash ),
2357 &actual_hash_length );
2358 if( status != PSA_SUCCESS )
2359 return( status );
2360 if( actual_hash_length != hash_length )
2361 return( PSA_ERROR_INVALID_SIGNATURE );
2362 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2363 return( PSA_ERROR_INVALID_SIGNATURE );
2364 return( PSA_SUCCESS );
2365}
2366
Gilles Peskine0a749c82019-11-28 19:33:58 +01002367psa_status_t psa_hash_compute( psa_algorithm_t alg,
2368 const uint8_t *input, size_t input_length,
2369 uint8_t *hash, size_t hash_size,
2370 size_t *hash_length )
2371{
2372 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2373 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2374
2375 *hash_length = hash_size;
2376 status = psa_hash_setup( &operation, alg );
2377 if( status != PSA_SUCCESS )
2378 goto exit;
2379 status = psa_hash_update( &operation, input, input_length );
2380 if( status != PSA_SUCCESS )
2381 goto exit;
2382 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2383 if( status != PSA_SUCCESS )
2384 goto exit;
2385
2386exit:
2387 if( status == PSA_SUCCESS )
2388 status = psa_hash_abort( &operation );
2389 else
2390 psa_hash_abort( &operation );
2391 return( status );
2392}
2393
2394psa_status_t psa_hash_compare( psa_algorithm_t alg,
2395 const uint8_t *input, size_t input_length,
2396 const uint8_t *hash, size_t hash_length )
2397{
2398 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2399 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2400
2401 status = psa_hash_setup( &operation, alg );
2402 if( status != PSA_SUCCESS )
2403 goto exit;
2404 status = psa_hash_update( &operation, input, input_length );
2405 if( status != PSA_SUCCESS )
2406 goto exit;
2407 status = psa_hash_verify( &operation, hash, hash_length );
2408 if( status != PSA_SUCCESS )
2409 goto exit;
2410
2411exit:
2412 if( status == PSA_SUCCESS )
2413 status = psa_hash_abort( &operation );
2414 else
2415 psa_hash_abort( &operation );
2416 return( status );
2417}
2418
Gilles Peskineeb35d782019-01-22 17:56:16 +01002419psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2420 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002421{
2422 if( target_operation->alg != 0 )
2423 return( PSA_ERROR_BAD_STATE );
2424
2425 switch( source_operation->alg )
2426 {
2427 case 0:
2428 return( PSA_ERROR_BAD_STATE );
2429#if defined(MBEDTLS_MD2_C)
2430 case PSA_ALG_MD2:
2431 mbedtls_md2_clone( &target_operation->ctx.md2,
2432 &source_operation->ctx.md2 );
2433 break;
2434#endif
2435#if defined(MBEDTLS_MD4_C)
2436 case PSA_ALG_MD4:
2437 mbedtls_md4_clone( &target_operation->ctx.md4,
2438 &source_operation->ctx.md4 );
2439 break;
2440#endif
2441#if defined(MBEDTLS_MD5_C)
2442 case PSA_ALG_MD5:
2443 mbedtls_md5_clone( &target_operation->ctx.md5,
2444 &source_operation->ctx.md5 );
2445 break;
2446#endif
2447#if defined(MBEDTLS_RIPEMD160_C)
2448 case PSA_ALG_RIPEMD160:
2449 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2450 &source_operation->ctx.ripemd160 );
2451 break;
2452#endif
2453#if defined(MBEDTLS_SHA1_C)
2454 case PSA_ALG_SHA_1:
2455 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2456 &source_operation->ctx.sha1 );
2457 break;
2458#endif
2459#if defined(MBEDTLS_SHA256_C)
2460 case PSA_ALG_SHA_224:
2461 case PSA_ALG_SHA_256:
2462 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2463 &source_operation->ctx.sha256 );
2464 break;
2465#endif
2466#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002467#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002468 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002469#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002470 case PSA_ALG_SHA_512:
2471 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2472 &source_operation->ctx.sha512 );
2473 break;
2474#endif
2475 default:
2476 return( PSA_ERROR_NOT_SUPPORTED );
2477 }
2478
2479 target_operation->alg = source_operation->alg;
2480 return( PSA_SUCCESS );
2481}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002482
2483
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002484/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002485/* MAC */
2486/****************************************************************/
2487
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002488static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002489 psa_algorithm_t alg,
2490 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002491 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002492 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002494 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002495 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002496
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002497 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002498 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002499
Gilles Peskine8c9def32018-02-08 10:02:12 +01002500 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2501 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002502 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002503 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002504 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002505 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002506 mode = MBEDTLS_MODE_STREAM;
2507 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 case PSA_ALG_CTR:
2509 mode = MBEDTLS_MODE_CTR;
2510 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002511 case PSA_ALG_CFB:
2512 mode = MBEDTLS_MODE_CFB;
2513 break;
2514 case PSA_ALG_OFB:
2515 mode = MBEDTLS_MODE_OFB;
2516 break;
2517 case PSA_ALG_CBC_NO_PADDING:
2518 mode = MBEDTLS_MODE_CBC;
2519 break;
2520 case PSA_ALG_CBC_PKCS7:
2521 mode = MBEDTLS_MODE_CBC;
2522 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002523 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002524 mode = MBEDTLS_MODE_CCM;
2525 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002526 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002527 mode = MBEDTLS_MODE_GCM;
2528 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002529 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2530 mode = MBEDTLS_MODE_CHACHAPOLY;
2531 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 default:
2533 return( NULL );
2534 }
2535 }
2536 else if( alg == PSA_ALG_CMAC )
2537 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 else
2539 return( NULL );
2540
2541 switch( key_type )
2542 {
2543 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002544 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 break;
2546 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002547 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2548 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002549 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002550 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002552 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002553 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2554 * but two-key Triple-DES is functionally three-key Triple-DES
2555 * with K1=K3, so that's how we present it to mbedtls. */
2556 if( key_bits == 128 )
2557 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002558 break;
2559 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002560 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002561 break;
2562 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002563 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002564 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002565 case PSA_KEY_TYPE_CHACHA20:
2566 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2567 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002568 default:
2569 return( NULL );
2570 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002571 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002572 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002573
Jaeden Amero23bbb752018-06-26 14:16:54 +01002574 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2575 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576}
2577
Gilles Peskinea05219c2018-11-16 16:02:56 +01002578#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002579static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002580{
Gilles Peskine2d277862018-06-18 15:41:12 +02002581 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002582 {
2583 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002584 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002585 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002586 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002587 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002588 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002589 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002590 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002591 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002592 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002593 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002594 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002595 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002596 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002597 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002598 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002600 return( 128 );
2601 default:
2602 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 }
2604}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002605#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002606
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607/* Initialize the MAC operation structure. Once this function has been
2608 * called, psa_mac_abort can run and will do the right thing. */
2609static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2610 psa_algorithm_t alg )
2611{
2612 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2613
2614 operation->alg = alg;
2615 operation->key_set = 0;
2616 operation->iv_set = 0;
2617 operation->iv_required = 0;
2618 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002619 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002620
2621#if defined(MBEDTLS_CMAC_C)
2622 if( alg == PSA_ALG_CMAC )
2623 {
2624 operation->iv_required = 0;
2625 mbedtls_cipher_init( &operation->ctx.cmac );
2626 status = PSA_SUCCESS;
2627 }
2628 else
2629#endif /* MBEDTLS_CMAC_C */
2630#if defined(MBEDTLS_MD_C)
2631 if( PSA_ALG_IS_HMAC( operation->alg ) )
2632 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002633 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2634 operation->ctx.hmac.hash_ctx.alg = 0;
2635 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002636 }
2637 else
2638#endif /* MBEDTLS_MD_C */
2639 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002640 if( ! PSA_ALG_IS_MAC( alg ) )
2641 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002642 }
2643
2644 if( status != PSA_SUCCESS )
2645 memset( operation, 0, sizeof( *operation ) );
2646 return( status );
2647}
2648
Gilles Peskine01126fa2018-07-12 17:04:55 +02002649#if defined(MBEDTLS_MD_C)
2650static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2651{
Gilles Peskine3f108122018-12-07 18:14:53 +01002652 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002653 return( psa_hash_abort( &hmac->hash_ctx ) );
2654}
2655#endif /* MBEDTLS_MD_C */
2656
Gilles Peskine8c9def32018-02-08 10:02:12 +01002657psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2658{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002659 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002661 /* The object has (apparently) been initialized but it is not
2662 * in use. It's ok to call abort on such an object, and there's
2663 * nothing to do. */
2664 return( PSA_SUCCESS );
2665 }
2666 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002668 if( operation->alg == PSA_ALG_CMAC )
2669 {
2670 mbedtls_cipher_free( &operation->ctx.cmac );
2671 }
2672 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002673#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002675 if( PSA_ALG_IS_HMAC( operation->alg ) )
2676 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002677 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002678 }
2679 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002680#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002681 {
2682 /* Sanity check (shouldn't happen: operation->alg should
2683 * always have been initialized to a valid value). */
2684 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002685 }
Moran Peker41deec42018-04-04 15:43:05 +03002686
Gilles Peskine8c9def32018-02-08 10:02:12 +01002687 operation->alg = 0;
2688 operation->key_set = 0;
2689 operation->iv_set = 0;
2690 operation->iv_required = 0;
2691 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002692 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002693
Gilles Peskine8c9def32018-02-08 10:02:12 +01002694 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002695
2696bad_state:
2697 /* If abort is called on an uninitialized object, we can't trust
2698 * anything. Wipe the object in case it contains confidential data.
2699 * This may result in a memory leak if a pointer gets overwritten,
2700 * but it's too late to do anything about this. */
2701 memset( operation, 0, sizeof( *operation ) );
2702 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002703}
2704
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002705#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002706static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002707 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002708 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002709 const mbedtls_cipher_info_t *cipher_info )
2710{
Janos Follath24eed8d2019-11-22 13:21:35 +00002711 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002712
2713 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002714
2715 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2716 if( ret != 0 )
2717 return( ret );
2718
2719 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2720 slot->data.raw.data,
2721 key_bits );
2722 return( ret );
2723}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002724#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002725
Gilles Peskine248051a2018-06-20 16:09:38 +02002726#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002727static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2728 const uint8_t *key,
2729 size_t key_length,
2730 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002731{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002732 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002734 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002735 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002736 psa_status_t status;
2737
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002738 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2739 * overflow below. This should never trigger if the hash algorithm
2740 * is implemented correctly. */
2741 /* The size checks against the ipad and opad buffers cannot be written
2742 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2743 * because that triggers -Wlogical-op on GCC 7.3. */
2744 if( block_size > sizeof( ipad ) )
2745 return( PSA_ERROR_NOT_SUPPORTED );
2746 if( block_size > sizeof( hmac->opad ) )
2747 return( PSA_ERROR_NOT_SUPPORTED );
2748 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002749 return( PSA_ERROR_NOT_SUPPORTED );
2750
Gilles Peskined223b522018-06-11 18:12:58 +02002751 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002752 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002753 status = psa_hash_compute( hash_alg, key, key_length,
2754 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002755 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002756 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757 }
Gilles Peskine96889972018-07-12 17:07:03 +02002758 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2759 * but it is permitted. It is common when HMAC is used in HKDF, for
2760 * example. Don't call `memcpy` in the 0-length because `key` could be
2761 * an invalid pointer which would make the behavior undefined. */
2762 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002763 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002764
Gilles Peskined223b522018-06-11 18:12:58 +02002765 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2766 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002767 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002768 ipad[i] ^= 0x36;
2769 memset( ipad + key_length, 0x36, block_size - key_length );
2770
2771 /* Copy the key material from ipad to opad, flipping the requisite bits,
2772 * and filling the rest of opad with the requisite constant. */
2773 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002774 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2775 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002776
Gilles Peskine01126fa2018-07-12 17:04:55 +02002777 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002778 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002779 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002780
Gilles Peskine01126fa2018-07-12 17:04:55 +02002781 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002782
2783cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002784 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002785
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 return( status );
2787}
Gilles Peskine248051a2018-06-20 16:09:38 +02002788#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002789
Gilles Peskine89167cb2018-07-08 20:12:23 +02002790static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002791 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002792 psa_algorithm_t alg,
2793 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002794{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002796 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002797 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002798 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002799 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002800 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002801 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002803 /* A context must be freshly initialized before it can be set up. */
2804 if( operation->alg != 0 )
2805 {
2806 return( PSA_ERROR_BAD_STATE );
2807 }
2808
Gilles Peskined911eb72018-08-14 15:18:45 +02002809 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002810 if( status != PSA_SUCCESS )
2811 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002812 if( is_sign )
2813 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002814
Gilles Peskine28f8f302019-07-24 13:30:31 +02002815 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002816 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002817 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002818 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002819
Gilles Peskine8c9def32018-02-08 10:02:12 +01002820#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002821 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002822 {
2823 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002824 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002825 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002826 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002827 if( cipher_info == NULL )
2828 {
2829 status = PSA_ERROR_NOT_SUPPORTED;
2830 goto exit;
2831 }
2832 operation->mac_size = cipher_info->block_size;
2833 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2834 status = mbedtls_to_psa_error( ret );
2835 }
2836 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002837#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002838#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002839 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002840 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002841 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002842 if( hash_alg == 0 )
2843 {
2844 status = PSA_ERROR_NOT_SUPPORTED;
2845 goto exit;
2846 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002847
2848 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2849 /* Sanity check. This shouldn't fail on a valid configuration. */
2850 if( operation->mac_size == 0 ||
2851 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2852 {
2853 status = PSA_ERROR_NOT_SUPPORTED;
2854 goto exit;
2855 }
2856
Gilles Peskine8e338702019-07-30 20:06:31 +02002857 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002858 {
2859 status = PSA_ERROR_INVALID_ARGUMENT;
2860 goto exit;
2861 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002862
Gilles Peskine01126fa2018-07-12 17:04:55 +02002863 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2864 slot->data.raw.data,
2865 slot->data.raw.bytes,
2866 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002867 }
2868 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002869#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002870 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002871 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002872 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002873 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002874
Gilles Peskined911eb72018-08-14 15:18:45 +02002875 if( truncated == 0 )
2876 {
2877 /* The "normal" case: untruncated algorithm. Nothing to do. */
2878 }
2879 else if( truncated < 4 )
2880 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002881 /* A very short MAC is too short for security since it can be
2882 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2883 * so we make this our minimum, even though 32 bits is still
2884 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002885 status = PSA_ERROR_NOT_SUPPORTED;
2886 }
2887 else if( truncated > operation->mac_size )
2888 {
2889 /* It's impossible to "truncate" to a larger length. */
2890 status = PSA_ERROR_INVALID_ARGUMENT;
2891 }
2892 else
2893 operation->mac_size = truncated;
2894
Gilles Peskinefbfac682018-07-08 20:51:54 +02002895exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002896 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002897 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002898 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002899 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002900 else
2901 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002902 operation->key_set = 1;
2903 }
2904 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905}
2906
Gilles Peskine89167cb2018-07-08 20:12:23 +02002907psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002908 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002909 psa_algorithm_t alg )
2910{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002911 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912}
2913
2914psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002915 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002916 psa_algorithm_t alg )
2917{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002918 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002919}
2920
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2922 const uint8_t *input,
2923 size_t input_length )
2924{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002925 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002926 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002927 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002928 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002929 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002930 operation->has_input = 1;
2931
Gilles Peskine8c9def32018-02-08 10:02:12 +01002932#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002934 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002935 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2936 input, input_length );
2937 status = mbedtls_to_psa_error( ret );
2938 }
2939 else
2940#endif /* MBEDTLS_CMAC_C */
2941#if defined(MBEDTLS_MD_C)
2942 if( PSA_ALG_IS_HMAC( operation->alg ) )
2943 {
2944 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2945 input_length );
2946 }
2947 else
2948#endif /* MBEDTLS_MD_C */
2949 {
2950 /* This shouldn't happen if `operation` was initialized by
2951 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002952 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002953 }
2954
Gilles Peskinefbfac682018-07-08 20:51:54 +02002955 if( status != PSA_SUCCESS )
2956 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002957 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002958}
2959
Gilles Peskine01126fa2018-07-12 17:04:55 +02002960#if defined(MBEDTLS_MD_C)
2961static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2962 uint8_t *mac,
2963 size_t mac_size )
2964{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002965 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002966 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2967 size_t hash_size = 0;
2968 size_t block_size = psa_get_hash_block_size( hash_alg );
2969 psa_status_t status;
2970
Gilles Peskine01126fa2018-07-12 17:04:55 +02002971 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2972 if( status != PSA_SUCCESS )
2973 return( status );
2974 /* From here on, tmp needs to be wiped. */
2975
2976 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2977 if( status != PSA_SUCCESS )
2978 goto exit;
2979
2980 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2981 if( status != PSA_SUCCESS )
2982 goto exit;
2983
2984 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2985 if( status != PSA_SUCCESS )
2986 goto exit;
2987
Gilles Peskined911eb72018-08-14 15:18:45 +02002988 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2989 if( status != PSA_SUCCESS )
2990 goto exit;
2991
2992 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002993
2994exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002995 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002996 return( status );
2997}
2998#endif /* MBEDTLS_MD_C */
2999
mohammad16036df908f2018-04-02 08:34:15 -07003000static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003001 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003002 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003003{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003004 if( ! operation->key_set )
3005 return( PSA_ERROR_BAD_STATE );
3006 if( operation->iv_required && ! operation->iv_set )
3007 return( PSA_ERROR_BAD_STATE );
3008
Gilles Peskine8c9def32018-02-08 10:02:12 +01003009 if( mac_size < operation->mac_size )
3010 return( PSA_ERROR_BUFFER_TOO_SMALL );
3011
Gilles Peskine8c9def32018-02-08 10:02:12 +01003012#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003013 if( operation->alg == PSA_ALG_CMAC )
3014 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003015 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3016 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3017 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003018 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003019 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003020 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003021 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 else
3023#endif /* MBEDTLS_CMAC_C */
3024#if defined(MBEDTLS_MD_C)
3025 if( PSA_ALG_IS_HMAC( operation->alg ) )
3026 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003027 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003028 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003029 }
3030 else
3031#endif /* MBEDTLS_MD_C */
3032 {
3033 /* This shouldn't happen if `operation` was initialized by
3034 * a setup function. */
3035 return( PSA_ERROR_BAD_STATE );
3036 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003037}
3038
Gilles Peskineacd4be32018-07-08 19:56:25 +02003039psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3040 uint8_t *mac,
3041 size_t mac_size,
3042 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003043{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003044 psa_status_t status;
3045
Jaeden Amero252ef282019-02-15 14:05:35 +00003046 if( operation->alg == 0 )
3047 {
3048 return( PSA_ERROR_BAD_STATE );
3049 }
3050
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003051 /* Fill the output buffer with something that isn't a valid mac
3052 * (barring an attack on the mac and deliberately-crafted input),
3053 * in case the caller doesn't check the return status properly. */
3054 *mac_length = mac_size;
3055 /* If mac_size is 0 then mac may be NULL and then the
3056 * call to memset would have undefined behavior. */
3057 if( mac_size != 0 )
3058 memset( mac, '!', mac_size );
3059
Gilles Peskine89167cb2018-07-08 20:12:23 +02003060 if( ! operation->is_sign )
3061 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003062 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003063 }
mohammad16036df908f2018-04-02 08:34:15 -07003064
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003065 status = psa_mac_finish_internal( operation, mac, mac_size );
3066
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003067 if( status == PSA_SUCCESS )
3068 {
3069 status = psa_mac_abort( operation );
3070 if( status == PSA_SUCCESS )
3071 *mac_length = operation->mac_size;
3072 else
3073 memset( mac, '!', mac_size );
3074 }
3075 else
3076 psa_mac_abort( operation );
3077 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003078}
3079
Gilles Peskineacd4be32018-07-08 19:56:25 +02003080psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3081 const uint8_t *mac,
3082 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003083{
Gilles Peskine828ed142018-06-18 23:25:51 +02003084 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003085 psa_status_t status;
3086
Jaeden Amero252ef282019-02-15 14:05:35 +00003087 if( operation->alg == 0 )
3088 {
3089 return( PSA_ERROR_BAD_STATE );
3090 }
3091
Gilles Peskine89167cb2018-07-08 20:12:23 +02003092 if( operation->is_sign )
3093 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003094 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003095 }
3096 if( operation->mac_size != mac_length )
3097 {
3098 status = PSA_ERROR_INVALID_SIGNATURE;
3099 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003100 }
mohammad16036df908f2018-04-02 08:34:15 -07003101
3102 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003103 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003104 if( status != PSA_SUCCESS )
3105 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003106
3107 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3108 status = PSA_ERROR_INVALID_SIGNATURE;
3109
3110cleanup:
3111 if( status == PSA_SUCCESS )
3112 status = psa_mac_abort( operation );
3113 else
3114 psa_mac_abort( operation );
3115
Gilles Peskine3f108122018-12-07 18:14:53 +01003116 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003117
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003118 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003119}
3120
3121
Gilles Peskine20035e32018-02-03 22:44:14 +01003122
Gilles Peskine20035e32018-02-03 22:44:14 +01003123/****************************************************************/
3124/* Asymmetric cryptography */
3125/****************************************************************/
3126
Gilles Peskine2b450e32018-06-27 15:42:46 +02003127#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003128/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003129 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003130static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3131 size_t hash_length,
3132 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003133{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003134 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003135 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003136 *md_alg = mbedtls_md_get_type( md_info );
3137
3138 /* The Mbed TLS RSA module uses an unsigned int for hash length
3139 * parameters. Validate that it fits so that we don't risk an
3140 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003141#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003142 if( hash_length > UINT_MAX )
3143 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003144#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003145
3146#if defined(MBEDTLS_PKCS1_V15)
3147 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3148 * must be correct. */
3149 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3150 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003151 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003152 if( md_info == NULL )
3153 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003154 if( mbedtls_md_get_size( md_info ) != hash_length )
3155 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003156 }
3157#endif /* MBEDTLS_PKCS1_V15 */
3158
3159#if defined(MBEDTLS_PKCS1_V21)
3160 /* PSS requires a hash internally. */
3161 if( PSA_ALG_IS_RSA_PSS( alg ) )
3162 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003163 if( md_info == NULL )
3164 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003165 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003166#endif /* MBEDTLS_PKCS1_V21 */
3167
Gilles Peskine61b91d42018-06-08 16:09:36 +02003168 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003169}
3170
Gilles Peskine2b450e32018-06-27 15:42:46 +02003171static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3172 psa_algorithm_t alg,
3173 const uint8_t *hash,
3174 size_t hash_length,
3175 uint8_t *signature,
3176 size_t signature_size,
3177 size_t *signature_length )
3178{
3179 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003180 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003181 mbedtls_md_type_t md_alg;
3182
3183 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3184 if( status != PSA_SUCCESS )
3185 return( status );
3186
Gilles Peskine630a18a2018-06-29 17:49:35 +02003187 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003188 return( PSA_ERROR_BUFFER_TOO_SMALL );
3189
3190#if defined(MBEDTLS_PKCS1_V15)
3191 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3192 {
3193 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3194 MBEDTLS_MD_NONE );
3195 ret = mbedtls_rsa_pkcs1_sign( rsa,
3196 mbedtls_ctr_drbg_random,
3197 &global_data.ctr_drbg,
3198 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003199 md_alg,
3200 (unsigned int) hash_length,
3201 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003202 signature );
3203 }
3204 else
3205#endif /* MBEDTLS_PKCS1_V15 */
3206#if defined(MBEDTLS_PKCS1_V21)
3207 if( PSA_ALG_IS_RSA_PSS( alg ) )
3208 {
3209 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3210 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3211 mbedtls_ctr_drbg_random,
3212 &global_data.ctr_drbg,
3213 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003214 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003215 (unsigned int) hash_length,
3216 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003217 signature );
3218 }
3219 else
3220#endif /* MBEDTLS_PKCS1_V21 */
3221 {
3222 return( PSA_ERROR_INVALID_ARGUMENT );
3223 }
3224
3225 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003226 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003227 return( mbedtls_to_psa_error( ret ) );
3228}
3229
3230static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3231 psa_algorithm_t alg,
3232 const uint8_t *hash,
3233 size_t hash_length,
3234 const uint8_t *signature,
3235 size_t signature_length )
3236{
3237 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003238 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003239 mbedtls_md_type_t md_alg;
3240
3241 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3242 if( status != PSA_SUCCESS )
3243 return( status );
3244
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003245 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3246 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003247
3248#if defined(MBEDTLS_PKCS1_V15)
3249 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3250 {
3251 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3252 MBEDTLS_MD_NONE );
3253 ret = mbedtls_rsa_pkcs1_verify( rsa,
3254 mbedtls_ctr_drbg_random,
3255 &global_data.ctr_drbg,
3256 MBEDTLS_RSA_PUBLIC,
3257 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003258 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003259 hash,
3260 signature );
3261 }
3262 else
3263#endif /* MBEDTLS_PKCS1_V15 */
3264#if defined(MBEDTLS_PKCS1_V21)
3265 if( PSA_ALG_IS_RSA_PSS( alg ) )
3266 {
3267 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3268 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3269 mbedtls_ctr_drbg_random,
3270 &global_data.ctr_drbg,
3271 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003272 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003273 (unsigned int) hash_length,
3274 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003275 signature );
3276 }
3277 else
3278#endif /* MBEDTLS_PKCS1_V21 */
3279 {
3280 return( PSA_ERROR_INVALID_ARGUMENT );
3281 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003282
3283 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3284 * the rest of the signature is invalid". This has little use in
3285 * practice and PSA doesn't report this distinction. */
3286 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3287 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003288 return( mbedtls_to_psa_error( ret ) );
3289}
3290#endif /* MBEDTLS_RSA_C */
3291
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003292#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003293/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3294 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3295 * (even though these functions don't modify it). */
3296static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3297 psa_algorithm_t alg,
3298 const uint8_t *hash,
3299 size_t hash_length,
3300 uint8_t *signature,
3301 size_t signature_size,
3302 size_t *signature_length )
3303{
Janos Follath24eed8d2019-11-22 13:21:35 +00003304 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003305 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003306 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003307 mbedtls_mpi_init( &r );
3308 mbedtls_mpi_init( &s );
3309
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003310 if( signature_size < 2 * curve_bytes )
3311 {
3312 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3313 goto cleanup;
3314 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003315
Gilles Peskinea05219c2018-11-16 16:02:56 +01003316#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003317 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3318 {
3319 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3320 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3321 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003322 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3323 &ecp->d, hash,
3324 hash_length, md_alg,
3325 mbedtls_ctr_drbg_random,
3326 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003327 }
3328 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003329#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003330 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003331 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003332 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3333 hash, hash_length,
3334 mbedtls_ctr_drbg_random,
3335 &global_data.ctr_drbg ) );
3336 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003337
3338 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3339 signature,
3340 curve_bytes ) );
3341 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3342 signature + curve_bytes,
3343 curve_bytes ) );
3344
3345cleanup:
3346 mbedtls_mpi_free( &r );
3347 mbedtls_mpi_free( &s );
3348 if( ret == 0 )
3349 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003350 return( mbedtls_to_psa_error( ret ) );
3351}
3352
3353static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3354 const uint8_t *hash,
3355 size_t hash_length,
3356 const uint8_t *signature,
3357 size_t signature_length )
3358{
Janos Follath24eed8d2019-11-22 13:21:35 +00003359 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003360 mbedtls_mpi r, s;
3361 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3362 mbedtls_mpi_init( &r );
3363 mbedtls_mpi_init( &s );
3364
3365 if( signature_length != 2 * curve_bytes )
3366 return( PSA_ERROR_INVALID_SIGNATURE );
3367
3368 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3369 signature,
3370 curve_bytes ) );
3371 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3372 signature + curve_bytes,
3373 curve_bytes ) );
3374
3375 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3376 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003377
3378cleanup:
3379 mbedtls_mpi_free( &r );
3380 mbedtls_mpi_free( &s );
3381 return( mbedtls_to_psa_error( ret ) );
3382}
3383#endif /* MBEDTLS_ECDSA_C */
3384
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003385psa_status_t psa_sign_hash( psa_key_handle_t handle,
3386 psa_algorithm_t alg,
3387 const uint8_t *hash,
3388 size_t hash_length,
3389 uint8_t *signature,
3390 size_t signature_size,
3391 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003392{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003393 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003394 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003395#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3396 const psa_drv_se_t *drv;
3397 psa_drv_se_context_t *drv_context;
3398#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003399
3400 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003401 /* Immediately reject a zero-length signature buffer. This guarantees
3402 * that signature must be a valid pointer. (On the other hand, the hash
3403 * buffer can in principle be empty since it doesn't actually have
3404 * to be a hash.) */
3405 if( signature_size == 0 )
3406 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003407
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003408 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003409 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003410 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003411 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003412 {
3413 status = PSA_ERROR_INVALID_ARGUMENT;
3414 goto exit;
3415 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003416
Gilles Peskineedc64242019-08-07 21:05:07 +02003417#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3418 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3419 {
3420 if( drv->asymmetric == NULL ||
3421 drv->asymmetric->p_sign == NULL )
3422 {
3423 status = PSA_ERROR_NOT_SUPPORTED;
3424 goto exit;
3425 }
3426 status = drv->asymmetric->p_sign( drv_context,
3427 slot->data.se.slot_number,
3428 alg,
3429 hash, hash_length,
3430 signature, signature_size,
3431 signature_length );
3432 }
3433 else
3434#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003435#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003436 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003437 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003438 status = psa_rsa_sign( slot->data.rsa,
3439 alg,
3440 hash, hash_length,
3441 signature, signature_size,
3442 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003443 }
3444 else
3445#endif /* defined(MBEDTLS_RSA_C) */
3446#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003447 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003448 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003449#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003450 if(
3451#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3452 PSA_ALG_IS_ECDSA( alg )
3453#else
3454 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3455#endif
3456 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003457 status = psa_ecdsa_sign( slot->data.ecp,
3458 alg,
3459 hash, hash_length,
3460 signature, signature_size,
3461 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003462 else
3463#endif /* defined(MBEDTLS_ECDSA_C) */
3464 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003465 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003466 }
itayzafrir5c753392018-05-08 11:18:38 +03003467 }
3468 else
3469#endif /* defined(MBEDTLS_ECP_C) */
3470 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003471 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003472 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003473
3474exit:
3475 /* Fill the unused part of the output buffer (the whole buffer on error,
3476 * the trailing part on success) with something that isn't a valid mac
3477 * (barring an attack on the mac and deliberately-crafted input),
3478 * in case the caller doesn't check the return status properly. */
3479 if( status == PSA_SUCCESS )
3480 memset( signature + *signature_length, '!',
3481 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003482 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003483 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003484 /* If signature_size is 0 then we have nothing to do. We must not call
3485 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003486 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003487}
3488
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003489psa_status_t psa_verify_hash( psa_key_handle_t handle,
3490 psa_algorithm_t alg,
3491 const uint8_t *hash,
3492 size_t hash_length,
3493 const uint8_t *signature,
3494 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003495{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003496 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003497 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003498#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3499 const psa_drv_se_t *drv;
3500 psa_drv_se_context_t *drv_context;
3501#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003502
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003503 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003504 if( status != PSA_SUCCESS )
3505 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003506
Gilles Peskineedc64242019-08-07 21:05:07 +02003507#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3508 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3509 {
3510 if( drv->asymmetric == NULL ||
3511 drv->asymmetric->p_verify == NULL )
3512 return( PSA_ERROR_NOT_SUPPORTED );
3513 return( drv->asymmetric->p_verify( drv_context,
3514 slot->data.se.slot_number,
3515 alg,
3516 hash, hash_length,
3517 signature, signature_length ) );
3518 }
3519 else
3520#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003521#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003522 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003523 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003524 return( psa_rsa_verify( slot->data.rsa,
3525 alg,
3526 hash, hash_length,
3527 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003528 }
3529 else
3530#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003531#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003532 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003533 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003534#if defined(MBEDTLS_ECDSA_C)
3535 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003536 return( psa_ecdsa_verify( slot->data.ecp,
3537 hash, hash_length,
3538 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003539 else
3540#endif /* defined(MBEDTLS_ECDSA_C) */
3541 {
3542 return( PSA_ERROR_INVALID_ARGUMENT );
3543 }
itayzafrir5c753392018-05-08 11:18:38 +03003544 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003545 else
3546#endif /* defined(MBEDTLS_ECP_C) */
3547 {
3548 return( PSA_ERROR_NOT_SUPPORTED );
3549 }
3550}
3551
Gilles Peskine072ac562018-06-30 00:21:29 +02003552#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3553static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3554 mbedtls_rsa_context *rsa )
3555{
3556 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3557 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3558 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3559 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3560}
3561#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3562
Gilles Peskinec5487a82018-12-03 18:08:14 +01003563psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003564 psa_algorithm_t alg,
3565 const uint8_t *input,
3566 size_t input_length,
3567 const uint8_t *salt,
3568 size_t salt_length,
3569 uint8_t *output,
3570 size_t output_size,
3571 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003572{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003573 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003574 psa_status_t status;
3575
Darryl Green5cc689a2018-07-24 15:34:10 +01003576 (void) input;
3577 (void) input_length;
3578 (void) salt;
3579 (void) output;
3580 (void) output_size;
3581
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003582 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003584 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3585 return( PSA_ERROR_INVALID_ARGUMENT );
3586
Gilles Peskine28f8f302019-07-24 13:30:31 +02003587 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003588 if( status != PSA_SUCCESS )
3589 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003590 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3591 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003593
3594#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003595 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003596 {
3597 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003598 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003599 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003600 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003602 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003603 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003604 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3605 mbedtls_ctr_drbg_random,
3606 &global_data.ctr_drbg,
3607 MBEDTLS_RSA_PUBLIC,
3608 input_length,
3609 input,
3610 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 }
3612 else
3613#endif /* MBEDTLS_PKCS1_V15 */
3614#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003615 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003616 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003617 psa_rsa_oaep_set_padding_mode( alg, rsa );
3618 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3619 mbedtls_ctr_drbg_random,
3620 &global_data.ctr_drbg,
3621 MBEDTLS_RSA_PUBLIC,
3622 salt, salt_length,
3623 input_length,
3624 input,
3625 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003626 }
3627 else
3628#endif /* MBEDTLS_PKCS1_V21 */
3629 {
3630 return( PSA_ERROR_INVALID_ARGUMENT );
3631 }
3632 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003633 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 return( mbedtls_to_psa_error( ret ) );
3635 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003636 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003637#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003638 {
3639 return( PSA_ERROR_NOT_SUPPORTED );
3640 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003641}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642
Gilles Peskinec5487a82018-12-03 18:08:14 +01003643psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003644 psa_algorithm_t alg,
3645 const uint8_t *input,
3646 size_t input_length,
3647 const uint8_t *salt,
3648 size_t salt_length,
3649 uint8_t *output,
3650 size_t output_size,
3651 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003652{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003653 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003654 psa_status_t status;
3655
Darryl Green5cc689a2018-07-24 15:34:10 +01003656 (void) input;
3657 (void) input_length;
3658 (void) salt;
3659 (void) output;
3660 (void) output_size;
3661
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003662 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003663
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003664 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3665 return( PSA_ERROR_INVALID_ARGUMENT );
3666
Gilles Peskine28f8f302019-07-24 13:30:31 +02003667 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003668 if( status != PSA_SUCCESS )
3669 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003670 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671 return( PSA_ERROR_INVALID_ARGUMENT );
3672
3673#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003674 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675 {
3676 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003677 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003678
Gilles Peskine630a18a2018-06-29 17:49:35 +02003679 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003680 return( PSA_ERROR_INVALID_ARGUMENT );
3681
3682#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003683 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003685 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3686 mbedtls_ctr_drbg_random,
3687 &global_data.ctr_drbg,
3688 MBEDTLS_RSA_PRIVATE,
3689 output_length,
3690 input,
3691 output,
3692 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003693 }
3694 else
3695#endif /* MBEDTLS_PKCS1_V15 */
3696#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003697 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003698 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003699 psa_rsa_oaep_set_padding_mode( alg, rsa );
3700 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3701 mbedtls_ctr_drbg_random,
3702 &global_data.ctr_drbg,
3703 MBEDTLS_RSA_PRIVATE,
3704 salt, salt_length,
3705 output_length,
3706 input,
3707 output,
3708 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003709 }
3710 else
3711#endif /* MBEDTLS_PKCS1_V21 */
3712 {
3713 return( PSA_ERROR_INVALID_ARGUMENT );
3714 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003715
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003716 return( mbedtls_to_psa_error( ret ) );
3717 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003718 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003719#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003720 {
3721 return( PSA_ERROR_NOT_SUPPORTED );
3722 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003723}
Gilles Peskine20035e32018-02-03 22:44:14 +01003724
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003725
3726
mohammad1603503973b2018-03-12 15:59:30 +02003727/****************************************************************/
3728/* Symmetric cryptography */
3729/****************************************************************/
3730
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003731/* Initialize the cipher operation structure. Once this function has been
3732 * called, psa_cipher_abort can run and will do the right thing. */
3733static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3734 psa_algorithm_t alg )
3735{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003736 if( ! PSA_ALG_IS_CIPHER( alg ) )
3737 {
3738 memset( operation, 0, sizeof( *operation ) );
3739 return( PSA_ERROR_INVALID_ARGUMENT );
3740 }
3741
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003742 operation->alg = alg;
3743 operation->key_set = 0;
3744 operation->iv_set = 0;
3745 operation->iv_required = 1;
3746 operation->iv_size = 0;
3747 operation->block_size = 0;
3748 mbedtls_cipher_init( &operation->ctx.cipher );
3749 return( PSA_SUCCESS );
3750}
3751
Gilles Peskinee553c652018-06-04 16:22:46 +02003752static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003753 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003754 psa_algorithm_t alg,
3755 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003756{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003757 int ret = 0;
3758 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003759 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003760 size_t key_bits;
3761 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003762 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3763 PSA_KEY_USAGE_ENCRYPT :
3764 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003765
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003766 /* A context must be freshly initialized before it can be set up. */
3767 if( operation->alg != 0 )
3768 {
3769 return( PSA_ERROR_BAD_STATE );
3770 }
3771
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003772 status = psa_cipher_init( operation, alg );
3773 if( status != PSA_SUCCESS )
3774 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003775
Gilles Peskine28f8f302019-07-24 13:30:31 +02003776 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003777 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003778 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003779 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003780
Gilles Peskine8e338702019-07-30 20:06:31 +02003781 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003782 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003783 {
3784 status = PSA_ERROR_NOT_SUPPORTED;
3785 goto exit;
3786 }
mohammad1603503973b2018-03-12 15:59:30 +02003787
mohammad1603503973b2018-03-12 15:59:30 +02003788 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003789 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003790 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003791
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003792#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003793 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003794 {
3795 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003796 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003797 memcpy( keys, slot->data.raw.data, 16 );
3798 memcpy( keys + 16, slot->data.raw.data, 8 );
3799 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3800 keys,
3801 192, cipher_operation );
3802 }
3803 else
3804#endif
3805 {
3806 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3807 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003808 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003809 }
Moran Peker41deec42018-04-04 15:43:05 +03003810 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003811 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003812
mohammad16038481e742018-03-18 13:57:31 +02003813#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003814 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003815 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003816 case PSA_ALG_CBC_NO_PADDING:
3817 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3818 MBEDTLS_PADDING_NONE );
3819 break;
3820 case PSA_ALG_CBC_PKCS7:
3821 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3822 MBEDTLS_PADDING_PKCS7 );
3823 break;
3824 default:
3825 /* The algorithm doesn't involve padding. */
3826 ret = 0;
3827 break;
3828 }
3829 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003830 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003831#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3832
mohammad1603503973b2018-03-12 15:59:30 +02003833 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003834 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003835 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003836 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003837 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003838 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003839 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003840#if defined(MBEDTLS_CHACHA20_C)
3841 else
3842 if( alg == PSA_ALG_CHACHA20 )
3843 operation->iv_size = 12;
3844#endif
mohammad1603503973b2018-03-12 15:59:30 +02003845
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003846exit:
3847 if( status == 0 )
3848 status = mbedtls_to_psa_error( ret );
3849 if( status != 0 )
3850 psa_cipher_abort( operation );
3851 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003852}
3853
Gilles Peskinefe119512018-07-08 21:39:34 +02003854psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003855 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003856 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003857{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003858 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003859}
3860
Gilles Peskinefe119512018-07-08 21:39:34 +02003861psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003862 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003863 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003864{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003865 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003866}
3867
Gilles Peskinefe119512018-07-08 21:39:34 +02003868psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003869 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003870 size_t iv_size,
3871 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003872{
itayzafrir534bd7c2018-08-02 13:56:32 +03003873 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003874 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003875 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003876 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003877 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003878 }
Moran Peker41deec42018-04-04 15:43:05 +03003879 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003880 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003881 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003882 goto exit;
3883 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003884 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3885 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003886 if( ret != 0 )
3887 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003888 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003889 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003890 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003891
mohammad16038481e742018-03-18 13:57:31 +02003892 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003893 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003894
Moran Peker395db872018-05-31 14:07:14 +03003895exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003897 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003898 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003899}
3900
Gilles Peskinefe119512018-07-08 21:39:34 +02003901psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003902 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003903 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003904{
itayzafrir534bd7c2018-08-02 13:56:32 +03003905 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003906 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003907 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003908 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003909 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003910 }
Moran Pekera28258c2018-05-29 16:25:04 +03003911 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003912 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003913 status = PSA_ERROR_INVALID_ARGUMENT;
3914 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003915 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003916 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3917 status = mbedtls_to_psa_error( ret );
3918exit:
3919 if( status == PSA_SUCCESS )
3920 operation->iv_set = 1;
3921 else
mohammad1603503973b2018-03-12 15:59:30 +02003922 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003923 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003924}
3925
Gilles Peskinee553c652018-06-04 16:22:46 +02003926psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3927 const uint8_t *input,
3928 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003929 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003930 size_t output_size,
3931 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003932{
itayzafrir534bd7c2018-08-02 13:56:32 +03003933 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003934 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003935 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003936
3937 if( operation->alg == 0 )
3938 {
3939 return( PSA_ERROR_BAD_STATE );
3940 }
3941
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003942 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003943 {
3944 /* Take the unprocessed partial block left over from previous
3945 * update calls, if any, plus the input to this call. Remove
3946 * the last partial block, if any. You get the data that will be
3947 * output in this call. */
3948 expected_output_size =
3949 ( operation->ctx.cipher.unprocessed_len + input_length )
3950 / operation->block_size * operation->block_size;
3951 }
3952 else
3953 {
3954 expected_output_size = input_length;
3955 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003956
Gilles Peskine89d789c2018-06-04 17:17:16 +02003957 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003958 {
3959 status = PSA_ERROR_BUFFER_TOO_SMALL;
3960 goto exit;
3961 }
mohammad160382759612018-03-12 18:16:40 +02003962
mohammad1603503973b2018-03-12 15:59:30 +02003963 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003964 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003965 status = mbedtls_to_psa_error( ret );
3966exit:
3967 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003968 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003969 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003970}
3971
Gilles Peskinee553c652018-06-04 16:22:46 +02003972psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3973 uint8_t *output,
3974 size_t output_size,
3975 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003976{
David Saadab4ecc272019-02-14 13:48:10 +02003977 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003978 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003979 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003980
mohammad1603503973b2018-03-12 15:59:30 +02003981 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003982 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003983 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003984 }
3985 if( operation->iv_required && ! operation->iv_set )
3986 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003987 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003988 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003989
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003990 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003991 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3992 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003993 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003994 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003995 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003996 }
3997
Janos Follath315b51c2018-07-09 16:04:51 +01003998 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3999 temp_output_buffer,
4000 output_length );
4001 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004002 {
Janos Follath315b51c2018-07-09 16:04:51 +01004003 status = mbedtls_to_psa_error( cipher_ret );
4004 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004005 }
Janos Follath315b51c2018-07-09 16:04:51 +01004006
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004007 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004008 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004009 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004010 memcpy( output, temp_output_buffer, *output_length );
4011 else
4012 {
Janos Follath315b51c2018-07-09 16:04:51 +01004013 status = PSA_ERROR_BUFFER_TOO_SMALL;
4014 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004015 }
mohammad1603503973b2018-03-12 15:59:30 +02004016
Gilles Peskine3f108122018-12-07 18:14:53 +01004017 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004018 status = psa_cipher_abort( operation );
4019
4020 return( status );
4021
4022error:
4023
4024 *output_length = 0;
4025
Gilles Peskine3f108122018-12-07 18:14:53 +01004026 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004027 (void) psa_cipher_abort( operation );
4028
4029 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004030}
4031
Gilles Peskinee553c652018-06-04 16:22:46 +02004032psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4033{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004034 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004035 {
4036 /* The object has (apparently) been initialized but it is not
4037 * in use. It's ok to call abort on such an object, and there's
4038 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004039 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004040 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004041
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004042 /* Sanity check (shouldn't happen: operation->alg should
4043 * always have been initialized to a valid value). */
4044 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4045 return( PSA_ERROR_BAD_STATE );
4046
mohammad1603503973b2018-03-12 15:59:30 +02004047 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004048
Moran Peker41deec42018-04-04 15:43:05 +03004049 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004050 operation->key_set = 0;
4051 operation->iv_set = 0;
4052 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004053 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004054 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004055
Moran Peker395db872018-05-31 14:07:14 +03004056 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004057}
4058
Gilles Peskinea0655c32018-04-30 17:06:50 +02004059
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004060
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004061
mohammad16035955c982018-04-26 00:53:03 +03004062/****************************************************************/
4063/* AEAD */
4064/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004065
Gilles Peskineedf9a652018-08-17 18:11:56 +02004066typedef struct
4067{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004068 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004069 const mbedtls_cipher_info_t *cipher_info;
4070 union
4071 {
4072#if defined(MBEDTLS_CCM_C)
4073 mbedtls_ccm_context ccm;
4074#endif /* MBEDTLS_CCM_C */
4075#if defined(MBEDTLS_GCM_C)
4076 mbedtls_gcm_context gcm;
4077#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004078#if defined(MBEDTLS_CHACHAPOLY_C)
4079 mbedtls_chachapoly_context chachapoly;
4080#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004082 psa_algorithm_t core_alg;
4083 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004084 uint8_t tag_length;
4085} aead_operation_t;
4086
Gilles Peskine30a9e412019-01-14 18:36:12 +01004087static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004089 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004090 {
4091#if defined(MBEDTLS_CCM_C)
4092 case PSA_ALG_CCM:
4093 mbedtls_ccm_free( &operation->ctx.ccm );
4094 break;
4095#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004096#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 case PSA_ALG_GCM:
4098 mbedtls_gcm_free( &operation->ctx.gcm );
4099 break;
4100#endif /* MBEDTLS_GCM_C */
4101 }
4102}
4103
4104static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004105 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004106 psa_key_usage_t usage,
4107 psa_algorithm_t alg )
4108{
4109 psa_status_t status;
4110 size_t key_bits;
4111 mbedtls_cipher_id_t cipher_id;
4112
Gilles Peskine28f8f302019-07-24 13:30:31 +02004113 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 if( status != PSA_SUCCESS )
4115 return( status );
4116
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004117 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004118
4119 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004120 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004121 &cipher_id );
4122 if( operation->cipher_info == NULL )
4123 return( PSA_ERROR_NOT_SUPPORTED );
4124
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004125 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 {
4127#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004128 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4129 operation->core_alg = PSA_ALG_CCM;
4130 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004131 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4132 * The call to mbedtls_ccm_encrypt_and_tag or
4133 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004134 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004135 return( PSA_ERROR_INVALID_ARGUMENT );
4136 mbedtls_ccm_init( &operation->ctx.ccm );
4137 status = mbedtls_to_psa_error(
4138 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4139 operation->slot->data.raw.data,
4140 (unsigned int) key_bits ) );
4141 if( status != 0 )
4142 goto cleanup;
4143 break;
4144#endif /* MBEDTLS_CCM_C */
4145
4146#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004147 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4148 operation->core_alg = PSA_ALG_GCM;
4149 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004150 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4151 * The call to mbedtls_gcm_crypt_and_tag or
4152 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004153 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004154 return( PSA_ERROR_INVALID_ARGUMENT );
4155 mbedtls_gcm_init( &operation->ctx.gcm );
4156 status = mbedtls_to_psa_error(
4157 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4158 operation->slot->data.raw.data,
4159 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004160 if( status != 0 )
4161 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004162 break;
4163#endif /* MBEDTLS_GCM_C */
4164
Gilles Peskine26869f22019-05-06 15:25:00 +02004165#if defined(MBEDTLS_CHACHAPOLY_C)
4166 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4167 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4168 operation->full_tag_length = 16;
4169 /* We only support the default tag length. */
4170 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4171 return( PSA_ERROR_NOT_SUPPORTED );
4172 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4173 status = mbedtls_to_psa_error(
4174 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4175 operation->slot->data.raw.data ) );
4176 if( status != 0 )
4177 goto cleanup;
4178 break;
4179#endif /* MBEDTLS_CHACHAPOLY_C */
4180
Gilles Peskineedf9a652018-08-17 18:11:56 +02004181 default:
4182 return( PSA_ERROR_NOT_SUPPORTED );
4183 }
4184
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004185 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4186 {
4187 status = PSA_ERROR_INVALID_ARGUMENT;
4188 goto cleanup;
4189 }
4190 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004191
Gilles Peskineedf9a652018-08-17 18:11:56 +02004192 return( PSA_SUCCESS );
4193
4194cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004195 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004196 return( status );
4197}
4198
Gilles Peskinec5487a82018-12-03 18:08:14 +01004199psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004200 psa_algorithm_t alg,
4201 const uint8_t *nonce,
4202 size_t nonce_length,
4203 const uint8_t *additional_data,
4204 size_t additional_data_length,
4205 const uint8_t *plaintext,
4206 size_t plaintext_length,
4207 uint8_t *ciphertext,
4208 size_t ciphertext_size,
4209 size_t *ciphertext_length )
4210{
mohammad16035955c982018-04-26 00:53:03 +03004211 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004213 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004214
mohammad1603f08a5502018-06-03 15:05:47 +03004215 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004216
Gilles Peskinec5487a82018-12-03 18:08:14 +01004217 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004218 if( status != PSA_SUCCESS )
4219 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004220
Gilles Peskineedf9a652018-08-17 18:11:56 +02004221 /* For all currently supported modes, the tag is at the end of the
4222 * ciphertext. */
4223 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4224 {
4225 status = PSA_ERROR_BUFFER_TOO_SMALL;
4226 goto exit;
4227 }
4228 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004229
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004230#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004231 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004232 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004233 status = mbedtls_to_psa_error(
4234 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4235 MBEDTLS_GCM_ENCRYPT,
4236 plaintext_length,
4237 nonce, nonce_length,
4238 additional_data, additional_data_length,
4239 plaintext, ciphertext,
4240 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004241 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004242 else
4243#endif /* MBEDTLS_GCM_C */
4244#if defined(MBEDTLS_CCM_C)
4245 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004246 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004247 status = mbedtls_to_psa_error(
4248 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4249 plaintext_length,
4250 nonce, nonce_length,
4251 additional_data,
4252 additional_data_length,
4253 plaintext, ciphertext,
4254 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004255 }
mohammad16035c8845f2018-05-09 05:40:09 -07004256 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004257#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004258#if defined(MBEDTLS_CHACHAPOLY_C)
4259 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4260 {
4261 if( nonce_length != 12 || operation.tag_length != 16 )
4262 {
4263 status = PSA_ERROR_NOT_SUPPORTED;
4264 goto exit;
4265 }
4266 status = mbedtls_to_psa_error(
4267 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4268 plaintext_length,
4269 nonce,
4270 additional_data,
4271 additional_data_length,
4272 plaintext,
4273 ciphertext,
4274 tag ) );
4275 }
4276 else
4277#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004278 {
mohammad1603554faad2018-06-03 15:07:38 +03004279 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004280 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004281
Gilles Peskineedf9a652018-08-17 18:11:56 +02004282 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4283 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004284
Gilles Peskineedf9a652018-08-17 18:11:56 +02004285exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004286 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004287 if( status == PSA_SUCCESS )
4288 *ciphertext_length = plaintext_length + operation.tag_length;
4289 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004290}
4291
Gilles Peskineee652a32018-06-01 19:23:52 +02004292/* Locate the tag in a ciphertext buffer containing the encrypted data
4293 * followed by the tag. Return the length of the part preceding the tag in
4294 * *plaintext_length. This is the size of the plaintext in modes where
4295 * the encrypted data has the same size as the plaintext, such as
4296 * CCM and GCM. */
4297static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4298 const uint8_t *ciphertext,
4299 size_t ciphertext_length,
4300 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004301 const uint8_t **p_tag )
4302{
4303 size_t payload_length;
4304 if( tag_length > ciphertext_length )
4305 return( PSA_ERROR_INVALID_ARGUMENT );
4306 payload_length = ciphertext_length - tag_length;
4307 if( payload_length > plaintext_size )
4308 return( PSA_ERROR_BUFFER_TOO_SMALL );
4309 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004310 return( PSA_SUCCESS );
4311}
4312
Gilles Peskinec5487a82018-12-03 18:08:14 +01004313psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004314 psa_algorithm_t alg,
4315 const uint8_t *nonce,
4316 size_t nonce_length,
4317 const uint8_t *additional_data,
4318 size_t additional_data_length,
4319 const uint8_t *ciphertext,
4320 size_t ciphertext_length,
4321 uint8_t *plaintext,
4322 size_t plaintext_size,
4323 size_t *plaintext_length )
4324{
mohammad16035955c982018-04-26 00:53:03 +03004325 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004326 aead_operation_t operation;
4327 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004328
Gilles Peskineee652a32018-06-01 19:23:52 +02004329 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004330
Gilles Peskinec5487a82018-12-03 18:08:14 +01004331 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004332 if( status != PSA_SUCCESS )
4333 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004334
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004335 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4336 ciphertext, ciphertext_length,
4337 plaintext_size, &tag );
4338 if( status != PSA_SUCCESS )
4339 goto exit;
4340
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004341#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004342 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004343 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004344 status = mbedtls_to_psa_error(
4345 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4346 ciphertext_length - operation.tag_length,
4347 nonce, nonce_length,
4348 additional_data,
4349 additional_data_length,
4350 tag, operation.tag_length,
4351 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004352 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004353 else
4354#endif /* MBEDTLS_GCM_C */
4355#if defined(MBEDTLS_CCM_C)
4356 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004357 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004358 status = mbedtls_to_psa_error(
4359 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4360 ciphertext_length - operation.tag_length,
4361 nonce, nonce_length,
4362 additional_data,
4363 additional_data_length,
4364 ciphertext, plaintext,
4365 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004366 }
mohammad160339574652018-06-01 04:39:53 -07004367 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004368#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004369#if defined(MBEDTLS_CHACHAPOLY_C)
4370 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4371 {
4372 if( nonce_length != 12 || operation.tag_length != 16 )
4373 {
4374 status = PSA_ERROR_NOT_SUPPORTED;
4375 goto exit;
4376 }
4377 status = mbedtls_to_psa_error(
4378 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4379 ciphertext_length - operation.tag_length,
4380 nonce,
4381 additional_data,
4382 additional_data_length,
4383 tag,
4384 ciphertext,
4385 plaintext ) );
4386 }
4387 else
4388#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004389 {
mohammad1603554faad2018-06-03 15:07:38 +03004390 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004391 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004392
Gilles Peskineedf9a652018-08-17 18:11:56 +02004393 if( status != PSA_SUCCESS && plaintext_size != 0 )
4394 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004395
Gilles Peskineedf9a652018-08-17 18:11:56 +02004396exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004397 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004398 if( status == PSA_SUCCESS )
4399 *plaintext_length = ciphertext_length - operation.tag_length;
4400 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004401}
4402
Gilles Peskinea0655c32018-04-30 17:06:50 +02004403
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004404
Gilles Peskine20035e32018-02-03 22:44:14 +01004405/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004406/* Generators */
4407/****************************************************************/
4408
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004409#define HKDF_STATE_INIT 0 /* no input yet */
4410#define HKDF_STATE_STARTED 1 /* got salt */
4411#define HKDF_STATE_KEYED 2 /* got key */
4412#define HKDF_STATE_OUTPUT 3 /* output started */
4413
Gilles Peskinecbe66502019-05-16 16:59:18 +02004414static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004416{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004417 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4418 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004419 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004420 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004421}
4422
4423
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004424psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004425{
4426 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004427 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004428 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004429 {
4430 /* The object has (apparently) been initialized but it is not
4431 * in use. It's ok to call abort on such an object, and there's
4432 * nothing to do. */
4433 }
4434 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004435#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004436 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004437 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004438 mbedtls_free( operation->ctx.hkdf.info );
4439 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004440 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004441 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004442 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004443 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004444 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004445 if( operation->ctx.tls12_prf.seed != NULL )
4446 {
4447 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4448 operation->ctx.tls12_prf.seed_length );
4449 mbedtls_free( operation->ctx.tls12_prf.seed );
4450 }
4451
4452 if( operation->ctx.tls12_prf.label != NULL )
4453 {
4454 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4455 operation->ctx.tls12_prf.label_length );
4456 mbedtls_free( operation->ctx.tls12_prf.label );
4457 }
4458
4459 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4460
4461 /* We leave the fields Ai and output_block to be erased safely by the
4462 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004463 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004464 else
4465#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004466 {
4467 status = PSA_ERROR_BAD_STATE;
4468 }
Janos Follath083036a2019-06-11 10:22:26 +01004469 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004470 return( status );
4471}
4472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474 size_t *capacity)
4475{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004476 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004477 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004478 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004479 return PSA_ERROR_BAD_STATE;
4480 }
4481
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004483 return( PSA_SUCCESS );
4484}
4485
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004487 size_t capacity )
4488{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004490 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004491 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004492 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004493 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004494 return( PSA_SUCCESS );
4495}
4496
Gilles Peskinebef7f142018-07-12 17:22:21 +02004497#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004498/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004499 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004500static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004501 psa_algorithm_t hash_alg,
4502 uint8_t *output,
4503 size_t output_length )
4504{
4505 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4506 psa_status_t status;
4507
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004508 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4509 return( PSA_ERROR_BAD_STATE );
4510 hkdf->state = HKDF_STATE_OUTPUT;
4511
Gilles Peskinebef7f142018-07-12 17:22:21 +02004512 while( output_length != 0 )
4513 {
4514 /* Copy what remains of the current block */
4515 uint8_t n = hash_length - hkdf->offset_in_block;
4516 if( n > output_length )
4517 n = (uint8_t) output_length;
4518 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4519 output += n;
4520 output_length -= n;
4521 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004522 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004523 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004524 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004525 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004526 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004527 * object was corrupted or if this function is called directly
4528 * inside the library. */
4529 if( hkdf->block_number == 0xff )
4530 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004531
4532 /* We need a new block */
4533 ++hkdf->block_number;
4534 hkdf->offset_in_block = 0;
4535 status = psa_hmac_setup_internal( &hkdf->hmac,
4536 hkdf->prk, hash_length,
4537 hash_alg );
4538 if( status != PSA_SUCCESS )
4539 return( status );
4540 if( hkdf->block_number != 1 )
4541 {
4542 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4543 hkdf->output_block,
4544 hash_length );
4545 if( status != PSA_SUCCESS )
4546 return( status );
4547 }
4548 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4549 hkdf->info,
4550 hkdf->info_length );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4554 &hkdf->block_number, 1 );
4555 if( status != PSA_SUCCESS )
4556 return( status );
4557 status = psa_hmac_finish_internal( &hkdf->hmac,
4558 hkdf->output_block,
4559 sizeof( hkdf->output_block ) );
4560 if( status != PSA_SUCCESS )
4561 return( status );
4562 }
4563
4564 return( PSA_SUCCESS );
4565}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566
Janos Follath7742fee2019-06-17 12:58:10 +01004567static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4568 psa_tls12_prf_key_derivation_t *tls12_prf,
4569 psa_algorithm_t alg )
4570{
4571 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4572 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004573 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4574 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004575
Janos Follath7742fee2019-06-17 12:58:10 +01004576 /* We can't be wanting more output after block 0xff, otherwise
4577 * the capacity check in psa_key_derivation_output_bytes() would have
4578 * prevented this call. It could happen only if the operation
4579 * object was corrupted or if this function is called directly
4580 * inside the library. */
4581 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004582 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004583
4584 /* We need a new block */
4585 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004586 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004587
4588 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4589 *
4590 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4591 *
4592 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4593 * HMAC_hash(secret, A(2) + seed) +
4594 * HMAC_hash(secret, A(3) + seed) + ...
4595 *
4596 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004597 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004598 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004599 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004600 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004601 * is currently extracted as `output_block` and where i is
4602 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004603 */
4604
Janos Follathea29bfb2019-06-19 12:21:20 +01004605 /* Save the hash context before using it, to preserve the hash state with
4606 * only the inner padding in it. We need this, because inner padding depends
4607 * on the key (secret in the RFC's terminology). */
4608 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4609 if( status != PSA_SUCCESS )
4610 goto cleanup;
4611
4612 /* Calculate A(i) where i = tls12_prf->block_number. */
4613 if( tls12_prf->block_number == 1 )
4614 {
4615 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4616 * the variable seed and in this instance means it in the context of the
4617 * P_hash function, where seed = label + seed.) */
4618 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4619 tls12_prf->label, tls12_prf->label_length );
4620 if( status != PSA_SUCCESS )
4621 goto cleanup;
4622 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4623 tls12_prf->seed, tls12_prf->seed_length );
4624 if( status != PSA_SUCCESS )
4625 goto cleanup;
4626 }
4627 else
4628 {
4629 /* A(i) = HMAC_hash(secret, A(i-1)) */
4630 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4631 tls12_prf->Ai, hash_length );
4632 if( status != PSA_SUCCESS )
4633 goto cleanup;
4634 }
4635
4636 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4637 tls12_prf->Ai, hash_length );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4641 if( status != PSA_SUCCESS )
4642 goto cleanup;
4643
4644 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4645 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4646 tls12_prf->Ai, hash_length );
4647 if( status != PSA_SUCCESS )
4648 goto cleanup;
4649 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4650 tls12_prf->label, tls12_prf->label_length );
4651 if( status != PSA_SUCCESS )
4652 goto cleanup;
4653 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4654 tls12_prf->seed, tls12_prf->seed_length );
4655 if( status != PSA_SUCCESS )
4656 goto cleanup;
4657 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4658 tls12_prf->output_block, hash_length );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4662 if( status != PSA_SUCCESS )
4663 goto cleanup;
4664
Janos Follath7742fee2019-06-17 12:58:10 +01004665
4666cleanup:
4667
Janos Follathea29bfb2019-06-19 12:21:20 +01004668 cleanup_status = psa_hash_abort( &backup );
4669 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4670 status = cleanup_status;
4671
4672 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004673}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004674
Janos Follath844eb0e2019-06-19 12:10:49 +01004675static psa_status_t psa_key_derivation_tls12_prf_read(
4676 psa_tls12_prf_key_derivation_t *tls12_prf,
4677 psa_algorithm_t alg,
4678 uint8_t *output,
4679 size_t output_length )
4680{
4681 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4682 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4683 psa_status_t status;
4684 uint8_t offset, length;
4685
4686 while( output_length != 0 )
4687 {
4688 /* Check if we have fully processed the current block. */
4689 if( tls12_prf->left_in_block == 0 )
4690 {
4691 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4692 alg );
4693 if( status != PSA_SUCCESS )
4694 return( status );
4695
4696 continue;
4697 }
4698
4699 if( tls12_prf->left_in_block > output_length )
4700 length = (uint8_t) output_length;
4701 else
4702 length = tls12_prf->left_in_block;
4703
4704 offset = hash_length - tls12_prf->left_in_block;
4705 memcpy( output, tls12_prf->output_block + offset, length );
4706 output += length;
4707 output_length -= length;
4708 tls12_prf->left_in_block -= length;
4709 }
4710
4711 return( PSA_SUCCESS );
4712}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004713#endif /* MBEDTLS_MD_C */
4714
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004715psa_status_t psa_key_derivation_output_bytes(
4716 psa_key_derivation_operation_t *operation,
4717 uint8_t *output,
4718 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004719{
4720 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004721 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004722
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004723 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004724 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004725 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004726 return PSA_ERROR_BAD_STATE;
4727 }
4728
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004734 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 goto exit;
4736 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004740 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004741 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4742 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004743 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004744 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004745 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004746 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004748
Gilles Peskinebef7f142018-07-12 17:22:21 +02004749#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004750 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004751 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004753 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004754 output, output_length );
4755 }
Janos Follathadbec812019-06-14 11:05:39 +01004756 else
Janos Follathadbec812019-06-14 11:05:39 +01004757 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004758 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004759 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004760 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004761 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004762 output_length );
4763 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004764 else
4765#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004766 {
4767 return( PSA_ERROR_BAD_STATE );
4768 }
4769
4770exit:
4771 if( status != PSA_SUCCESS )
4772 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004773 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004774 * This allows us to differentiate between exhausted operations and
4775 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4776 * operations. */
4777 psa_algorithm_t alg = operation->alg;
4778 psa_key_derivation_abort( operation );
4779 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004780 memset( output, '!', output_length );
4781 }
4782 return( status );
4783}
4784
Gilles Peskine08542d82018-07-19 17:05:42 +02004785#if defined(MBEDTLS_DES_C)
4786static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4787{
4788 if( data_size >= 8 )
4789 mbedtls_des_key_set_parity( data );
4790 if( data_size >= 16 )
4791 mbedtls_des_key_set_parity( data + 8 );
4792 if( data_size >= 24 )
4793 mbedtls_des_key_set_parity( data + 16 );
4794}
4795#endif /* MBEDTLS_DES_C */
4796
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004797static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004798 psa_key_slot_t *slot,
4799 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004800 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004801{
4802 uint8_t *data = NULL;
4803 size_t bytes = PSA_BITS_TO_BYTES( bits );
4804 psa_status_t status;
4805
Gilles Peskine8e338702019-07-30 20:06:31 +02004806 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004807 return( PSA_ERROR_INVALID_ARGUMENT );
4808 if( bits % 8 != 0 )
4809 return( PSA_ERROR_INVALID_ARGUMENT );
4810 data = mbedtls_calloc( 1, bytes );
4811 if( data == NULL )
4812 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4813
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004814 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 if( status != PSA_SUCCESS )
4816 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004817#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004818 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004819 psa_des_set_key_parity( data, bytes );
4820#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004821 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004822
4823exit:
4824 mbedtls_free( data );
4825 return( status );
4826}
4827
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004828psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004829 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004830 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004831{
4832 psa_status_t status;
4833 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004834 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004835
4836 /* Reject any attempt to create a zero-length key so that we don't
4837 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4838 if( psa_get_key_bits( attributes ) == 0 )
4839 return( PSA_ERROR_INVALID_ARGUMENT );
4840
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004841 if( ! operation->can_output_key )
4842 return( PSA_ERROR_NOT_PERMITTED );
4843
Gilles Peskinedf179142019-07-15 22:02:14 +02004844 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4845 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004846#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4847 if( driver != NULL )
4848 {
4849 /* Deriving a key in a secure element is not implemented yet. */
4850 status = PSA_ERROR_NOT_SUPPORTED;
4851 }
4852#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004853 if( status == PSA_SUCCESS )
4854 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004855 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004856 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004857 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004858 }
4859 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004860 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 if( status != PSA_SUCCESS )
4862 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004863 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004864 *handle = 0;
4865 }
4866 return( status );
4867}
4868
Gilles Peskineeab56e42018-07-12 17:12:33 +02004869
4870
4871/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004872/* Key derivation */
4873/****************************************************************/
4874
Gilles Peskine969c5d62019-01-16 15:53:06 +01004875static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004876 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004877 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004878{
Janos Follath5fe19732019-06-20 15:09:30 +01004879 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4880 * initialisers for this union leave some bytes unspecified.) */
4881 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4882
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004884#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4886 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4887 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004888 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004889 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004890 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4891 if( hash_size == 0 )
4892 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4894 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004895 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 {
4897 return( PSA_ERROR_NOT_SUPPORTED );
4898 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004899 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004900 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004901 }
4902#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004903 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004904 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004905}
4906
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004907psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 psa_algorithm_t alg )
4909{
4910 psa_status_t status;
4911
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004912 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913 return( PSA_ERROR_BAD_STATE );
4914
Gilles Peskine6843c292019-01-18 16:44:49 +01004915 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4916 return( PSA_ERROR_INVALID_ARGUMENT );
4917 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004918 {
4919 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 }
4922 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4923 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004924 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004925 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004926 else
4927 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004928
4929 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004930 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004931 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004932}
4933
4934#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004935static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004936 psa_algorithm_t hash_alg,
4937 psa_key_derivation_step_t step,
4938 const uint8_t *data,
4939 size_t data_length )
4940{
4941 psa_status_t status;
4942 switch( step )
4943 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004944 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004945 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004947 status = psa_hmac_setup_internal( &hkdf->hmac,
4948 data, data_length,
4949 hash_alg );
4950 if( status != PSA_SUCCESS )
4951 return( status );
4952 hkdf->state = HKDF_STATE_STARTED;
4953 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004954 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004955 /* If no salt was provided, use an empty salt. */
4956 if( hkdf->state == HKDF_STATE_INIT )
4957 {
4958 status = psa_hmac_setup_internal( &hkdf->hmac,
4959 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004960 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004961 if( status != PSA_SUCCESS )
4962 return( status );
4963 hkdf->state = HKDF_STATE_STARTED;
4964 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004965 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004966 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004967 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4968 data, data_length );
4969 if( status != PSA_SUCCESS )
4970 return( status );
4971 status = psa_hmac_finish_internal( &hkdf->hmac,
4972 hkdf->prk,
4973 sizeof( hkdf->prk ) );
4974 if( status != PSA_SUCCESS )
4975 return( status );
4976 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4977 hkdf->block_number = 0;
4978 hkdf->state = HKDF_STATE_KEYED;
4979 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004980 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004981 if( hkdf->state == HKDF_STATE_OUTPUT )
4982 return( PSA_ERROR_BAD_STATE );
4983 if( hkdf->info_set )
4984 return( PSA_ERROR_BAD_STATE );
4985 hkdf->info_length = data_length;
4986 if( data_length != 0 )
4987 {
4988 hkdf->info = mbedtls_calloc( 1, data_length );
4989 if( hkdf->info == NULL )
4990 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4991 memcpy( hkdf->info, data, data_length );
4992 }
4993 hkdf->info_set = 1;
4994 return( PSA_SUCCESS );
4995 default:
4996 return( PSA_ERROR_INVALID_ARGUMENT );
4997 }
4998}
Janos Follathb03233e2019-06-11 15:30:30 +01004999
Janos Follathf08e2652019-06-13 09:05:41 +01005000static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5001 const uint8_t *data,
5002 size_t data_length )
5003{
5004 if( prf->state != TLS12_PRF_STATE_INIT )
5005 return( PSA_ERROR_BAD_STATE );
5006
Janos Follathd6dce9f2019-07-04 09:11:38 +01005007 if( data_length != 0 )
5008 {
5009 prf->seed = mbedtls_calloc( 1, data_length );
5010 if( prf->seed == NULL )
5011 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005012
Janos Follathd6dce9f2019-07-04 09:11:38 +01005013 memcpy( prf->seed, data, data_length );
5014 prf->seed_length = data_length;
5015 }
Janos Follathf08e2652019-06-13 09:05:41 +01005016
5017 prf->state = TLS12_PRF_STATE_SEED_SET;
5018
5019 return( PSA_SUCCESS );
5020}
5021
Janos Follath81550542019-06-13 14:26:34 +01005022static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5023 psa_algorithm_t hash_alg,
5024 const uint8_t *data,
5025 size_t data_length )
5026{
5027 psa_status_t status;
5028 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5029 return( PSA_ERROR_BAD_STATE );
5030
5031 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5032 if( status != PSA_SUCCESS )
5033 return( status );
5034
5035 prf->state = TLS12_PRF_STATE_KEY_SET;
5036
5037 return( PSA_SUCCESS );
5038}
5039
Janos Follath6660f0e2019-06-17 08:44:03 +01005040static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5041 psa_tls12_prf_key_derivation_t *prf,
5042 psa_algorithm_t hash_alg,
5043 const uint8_t *data,
5044 size_t data_length )
5045{
5046 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005047 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5048 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005049
5050 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5051 return( PSA_ERROR_INVALID_ARGUMENT );
5052
5053 /* Quoting RFC 4279, Section 2:
5054 *
5055 * The premaster secret is formed as follows: if the PSK is N octets
5056 * long, concatenate a uint16 with the value N, N zero octets, a second
5057 * uint16 with the value N, and the PSK itself.
5058 */
5059
Janos Follath40e13932019-06-26 13:22:29 +01005060 *cur++ = ( data_length >> 8 ) & 0xff;
5061 *cur++ = ( data_length >> 0 ) & 0xff;
5062 memset( cur, 0, data_length );
5063 cur += data_length;
5064 *cur++ = pms[0];
5065 *cur++ = pms[1];
5066 memcpy( cur, data, data_length );
5067 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005068
Janos Follath40e13932019-06-26 13:22:29 +01005069 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005070
5071 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5072 return( status );
5073}
5074
Janos Follath63028dd2019-06-13 09:15:47 +01005075static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5076 const uint8_t *data,
5077 size_t data_length )
5078{
5079 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5080 return( PSA_ERROR_BAD_STATE );
5081
Janos Follathd6dce9f2019-07-04 09:11:38 +01005082 if( data_length != 0 )
5083 {
5084 prf->label = mbedtls_calloc( 1, data_length );
5085 if( prf->label == NULL )
5086 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005087
Janos Follathd6dce9f2019-07-04 09:11:38 +01005088 memcpy( prf->label, data, data_length );
5089 prf->label_length = data_length;
5090 }
Janos Follath63028dd2019-06-13 09:15:47 +01005091
5092 prf->state = TLS12_PRF_STATE_LABEL_SET;
5093
5094 return( PSA_SUCCESS );
5095}
5096
Janos Follathb03233e2019-06-11 15:30:30 +01005097static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5098 psa_algorithm_t hash_alg,
5099 psa_key_derivation_step_t step,
5100 const uint8_t *data,
5101 size_t data_length )
5102{
Janos Follathb03233e2019-06-11 15:30:30 +01005103 switch( step )
5104 {
Janos Follathf08e2652019-06-13 09:05:41 +01005105 case PSA_KEY_DERIVATION_INPUT_SEED:
5106 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005107 case PSA_KEY_DERIVATION_INPUT_SECRET:
5108 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005109 case PSA_KEY_DERIVATION_INPUT_LABEL:
5110 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005111 default:
5112 return( PSA_ERROR_INVALID_ARGUMENT );
5113 }
5114}
Janos Follath6660f0e2019-06-17 08:44:03 +01005115
5116static psa_status_t psa_tls12_prf_psk_to_ms_input(
5117 psa_tls12_prf_key_derivation_t *prf,
5118 psa_algorithm_t hash_alg,
5119 psa_key_derivation_step_t step,
5120 const uint8_t *data,
5121 size_t data_length )
5122{
5123 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005124 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005125 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5126 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005127 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005128
5129 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5130}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005131#endif /* MBEDTLS_MD_C */
5132
Gilles Peskineb8965192019-09-24 16:21:10 +02005133/** Check whether the given key type is acceptable for the given
5134 * input step of a key derivation.
5135 *
5136 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5137 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5138 * Both secret and non-secret inputs can alternatively have the type
5139 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5140 * that the input was passed as a buffer rather than via a key object.
5141 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005142static int psa_key_derivation_check_input_type(
5143 psa_key_derivation_step_t step,
5144 psa_key_type_t key_type )
5145{
5146 switch( step )
5147 {
5148 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005149 if( key_type == PSA_KEY_TYPE_DERIVE )
5150 return( PSA_SUCCESS );
5151 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005152 return( PSA_SUCCESS );
5153 break;
5154 case PSA_KEY_DERIVATION_INPUT_LABEL:
5155 case PSA_KEY_DERIVATION_INPUT_SALT:
5156 case PSA_KEY_DERIVATION_INPUT_INFO:
5157 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005158 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5159 return( PSA_SUCCESS );
5160 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005161 return( PSA_SUCCESS );
5162 break;
5163 }
5164 return( PSA_ERROR_INVALID_ARGUMENT );
5165}
5166
Janos Follathb80a94e2019-06-12 15:54:46 +01005167static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005168 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005169 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005170 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005171 const uint8_t *data,
5172 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005173{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005174 psa_status_t status;
5175 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5176
5177 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005178 if( status != PSA_SUCCESS )
5179 goto exit;
5180
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005182 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005183 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005184 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005185 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005186 step, data, data_length );
5187 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005188 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189 {
Janos Follathb03233e2019-06-11 15:30:30 +01005190 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5191 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5192 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005193 }
5194 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5195 {
5196 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5197 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5198 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 }
5200 else
5201#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005202 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005203 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005204 return( PSA_ERROR_BAD_STATE );
5205 }
5206
Gilles Peskine224b0d62019-09-23 18:13:17 +02005207exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005208 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005209 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210 return( status );
5211}
5212
Janos Follath51f4a0f2019-06-14 11:35:55 +01005213psa_status_t psa_key_derivation_input_bytes(
5214 psa_key_derivation_operation_t *operation,
5215 psa_key_derivation_step_t step,
5216 const uint8_t *data,
5217 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005218{
Gilles Peskineb8965192019-09-24 16:21:10 +02005219 return( psa_key_derivation_input_internal( operation, step,
5220 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005221 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005222}
5223
Janos Follath51f4a0f2019-06-14 11:35:55 +01005224psa_status_t psa_key_derivation_input_key(
5225 psa_key_derivation_operation_t *operation,
5226 psa_key_derivation_step_t step,
5227 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005228{
5229 psa_key_slot_t *slot;
5230 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005231
Gilles Peskine28f8f302019-07-24 13:30:31 +02005232 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005233 PSA_KEY_USAGE_DERIVE,
5234 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005235 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005236 {
5237 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005238 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005239 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005240
5241 /* Passing a key object as a SECRET input unlocks the permission
5242 * to output to a key object. */
5243 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5244 operation->can_output_key = 1;
5245
Janos Follathb80a94e2019-06-12 15:54:46 +01005246 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005247 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005248 slot->data.raw.data,
5249 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005250}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005251
5252
5253
5254/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005255/* Key agreement */
5256/****************************************************************/
5257
Gilles Peskinea05219c2018-11-16 16:02:56 +01005258#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005259static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5260 size_t peer_key_length,
5261 const mbedtls_ecp_keypair *our_key,
5262 uint8_t *shared_secret,
5263 size_t shared_secret_size,
5264 size_t *shared_secret_length )
5265{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005266 mbedtls_ecp_keypair *their_key = NULL;
5267 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005268 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005269 size_t bits = 0;
5270 psa_ecc_curve_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005271 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005272
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005273 status = psa_import_ec_public_key( curve,
5274 peer_key, peer_key_length,
5275 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005276 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005277 goto exit;
5278
Jaeden Amero97271b32019-01-10 19:38:51 +00005279 status = mbedtls_to_psa_error(
5280 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5281 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005282 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005283 status = mbedtls_to_psa_error(
5284 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5285 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005286 goto exit;
5287
Jaeden Amero97271b32019-01-10 19:38:51 +00005288 status = mbedtls_to_psa_error(
5289 mbedtls_ecdh_calc_secret( &ecdh,
5290 shared_secret_length,
5291 shared_secret, shared_secret_size,
5292 mbedtls_ctr_drbg_random,
5293 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005294 if( status != PSA_SUCCESS )
5295 goto exit;
5296 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5297 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005298
5299exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005300 if( status != PSA_SUCCESS )
5301 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005302 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005303 mbedtls_ecp_keypair_free( their_key );
5304 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005305 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005306}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005307#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005308
Gilles Peskine01d718c2018-09-18 12:01:02 +02005309#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5310
Gilles Peskine0216fe12019-04-11 21:23:21 +02005311static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5312 psa_key_slot_t *private_key,
5313 const uint8_t *peer_key,
5314 size_t peer_key_length,
5315 uint8_t *shared_secret,
5316 size_t shared_secret_size,
5317 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005318{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005319 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005320 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005321#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005322 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005323 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005324 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005325 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5326 private_key->data.ecp,
5327 shared_secret, shared_secret_size,
5328 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005329#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005330 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005331 (void) private_key;
5332 (void) peer_key;
5333 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005334 (void) shared_secret;
5335 (void) shared_secret_size;
5336 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005337 return( PSA_ERROR_NOT_SUPPORTED );
5338 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005339}
5340
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005341/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005342 * to potentially free embedded data structures and wipe confidential data.
5343 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005344static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005345 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005346 psa_key_slot_t *private_key,
5347 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005348 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005349{
5350 psa_status_t status;
5351 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5352 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005353 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005354
5355 /* Step 1: run the secret agreement algorithm to generate the shared
5356 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005357 status = psa_key_agreement_raw_internal( ka_alg,
5358 private_key,
5359 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005360 shared_secret,
5361 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005362 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005363 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005364 goto exit;
5365
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005366 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005367 * the shared secret. A shared secret is permitted wherever a key
5368 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005369 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005370 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005371 shared_secret,
5372 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005373
Gilles Peskine12313cd2018-06-20 00:20:32 +02005374exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005375 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005376 return( status );
5377}
5378
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005379psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005380 psa_key_derivation_step_t step,
5381 psa_key_handle_t private_key,
5382 const uint8_t *peer_key,
5383 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005384{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005385 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005386 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005388 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005389 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005390 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005391 if( status != PSA_SUCCESS )
5392 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005393 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005394 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005395 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005396 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005397 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005398 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005399}
5400
Gilles Peskinebe697d82019-05-16 18:00:41 +02005401psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5402 psa_key_handle_t private_key,
5403 const uint8_t *peer_key,
5404 size_t peer_key_length,
5405 uint8_t *output,
5406 size_t output_size,
5407 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005408{
5409 psa_key_slot_t *slot;
5410 psa_status_t status;
5411
5412 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5413 {
5414 status = PSA_ERROR_INVALID_ARGUMENT;
5415 goto exit;
5416 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005417 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005418 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005419 if( status != PSA_SUCCESS )
5420 goto exit;
5421
5422 status = psa_key_agreement_raw_internal( alg, slot,
5423 peer_key, peer_key_length,
5424 output, output_size,
5425 output_length );
5426
5427exit:
5428 if( status != PSA_SUCCESS )
5429 {
5430 /* If an error happens and is not handled properly, the output
5431 * may be used as a key to protect sensitive data. Arrange for such
5432 * a key to be random, which is likely to result in decryption or
5433 * verification errors. This is better than filling the buffer with
5434 * some constant data such as zeros, which would result in the data
5435 * being protected with a reproducible, easily knowable key.
5436 */
5437 psa_generate_random( output, output_size );
5438 *output_length = output_size;
5439 }
5440 return( status );
5441}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005442
5443
5444/****************************************************************/
5445/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005446/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005447
Gilles Peskine4c317f42018-07-12 01:24:09 +02005448psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005449 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005450{
Janos Follath24eed8d2019-11-22 13:21:35 +00005451 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005452 GUARD_MODULE_INITIALIZED;
5453
Gilles Peskinef181eca2019-08-07 13:49:00 +02005454 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5455 {
5456 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5457 output,
5458 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5459 if( ret != 0 )
5460 return( mbedtls_to_psa_error( ret ) );
5461 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5462 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5463 }
5464
Gilles Peskinee59236f2018-01-27 23:32:46 +01005465 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5466 return( mbedtls_to_psa_error( ret ) );
5467}
5468
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005469#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5470#include "mbedtls/entropy_poll.h"
5471
Gilles Peskine7228da22019-07-15 11:06:15 +02005472psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005473 size_t seed_size )
5474{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005475 if( global_data.initialized )
5476 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005477
5478 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5479 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5480 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5481 return( PSA_ERROR_INVALID_ARGUMENT );
5482
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005483 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005484}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005485#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005486
Gilles Peskinee56e8782019-04-26 17:34:02 +02005487#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5488static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5489 size_t domain_parameters_size,
5490 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005491{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005492 size_t i;
5493 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005494
Gilles Peskinee56e8782019-04-26 17:34:02 +02005495 if( domain_parameters_size == 0 )
5496 {
5497 *exponent = 65537;
5498 return( PSA_SUCCESS );
5499 }
5500
5501 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5502 * support values that fit in a 32-bit integer, which is larger than
5503 * int on just about every platform anyway. */
5504 if( domain_parameters_size > sizeof( acc ) )
5505 return( PSA_ERROR_NOT_SUPPORTED );
5506 for( i = 0; i < domain_parameters_size; i++ )
5507 acc = ( acc << 8 ) | domain_parameters[i];
5508 if( acc > INT_MAX )
5509 return( PSA_ERROR_NOT_SUPPORTED );
5510 *exponent = acc;
5511 return( PSA_SUCCESS );
5512}
5513#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5514
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005515static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005516 psa_key_slot_t *slot, size_t bits,
5517 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005518{
Gilles Peskine8e338702019-07-30 20:06:31 +02005519 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005520
Gilles Peskinee56e8782019-04-26 17:34:02 +02005521 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005522 return( PSA_ERROR_INVALID_ARGUMENT );
5523
Gilles Peskinee59236f2018-01-27 23:32:46 +01005524 if( key_type_is_raw_bytes( type ) )
5525 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005526 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005527 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5528 if( status != PSA_SUCCESS )
5529 return( status );
5530 status = psa_generate_random( slot->data.raw.data,
5531 slot->data.raw.bytes );
5532 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005534#if defined(MBEDTLS_DES_C)
5535 if( type == PSA_KEY_TYPE_DES )
5536 psa_des_set_key_parity( slot->data.raw.data,
5537 slot->data.raw.bytes );
5538#endif /* MBEDTLS_DES_C */
5539 }
5540 else
5541
5542#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005543 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005544 {
5545 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005546 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005547 int exponent;
5548 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005549 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5550 return( PSA_ERROR_NOT_SUPPORTED );
5551 /* Accept only byte-aligned keys, for the same reasons as
5552 * in psa_import_rsa_key(). */
5553 if( bits % 8 != 0 )
5554 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005555 status = psa_read_rsa_exponent( domain_parameters,
5556 domain_parameters_size,
5557 &exponent );
5558 if( status != PSA_SUCCESS )
5559 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005560 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5561 if( rsa == NULL )
5562 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5563 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5564 ret = mbedtls_rsa_gen_key( rsa,
5565 mbedtls_ctr_drbg_random,
5566 &global_data.ctr_drbg,
5567 (unsigned int) bits,
5568 exponent );
5569 if( ret != 0 )
5570 {
5571 mbedtls_rsa_free( rsa );
5572 mbedtls_free( rsa );
5573 return( mbedtls_to_psa_error( ret ) );
5574 }
5575 slot->data.rsa = rsa;
5576 }
5577 else
5578#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5579
5580#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005581 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005582 {
5583 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005584 mbedtls_ecp_group_id grp_id =
5585 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005586 const mbedtls_ecp_curve_info *curve_info =
5587 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5588 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005590 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005591 return( PSA_ERROR_NOT_SUPPORTED );
5592 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5593 return( PSA_ERROR_NOT_SUPPORTED );
5594 if( curve_info->bit_size != bits )
5595 return( PSA_ERROR_INVALID_ARGUMENT );
5596 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5597 if( ecp == NULL )
5598 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5599 mbedtls_ecp_keypair_init( ecp );
5600 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5601 mbedtls_ctr_drbg_random,
5602 &global_data.ctr_drbg );
5603 if( ret != 0 )
5604 {
5605 mbedtls_ecp_keypair_free( ecp );
5606 mbedtls_free( ecp );
5607 return( mbedtls_to_psa_error( ret ) );
5608 }
5609 slot->data.ecp = ecp;
5610 }
5611 else
5612#endif /* MBEDTLS_ECP_C */
5613
5614 return( PSA_ERROR_NOT_SUPPORTED );
5615
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005616 return( PSA_SUCCESS );
5617}
Darryl Green0c6575a2018-11-07 16:05:30 +00005618
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005619psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005620 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005621{
5622 psa_status_t status;
5623 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005624 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005625
Gilles Peskine0f84d622019-09-12 19:03:13 +02005626 /* Reject any attempt to create a zero-length key so that we don't
5627 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5628 if( psa_get_key_bits( attributes ) == 0 )
5629 return( PSA_ERROR_INVALID_ARGUMENT );
5630
Gilles Peskinedf179142019-07-15 22:02:14 +02005631 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5632 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005633 if( status != PSA_SUCCESS )
5634 goto exit;
5635
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005636#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5637 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005638 {
Gilles Peskine11792082019-08-06 18:36:36 +02005639 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5640 size_t pubkey_length = 0; /* We don't support this feature yet */
5641 if( drv->key_management == NULL ||
5642 drv->key_management->p_generate == NULL )
5643 {
5644 status = PSA_ERROR_NOT_SUPPORTED;
5645 goto exit;
5646 }
5647 status = drv->key_management->p_generate(
5648 psa_get_se_driver_context( driver ),
5649 slot->data.se.slot_number, attributes,
5650 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005651 }
Gilles Peskine11792082019-08-06 18:36:36 +02005652 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005653#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005654 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005655 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005656 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005657 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005658 }
Gilles Peskine11792082019-08-06 18:36:36 +02005659
5660exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005661 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005662 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005663 if( status != PSA_SUCCESS )
5664 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005665 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005666 *handle = 0;
5667 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005668 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005669}
5670
5671
Gilles Peskinee59236f2018-01-27 23:32:46 +01005672
5673/****************************************************************/
5674/* Module setup */
5675/****************************************************************/
5676
Gilles Peskine5e769522018-11-20 21:59:56 +01005677psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5678 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5679 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5680{
5681 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5682 return( PSA_ERROR_BAD_STATE );
5683 global_data.entropy_init = entropy_init;
5684 global_data.entropy_free = entropy_free;
5685 return( PSA_SUCCESS );
5686}
5687
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688void mbedtls_psa_crypto_free( void )
5689{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005690 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005691 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5692 {
5693 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005694 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005695 }
5696 /* Wipe all remaining data, including configuration.
5697 * In particular, this sets all state indicator to the value
5698 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005699 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005700#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005701 /* Unregister all secure element drivers, so that we restart from
5702 * a pristine state. */
5703 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005704#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005705}
5706
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005707#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5708/** Recover a transaction that was interrupted by a power failure.
5709 *
5710 * This function is called during initialization, before psa_crypto_init()
5711 * returns. If this function returns a failure status, the initialization
5712 * fails.
5713 */
5714static psa_status_t psa_crypto_recover_transaction(
5715 const psa_crypto_transaction_t *transaction )
5716{
5717 switch( transaction->unknown.type )
5718 {
5719 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5720 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005721 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005722 * is implemented.
5723 * https://github.com/ARMmbed/mbed-crypto/issues/218
5724 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005725 default:
5726 /* We found an unsupported transaction in the storage.
5727 * We don't know what state the storage is in. Give up. */
5728 return( PSA_ERROR_STORAGE_FAILURE );
5729 }
5730}
5731#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5732
Gilles Peskinee59236f2018-01-27 23:32:46 +01005733psa_status_t psa_crypto_init( void )
5734{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005735 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005736 const unsigned char drbg_seed[] = "PSA";
5737
Gilles Peskinec6b69072018-11-20 21:42:52 +01005738 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005739 if( global_data.initialized != 0 )
5740 return( PSA_SUCCESS );
5741
Gilles Peskine5e769522018-11-20 21:59:56 +01005742 /* Set default configuration if
5743 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5744 if( global_data.entropy_init == NULL )
5745 global_data.entropy_init = mbedtls_entropy_init;
5746 if( global_data.entropy_free == NULL )
5747 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005748
Gilles Peskinec6b69072018-11-20 21:42:52 +01005749 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005750 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005751#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5752 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5753 /* The PSA entropy injection feature depends on using NV seed as an entropy
5754 * source. Add NV seed as an entropy source for PSA entropy injection. */
5755 mbedtls_entropy_add_source( &global_data.entropy,
5756 mbedtls_nv_seed_poll, NULL,
5757 MBEDTLS_ENTROPY_BLOCK_SIZE,
5758 MBEDTLS_ENTROPY_SOURCE_STRONG );
5759#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005760 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005761 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005762 status = mbedtls_to_psa_error(
5763 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5764 mbedtls_entropy_func,
5765 &global_data.entropy,
5766 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5767 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005768 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005769 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005770
Gilles Peskine66fb1262018-12-10 16:29:04 +01005771 status = psa_initialize_key_slots( );
5772 if( status != PSA_SUCCESS )
5773 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005774
Gilles Peskined9348f22019-10-01 15:22:29 +02005775#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5776 status = psa_init_all_se_drivers( );
5777 if( status != PSA_SUCCESS )
5778 goto exit;
5779#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5780
Gilles Peskine4b734222019-07-24 15:56:31 +02005781#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005782 status = psa_crypto_load_transaction( );
5783 if( status == PSA_SUCCESS )
5784 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005785 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5786 if( status != PSA_SUCCESS )
5787 goto exit;
5788 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005789 }
5790 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5791 {
5792 /* There's no transaction to complete. It's all good. */
5793 status = PSA_SUCCESS;
5794 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005795#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005796
Gilles Peskinec6b69072018-11-20 21:42:52 +01005797 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005798 global_data.initialized = 1;
5799
5800exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005801 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005802 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005803 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005804}
5805
5806#endif /* MBEDTLS_PSA_CRYPTO_C */