blob: bb83db0c817b210fd4f48b6731b811b62c3cbe73 [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)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100379mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_family_t curve,
Gilles Peskine5055b232019-12-12 17:49:31 +0100380 size_t byte_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +0200381{
382 switch( curve )
383 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100384 case PSA_ECC_FAMILY_SECP_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100385 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100402 case PSA_ECC_FAMILY_BRAINPOOL_P_R1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100403 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100416 case PSA_ECC_FAMILY_MONTGOMERY:
Gilles Peskine228abc52019-12-03 17:24:19 +0100417 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
Paul Elliott8ff510a2020-06-02 17:19:28 +0100428 case PSA_ECC_FAMILY_SECP_K1:
Gilles Peskine228abc52019-12-03 17:24:19 +0100429 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)
Paul Elliott8ff510a2020-06-02 17:19:28 +0100589static psa_status_t psa_prepare_import_ec_key( psa_ecc_family_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. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100623static psa_status_t psa_import_ec_public_key( psa_ecc_family_t curve,
Jaeden Ameroccdce902019-01-10 11:42:27 +0000624 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. */
Paul Elliott8ff510a2020-06-02 17:19:28 +0100662static psa_status_t psa_import_ec_private_key( psa_ecc_family_t curve,
Gilles Peskinef76aa772018-10-29 19:24:33 +0100663 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 {
Paul Elliott8ff510a2020-06-02 17:19:28 +0100772 status = psa_import_ec_private_key( PSA_KEY_TYPE_ECC_GET_FAMILY( 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(
Paul Elliott8ff510a2020-06-02 17:19:28 +0100779 PSA_KEY_TYPE_ECC_GET_FAMILY( 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{
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001502 psa_status_t status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001503
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001504 status = psa_validate_key_location( psa_get_key_lifetime( attributes ),
1505 p_drv );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001506 if( status != PSA_SUCCESS )
1507 return( status );
1508
Steven Cooreman8c1e7592020-06-17 14:52:05 +02001509 status = psa_validate_key_persistence( psa_get_key_lifetime( attributes ),
1510 psa_get_key_id( attributes ) );
Steven Cooreman81fe7c32020-06-08 18:37:19 +02001511 if( status != PSA_SUCCESS )
1512 return( status );
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001513
1514 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001515 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001516 return( status );
1517
1518 /* Refuse to create overly large keys.
1519 * Note that this doesn't trigger on import if the attributes don't
1520 * explicitly specify a size (so psa_get_key_bits returns 0), so
1521 * psa_import_key() needs its own checks. */
1522 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1523 return( PSA_ERROR_NOT_SUPPORTED );
1524
Gilles Peskine91e8c332019-08-02 19:19:39 +02001525 /* Reject invalid flags. These should not be reachable through the API. */
1526 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1527 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1528 return( PSA_ERROR_INVALID_ARGUMENT );
1529
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001530 return( PSA_SUCCESS );
1531}
1532
Gilles Peskine4747d192019-04-17 15:05:45 +02001533/** Prepare a key slot to receive key material.
1534 *
1535 * This function allocates a key slot and sets its metadata.
1536 *
1537 * If this function fails, call psa_fail_key_creation().
1538 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001539 * This function is intended to be used as follows:
1540 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1541 * it with the specified attributes, and assign it a handle.
1542 * -# Populate the slot with the key material.
1543 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1544 * In case of failure at any step, stop the sequence and call
1545 * psa_fail_key_creation().
1546 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001547 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001548 * \param[in] attributes Key attributes for the new key.
1549 * \param[out] handle On success, a handle for the allocated slot.
1550 * \param[out] p_slot On success, a pointer to the prepared slot.
1551 * \param[out] p_drv On any return, the driver for the key, if any.
1552 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001553 *
1554 * \retval #PSA_SUCCESS
1555 * The key slot is ready to receive key material.
1556 * \return If this function fails, the key slot is an invalid state.
1557 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001558 */
1559static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001560 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001561 const psa_key_attributes_t *attributes,
1562 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001563 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001564 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001565{
1566 psa_status_t status;
1567 psa_key_slot_t *slot;
1568
Gilles Peskinedf179142019-07-15 22:02:14 +02001569 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001570 *p_drv = NULL;
1571
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001572 status = psa_validate_key_attributes( attributes, p_drv );
1573 if( status != PSA_SUCCESS )
1574 return( status );
1575
Gilles Peskineedbed562019-08-07 18:19:59 +02001576 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001577 if( status != PSA_SUCCESS )
1578 return( status );
1579 slot = *p_slot;
1580
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001581 /* We're storing the declared bit-size of the key. It's up to each
1582 * creation mechanism to verify that this information is correct.
1583 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001584 * an input (generate, device) but not for those where the bit-size
1585 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001586
1587 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001588
Gilles Peskine91e8c332019-08-02 19:19:39 +02001589 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001590 * external-only flags, query `attributes`. Thanks to the check
1591 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001592 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001593 * may have set. */
1594 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001595
Gilles Peskinecbaff462019-07-12 23:46:04 +02001596#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001597 /* For a key in a secure element, we need to do three things
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001598 * when creating or registering a persistent key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001599 * create the key file in internal storage, create the
1600 * key inside the secure element, and update the driver's
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001601 * persistent data. This is done by starting a transaction that will
1602 * encompass these three actions.
1603 * For registering a volatile key, we just need to find an appropriate
1604 * slot number inside the SE. Since the key is designated volatile, creating
1605 * a transaction is not required. */
Gilles Peskine60450a42019-07-25 11:32:45 +02001606 /* The first thing to do is to find a slot number for the new key.
1607 * We save the slot number in persistent storage as part of the
1608 * transaction data. It will be needed to recover if the power
1609 * fails during the key creation process, to clean up on the secure
1610 * element side after restarting. Obtaining a slot number from the
1611 * secure element driver updates its persistent state, but we do not yet
1612 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001613 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001614 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001615 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001616 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001617 &slot->data.se.slot_number );
1618 if( status != PSA_SUCCESS )
1619 return( status );
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001620
1621 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( attributes->core.lifetime ) )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001622 {
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001623 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
1624 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
1625 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
1626 psa_crypto_transaction.key.id = slot->attr.id;
1627 status = psa_crypto_save_transaction( );
1628 if( status != PSA_SUCCESS )
1629 {
1630 (void) psa_crypto_stop_transaction( );
1631 return( status );
1632 }
Gilles Peskine66be51c2019-07-25 18:02:52 +02001633 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001634 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001635
1636 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1637 {
1638 /* Key registration only makes sense with a secure element. */
1639 return( PSA_ERROR_INVALID_ARGUMENT );
1640 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001641#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1642
Darryl Green0c6575a2018-11-07 16:05:30 +00001643 return( status );
1644}
Gilles Peskine4747d192019-04-17 15:05:45 +02001645
1646/** Finalize the creation of a key once its key material has been set.
1647 *
1648 * This entails writing the key to persistent storage.
1649 *
1650 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001651 * See the documentation of psa_start_key_creation() for the intended use
1652 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001653 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001654 * \param[in,out] slot Pointer to the slot with key material.
1655 * \param[in] driver The secure element driver for the key,
1656 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001657 *
1658 * \retval #PSA_SUCCESS
1659 * The key was successfully created. The handle is now valid.
1660 * \return If this function fails, the key slot is an invalid state.
1661 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001662 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001663static psa_status_t psa_finish_key_creation(
1664 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001665 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001666{
1667 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001668 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001669 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001670
1671#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Steven Cooremanc59de6a2020-06-08 18:28:25 +02001672 if( ! PSA_KEY_LIFETIME_IS_VOLATILE( slot->attr.lifetime ) )
Gilles Peskine4747d192019-04-17 15:05:45 +02001673 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001674#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1675 if( driver != NULL )
1676 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001677 psa_se_key_data_storage_t data;
1678#if defined(static_assert)
1679 static_assert( sizeof( slot->data.se.slot_number ) ==
1680 sizeof( data.slot_number ),
1681 "Slot number size does not match psa_se_key_data_storage_t" );
1682 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1683 "Bit-size size does not match psa_se_key_data_storage_t" );
1684#endif
1685 memcpy( &data.slot_number, &slot->data.se.slot_number,
1686 sizeof( slot->data.se.slot_number ) );
1687 memcpy( &data.bits, &slot->attr.bits,
1688 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001689 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001690 (uint8_t*) &data,
1691 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001692 }
1693 else
1694#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1695 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001696 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001697 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001698 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001699 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1700 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001701 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001702 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1703 status = psa_internal_export_key( slot,
1704 buffer, buffer_size, &length,
1705 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001706 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001707 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001708 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001709
Gilles Peskinef9168942019-09-12 19:20:29 +02001710 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001711 mbedtls_free( buffer );
1712 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001713 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001714#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1715
Gilles Peskinecbaff462019-07-12 23:46:04 +02001716#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001717 /* Finish the transaction for a key creation. This does not
1718 * happen when registering an existing key. Detect this case
1719 * by checking whether a transaction is in progress (actual
Steven Cooremanbbeaf182020-06-08 18:29:44 +02001720 * creation of a persistent key in a secure element requires a transaction,
1721 * but registration or volatile key creation doesn't use one). */
Gilles Peskined7729582019-08-05 15:55:54 +02001722 if( driver != NULL &&
1723 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001724 {
1725 status = psa_save_se_persistent_data( driver );
1726 if( status != PSA_SUCCESS )
1727 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001728 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001729 return( status );
1730 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001731 status = psa_crypto_stop_transaction( );
1732 if( status != PSA_SUCCESS )
1733 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001734 }
1735#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1736
Gilles Peskine4747d192019-04-17 15:05:45 +02001737 return( status );
1738}
1739
1740/** Abort the creation of a key.
1741 *
1742 * You may call this function after calling psa_start_key_creation(),
1743 * or after psa_finish_key_creation() fails. In other circumstances, this
1744 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001745 * See the documentation of psa_start_key_creation() for the intended use
1746 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001747 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001748 * \param[in,out] slot Pointer to the slot with key material.
1749 * \param[in] driver The secure element driver for the key,
1750 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001751 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001752static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001753 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001754{
Gilles Peskine011e4282019-06-26 18:34:38 +02001755 (void) driver;
1756
Gilles Peskine4747d192019-04-17 15:05:45 +02001757 if( slot == NULL )
1758 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001759
1760#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001761 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001762 * element, and the failure happened later (when saving metadata
1763 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001764 * element.
1765 * https://github.com/ARMmbed/mbed-crypto/issues/217
1766 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001767
Gilles Peskined7729582019-08-05 15:55:54 +02001768 /* Abort the ongoing transaction if any (there may not be one if
1769 * the creation process failed before starting one, or if the
1770 * key creation is a registration of a key in a secure element).
1771 * Earlier functions must already have done what it takes to undo any
1772 * partial creation. All that's left is to update the transaction data
1773 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001774 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001775#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1776
Gilles Peskine4747d192019-04-17 15:05:45 +02001777 psa_wipe_key_slot( slot );
1778}
1779
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001780/** Validate optional attributes during key creation.
1781 *
1782 * Some key attributes are optional during key creation. If they are
1783 * specified in the attributes structure, check that they are consistent
1784 * with the data in the slot.
1785 *
1786 * This function should be called near the end of key creation, after
1787 * the slot in memory is fully populated but before saving persistent data.
1788 */
1789static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001790 const psa_key_slot_t *slot,
1791 const psa_key_attributes_t *attributes )
1792{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001793 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001794 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001795 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001796 return( PSA_ERROR_INVALID_ARGUMENT );
1797 }
1798
1799 if( attributes->domain_parameters_size != 0 )
1800 {
1801#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001802 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001803 {
1804 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001805 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001806 mbedtls_mpi_init( &actual );
1807 mbedtls_mpi_init( &required );
1808 ret = mbedtls_rsa_export( slot->data.rsa,
1809 NULL, NULL, NULL, NULL, &actual );
1810 if( ret != 0 )
1811 goto rsa_exit;
1812 ret = mbedtls_mpi_read_binary( &required,
1813 attributes->domain_parameters,
1814 attributes->domain_parameters_size );
1815 if( ret != 0 )
1816 goto rsa_exit;
1817 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1818 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1819 rsa_exit:
1820 mbedtls_mpi_free( &actual );
1821 mbedtls_mpi_free( &required );
1822 if( ret != 0)
1823 return( mbedtls_to_psa_error( ret ) );
1824 }
1825 else
1826#endif
1827 {
1828 return( PSA_ERROR_INVALID_ARGUMENT );
1829 }
1830 }
1831
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001832 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001833 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001834 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001835 return( PSA_ERROR_INVALID_ARGUMENT );
1836 }
1837
1838 return( PSA_SUCCESS );
1839}
1840
Gilles Peskine4747d192019-04-17 15:05:45 +02001841psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001842 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001843 size_t data_length,
1844 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001845{
1846 psa_status_t status;
1847 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001848 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001849
Gilles Peskine0f84d622019-09-12 19:03:13 +02001850 /* Reject zero-length symmetric keys (including raw data key objects).
1851 * This also rejects any key which might be encoded as an empty string,
1852 * which is never valid. */
1853 if( data_length == 0 )
1854 return( PSA_ERROR_INVALID_ARGUMENT );
1855
Gilles Peskinedf179142019-07-15 22:02:14 +02001856 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1857 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001858 if( status != PSA_SUCCESS )
1859 goto exit;
1860
Gilles Peskine5d309672019-07-12 23:47:28 +02001861#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1862 if( driver != NULL )
1863 {
1864 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001865 /* The driver should set the number of key bits, however in
1866 * case it doesn't, we initialize bits to an invalid value. */
1867 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001868 if( drv->key_management == NULL ||
1869 drv->key_management->p_import == NULL )
1870 {
1871 status = PSA_ERROR_NOT_SUPPORTED;
1872 goto exit;
1873 }
1874 status = drv->key_management->p_import(
1875 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001876 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001877 &bits );
1878 if( status != PSA_SUCCESS )
1879 goto exit;
1880 if( bits > PSA_MAX_KEY_BITS )
1881 {
1882 status = PSA_ERROR_NOT_SUPPORTED;
1883 goto exit;
1884 }
1885 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001886 }
1887 else
1888#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1889 {
1890 status = psa_import_key_into_slot( slot, data, data_length );
1891 if( status != PSA_SUCCESS )
1892 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001893 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001894 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001895 if( status != PSA_SUCCESS )
1896 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001897
Gilles Peskine011e4282019-06-26 18:34:38 +02001898 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001899exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001900 if( status != PSA_SUCCESS )
1901 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001902 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001903 *handle = 0;
1904 }
1905 return( status );
1906}
1907
Gilles Peskined7729582019-08-05 15:55:54 +02001908#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1909psa_status_t mbedtls_psa_register_se_key(
1910 const psa_key_attributes_t *attributes )
1911{
1912 psa_status_t status;
1913 psa_key_slot_t *slot = NULL;
1914 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001915 psa_key_handle_t handle = 0;
1916
1917 /* Leaving attributes unspecified is not currently supported.
1918 * It could make sense to query the key type and size from the
1919 * secure element, but not all secure elements support this
1920 * and the driver HAL doesn't currently support it. */
1921 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1922 return( PSA_ERROR_NOT_SUPPORTED );
1923 if( psa_get_key_bits( attributes ) == 0 )
1924 return( PSA_ERROR_NOT_SUPPORTED );
1925
1926 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1927 &handle, &slot, &driver );
1928 if( status != PSA_SUCCESS )
1929 goto exit;
1930
Gilles Peskined7729582019-08-05 15:55:54 +02001931 status = psa_finish_key_creation( slot, driver );
1932
1933exit:
1934 if( status != PSA_SUCCESS )
1935 {
1936 psa_fail_key_creation( slot, driver );
1937 }
1938 /* Registration doesn't keep the key in RAM. */
1939 psa_close_key( handle );
1940 return( status );
1941}
1942#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1943
Gilles Peskinef603c712019-01-19 13:40:11 +01001944static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001945 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001946{
1947 psa_status_t status;
1948 uint8_t *buffer = NULL;
1949 size_t buffer_size = 0;
1950 size_t length;
1951
Gilles Peskine8e338702019-07-30 20:06:31 +02001952 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001953 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001954 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001955 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001956 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001957 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1958 if( status != PSA_SUCCESS )
1959 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001960 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001961 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001962
1963exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001964 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001965 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001966 return( status );
1967}
1968
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001969psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1970 const psa_key_attributes_t *specified_attributes,
1971 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001972{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001973 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001974 psa_key_slot_t *source_slot = NULL;
1975 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001976 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001977 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001978
Gilles Peskine28f8f302019-07-24 13:30:31 +02001979 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001980 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001981 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001982 goto exit;
1983
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001984 status = psa_validate_optional_attributes( source_slot,
1985 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001986 if( status != PSA_SUCCESS )
1987 goto exit;
1988
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001989 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001990 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001991 if( status != PSA_SUCCESS )
1992 goto exit;
1993
Gilles Peskinedf179142019-07-15 22:02:14 +02001994 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1995 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001996 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 if( status != PSA_SUCCESS )
1998 goto exit;
1999
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002000#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
2001 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01002002 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002003 /* Copying to a secure element is not implemented yet. */
2004 status = PSA_ERROR_NOT_SUPPORTED;
2005 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002006 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02002007#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01002008
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002009 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01002010 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02002011 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01002012
Gilles Peskine011e4282019-06-26 18:34:38 +02002013 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002014exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002015 if( status != PSA_SUCCESS )
2016 {
Gilles Peskine011e4282019-06-26 18:34:38 +02002017 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02002018 *target_handle = 0;
2019 }
2020 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002021}
2022
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002023
2024
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002025/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002026/* Message digests */
2027/****************************************************************/
2028
Gilles Peskineb16841e2019-10-10 20:36:12 +02002029#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002030static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002031{
2032 switch( alg )
2033 {
2034#if defined(MBEDTLS_MD2_C)
2035 case PSA_ALG_MD2:
2036 return( &mbedtls_md2_info );
2037#endif
2038#if defined(MBEDTLS_MD4_C)
2039 case PSA_ALG_MD4:
2040 return( &mbedtls_md4_info );
2041#endif
2042#if defined(MBEDTLS_MD5_C)
2043 case PSA_ALG_MD5:
2044 return( &mbedtls_md5_info );
2045#endif
2046#if defined(MBEDTLS_RIPEMD160_C)
2047 case PSA_ALG_RIPEMD160:
2048 return( &mbedtls_ripemd160_info );
2049#endif
2050#if defined(MBEDTLS_SHA1_C)
2051 case PSA_ALG_SHA_1:
2052 return( &mbedtls_sha1_info );
2053#endif
2054#if defined(MBEDTLS_SHA256_C)
2055 case PSA_ALG_SHA_224:
2056 return( &mbedtls_sha224_info );
2057 case PSA_ALG_SHA_256:
2058 return( &mbedtls_sha256_info );
2059#endif
2060#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002061#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002062 case PSA_ALG_SHA_384:
2063 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002064#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002065 case PSA_ALG_SHA_512:
2066 return( &mbedtls_sha512_info );
2067#endif
2068 default:
2069 return( NULL );
2070 }
2071}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002072#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002073
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002074psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2075{
2076 switch( operation->alg )
2077 {
Gilles Peskine81736312018-06-26 15:04:31 +02002078 case 0:
2079 /* The object has (apparently) been initialized but it is not
2080 * in use. It's ok to call abort on such an object, and there's
2081 * nothing to do. */
2082 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002083#if defined(MBEDTLS_MD2_C)
2084 case PSA_ALG_MD2:
2085 mbedtls_md2_free( &operation->ctx.md2 );
2086 break;
2087#endif
2088#if defined(MBEDTLS_MD4_C)
2089 case PSA_ALG_MD4:
2090 mbedtls_md4_free( &operation->ctx.md4 );
2091 break;
2092#endif
2093#if defined(MBEDTLS_MD5_C)
2094 case PSA_ALG_MD5:
2095 mbedtls_md5_free( &operation->ctx.md5 );
2096 break;
2097#endif
2098#if defined(MBEDTLS_RIPEMD160_C)
2099 case PSA_ALG_RIPEMD160:
2100 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2101 break;
2102#endif
2103#if defined(MBEDTLS_SHA1_C)
2104 case PSA_ALG_SHA_1:
2105 mbedtls_sha1_free( &operation->ctx.sha1 );
2106 break;
2107#endif
2108#if defined(MBEDTLS_SHA256_C)
2109 case PSA_ALG_SHA_224:
2110 case PSA_ALG_SHA_256:
2111 mbedtls_sha256_free( &operation->ctx.sha256 );
2112 break;
2113#endif
2114#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002115#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002116 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002117#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002118 case PSA_ALG_SHA_512:
2119 mbedtls_sha512_free( &operation->ctx.sha512 );
2120 break;
2121#endif
2122 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002123 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002124 }
2125 operation->alg = 0;
2126 return( PSA_SUCCESS );
2127}
2128
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002129psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002130 psa_algorithm_t alg )
2131{
Janos Follath24eed8d2019-11-22 13:21:35 +00002132 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002133
2134 /* A context must be freshly initialized before it can be set up. */
2135 if( operation->alg != 0 )
2136 {
2137 return( PSA_ERROR_BAD_STATE );
2138 }
2139
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002140 switch( alg )
2141 {
2142#if defined(MBEDTLS_MD2_C)
2143 case PSA_ALG_MD2:
2144 mbedtls_md2_init( &operation->ctx.md2 );
2145 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2146 break;
2147#endif
2148#if defined(MBEDTLS_MD4_C)
2149 case PSA_ALG_MD4:
2150 mbedtls_md4_init( &operation->ctx.md4 );
2151 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2152 break;
2153#endif
2154#if defined(MBEDTLS_MD5_C)
2155 case PSA_ALG_MD5:
2156 mbedtls_md5_init( &operation->ctx.md5 );
2157 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2158 break;
2159#endif
2160#if defined(MBEDTLS_RIPEMD160_C)
2161 case PSA_ALG_RIPEMD160:
2162 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2163 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2164 break;
2165#endif
2166#if defined(MBEDTLS_SHA1_C)
2167 case PSA_ALG_SHA_1:
2168 mbedtls_sha1_init( &operation->ctx.sha1 );
2169 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2170 break;
2171#endif
2172#if defined(MBEDTLS_SHA256_C)
2173 case PSA_ALG_SHA_224:
2174 mbedtls_sha256_init( &operation->ctx.sha256 );
2175 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2176 break;
2177 case PSA_ALG_SHA_256:
2178 mbedtls_sha256_init( &operation->ctx.sha256 );
2179 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2180 break;
2181#endif
2182#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002183#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002184 case PSA_ALG_SHA_384:
2185 mbedtls_sha512_init( &operation->ctx.sha512 );
2186 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2187 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002188#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002189 case PSA_ALG_SHA_512:
2190 mbedtls_sha512_init( &operation->ctx.sha512 );
2191 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2192 break;
2193#endif
2194 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002195 return( PSA_ALG_IS_HASH( alg ) ?
2196 PSA_ERROR_NOT_SUPPORTED :
2197 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002198 }
2199 if( ret == 0 )
2200 operation->alg = alg;
2201 else
2202 psa_hash_abort( operation );
2203 return( mbedtls_to_psa_error( ret ) );
2204}
2205
2206psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2207 const uint8_t *input,
2208 size_t input_length )
2209{
Janos Follath24eed8d2019-11-22 13:21:35 +00002210 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002211
2212 /* Don't require hash implementations to behave correctly on a
2213 * zero-length input, which may have an invalid pointer. */
2214 if( input_length == 0 )
2215 return( PSA_SUCCESS );
2216
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002217 switch( operation->alg )
2218 {
2219#if defined(MBEDTLS_MD2_C)
2220 case PSA_ALG_MD2:
2221 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2222 input, input_length );
2223 break;
2224#endif
2225#if defined(MBEDTLS_MD4_C)
2226 case PSA_ALG_MD4:
2227 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2228 input, input_length );
2229 break;
2230#endif
2231#if defined(MBEDTLS_MD5_C)
2232 case PSA_ALG_MD5:
2233 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2234 input, input_length );
2235 break;
2236#endif
2237#if defined(MBEDTLS_RIPEMD160_C)
2238 case PSA_ALG_RIPEMD160:
2239 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2240 input, input_length );
2241 break;
2242#endif
2243#if defined(MBEDTLS_SHA1_C)
2244 case PSA_ALG_SHA_1:
2245 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2246 input, input_length );
2247 break;
2248#endif
2249#if defined(MBEDTLS_SHA256_C)
2250 case PSA_ALG_SHA_224:
2251 case PSA_ALG_SHA_256:
2252 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2253 input, input_length );
2254 break;
2255#endif
2256#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002257#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002258 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002259#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002260 case PSA_ALG_SHA_512:
2261 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2262 input, input_length );
2263 break;
2264#endif
2265 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002266 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002267 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002268
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002269 if( ret != 0 )
2270 psa_hash_abort( operation );
2271 return( mbedtls_to_psa_error( ret ) );
2272}
2273
2274psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2275 uint8_t *hash,
2276 size_t hash_size,
2277 size_t *hash_length )
2278{
itayzafrir40835d42018-08-02 13:14:17 +03002279 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002280 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002281 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002282
2283 /* Fill the output buffer with something that isn't a valid hash
2284 * (barring an attack on the hash and deliberately-crafted input),
2285 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002286 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002287 /* If hash_size is 0 then hash may be NULL and then the
2288 * call to memset would have undefined behavior. */
2289 if( hash_size != 0 )
2290 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002291
2292 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002293 {
2294 status = PSA_ERROR_BUFFER_TOO_SMALL;
2295 goto exit;
2296 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002297
2298 switch( operation->alg )
2299 {
2300#if defined(MBEDTLS_MD2_C)
2301 case PSA_ALG_MD2:
2302 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2303 break;
2304#endif
2305#if defined(MBEDTLS_MD4_C)
2306 case PSA_ALG_MD4:
2307 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2308 break;
2309#endif
2310#if defined(MBEDTLS_MD5_C)
2311 case PSA_ALG_MD5:
2312 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2313 break;
2314#endif
2315#if defined(MBEDTLS_RIPEMD160_C)
2316 case PSA_ALG_RIPEMD160:
2317 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2318 break;
2319#endif
2320#if defined(MBEDTLS_SHA1_C)
2321 case PSA_ALG_SHA_1:
2322 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2323 break;
2324#endif
2325#if defined(MBEDTLS_SHA256_C)
2326 case PSA_ALG_SHA_224:
2327 case PSA_ALG_SHA_256:
2328 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2329 break;
2330#endif
2331#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002332#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002334#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002335 case PSA_ALG_SHA_512:
2336 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2337 break;
2338#endif
2339 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002340 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002341 }
itayzafrir40835d42018-08-02 13:14:17 +03002342 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002343
itayzafrir40835d42018-08-02 13:14:17 +03002344exit:
2345 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002346 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002347 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002348 return( psa_hash_abort( operation ) );
2349 }
2350 else
2351 {
2352 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002353 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002354 }
2355}
2356
Gilles Peskine2d277862018-06-18 15:41:12 +02002357psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2358 const uint8_t *hash,
2359 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002360{
2361 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2362 size_t actual_hash_length;
2363 psa_status_t status = psa_hash_finish( operation,
2364 actual_hash, sizeof( actual_hash ),
2365 &actual_hash_length );
2366 if( status != PSA_SUCCESS )
2367 return( status );
2368 if( actual_hash_length != hash_length )
2369 return( PSA_ERROR_INVALID_SIGNATURE );
2370 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2371 return( PSA_ERROR_INVALID_SIGNATURE );
2372 return( PSA_SUCCESS );
2373}
2374
Gilles Peskine0a749c82019-11-28 19:33:58 +01002375psa_status_t psa_hash_compute( psa_algorithm_t alg,
2376 const uint8_t *input, size_t input_length,
2377 uint8_t *hash, size_t hash_size,
2378 size_t *hash_length )
2379{
2380 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2381 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2382
2383 *hash_length = hash_size;
2384 status = psa_hash_setup( &operation, alg );
2385 if( status != PSA_SUCCESS )
2386 goto exit;
2387 status = psa_hash_update( &operation, input, input_length );
2388 if( status != PSA_SUCCESS )
2389 goto exit;
2390 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2391 if( status != PSA_SUCCESS )
2392 goto exit;
2393
2394exit:
2395 if( status == PSA_SUCCESS )
2396 status = psa_hash_abort( &operation );
2397 else
2398 psa_hash_abort( &operation );
2399 return( status );
2400}
2401
2402psa_status_t psa_hash_compare( psa_algorithm_t alg,
2403 const uint8_t *input, size_t input_length,
2404 const uint8_t *hash, size_t hash_length )
2405{
2406 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2407 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2408
2409 status = psa_hash_setup( &operation, alg );
2410 if( status != PSA_SUCCESS )
2411 goto exit;
2412 status = psa_hash_update( &operation, input, input_length );
2413 if( status != PSA_SUCCESS )
2414 goto exit;
2415 status = psa_hash_verify( &operation, hash, hash_length );
2416 if( status != PSA_SUCCESS )
2417 goto exit;
2418
2419exit:
2420 if( status == PSA_SUCCESS )
2421 status = psa_hash_abort( &operation );
2422 else
2423 psa_hash_abort( &operation );
2424 return( status );
2425}
2426
Gilles Peskineeb35d782019-01-22 17:56:16 +01002427psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2428 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002429{
2430 if( target_operation->alg != 0 )
2431 return( PSA_ERROR_BAD_STATE );
2432
2433 switch( source_operation->alg )
2434 {
2435 case 0:
2436 return( PSA_ERROR_BAD_STATE );
2437#if defined(MBEDTLS_MD2_C)
2438 case PSA_ALG_MD2:
2439 mbedtls_md2_clone( &target_operation->ctx.md2,
2440 &source_operation->ctx.md2 );
2441 break;
2442#endif
2443#if defined(MBEDTLS_MD4_C)
2444 case PSA_ALG_MD4:
2445 mbedtls_md4_clone( &target_operation->ctx.md4,
2446 &source_operation->ctx.md4 );
2447 break;
2448#endif
2449#if defined(MBEDTLS_MD5_C)
2450 case PSA_ALG_MD5:
2451 mbedtls_md5_clone( &target_operation->ctx.md5,
2452 &source_operation->ctx.md5 );
2453 break;
2454#endif
2455#if defined(MBEDTLS_RIPEMD160_C)
2456 case PSA_ALG_RIPEMD160:
2457 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2458 &source_operation->ctx.ripemd160 );
2459 break;
2460#endif
2461#if defined(MBEDTLS_SHA1_C)
2462 case PSA_ALG_SHA_1:
2463 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2464 &source_operation->ctx.sha1 );
2465 break;
2466#endif
2467#if defined(MBEDTLS_SHA256_C)
2468 case PSA_ALG_SHA_224:
2469 case PSA_ALG_SHA_256:
2470 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2471 &source_operation->ctx.sha256 );
2472 break;
2473#endif
2474#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002475#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002476 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002477#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002478 case PSA_ALG_SHA_512:
2479 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2480 &source_operation->ctx.sha512 );
2481 break;
2482#endif
2483 default:
2484 return( PSA_ERROR_NOT_SUPPORTED );
2485 }
2486
2487 target_operation->alg = source_operation->alg;
2488 return( PSA_SUCCESS );
2489}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002490
2491
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002492/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493/* MAC */
2494/****************************************************************/
2495
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002496static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002497 psa_algorithm_t alg,
2498 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002499 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002500 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002501{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002502 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002503 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002504
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002505 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002506 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002507
Gilles Peskine8c9def32018-02-08 10:02:12 +01002508 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2509 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002510 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002512 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002513 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 mode = MBEDTLS_MODE_STREAM;
2515 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002516 case PSA_ALG_CTR:
2517 mode = MBEDTLS_MODE_CTR;
2518 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002519 case PSA_ALG_CFB:
2520 mode = MBEDTLS_MODE_CFB;
2521 break;
2522 case PSA_ALG_OFB:
2523 mode = MBEDTLS_MODE_OFB;
2524 break;
2525 case PSA_ALG_CBC_NO_PADDING:
2526 mode = MBEDTLS_MODE_CBC;
2527 break;
2528 case PSA_ALG_CBC_PKCS7:
2529 mode = MBEDTLS_MODE_CBC;
2530 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002531 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 mode = MBEDTLS_MODE_CCM;
2533 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002534 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002535 mode = MBEDTLS_MODE_GCM;
2536 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002537 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2538 mode = MBEDTLS_MODE_CHACHAPOLY;
2539 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002540 default:
2541 return( NULL );
2542 }
2543 }
2544 else if( alg == PSA_ALG_CMAC )
2545 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002546 else
2547 return( NULL );
2548
2549 switch( key_type )
2550 {
2551 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002552 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002553 break;
2554 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002555 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2556 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002557 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002558 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002559 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002560 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002561 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2562 * but two-key Triple-DES is functionally three-key Triple-DES
2563 * with K1=K3, so that's how we present it to mbedtls. */
2564 if( key_bits == 128 )
2565 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002566 break;
2567 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002568 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002569 break;
2570 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002571 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002572 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002573 case PSA_KEY_TYPE_CHACHA20:
2574 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2575 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002576 default:
2577 return( NULL );
2578 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002579 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002580 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002581
Jaeden Amero23bbb752018-06-26 14:16:54 +01002582 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2583 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002584}
2585
Gilles Peskinea05219c2018-11-16 16:02:56 +01002586#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002587static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002588{
Gilles Peskine2d277862018-06-18 15:41:12 +02002589 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 {
2591 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002592 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002593 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002594 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002595 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002596 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002597 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002598 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002599 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002600 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002601 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002602 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002603 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002604 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002605 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002606 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002607 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002608 return( 128 );
2609 default:
2610 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002611 }
2612}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002613#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002614
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002615/* Initialize the MAC operation structure. Once this function has been
2616 * called, psa_mac_abort can run and will do the right thing. */
2617static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2618 psa_algorithm_t alg )
2619{
2620 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2621
2622 operation->alg = alg;
2623 operation->key_set = 0;
2624 operation->iv_set = 0;
2625 operation->iv_required = 0;
2626 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002627 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002628
2629#if defined(MBEDTLS_CMAC_C)
2630 if( alg == PSA_ALG_CMAC )
2631 {
2632 operation->iv_required = 0;
2633 mbedtls_cipher_init( &operation->ctx.cmac );
2634 status = PSA_SUCCESS;
2635 }
2636 else
2637#endif /* MBEDTLS_CMAC_C */
2638#if defined(MBEDTLS_MD_C)
2639 if( PSA_ALG_IS_HMAC( operation->alg ) )
2640 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002641 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2642 operation->ctx.hmac.hash_ctx.alg = 0;
2643 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002644 }
2645 else
2646#endif /* MBEDTLS_MD_C */
2647 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002648 if( ! PSA_ALG_IS_MAC( alg ) )
2649 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002650 }
2651
2652 if( status != PSA_SUCCESS )
2653 memset( operation, 0, sizeof( *operation ) );
2654 return( status );
2655}
2656
Gilles Peskine01126fa2018-07-12 17:04:55 +02002657#if defined(MBEDTLS_MD_C)
2658static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2659{
Gilles Peskine3f108122018-12-07 18:14:53 +01002660 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002661 return( psa_hash_abort( &hmac->hash_ctx ) );
2662}
2663#endif /* MBEDTLS_MD_C */
2664
Gilles Peskine8c9def32018-02-08 10:02:12 +01002665psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2666{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002667 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002668 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002669 /* The object has (apparently) been initialized but it is not
2670 * in use. It's ok to call abort on such an object, and there's
2671 * nothing to do. */
2672 return( PSA_SUCCESS );
2673 }
2674 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002675#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002676 if( operation->alg == PSA_ALG_CMAC )
2677 {
2678 mbedtls_cipher_free( &operation->ctx.cmac );
2679 }
2680 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002682#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002683 if( PSA_ALG_IS_HMAC( operation->alg ) )
2684 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002685 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002686 }
2687 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002688#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002689 {
2690 /* Sanity check (shouldn't happen: operation->alg should
2691 * always have been initialized to a valid value). */
2692 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002693 }
Moran Peker41deec42018-04-04 15:43:05 +03002694
Gilles Peskine8c9def32018-02-08 10:02:12 +01002695 operation->alg = 0;
2696 operation->key_set = 0;
2697 operation->iv_set = 0;
2698 operation->iv_required = 0;
2699 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002700 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002701
Gilles Peskine8c9def32018-02-08 10:02:12 +01002702 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002703
2704bad_state:
2705 /* If abort is called on an uninitialized object, we can't trust
2706 * anything. Wipe the object in case it contains confidential data.
2707 * This may result in a memory leak if a pointer gets overwritten,
2708 * but it's too late to do anything about this. */
2709 memset( operation, 0, sizeof( *operation ) );
2710 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002711}
2712
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002713#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002714static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002715 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002716 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002717 const mbedtls_cipher_info_t *cipher_info )
2718{
Janos Follath24eed8d2019-11-22 13:21:35 +00002719 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720
2721 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002722
2723 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2724 if( ret != 0 )
2725 return( ret );
2726
2727 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2728 slot->data.raw.data,
2729 key_bits );
2730 return( ret );
2731}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002732#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002733
Gilles Peskine248051a2018-06-20 16:09:38 +02002734#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002735static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2736 const uint8_t *key,
2737 size_t key_length,
2738 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002739{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002740 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002741 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002742 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002743 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002744 psa_status_t status;
2745
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002746 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2747 * overflow below. This should never trigger if the hash algorithm
2748 * is implemented correctly. */
2749 /* The size checks against the ipad and opad buffers cannot be written
2750 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2751 * because that triggers -Wlogical-op on GCC 7.3. */
2752 if( block_size > sizeof( ipad ) )
2753 return( PSA_ERROR_NOT_SUPPORTED );
2754 if( block_size > sizeof( hmac->opad ) )
2755 return( PSA_ERROR_NOT_SUPPORTED );
2756 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757 return( PSA_ERROR_NOT_SUPPORTED );
2758
Gilles Peskined223b522018-06-11 18:12:58 +02002759 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760 {
Gilles Peskine84b8fc82019-11-28 20:07:20 +01002761 status = psa_hash_compute( hash_alg, key, key_length,
2762 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002763 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002764 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002765 }
Gilles Peskine96889972018-07-12 17:07:03 +02002766 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2767 * but it is permitted. It is common when HMAC is used in HKDF, for
2768 * example. Don't call `memcpy` in the 0-length because `key` could be
2769 * an invalid pointer which would make the behavior undefined. */
2770 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002771 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002772
Gilles Peskined223b522018-06-11 18:12:58 +02002773 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2774 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002775 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002776 ipad[i] ^= 0x36;
2777 memset( ipad + key_length, 0x36, block_size - key_length );
2778
2779 /* Copy the key material from ipad to opad, flipping the requisite bits,
2780 * and filling the rest of opad with the requisite constant. */
2781 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002782 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2783 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002784
Gilles Peskine01126fa2018-07-12 17:04:55 +02002785 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002786 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002787 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002788
Gilles Peskine01126fa2018-07-12 17:04:55 +02002789 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002790
2791cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002792 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002793
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002794 return( status );
2795}
Gilles Peskine248051a2018-06-20 16:09:38 +02002796#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002797
Gilles Peskine89167cb2018-07-08 20:12:23 +02002798static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002799 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002800 psa_algorithm_t alg,
2801 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002802{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002803 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002804 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002805 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002806 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002807 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002808 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002809 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002810
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002811 /* A context must be freshly initialized before it can be set up. */
2812 if( operation->alg != 0 )
2813 {
2814 return( PSA_ERROR_BAD_STATE );
2815 }
2816
Gilles Peskined911eb72018-08-14 15:18:45 +02002817 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002818 if( status != PSA_SUCCESS )
2819 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002820 if( is_sign )
2821 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002822
Gilles Peskine28f8f302019-07-24 13:30:31 +02002823 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002824 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002825 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002826 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002827
Gilles Peskine8c9def32018-02-08 10:02:12 +01002828#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002829 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002830 {
2831 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002832 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002833 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002834 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002835 if( cipher_info == NULL )
2836 {
2837 status = PSA_ERROR_NOT_SUPPORTED;
2838 goto exit;
2839 }
2840 operation->mac_size = cipher_info->block_size;
2841 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2842 status = mbedtls_to_psa_error( ret );
2843 }
2844 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002845#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002846#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002847 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002848 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002849 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002850 if( hash_alg == 0 )
2851 {
2852 status = PSA_ERROR_NOT_SUPPORTED;
2853 goto exit;
2854 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002855
2856 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2857 /* Sanity check. This shouldn't fail on a valid configuration. */
2858 if( operation->mac_size == 0 ||
2859 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2860 {
2861 status = PSA_ERROR_NOT_SUPPORTED;
2862 goto exit;
2863 }
2864
Gilles Peskine8e338702019-07-30 20:06:31 +02002865 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002866 {
2867 status = PSA_ERROR_INVALID_ARGUMENT;
2868 goto exit;
2869 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002870
Gilles Peskine01126fa2018-07-12 17:04:55 +02002871 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2872 slot->data.raw.data,
2873 slot->data.raw.bytes,
2874 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002875 }
2876 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002877#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002878 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002879 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002880 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002881 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002882
Gilles Peskined911eb72018-08-14 15:18:45 +02002883 if( truncated == 0 )
2884 {
2885 /* The "normal" case: untruncated algorithm. Nothing to do. */
2886 }
2887 else if( truncated < 4 )
2888 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002889 /* A very short MAC is too short for security since it can be
2890 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2891 * so we make this our minimum, even though 32 bits is still
2892 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002893 status = PSA_ERROR_NOT_SUPPORTED;
2894 }
2895 else if( truncated > operation->mac_size )
2896 {
2897 /* It's impossible to "truncate" to a larger length. */
2898 status = PSA_ERROR_INVALID_ARGUMENT;
2899 }
2900 else
2901 operation->mac_size = truncated;
2902
Gilles Peskinefbfac682018-07-08 20:51:54 +02002903exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002904 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002905 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002906 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002907 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002908 else
2909 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002910 operation->key_set = 1;
2911 }
2912 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002913}
2914
Gilles Peskine89167cb2018-07-08 20:12:23 +02002915psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002916 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002917 psa_algorithm_t alg )
2918{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002919 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002920}
2921
2922psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002923 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002924 psa_algorithm_t alg )
2925{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002926 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002927}
2928
Gilles Peskine8c9def32018-02-08 10:02:12 +01002929psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2930 const uint8_t *input,
2931 size_t input_length )
2932{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002933 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002934 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002935 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002936 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002937 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002938 operation->has_input = 1;
2939
Gilles Peskine8c9def32018-02-08 10:02:12 +01002940#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002941 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002942 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002943 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2944 input, input_length );
2945 status = mbedtls_to_psa_error( ret );
2946 }
2947 else
2948#endif /* MBEDTLS_CMAC_C */
2949#if defined(MBEDTLS_MD_C)
2950 if( PSA_ALG_IS_HMAC( operation->alg ) )
2951 {
2952 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2953 input_length );
2954 }
2955 else
2956#endif /* MBEDTLS_MD_C */
2957 {
2958 /* This shouldn't happen if `operation` was initialized by
2959 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002960 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002961 }
2962
Gilles Peskinefbfac682018-07-08 20:51:54 +02002963 if( status != PSA_SUCCESS )
2964 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002965 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002966}
2967
Gilles Peskine01126fa2018-07-12 17:04:55 +02002968#if defined(MBEDTLS_MD_C)
2969static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2970 uint8_t *mac,
2971 size_t mac_size )
2972{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002973 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002974 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2975 size_t hash_size = 0;
2976 size_t block_size = psa_get_hash_block_size( hash_alg );
2977 psa_status_t status;
2978
Gilles Peskine01126fa2018-07-12 17:04:55 +02002979 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2980 if( status != PSA_SUCCESS )
2981 return( status );
2982 /* From here on, tmp needs to be wiped. */
2983
2984 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2985 if( status != PSA_SUCCESS )
2986 goto exit;
2987
2988 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2989 if( status != PSA_SUCCESS )
2990 goto exit;
2991
2992 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2993 if( status != PSA_SUCCESS )
2994 goto exit;
2995
Gilles Peskined911eb72018-08-14 15:18:45 +02002996 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2997 if( status != PSA_SUCCESS )
2998 goto exit;
2999
3000 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003001
3002exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01003003 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02003004 return( status );
3005}
3006#endif /* MBEDTLS_MD_C */
3007
mohammad16036df908f2018-04-02 08:34:15 -07003008static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02003009 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003010 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003011{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02003012 if( ! operation->key_set )
3013 return( PSA_ERROR_BAD_STATE );
3014 if( operation->iv_required && ! operation->iv_set )
3015 return( PSA_ERROR_BAD_STATE );
3016
Gilles Peskine8c9def32018-02-08 10:02:12 +01003017 if( mac_size < operation->mac_size )
3018 return( PSA_ERROR_BUFFER_TOO_SMALL );
3019
Gilles Peskine8c9def32018-02-08 10:02:12 +01003020#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003021 if( operation->alg == PSA_ALG_CMAC )
3022 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003023 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3024 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3025 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003026 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003027 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003028 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003029 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003030 else
3031#endif /* MBEDTLS_CMAC_C */
3032#if defined(MBEDTLS_MD_C)
3033 if( PSA_ALG_IS_HMAC( operation->alg ) )
3034 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003035 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003036 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003037 }
3038 else
3039#endif /* MBEDTLS_MD_C */
3040 {
3041 /* This shouldn't happen if `operation` was initialized by
3042 * a setup function. */
3043 return( PSA_ERROR_BAD_STATE );
3044 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003045}
3046
Gilles Peskineacd4be32018-07-08 19:56:25 +02003047psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3048 uint8_t *mac,
3049 size_t mac_size,
3050 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003051{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003052 psa_status_t status;
3053
Jaeden Amero252ef282019-02-15 14:05:35 +00003054 if( operation->alg == 0 )
3055 {
3056 return( PSA_ERROR_BAD_STATE );
3057 }
3058
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003059 /* Fill the output buffer with something that isn't a valid mac
3060 * (barring an attack on the mac and deliberately-crafted input),
3061 * in case the caller doesn't check the return status properly. */
3062 *mac_length = mac_size;
3063 /* If mac_size is 0 then mac may be NULL and then the
3064 * call to memset would have undefined behavior. */
3065 if( mac_size != 0 )
3066 memset( mac, '!', mac_size );
3067
Gilles Peskine89167cb2018-07-08 20:12:23 +02003068 if( ! operation->is_sign )
3069 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003070 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003071 }
mohammad16036df908f2018-04-02 08:34:15 -07003072
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003073 status = psa_mac_finish_internal( operation, mac, mac_size );
3074
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003075 if( status == PSA_SUCCESS )
3076 {
3077 status = psa_mac_abort( operation );
3078 if( status == PSA_SUCCESS )
3079 *mac_length = operation->mac_size;
3080 else
3081 memset( mac, '!', mac_size );
3082 }
3083 else
3084 psa_mac_abort( operation );
3085 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003086}
3087
Gilles Peskineacd4be32018-07-08 19:56:25 +02003088psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3089 const uint8_t *mac,
3090 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003091{
Gilles Peskine828ed142018-06-18 23:25:51 +02003092 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003093 psa_status_t status;
3094
Jaeden Amero252ef282019-02-15 14:05:35 +00003095 if( operation->alg == 0 )
3096 {
3097 return( PSA_ERROR_BAD_STATE );
3098 }
3099
Gilles Peskine89167cb2018-07-08 20:12:23 +02003100 if( operation->is_sign )
3101 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003103 }
3104 if( operation->mac_size != mac_length )
3105 {
3106 status = PSA_ERROR_INVALID_SIGNATURE;
3107 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003108 }
mohammad16036df908f2018-04-02 08:34:15 -07003109
3110 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003111 actual_mac, sizeof( actual_mac ) );
Gilles Peskine28cd4162020-01-20 16:31:06 +01003112 if( status != PSA_SUCCESS )
3113 goto cleanup;
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003114
3115 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3116 status = PSA_ERROR_INVALID_SIGNATURE;
3117
3118cleanup:
3119 if( status == PSA_SUCCESS )
3120 status = psa_mac_abort( operation );
3121 else
3122 psa_mac_abort( operation );
3123
Gilles Peskine3f108122018-12-07 18:14:53 +01003124 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003125
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003126 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003127}
3128
3129
Gilles Peskine20035e32018-02-03 22:44:14 +01003130
Gilles Peskine20035e32018-02-03 22:44:14 +01003131/****************************************************************/
3132/* Asymmetric cryptography */
3133/****************************************************************/
3134
Gilles Peskine2b450e32018-06-27 15:42:46 +02003135#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003136/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003137 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003138static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3139 size_t hash_length,
3140 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003141{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003142 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003143 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003144 *md_alg = mbedtls_md_get_type( md_info );
3145
3146 /* The Mbed TLS RSA module uses an unsigned int for hash length
3147 * parameters. Validate that it fits so that we don't risk an
3148 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003149#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003150 if( hash_length > UINT_MAX )
3151 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003152#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003153
3154#if defined(MBEDTLS_PKCS1_V15)
3155 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3156 * must be correct. */
3157 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3158 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003159 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003160 if( md_info == NULL )
3161 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003162 if( mbedtls_md_get_size( md_info ) != hash_length )
3163 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003164 }
3165#endif /* MBEDTLS_PKCS1_V15 */
3166
3167#if defined(MBEDTLS_PKCS1_V21)
3168 /* PSS requires a hash internally. */
3169 if( PSA_ALG_IS_RSA_PSS( alg ) )
3170 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003171 if( md_info == NULL )
3172 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003173 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003174#endif /* MBEDTLS_PKCS1_V21 */
3175
Gilles Peskine61b91d42018-06-08 16:09:36 +02003176 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003177}
3178
Gilles Peskine2b450e32018-06-27 15:42:46 +02003179static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3180 psa_algorithm_t alg,
3181 const uint8_t *hash,
3182 size_t hash_length,
3183 uint8_t *signature,
3184 size_t signature_size,
3185 size_t *signature_length )
3186{
3187 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003188 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003189 mbedtls_md_type_t md_alg;
3190
3191 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3192 if( status != PSA_SUCCESS )
3193 return( status );
3194
Gilles Peskine630a18a2018-06-29 17:49:35 +02003195 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003196 return( PSA_ERROR_BUFFER_TOO_SMALL );
3197
3198#if defined(MBEDTLS_PKCS1_V15)
3199 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3200 {
3201 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3202 MBEDTLS_MD_NONE );
3203 ret = mbedtls_rsa_pkcs1_sign( rsa,
3204 mbedtls_ctr_drbg_random,
3205 &global_data.ctr_drbg,
3206 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003207 md_alg,
3208 (unsigned int) hash_length,
3209 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003210 signature );
3211 }
3212 else
3213#endif /* MBEDTLS_PKCS1_V15 */
3214#if defined(MBEDTLS_PKCS1_V21)
3215 if( PSA_ALG_IS_RSA_PSS( alg ) )
3216 {
3217 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3218 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3219 mbedtls_ctr_drbg_random,
3220 &global_data.ctr_drbg,
3221 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003222 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003223 (unsigned int) hash_length,
3224 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003225 signature );
3226 }
3227 else
3228#endif /* MBEDTLS_PKCS1_V21 */
3229 {
3230 return( PSA_ERROR_INVALID_ARGUMENT );
3231 }
3232
3233 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003234 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003235 return( mbedtls_to_psa_error( ret ) );
3236}
3237
3238static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3239 psa_algorithm_t alg,
3240 const uint8_t *hash,
3241 size_t hash_length,
3242 const uint8_t *signature,
3243 size_t signature_length )
3244{
3245 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003246 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003247 mbedtls_md_type_t md_alg;
3248
3249 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3250 if( status != PSA_SUCCESS )
3251 return( status );
3252
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003253 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3254 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003255
3256#if defined(MBEDTLS_PKCS1_V15)
3257 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3258 {
3259 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3260 MBEDTLS_MD_NONE );
3261 ret = mbedtls_rsa_pkcs1_verify( rsa,
3262 mbedtls_ctr_drbg_random,
3263 &global_data.ctr_drbg,
3264 MBEDTLS_RSA_PUBLIC,
3265 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003266 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003267 hash,
3268 signature );
3269 }
3270 else
3271#endif /* MBEDTLS_PKCS1_V15 */
3272#if defined(MBEDTLS_PKCS1_V21)
3273 if( PSA_ALG_IS_RSA_PSS( alg ) )
3274 {
3275 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3276 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3277 mbedtls_ctr_drbg_random,
3278 &global_data.ctr_drbg,
3279 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003280 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003281 (unsigned int) hash_length,
3282 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003283 signature );
3284 }
3285 else
3286#endif /* MBEDTLS_PKCS1_V21 */
3287 {
3288 return( PSA_ERROR_INVALID_ARGUMENT );
3289 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003290
3291 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3292 * the rest of the signature is invalid". This has little use in
3293 * practice and PSA doesn't report this distinction. */
3294 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3295 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003296 return( mbedtls_to_psa_error( ret ) );
3297}
3298#endif /* MBEDTLS_RSA_C */
3299
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003300#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003301/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3302 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3303 * (even though these functions don't modify it). */
3304static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3305 psa_algorithm_t alg,
3306 const uint8_t *hash,
3307 size_t hash_length,
3308 uint8_t *signature,
3309 size_t signature_size,
3310 size_t *signature_length )
3311{
Janos Follath24eed8d2019-11-22 13:21:35 +00003312 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003313 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003314 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003315 mbedtls_mpi_init( &r );
3316 mbedtls_mpi_init( &s );
3317
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003318 if( signature_size < 2 * curve_bytes )
3319 {
3320 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3321 goto cleanup;
3322 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003323
Gilles Peskinea05219c2018-11-16 16:02:56 +01003324#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003325 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3326 {
3327 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3328 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3329 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003330 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3331 &ecp->d, hash,
3332 hash_length, md_alg,
3333 mbedtls_ctr_drbg_random,
3334 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003335 }
3336 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003337#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003338 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003339 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003340 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3341 hash, hash_length,
3342 mbedtls_ctr_drbg_random,
3343 &global_data.ctr_drbg ) );
3344 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003345
3346 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3347 signature,
3348 curve_bytes ) );
3349 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3350 signature + curve_bytes,
3351 curve_bytes ) );
3352
3353cleanup:
3354 mbedtls_mpi_free( &r );
3355 mbedtls_mpi_free( &s );
3356 if( ret == 0 )
3357 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003358 return( mbedtls_to_psa_error( ret ) );
3359}
3360
3361static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3362 const uint8_t *hash,
3363 size_t hash_length,
3364 const uint8_t *signature,
3365 size_t signature_length )
3366{
Janos Follath24eed8d2019-11-22 13:21:35 +00003367 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003368 mbedtls_mpi r, s;
3369 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3370 mbedtls_mpi_init( &r );
3371 mbedtls_mpi_init( &s );
3372
3373 if( signature_length != 2 * curve_bytes )
3374 return( PSA_ERROR_INVALID_SIGNATURE );
3375
3376 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3377 signature,
3378 curve_bytes ) );
3379 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3380 signature + curve_bytes,
3381 curve_bytes ) );
3382
3383 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3384 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003385
3386cleanup:
3387 mbedtls_mpi_free( &r );
3388 mbedtls_mpi_free( &s );
3389 return( mbedtls_to_psa_error( ret ) );
3390}
3391#endif /* MBEDTLS_ECDSA_C */
3392
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003393psa_status_t psa_sign_hash( psa_key_handle_t handle,
3394 psa_algorithm_t alg,
3395 const uint8_t *hash,
3396 size_t hash_length,
3397 uint8_t *signature,
3398 size_t signature_size,
3399 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003400{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003401 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003402 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003403#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3404 const psa_drv_se_t *drv;
3405 psa_drv_se_context_t *drv_context;
3406#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003407
3408 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003409 /* Immediately reject a zero-length signature buffer. This guarantees
3410 * that signature must be a valid pointer. (On the other hand, the hash
3411 * buffer can in principle be empty since it doesn't actually have
3412 * to be a hash.) */
3413 if( signature_size == 0 )
3414 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003415
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003416 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003417 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003418 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003419 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003420 {
3421 status = PSA_ERROR_INVALID_ARGUMENT;
3422 goto exit;
3423 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003424
Gilles Peskineedc64242019-08-07 21:05:07 +02003425#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3426 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3427 {
3428 if( drv->asymmetric == NULL ||
3429 drv->asymmetric->p_sign == NULL )
3430 {
3431 status = PSA_ERROR_NOT_SUPPORTED;
3432 goto exit;
3433 }
3434 status = drv->asymmetric->p_sign( drv_context,
3435 slot->data.se.slot_number,
3436 alg,
3437 hash, hash_length,
3438 signature, signature_size,
3439 signature_length );
3440 }
3441 else
3442#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003443#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003444 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003445 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003446 status = psa_rsa_sign( slot->data.rsa,
3447 alg,
3448 hash, hash_length,
3449 signature, signature_size,
3450 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003451 }
3452 else
3453#endif /* defined(MBEDTLS_RSA_C) */
3454#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003455 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003456 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003457#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003458 if(
3459#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3460 PSA_ALG_IS_ECDSA( alg )
3461#else
3462 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3463#endif
3464 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003465 status = psa_ecdsa_sign( slot->data.ecp,
3466 alg,
3467 hash, hash_length,
3468 signature, signature_size,
3469 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003470 else
3471#endif /* defined(MBEDTLS_ECDSA_C) */
3472 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003473 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003474 }
itayzafrir5c753392018-05-08 11:18:38 +03003475 }
3476 else
3477#endif /* defined(MBEDTLS_ECP_C) */
3478 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003479 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003480 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003481
3482exit:
3483 /* Fill the unused part of the output buffer (the whole buffer on error,
3484 * the trailing part on success) with something that isn't a valid mac
3485 * (barring an attack on the mac and deliberately-crafted input),
3486 * in case the caller doesn't check the return status properly. */
3487 if( status == PSA_SUCCESS )
3488 memset( signature + *signature_length, '!',
3489 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003490 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003491 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003492 /* If signature_size is 0 then we have nothing to do. We must not call
3493 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003494 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003495}
3496
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003497psa_status_t psa_verify_hash( psa_key_handle_t handle,
3498 psa_algorithm_t alg,
3499 const uint8_t *hash,
3500 size_t hash_length,
3501 const uint8_t *signature,
3502 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003503{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003504 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003505 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003506#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3507 const psa_drv_se_t *drv;
3508 psa_drv_se_context_t *drv_context;
3509#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003510
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003511 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003512 if( status != PSA_SUCCESS )
3513 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003514
Gilles Peskineedc64242019-08-07 21:05:07 +02003515#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3516 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3517 {
3518 if( drv->asymmetric == NULL ||
3519 drv->asymmetric->p_verify == NULL )
3520 return( PSA_ERROR_NOT_SUPPORTED );
3521 return( drv->asymmetric->p_verify( drv_context,
3522 slot->data.se.slot_number,
3523 alg,
3524 hash, hash_length,
3525 signature, signature_length ) );
3526 }
3527 else
3528#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003529#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003530 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003531 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003532 return( psa_rsa_verify( slot->data.rsa,
3533 alg,
3534 hash, hash_length,
3535 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003536 }
3537 else
3538#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003539#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003540 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003541 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003542#if defined(MBEDTLS_ECDSA_C)
3543 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003544 return( psa_ecdsa_verify( slot->data.ecp,
3545 hash, hash_length,
3546 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003547 else
3548#endif /* defined(MBEDTLS_ECDSA_C) */
3549 {
3550 return( PSA_ERROR_INVALID_ARGUMENT );
3551 }
itayzafrir5c753392018-05-08 11:18:38 +03003552 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003553 else
3554#endif /* defined(MBEDTLS_ECP_C) */
3555 {
3556 return( PSA_ERROR_NOT_SUPPORTED );
3557 }
3558}
3559
Gilles Peskine072ac562018-06-30 00:21:29 +02003560#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3561static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3562 mbedtls_rsa_context *rsa )
3563{
3564 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3565 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3566 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3567 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3568}
3569#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3570
Gilles Peskinec5487a82018-12-03 18:08:14 +01003571psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003572 psa_algorithm_t alg,
3573 const uint8_t *input,
3574 size_t input_length,
3575 const uint8_t *salt,
3576 size_t salt_length,
3577 uint8_t *output,
3578 size_t output_size,
3579 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003580{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003581 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003582 psa_status_t status;
3583
Darryl Green5cc689a2018-07-24 15:34:10 +01003584 (void) input;
3585 (void) input_length;
3586 (void) salt;
3587 (void) output;
3588 (void) output_size;
3589
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003590 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003591
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003592 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3593 return( PSA_ERROR_INVALID_ARGUMENT );
3594
Gilles Peskine28f8f302019-07-24 13:30:31 +02003595 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003596 if( status != PSA_SUCCESS )
3597 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003598 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3599 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003600 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003601
3602#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003603 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003604 {
3605 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003606 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003607 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003608 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003609#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003610 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003611 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003612 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3613 mbedtls_ctr_drbg_random,
3614 &global_data.ctr_drbg,
3615 MBEDTLS_RSA_PUBLIC,
3616 input_length,
3617 input,
3618 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003619 }
3620 else
3621#endif /* MBEDTLS_PKCS1_V15 */
3622#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003623 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003624 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003625 psa_rsa_oaep_set_padding_mode( alg, rsa );
3626 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3627 mbedtls_ctr_drbg_random,
3628 &global_data.ctr_drbg,
3629 MBEDTLS_RSA_PUBLIC,
3630 salt, salt_length,
3631 input_length,
3632 input,
3633 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003634 }
3635 else
3636#endif /* MBEDTLS_PKCS1_V21 */
3637 {
3638 return( PSA_ERROR_INVALID_ARGUMENT );
3639 }
3640 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003641 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003642 return( mbedtls_to_psa_error( ret ) );
3643 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003644 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003645#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003646 {
3647 return( PSA_ERROR_NOT_SUPPORTED );
3648 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003649}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003650
Gilles Peskinec5487a82018-12-03 18:08:14 +01003651psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003652 psa_algorithm_t alg,
3653 const uint8_t *input,
3654 size_t input_length,
3655 const uint8_t *salt,
3656 size_t salt_length,
3657 uint8_t *output,
3658 size_t output_size,
3659 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003660{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003661 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003662 psa_status_t status;
3663
Darryl Green5cc689a2018-07-24 15:34:10 +01003664 (void) input;
3665 (void) input_length;
3666 (void) salt;
3667 (void) output;
3668 (void) output_size;
3669
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003670 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003672 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3673 return( PSA_ERROR_INVALID_ARGUMENT );
3674
Gilles Peskine28f8f302019-07-24 13:30:31 +02003675 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003676 if( status != PSA_SUCCESS )
3677 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003678 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003679 return( PSA_ERROR_INVALID_ARGUMENT );
3680
3681#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003682 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003683 {
3684 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003685 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003686
Gilles Peskine630a18a2018-06-29 17:49:35 +02003687 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003688 return( PSA_ERROR_INVALID_ARGUMENT );
3689
3690#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003691 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003692 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003693 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3694 mbedtls_ctr_drbg_random,
3695 &global_data.ctr_drbg,
3696 MBEDTLS_RSA_PRIVATE,
3697 output_length,
3698 input,
3699 output,
3700 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003701 }
3702 else
3703#endif /* MBEDTLS_PKCS1_V15 */
3704#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003705 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003706 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003707 psa_rsa_oaep_set_padding_mode( alg, rsa );
3708 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3709 mbedtls_ctr_drbg_random,
3710 &global_data.ctr_drbg,
3711 MBEDTLS_RSA_PRIVATE,
3712 salt, salt_length,
3713 output_length,
3714 input,
3715 output,
3716 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003717 }
3718 else
3719#endif /* MBEDTLS_PKCS1_V21 */
3720 {
3721 return( PSA_ERROR_INVALID_ARGUMENT );
3722 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003723
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003724 return( mbedtls_to_psa_error( ret ) );
3725 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003726 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003727#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003728 {
3729 return( PSA_ERROR_NOT_SUPPORTED );
3730 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003731}
Gilles Peskine20035e32018-02-03 22:44:14 +01003732
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003733
3734
mohammad1603503973b2018-03-12 15:59:30 +02003735/****************************************************************/
3736/* Symmetric cryptography */
3737/****************************************************************/
3738
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003739/* Initialize the cipher operation structure. Once this function has been
3740 * called, psa_cipher_abort can run and will do the right thing. */
3741static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3742 psa_algorithm_t alg )
3743{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003744 if( ! PSA_ALG_IS_CIPHER( alg ) )
3745 {
3746 memset( operation, 0, sizeof( *operation ) );
3747 return( PSA_ERROR_INVALID_ARGUMENT );
3748 }
3749
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003750 operation->alg = alg;
3751 operation->key_set = 0;
3752 operation->iv_set = 0;
3753 operation->iv_required = 1;
3754 operation->iv_size = 0;
3755 operation->block_size = 0;
3756 mbedtls_cipher_init( &operation->ctx.cipher );
3757 return( PSA_SUCCESS );
3758}
3759
Gilles Peskinee553c652018-06-04 16:22:46 +02003760static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003761 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003762 psa_algorithm_t alg,
3763 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003764{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003765 int ret = 0;
3766 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003767 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003768 size_t key_bits;
3769 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003770 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3771 PSA_KEY_USAGE_ENCRYPT :
3772 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003773
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003774 /* A context must be freshly initialized before it can be set up. */
3775 if( operation->alg != 0 )
3776 {
3777 return( PSA_ERROR_BAD_STATE );
3778 }
3779
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003780 status = psa_cipher_init( operation, alg );
3781 if( status != PSA_SUCCESS )
3782 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003783
Gilles Peskine28f8f302019-07-24 13:30:31 +02003784 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003785 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003786 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003787 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003788
Gilles Peskine8e338702019-07-30 20:06:31 +02003789 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003790 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003791 {
3792 status = PSA_ERROR_NOT_SUPPORTED;
3793 goto exit;
3794 }
mohammad1603503973b2018-03-12 15:59:30 +02003795
mohammad1603503973b2018-03-12 15:59:30 +02003796 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003797 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003798 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003799
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003800#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003801 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003802 {
3803 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003804 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003805 memcpy( keys, slot->data.raw.data, 16 );
3806 memcpy( keys + 16, slot->data.raw.data, 8 );
3807 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3808 keys,
3809 192, cipher_operation );
3810 }
3811 else
3812#endif
3813 {
3814 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3815 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003816 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003817 }
Moran Peker41deec42018-04-04 15:43:05 +03003818 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003819 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003820
mohammad16038481e742018-03-18 13:57:31 +02003821#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003822 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003823 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003824 case PSA_ALG_CBC_NO_PADDING:
3825 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3826 MBEDTLS_PADDING_NONE );
3827 break;
3828 case PSA_ALG_CBC_PKCS7:
3829 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3830 MBEDTLS_PADDING_PKCS7 );
3831 break;
3832 default:
3833 /* The algorithm doesn't involve padding. */
3834 ret = 0;
3835 break;
3836 }
3837 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003838 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003839#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3840
mohammad1603503973b2018-03-12 15:59:30 +02003841 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003842 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003843 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003844 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003845 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003846 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003847 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003848#if defined(MBEDTLS_CHACHA20_C)
3849 else
3850 if( alg == PSA_ALG_CHACHA20 )
3851 operation->iv_size = 12;
3852#endif
mohammad1603503973b2018-03-12 15:59:30 +02003853
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003854exit:
3855 if( status == 0 )
3856 status = mbedtls_to_psa_error( ret );
3857 if( status != 0 )
3858 psa_cipher_abort( operation );
3859 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003860}
3861
Gilles Peskinefe119512018-07-08 21:39:34 +02003862psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003863 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003864 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003865{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003866 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003867}
3868
Gilles Peskinefe119512018-07-08 21:39:34 +02003869psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003870 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003871 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003872{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003873 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003874}
3875
Gilles Peskinefe119512018-07-08 21:39:34 +02003876psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003877 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003878 size_t iv_size,
3879 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003880{
itayzafrir534bd7c2018-08-02 13:56:32 +03003881 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003882 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003883 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003885 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003886 }
Moran Peker41deec42018-04-04 15:43:05 +03003887 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003888 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003890 goto exit;
3891 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003892 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3893 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003894 if( ret != 0 )
3895 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003897 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003898 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003899
mohammad16038481e742018-03-18 13:57:31 +02003900 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003901 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003902
Moran Peker395db872018-05-31 14:07:14 +03003903exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003905 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003906 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003907}
3908
Gilles Peskinefe119512018-07-08 21:39:34 +02003909psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003910 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003911 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003912{
itayzafrir534bd7c2018-08-02 13:56:32 +03003913 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003914 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003915 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003916 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003917 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003918 }
Moran Pekera28258c2018-05-29 16:25:04 +03003919 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003920 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003921 status = PSA_ERROR_INVALID_ARGUMENT;
3922 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003923 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3925 status = mbedtls_to_psa_error( ret );
3926exit:
3927 if( status == PSA_SUCCESS )
3928 operation->iv_set = 1;
3929 else
mohammad1603503973b2018-03-12 15:59:30 +02003930 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003931 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003932}
3933
Gilles Peskinee553c652018-06-04 16:22:46 +02003934psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3935 const uint8_t *input,
3936 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003937 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003938 size_t output_size,
3939 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003940{
itayzafrir534bd7c2018-08-02 13:56:32 +03003941 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003942 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003943 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003944
3945 if( operation->alg == 0 )
3946 {
3947 return( PSA_ERROR_BAD_STATE );
3948 }
3949
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003950 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003951 {
3952 /* Take the unprocessed partial block left over from previous
3953 * update calls, if any, plus the input to this call. Remove
3954 * the last partial block, if any. You get the data that will be
3955 * output in this call. */
3956 expected_output_size =
3957 ( operation->ctx.cipher.unprocessed_len + input_length )
3958 / operation->block_size * operation->block_size;
3959 }
3960 else
3961 {
3962 expected_output_size = input_length;
3963 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003964
Gilles Peskine89d789c2018-06-04 17:17:16 +02003965 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003966 {
3967 status = PSA_ERROR_BUFFER_TOO_SMALL;
3968 goto exit;
3969 }
mohammad160382759612018-03-12 18:16:40 +02003970
mohammad1603503973b2018-03-12 15:59:30 +02003971 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003972 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003973 status = mbedtls_to_psa_error( ret );
3974exit:
3975 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003976 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003977 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003978}
3979
Gilles Peskinee553c652018-06-04 16:22:46 +02003980psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3981 uint8_t *output,
3982 size_t output_size,
3983 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003984{
David Saadab4ecc272019-02-14 13:48:10 +02003985 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003986 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003987 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003988
mohammad1603503973b2018-03-12 15:59:30 +02003989 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003990 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003991 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003992 }
3993 if( operation->iv_required && ! operation->iv_set )
3994 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003995 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003996 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003997
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003998 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003999 operation->alg == PSA_ALG_CBC_NO_PADDING &&
4000 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03004001 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02004002 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01004003 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004004 }
4005
Janos Follath315b51c2018-07-09 16:04:51 +01004006 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
4007 temp_output_buffer,
4008 output_length );
4009 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02004010 {
Janos Follath315b51c2018-07-09 16:04:51 +01004011 status = mbedtls_to_psa_error( cipher_ret );
4012 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02004013 }
Janos Follath315b51c2018-07-09 16:04:51 +01004014
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004015 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01004016 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004017 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004018 memcpy( output, temp_output_buffer, *output_length );
4019 else
4020 {
Janos Follath315b51c2018-07-09 16:04:51 +01004021 status = PSA_ERROR_BUFFER_TOO_SMALL;
4022 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004023 }
mohammad1603503973b2018-03-12 15:59:30 +02004024
Gilles Peskine3f108122018-12-07 18:14:53 +01004025 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004026 status = psa_cipher_abort( operation );
4027
4028 return( status );
4029
4030error:
4031
4032 *output_length = 0;
4033
Gilles Peskine3f108122018-12-07 18:14:53 +01004034 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004035 (void) psa_cipher_abort( operation );
4036
4037 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004038}
4039
Gilles Peskinee553c652018-06-04 16:22:46 +02004040psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4041{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004042 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004043 {
4044 /* The object has (apparently) been initialized but it is not
4045 * in use. It's ok to call abort on such an object, and there's
4046 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004047 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004048 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004049
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004050 /* Sanity check (shouldn't happen: operation->alg should
4051 * always have been initialized to a valid value). */
4052 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4053 return( PSA_ERROR_BAD_STATE );
4054
mohammad1603503973b2018-03-12 15:59:30 +02004055 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004056
Moran Peker41deec42018-04-04 15:43:05 +03004057 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004058 operation->key_set = 0;
4059 operation->iv_set = 0;
4060 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004061 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004062 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004063
Moran Peker395db872018-05-31 14:07:14 +03004064 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004065}
4066
Gilles Peskinea0655c32018-04-30 17:06:50 +02004067
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004068
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004069
mohammad16035955c982018-04-26 00:53:03 +03004070/****************************************************************/
4071/* AEAD */
4072/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004073
Gilles Peskineedf9a652018-08-17 18:11:56 +02004074typedef struct
4075{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004076 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004077 const mbedtls_cipher_info_t *cipher_info;
4078 union
4079 {
4080#if defined(MBEDTLS_CCM_C)
4081 mbedtls_ccm_context ccm;
4082#endif /* MBEDTLS_CCM_C */
4083#if defined(MBEDTLS_GCM_C)
4084 mbedtls_gcm_context gcm;
4085#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004086#if defined(MBEDTLS_CHACHAPOLY_C)
4087 mbedtls_chachapoly_context chachapoly;
4088#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004089 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004090 psa_algorithm_t core_alg;
4091 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004092 uint8_t tag_length;
4093} aead_operation_t;
4094
Gilles Peskine30a9e412019-01-14 18:36:12 +01004095static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004096{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004097 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004098 {
4099#if defined(MBEDTLS_CCM_C)
4100 case PSA_ALG_CCM:
4101 mbedtls_ccm_free( &operation->ctx.ccm );
4102 break;
4103#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004104#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 case PSA_ALG_GCM:
4106 mbedtls_gcm_free( &operation->ctx.gcm );
4107 break;
4108#endif /* MBEDTLS_GCM_C */
4109 }
4110}
4111
4112static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004113 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004114 psa_key_usage_t usage,
4115 psa_algorithm_t alg )
4116{
4117 psa_status_t status;
4118 size_t key_bits;
4119 mbedtls_cipher_id_t cipher_id;
4120
Gilles Peskine28f8f302019-07-24 13:30:31 +02004121 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004122 if( status != PSA_SUCCESS )
4123 return( status );
4124
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004125 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126
4127 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004128 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004129 &cipher_id );
4130 if( operation->cipher_info == NULL )
4131 return( PSA_ERROR_NOT_SUPPORTED );
4132
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004133 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004134 {
4135#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004136 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4137 operation->core_alg = PSA_ALG_CCM;
4138 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004139 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4140 * The call to mbedtls_ccm_encrypt_and_tag or
4141 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004142 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004143 return( PSA_ERROR_INVALID_ARGUMENT );
4144 mbedtls_ccm_init( &operation->ctx.ccm );
4145 status = mbedtls_to_psa_error(
4146 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4147 operation->slot->data.raw.data,
4148 (unsigned int) key_bits ) );
4149 if( status != 0 )
4150 goto cleanup;
4151 break;
4152#endif /* MBEDTLS_CCM_C */
4153
4154#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004155 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4156 operation->core_alg = PSA_ALG_GCM;
4157 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004158 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4159 * The call to mbedtls_gcm_crypt_and_tag or
4160 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004161 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004162 return( PSA_ERROR_INVALID_ARGUMENT );
4163 mbedtls_gcm_init( &operation->ctx.gcm );
4164 status = mbedtls_to_psa_error(
4165 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4166 operation->slot->data.raw.data,
4167 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004168 if( status != 0 )
4169 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004170 break;
4171#endif /* MBEDTLS_GCM_C */
4172
Gilles Peskine26869f22019-05-06 15:25:00 +02004173#if defined(MBEDTLS_CHACHAPOLY_C)
4174 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4175 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4176 operation->full_tag_length = 16;
4177 /* We only support the default tag length. */
4178 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4179 return( PSA_ERROR_NOT_SUPPORTED );
4180 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4181 status = mbedtls_to_psa_error(
4182 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4183 operation->slot->data.raw.data ) );
4184 if( status != 0 )
4185 goto cleanup;
4186 break;
4187#endif /* MBEDTLS_CHACHAPOLY_C */
4188
Gilles Peskineedf9a652018-08-17 18:11:56 +02004189 default:
4190 return( PSA_ERROR_NOT_SUPPORTED );
4191 }
4192
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004193 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4194 {
4195 status = PSA_ERROR_INVALID_ARGUMENT;
4196 goto cleanup;
4197 }
4198 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004199
Gilles Peskineedf9a652018-08-17 18:11:56 +02004200 return( PSA_SUCCESS );
4201
4202cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004203 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004204 return( status );
4205}
4206
Gilles Peskinec5487a82018-12-03 18:08:14 +01004207psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004208 psa_algorithm_t alg,
4209 const uint8_t *nonce,
4210 size_t nonce_length,
4211 const uint8_t *additional_data,
4212 size_t additional_data_length,
4213 const uint8_t *plaintext,
4214 size_t plaintext_length,
4215 uint8_t *ciphertext,
4216 size_t ciphertext_size,
4217 size_t *ciphertext_length )
4218{
mohammad16035955c982018-04-26 00:53:03 +03004219 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004220 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004221 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004222
mohammad1603f08a5502018-06-03 15:05:47 +03004223 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004224
Gilles Peskinec5487a82018-12-03 18:08:14 +01004225 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004226 if( status != PSA_SUCCESS )
4227 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004228
Gilles Peskineedf9a652018-08-17 18:11:56 +02004229 /* For all currently supported modes, the tag is at the end of the
4230 * ciphertext. */
4231 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4232 {
4233 status = PSA_ERROR_BUFFER_TOO_SMALL;
4234 goto exit;
4235 }
4236 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004237
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004238#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004239 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004240 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004241 status = mbedtls_to_psa_error(
4242 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4243 MBEDTLS_GCM_ENCRYPT,
4244 plaintext_length,
4245 nonce, nonce_length,
4246 additional_data, additional_data_length,
4247 plaintext, ciphertext,
4248 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004249 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004250 else
4251#endif /* MBEDTLS_GCM_C */
4252#if defined(MBEDTLS_CCM_C)
4253 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004254 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004255 status = mbedtls_to_psa_error(
4256 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4257 plaintext_length,
4258 nonce, nonce_length,
4259 additional_data,
4260 additional_data_length,
4261 plaintext, ciphertext,
4262 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004263 }
mohammad16035c8845f2018-05-09 05:40:09 -07004264 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004265#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004266#if defined(MBEDTLS_CHACHAPOLY_C)
4267 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4268 {
4269 if( nonce_length != 12 || operation.tag_length != 16 )
4270 {
4271 status = PSA_ERROR_NOT_SUPPORTED;
4272 goto exit;
4273 }
4274 status = mbedtls_to_psa_error(
4275 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4276 plaintext_length,
4277 nonce,
4278 additional_data,
4279 additional_data_length,
4280 plaintext,
4281 ciphertext,
4282 tag ) );
4283 }
4284 else
4285#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004286 {
mohammad1603554faad2018-06-03 15:07:38 +03004287 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004288 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004289
Gilles Peskineedf9a652018-08-17 18:11:56 +02004290 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4291 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004292
Gilles Peskineedf9a652018-08-17 18:11:56 +02004293exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004294 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004295 if( status == PSA_SUCCESS )
4296 *ciphertext_length = plaintext_length + operation.tag_length;
4297 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004298}
4299
Gilles Peskineee652a32018-06-01 19:23:52 +02004300/* Locate the tag in a ciphertext buffer containing the encrypted data
4301 * followed by the tag. Return the length of the part preceding the tag in
4302 * *plaintext_length. This is the size of the plaintext in modes where
4303 * the encrypted data has the same size as the plaintext, such as
4304 * CCM and GCM. */
4305static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4306 const uint8_t *ciphertext,
4307 size_t ciphertext_length,
4308 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004309 const uint8_t **p_tag )
4310{
4311 size_t payload_length;
4312 if( tag_length > ciphertext_length )
4313 return( PSA_ERROR_INVALID_ARGUMENT );
4314 payload_length = ciphertext_length - tag_length;
4315 if( payload_length > plaintext_size )
4316 return( PSA_ERROR_BUFFER_TOO_SMALL );
4317 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004318 return( PSA_SUCCESS );
4319}
4320
Gilles Peskinec5487a82018-12-03 18:08:14 +01004321psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004322 psa_algorithm_t alg,
4323 const uint8_t *nonce,
4324 size_t nonce_length,
4325 const uint8_t *additional_data,
4326 size_t additional_data_length,
4327 const uint8_t *ciphertext,
4328 size_t ciphertext_length,
4329 uint8_t *plaintext,
4330 size_t plaintext_size,
4331 size_t *plaintext_length )
4332{
mohammad16035955c982018-04-26 00:53:03 +03004333 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004334 aead_operation_t operation;
4335 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004336
Gilles Peskineee652a32018-06-01 19:23:52 +02004337 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004338
Gilles Peskinec5487a82018-12-03 18:08:14 +01004339 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004340 if( status != PSA_SUCCESS )
4341 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004342
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004343 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4344 ciphertext, ciphertext_length,
4345 plaintext_size, &tag );
4346 if( status != PSA_SUCCESS )
4347 goto exit;
4348
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004349#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004350 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004351 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004352 status = mbedtls_to_psa_error(
4353 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4354 ciphertext_length - operation.tag_length,
4355 nonce, nonce_length,
4356 additional_data,
4357 additional_data_length,
4358 tag, operation.tag_length,
4359 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004360 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004361 else
4362#endif /* MBEDTLS_GCM_C */
4363#if defined(MBEDTLS_CCM_C)
4364 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004365 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004366 status = mbedtls_to_psa_error(
4367 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4368 ciphertext_length - operation.tag_length,
4369 nonce, nonce_length,
4370 additional_data,
4371 additional_data_length,
4372 ciphertext, plaintext,
4373 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004374 }
mohammad160339574652018-06-01 04:39:53 -07004375 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004376#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004377#if defined(MBEDTLS_CHACHAPOLY_C)
4378 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4379 {
4380 if( nonce_length != 12 || operation.tag_length != 16 )
4381 {
4382 status = PSA_ERROR_NOT_SUPPORTED;
4383 goto exit;
4384 }
4385 status = mbedtls_to_psa_error(
4386 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4387 ciphertext_length - operation.tag_length,
4388 nonce,
4389 additional_data,
4390 additional_data_length,
4391 tag,
4392 ciphertext,
4393 plaintext ) );
4394 }
4395 else
4396#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004397 {
mohammad1603554faad2018-06-03 15:07:38 +03004398 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004399 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004400
Gilles Peskineedf9a652018-08-17 18:11:56 +02004401 if( status != PSA_SUCCESS && plaintext_size != 0 )
4402 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004403
Gilles Peskineedf9a652018-08-17 18:11:56 +02004404exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004405 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004406 if( status == PSA_SUCCESS )
4407 *plaintext_length = ciphertext_length - operation.tag_length;
4408 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004409}
4410
Gilles Peskinea0655c32018-04-30 17:06:50 +02004411
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004412
Gilles Peskine20035e32018-02-03 22:44:14 +01004413/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004414/* Generators */
4415/****************************************************************/
4416
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004417#define HKDF_STATE_INIT 0 /* no input yet */
4418#define HKDF_STATE_STARTED 1 /* got salt */
4419#define HKDF_STATE_KEYED 2 /* got key */
4420#define HKDF_STATE_OUTPUT 3 /* output started */
4421
Gilles Peskinecbe66502019-05-16 16:59:18 +02004422static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004423 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004424{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004425 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4426 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004428 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004429}
4430
4431
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004432psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004433{
4434 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004435 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004436 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004437 {
4438 /* The object has (apparently) been initialized but it is not
4439 * in use. It's ok to call abort on such an object, and there's
4440 * nothing to do. */
4441 }
4442 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004443#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004444 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004445 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004446 mbedtls_free( operation->ctx.hkdf.info );
4447 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004448 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004449 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004450 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004451 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004452 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004453 if( operation->ctx.tls12_prf.seed != NULL )
4454 {
4455 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4456 operation->ctx.tls12_prf.seed_length );
4457 mbedtls_free( operation->ctx.tls12_prf.seed );
4458 }
4459
4460 if( operation->ctx.tls12_prf.label != NULL )
4461 {
4462 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4463 operation->ctx.tls12_prf.label_length );
4464 mbedtls_free( operation->ctx.tls12_prf.label );
4465 }
4466
4467 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4468
4469 /* We leave the fields Ai and output_block to be erased safely by the
4470 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004471 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004472 else
4473#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474 {
4475 status = PSA_ERROR_BAD_STATE;
4476 }
Janos Follath083036a2019-06-11 10:22:26 +01004477 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004478 return( status );
4479}
4480
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004481psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004482 size_t *capacity)
4483{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004485 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004486 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004487 return PSA_ERROR_BAD_STATE;
4488 }
4489
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004490 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004491 return( PSA_SUCCESS );
4492}
4493
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004494psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004495 size_t capacity )
4496{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004497 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004498 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004499 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004500 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004501 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004502 return( PSA_SUCCESS );
4503}
4504
Gilles Peskinebef7f142018-07-12 17:22:21 +02004505#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004506/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004507 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004508static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004509 psa_algorithm_t hash_alg,
4510 uint8_t *output,
4511 size_t output_length )
4512{
4513 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4514 psa_status_t status;
4515
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004516 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4517 return( PSA_ERROR_BAD_STATE );
4518 hkdf->state = HKDF_STATE_OUTPUT;
4519
Gilles Peskinebef7f142018-07-12 17:22:21 +02004520 while( output_length != 0 )
4521 {
4522 /* Copy what remains of the current block */
4523 uint8_t n = hash_length - hkdf->offset_in_block;
4524 if( n > output_length )
4525 n = (uint8_t) output_length;
4526 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4527 output += n;
4528 output_length -= n;
4529 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004530 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004531 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004532 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004533 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004534 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004535 * object was corrupted or if this function is called directly
4536 * inside the library. */
4537 if( hkdf->block_number == 0xff )
4538 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004539
4540 /* We need a new block */
4541 ++hkdf->block_number;
4542 hkdf->offset_in_block = 0;
4543 status = psa_hmac_setup_internal( &hkdf->hmac,
4544 hkdf->prk, hash_length,
4545 hash_alg );
4546 if( status != PSA_SUCCESS )
4547 return( status );
4548 if( hkdf->block_number != 1 )
4549 {
4550 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4551 hkdf->output_block,
4552 hash_length );
4553 if( status != PSA_SUCCESS )
4554 return( status );
4555 }
4556 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4557 hkdf->info,
4558 hkdf->info_length );
4559 if( status != PSA_SUCCESS )
4560 return( status );
4561 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4562 &hkdf->block_number, 1 );
4563 if( status != PSA_SUCCESS )
4564 return( status );
4565 status = psa_hmac_finish_internal( &hkdf->hmac,
4566 hkdf->output_block,
4567 sizeof( hkdf->output_block ) );
4568 if( status != PSA_SUCCESS )
4569 return( status );
4570 }
4571
4572 return( PSA_SUCCESS );
4573}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004574
Janos Follath7742fee2019-06-17 12:58:10 +01004575static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4576 psa_tls12_prf_key_derivation_t *tls12_prf,
4577 psa_algorithm_t alg )
4578{
4579 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4580 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004581 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4582 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004583
Janos Follath7742fee2019-06-17 12:58:10 +01004584 /* We can't be wanting more output after block 0xff, otherwise
4585 * the capacity check in psa_key_derivation_output_bytes() would have
4586 * prevented this call. It could happen only if the operation
4587 * object was corrupted or if this function is called directly
4588 * inside the library. */
4589 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004590 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004591
4592 /* We need a new block */
4593 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004594 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004595
4596 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4597 *
4598 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4599 *
4600 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4601 * HMAC_hash(secret, A(2) + seed) +
4602 * HMAC_hash(secret, A(3) + seed) + ...
4603 *
4604 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004605 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004606 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004607 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004608 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004609 * is currently extracted as `output_block` and where i is
4610 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004611 */
4612
Janos Follathea29bfb2019-06-19 12:21:20 +01004613 /* Save the hash context before using it, to preserve the hash state with
4614 * only the inner padding in it. We need this, because inner padding depends
4615 * on the key (secret in the RFC's terminology). */
4616 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4617 if( status != PSA_SUCCESS )
4618 goto cleanup;
4619
4620 /* Calculate A(i) where i = tls12_prf->block_number. */
4621 if( tls12_prf->block_number == 1 )
4622 {
4623 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4624 * the variable seed and in this instance means it in the context of the
4625 * P_hash function, where seed = label + seed.) */
4626 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4627 tls12_prf->label, tls12_prf->label_length );
4628 if( status != PSA_SUCCESS )
4629 goto cleanup;
4630 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4631 tls12_prf->seed, tls12_prf->seed_length );
4632 if( status != PSA_SUCCESS )
4633 goto cleanup;
4634 }
4635 else
4636 {
4637 /* A(i) = HMAC_hash(secret, A(i-1)) */
4638 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4639 tls12_prf->Ai, hash_length );
4640 if( status != PSA_SUCCESS )
4641 goto cleanup;
4642 }
4643
4644 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4645 tls12_prf->Ai, hash_length );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4649 if( status != PSA_SUCCESS )
4650 goto cleanup;
4651
4652 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4653 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4654 tls12_prf->Ai, hash_length );
4655 if( status != PSA_SUCCESS )
4656 goto cleanup;
4657 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4658 tls12_prf->label, tls12_prf->label_length );
4659 if( status != PSA_SUCCESS )
4660 goto cleanup;
4661 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4662 tls12_prf->seed, tls12_prf->seed_length );
4663 if( status != PSA_SUCCESS )
4664 goto cleanup;
4665 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4666 tls12_prf->output_block, hash_length );
4667 if( status != PSA_SUCCESS )
4668 goto cleanup;
4669 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4670 if( status != PSA_SUCCESS )
4671 goto cleanup;
4672
Janos Follath7742fee2019-06-17 12:58:10 +01004673
4674cleanup:
4675
Janos Follathea29bfb2019-06-19 12:21:20 +01004676 cleanup_status = psa_hash_abort( &backup );
4677 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4678 status = cleanup_status;
4679
4680 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004681}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004682
Janos Follath844eb0e2019-06-19 12:10:49 +01004683static psa_status_t psa_key_derivation_tls12_prf_read(
4684 psa_tls12_prf_key_derivation_t *tls12_prf,
4685 psa_algorithm_t alg,
4686 uint8_t *output,
4687 size_t output_length )
4688{
4689 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4690 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4691 psa_status_t status;
4692 uint8_t offset, length;
4693
4694 while( output_length != 0 )
4695 {
4696 /* Check if we have fully processed the current block. */
4697 if( tls12_prf->left_in_block == 0 )
4698 {
4699 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4700 alg );
4701 if( status != PSA_SUCCESS )
4702 return( status );
4703
4704 continue;
4705 }
4706
4707 if( tls12_prf->left_in_block > output_length )
4708 length = (uint8_t) output_length;
4709 else
4710 length = tls12_prf->left_in_block;
4711
4712 offset = hash_length - tls12_prf->left_in_block;
4713 memcpy( output, tls12_prf->output_block + offset, length );
4714 output += length;
4715 output_length -= length;
4716 tls12_prf->left_in_block -= length;
4717 }
4718
4719 return( PSA_SUCCESS );
4720}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004721#endif /* MBEDTLS_MD_C */
4722
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004723psa_status_t psa_key_derivation_output_bytes(
4724 psa_key_derivation_operation_t *operation,
4725 uint8_t *output,
4726 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004727{
4728 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004729 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004730
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004731 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004732 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004733 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004734 return PSA_ERROR_BAD_STATE;
4735 }
4736
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004737 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004738 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004739 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004740 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004741 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004742 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004743 goto exit;
4744 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004745 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004746 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004747 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004748 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004749 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4750 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004752 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004753 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004754 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004755 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004756
Gilles Peskinebef7f142018-07-12 17:22:21 +02004757#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004758 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004759 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004760 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004761 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004762 output, output_length );
4763 }
Janos Follathadbec812019-06-14 11:05:39 +01004764 else
Janos Follathadbec812019-06-14 11:05:39 +01004765 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004766 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004767 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004768 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004769 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004770 output_length );
4771 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004772 else
4773#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004774 {
4775 return( PSA_ERROR_BAD_STATE );
4776 }
4777
4778exit:
4779 if( status != PSA_SUCCESS )
4780 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004781 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004782 * This allows us to differentiate between exhausted operations and
4783 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4784 * operations. */
4785 psa_algorithm_t alg = operation->alg;
4786 psa_key_derivation_abort( operation );
4787 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004788 memset( output, '!', output_length );
4789 }
4790 return( status );
4791}
4792
Gilles Peskine08542d82018-07-19 17:05:42 +02004793#if defined(MBEDTLS_DES_C)
4794static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4795{
4796 if( data_size >= 8 )
4797 mbedtls_des_key_set_parity( data );
4798 if( data_size >= 16 )
4799 mbedtls_des_key_set_parity( data + 8 );
4800 if( data_size >= 24 )
4801 mbedtls_des_key_set_parity( data + 16 );
4802}
4803#endif /* MBEDTLS_DES_C */
4804
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004805static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004806 psa_key_slot_t *slot,
4807 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004808 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004809{
4810 uint8_t *data = NULL;
4811 size_t bytes = PSA_BITS_TO_BYTES( bits );
4812 psa_status_t status;
4813
Gilles Peskine8e338702019-07-30 20:06:31 +02004814 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004815 return( PSA_ERROR_INVALID_ARGUMENT );
4816 if( bits % 8 != 0 )
4817 return( PSA_ERROR_INVALID_ARGUMENT );
4818 data = mbedtls_calloc( 1, bytes );
4819 if( data == NULL )
4820 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4821
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004822 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004823 if( status != PSA_SUCCESS )
4824 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004825#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004826 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004827 psa_des_set_key_parity( data, bytes );
4828#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004829 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004830
4831exit:
4832 mbedtls_free( data );
4833 return( status );
4834}
4835
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004836psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004837 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004838 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004839{
4840 psa_status_t status;
4841 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004842 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004843
4844 /* Reject any attempt to create a zero-length key so that we don't
4845 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4846 if( psa_get_key_bits( attributes ) == 0 )
4847 return( PSA_ERROR_INVALID_ARGUMENT );
4848
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004849 if( ! operation->can_output_key )
4850 return( PSA_ERROR_NOT_PERMITTED );
4851
Gilles Peskinedf179142019-07-15 22:02:14 +02004852 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4853 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004854#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4855 if( driver != NULL )
4856 {
4857 /* Deriving a key in a secure element is not implemented yet. */
4858 status = PSA_ERROR_NOT_SUPPORTED;
4859 }
4860#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004861 if( status == PSA_SUCCESS )
4862 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004863 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004864 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004865 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004866 }
4867 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004868 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004869 if( status != PSA_SUCCESS )
4870 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004871 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004872 *handle = 0;
4873 }
4874 return( status );
4875}
4876
Gilles Peskineeab56e42018-07-12 17:12:33 +02004877
4878
4879/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004880/* Key derivation */
4881/****************************************************************/
4882
Gilles Peskine969c5d62019-01-16 15:53:06 +01004883static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004884 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004885 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004886{
Janos Follath5fe19732019-06-20 15:09:30 +01004887 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4888 * initialisers for this union leave some bytes unspecified.) */
4889 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4890
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004893 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4894 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4895 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004896 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004897 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004898 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4899 if( hash_size == 0 )
4900 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004901 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4902 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004903 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004904 {
4905 return( PSA_ERROR_NOT_SUPPORTED );
4906 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004907 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004908 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004909 }
4910#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004911 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004912 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004913}
4914
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916 psa_algorithm_t alg )
4917{
4918 psa_status_t status;
4919
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004920 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004921 return( PSA_ERROR_BAD_STATE );
4922
Gilles Peskine6843c292019-01-18 16:44:49 +01004923 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4924 return( PSA_ERROR_INVALID_ARGUMENT );
4925 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004926 {
4927 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004928 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004929 }
4930 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4931 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004932 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004933 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004934 else
4935 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004936
4937 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004938 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004939 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004940}
4941
4942#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004943static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004944 psa_algorithm_t hash_alg,
4945 psa_key_derivation_step_t step,
4946 const uint8_t *data,
4947 size_t data_length )
4948{
4949 psa_status_t status;
4950 switch( step )
4951 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004952 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004953 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004954 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004955 status = psa_hmac_setup_internal( &hkdf->hmac,
4956 data, data_length,
4957 hash_alg );
4958 if( status != PSA_SUCCESS )
4959 return( status );
4960 hkdf->state = HKDF_STATE_STARTED;
4961 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004962 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004963 /* If no salt was provided, use an empty salt. */
4964 if( hkdf->state == HKDF_STATE_INIT )
4965 {
4966 status = psa_hmac_setup_internal( &hkdf->hmac,
4967 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004968 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004969 if( status != PSA_SUCCESS )
4970 return( status );
4971 hkdf->state = HKDF_STATE_STARTED;
4972 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004973 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004974 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004975 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4976 data, data_length );
4977 if( status != PSA_SUCCESS )
4978 return( status );
4979 status = psa_hmac_finish_internal( &hkdf->hmac,
4980 hkdf->prk,
4981 sizeof( hkdf->prk ) );
4982 if( status != PSA_SUCCESS )
4983 return( status );
4984 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4985 hkdf->block_number = 0;
4986 hkdf->state = HKDF_STATE_KEYED;
4987 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004988 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004989 if( hkdf->state == HKDF_STATE_OUTPUT )
4990 return( PSA_ERROR_BAD_STATE );
4991 if( hkdf->info_set )
4992 return( PSA_ERROR_BAD_STATE );
4993 hkdf->info_length = data_length;
4994 if( data_length != 0 )
4995 {
4996 hkdf->info = mbedtls_calloc( 1, data_length );
4997 if( hkdf->info == NULL )
4998 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4999 memcpy( hkdf->info, data, data_length );
5000 }
5001 hkdf->info_set = 1;
5002 return( PSA_SUCCESS );
5003 default:
5004 return( PSA_ERROR_INVALID_ARGUMENT );
5005 }
5006}
Janos Follathb03233e2019-06-11 15:30:30 +01005007
Janos Follathf08e2652019-06-13 09:05:41 +01005008static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
5009 const uint8_t *data,
5010 size_t data_length )
5011{
5012 if( prf->state != TLS12_PRF_STATE_INIT )
5013 return( PSA_ERROR_BAD_STATE );
5014
Janos Follathd6dce9f2019-07-04 09:11:38 +01005015 if( data_length != 0 )
5016 {
5017 prf->seed = mbedtls_calloc( 1, data_length );
5018 if( prf->seed == NULL )
5019 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005020
Janos Follathd6dce9f2019-07-04 09:11:38 +01005021 memcpy( prf->seed, data, data_length );
5022 prf->seed_length = data_length;
5023 }
Janos Follathf08e2652019-06-13 09:05:41 +01005024
5025 prf->state = TLS12_PRF_STATE_SEED_SET;
5026
5027 return( PSA_SUCCESS );
5028}
5029
Janos Follath81550542019-06-13 14:26:34 +01005030static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5031 psa_algorithm_t hash_alg,
5032 const uint8_t *data,
5033 size_t data_length )
5034{
5035 psa_status_t status;
5036 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5037 return( PSA_ERROR_BAD_STATE );
5038
5039 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5040 if( status != PSA_SUCCESS )
5041 return( status );
5042
5043 prf->state = TLS12_PRF_STATE_KEY_SET;
5044
5045 return( PSA_SUCCESS );
5046}
5047
Janos Follath6660f0e2019-06-17 08:44:03 +01005048static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5049 psa_tls12_prf_key_derivation_t *prf,
5050 psa_algorithm_t hash_alg,
5051 const uint8_t *data,
5052 size_t data_length )
5053{
5054 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005055 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5056 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005057
5058 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5059 return( PSA_ERROR_INVALID_ARGUMENT );
5060
5061 /* Quoting RFC 4279, Section 2:
5062 *
5063 * The premaster secret is formed as follows: if the PSK is N octets
5064 * long, concatenate a uint16 with the value N, N zero octets, a second
5065 * uint16 with the value N, and the PSK itself.
5066 */
5067
Janos Follath40e13932019-06-26 13:22:29 +01005068 *cur++ = ( data_length >> 8 ) & 0xff;
5069 *cur++ = ( data_length >> 0 ) & 0xff;
5070 memset( cur, 0, data_length );
5071 cur += data_length;
5072 *cur++ = pms[0];
5073 *cur++ = pms[1];
5074 memcpy( cur, data, data_length );
5075 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005076
Janos Follath40e13932019-06-26 13:22:29 +01005077 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005078
5079 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5080 return( status );
5081}
5082
Janos Follath63028dd2019-06-13 09:15:47 +01005083static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5084 const uint8_t *data,
5085 size_t data_length )
5086{
5087 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5088 return( PSA_ERROR_BAD_STATE );
5089
Janos Follathd6dce9f2019-07-04 09:11:38 +01005090 if( data_length != 0 )
5091 {
5092 prf->label = mbedtls_calloc( 1, data_length );
5093 if( prf->label == NULL )
5094 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005095
Janos Follathd6dce9f2019-07-04 09:11:38 +01005096 memcpy( prf->label, data, data_length );
5097 prf->label_length = data_length;
5098 }
Janos Follath63028dd2019-06-13 09:15:47 +01005099
5100 prf->state = TLS12_PRF_STATE_LABEL_SET;
5101
5102 return( PSA_SUCCESS );
5103}
5104
Janos Follathb03233e2019-06-11 15:30:30 +01005105static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5106 psa_algorithm_t hash_alg,
5107 psa_key_derivation_step_t step,
5108 const uint8_t *data,
5109 size_t data_length )
5110{
Janos Follathb03233e2019-06-11 15:30:30 +01005111 switch( step )
5112 {
Janos Follathf08e2652019-06-13 09:05:41 +01005113 case PSA_KEY_DERIVATION_INPUT_SEED:
5114 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005115 case PSA_KEY_DERIVATION_INPUT_SECRET:
5116 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005117 case PSA_KEY_DERIVATION_INPUT_LABEL:
5118 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005119 default:
5120 return( PSA_ERROR_INVALID_ARGUMENT );
5121 }
5122}
Janos Follath6660f0e2019-06-17 08:44:03 +01005123
5124static psa_status_t psa_tls12_prf_psk_to_ms_input(
5125 psa_tls12_prf_key_derivation_t *prf,
5126 psa_algorithm_t hash_alg,
5127 psa_key_derivation_step_t step,
5128 const uint8_t *data,
5129 size_t data_length )
5130{
5131 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005132 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005133 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5134 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005135 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005136
5137 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5138}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005139#endif /* MBEDTLS_MD_C */
5140
Gilles Peskineb8965192019-09-24 16:21:10 +02005141/** Check whether the given key type is acceptable for the given
5142 * input step of a key derivation.
5143 *
5144 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5145 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5146 * Both secret and non-secret inputs can alternatively have the type
5147 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5148 * that the input was passed as a buffer rather than via a key object.
5149 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005150static int psa_key_derivation_check_input_type(
5151 psa_key_derivation_step_t step,
5152 psa_key_type_t key_type )
5153{
5154 switch( step )
5155 {
5156 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005157 if( key_type == PSA_KEY_TYPE_DERIVE )
5158 return( PSA_SUCCESS );
5159 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005160 return( PSA_SUCCESS );
5161 break;
5162 case PSA_KEY_DERIVATION_INPUT_LABEL:
5163 case PSA_KEY_DERIVATION_INPUT_SALT:
5164 case PSA_KEY_DERIVATION_INPUT_INFO:
5165 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005166 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5167 return( PSA_SUCCESS );
5168 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005169 return( PSA_SUCCESS );
5170 break;
5171 }
5172 return( PSA_ERROR_INVALID_ARGUMENT );
5173}
5174
Janos Follathb80a94e2019-06-12 15:54:46 +01005175static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005176 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005177 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005178 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005179 const uint8_t *data,
5180 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005181{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005182 psa_status_t status;
5183 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5184
5185 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005186 if( status != PSA_SUCCESS )
5187 goto exit;
5188
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005189#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005190 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005191 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005192 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005193 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005194 step, data, data_length );
5195 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005196 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005197 {
Janos Follathb03233e2019-06-11 15:30:30 +01005198 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5199 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5200 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005201 }
5202 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5203 {
5204 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5205 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5206 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005207 }
5208 else
5209#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005210 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005211 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005212 return( PSA_ERROR_BAD_STATE );
5213 }
5214
Gilles Peskine224b0d62019-09-23 18:13:17 +02005215exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005216 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005217 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005218 return( status );
5219}
5220
Janos Follath51f4a0f2019-06-14 11:35:55 +01005221psa_status_t psa_key_derivation_input_bytes(
5222 psa_key_derivation_operation_t *operation,
5223 psa_key_derivation_step_t step,
5224 const uint8_t *data,
5225 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005226{
Gilles Peskineb8965192019-09-24 16:21:10 +02005227 return( psa_key_derivation_input_internal( operation, step,
5228 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005229 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005230}
5231
Janos Follath51f4a0f2019-06-14 11:35:55 +01005232psa_status_t psa_key_derivation_input_key(
5233 psa_key_derivation_operation_t *operation,
5234 psa_key_derivation_step_t step,
5235 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005236{
5237 psa_key_slot_t *slot;
5238 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005239
Gilles Peskine28f8f302019-07-24 13:30:31 +02005240 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005241 PSA_KEY_USAGE_DERIVE,
5242 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005243 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005244 {
5245 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005246 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005247 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005248
5249 /* Passing a key object as a SECRET input unlocks the permission
5250 * to output to a key object. */
5251 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5252 operation->can_output_key = 1;
5253
Janos Follathb80a94e2019-06-12 15:54:46 +01005254 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005255 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005256 slot->data.raw.data,
5257 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005258}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005259
5260
5261
5262/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005263/* Key agreement */
5264/****************************************************************/
5265
Gilles Peskinea05219c2018-11-16 16:02:56 +01005266#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005267static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5268 size_t peer_key_length,
5269 const mbedtls_ecp_keypair *our_key,
5270 uint8_t *shared_secret,
5271 size_t shared_secret_size,
5272 size_t *shared_secret_length )
5273{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005274 mbedtls_ecp_keypair *their_key = NULL;
5275 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005276 psa_status_t status;
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005277 size_t bits = 0;
Paul Elliott8ff510a2020-06-02 17:19:28 +01005278 psa_ecc_family_t curve = mbedtls_ecc_group_to_psa( our_key->grp.id, &bits );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005279 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005280
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005281 status = psa_import_ec_public_key( curve,
5282 peer_key, peer_key_length,
5283 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005284 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005285 goto exit;
5286
Jaeden Amero97271b32019-01-10 19:38:51 +00005287 status = mbedtls_to_psa_error(
5288 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5289 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005291 status = mbedtls_to_psa_error(
5292 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5293 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005294 goto exit;
5295
Jaeden Amero97271b32019-01-10 19:38:51 +00005296 status = mbedtls_to_psa_error(
5297 mbedtls_ecdh_calc_secret( &ecdh,
5298 shared_secret_length,
5299 shared_secret, shared_secret_size,
5300 mbedtls_ctr_drbg_random,
5301 &global_data.ctr_drbg ) );
Gilles Peskinec7ef5b32019-12-12 16:58:00 +01005302 if( status != PSA_SUCCESS )
5303 goto exit;
5304 if( PSA_BITS_TO_BYTES( bits ) != *shared_secret_length )
5305 status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005306
5307exit:
Gilles Peskine3e819b72019-12-20 14:09:55 +01005308 if( status != PSA_SUCCESS )
5309 mbedtls_platform_zeroize( shared_secret, shared_secret_size );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005310 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005311 mbedtls_ecp_keypair_free( their_key );
5312 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005313 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005314}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005315#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005316
Gilles Peskine01d718c2018-09-18 12:01:02 +02005317#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5318
Gilles Peskine0216fe12019-04-11 21:23:21 +02005319static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5320 psa_key_slot_t *private_key,
5321 const uint8_t *peer_key,
5322 size_t peer_key_length,
5323 uint8_t *shared_secret,
5324 size_t shared_secret_size,
5325 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005327 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005328 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005329#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005330 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005331 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005332 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005333 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5334 private_key->data.ecp,
5335 shared_secret, shared_secret_size,
5336 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005337#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005339 (void) private_key;
5340 (void) peer_key;
5341 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005342 (void) shared_secret;
5343 (void) shared_secret_size;
5344 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005345 return( PSA_ERROR_NOT_SUPPORTED );
5346 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005347}
5348
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005349/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005350 * to potentially free embedded data structures and wipe confidential data.
5351 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005352static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005353 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005354 psa_key_slot_t *private_key,
5355 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005356 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005357{
5358 psa_status_t status;
5359 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5360 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005361 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005362
5363 /* Step 1: run the secret agreement algorithm to generate the shared
5364 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005365 status = psa_key_agreement_raw_internal( ka_alg,
5366 private_key,
5367 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005368 shared_secret,
5369 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005370 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005371 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372 goto exit;
5373
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005374 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005375 * the shared secret. A shared secret is permitted wherever a key
5376 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005377 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005378 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005379 shared_secret,
5380 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005381
Gilles Peskine12313cd2018-06-20 00:20:32 +02005382exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005383 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005384 return( status );
5385}
5386
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005387psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005388 psa_key_derivation_step_t step,
5389 psa_key_handle_t private_key,
5390 const uint8_t *peer_key,
5391 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005392{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005393 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005394 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005395 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005396 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005397 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005398 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005399 if( status != PSA_SUCCESS )
5400 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005401 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005402 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005403 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005404 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005405 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005406 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005407}
5408
Gilles Peskinebe697d82019-05-16 18:00:41 +02005409psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5410 psa_key_handle_t private_key,
5411 const uint8_t *peer_key,
5412 size_t peer_key_length,
5413 uint8_t *output,
5414 size_t output_size,
5415 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005416{
5417 psa_key_slot_t *slot;
5418 psa_status_t status;
5419
5420 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5421 {
5422 status = PSA_ERROR_INVALID_ARGUMENT;
5423 goto exit;
5424 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005425 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005426 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005427 if( status != PSA_SUCCESS )
5428 goto exit;
5429
5430 status = psa_key_agreement_raw_internal( alg, slot,
5431 peer_key, peer_key_length,
5432 output, output_size,
5433 output_length );
5434
5435exit:
5436 if( status != PSA_SUCCESS )
5437 {
5438 /* If an error happens and is not handled properly, the output
5439 * may be used as a key to protect sensitive data. Arrange for such
5440 * a key to be random, which is likely to result in decryption or
5441 * verification errors. This is better than filling the buffer with
5442 * some constant data such as zeros, which would result in the data
5443 * being protected with a reproducible, easily knowable key.
5444 */
5445 psa_generate_random( output, output_size );
5446 *output_length = output_size;
5447 }
5448 return( status );
5449}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005450
5451
5452/****************************************************************/
5453/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005454/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005455
Gilles Peskine4c317f42018-07-12 01:24:09 +02005456psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005457 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005458{
Janos Follath24eed8d2019-11-22 13:21:35 +00005459 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005460 GUARD_MODULE_INITIALIZED;
5461
Gilles Peskinef181eca2019-08-07 13:49:00 +02005462 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5463 {
5464 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5465 output,
5466 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5467 if( ret != 0 )
5468 return( mbedtls_to_psa_error( ret ) );
5469 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5470 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5471 }
5472
Gilles Peskinee59236f2018-01-27 23:32:46 +01005473 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5474 return( mbedtls_to_psa_error( ret ) );
5475}
5476
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005477#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5478#include "mbedtls/entropy_poll.h"
5479
Gilles Peskine7228da22019-07-15 11:06:15 +02005480psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005481 size_t seed_size )
5482{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005483 if( global_data.initialized )
5484 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005485
5486 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5487 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5488 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5489 return( PSA_ERROR_INVALID_ARGUMENT );
5490
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005491 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005492}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005493#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005494
Gilles Peskinee56e8782019-04-26 17:34:02 +02005495#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5496static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5497 size_t domain_parameters_size,
5498 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005499{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500 size_t i;
5501 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005502
Gilles Peskinee56e8782019-04-26 17:34:02 +02005503 if( domain_parameters_size == 0 )
5504 {
5505 *exponent = 65537;
5506 return( PSA_SUCCESS );
5507 }
5508
5509 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5510 * support values that fit in a 32-bit integer, which is larger than
5511 * int on just about every platform anyway. */
5512 if( domain_parameters_size > sizeof( acc ) )
5513 return( PSA_ERROR_NOT_SUPPORTED );
5514 for( i = 0; i < domain_parameters_size; i++ )
5515 acc = ( acc << 8 ) | domain_parameters[i];
5516 if( acc > INT_MAX )
5517 return( PSA_ERROR_NOT_SUPPORTED );
5518 *exponent = acc;
5519 return( PSA_SUCCESS );
5520}
5521#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5522
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005523static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005524 psa_key_slot_t *slot, size_t bits,
5525 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005526{
Gilles Peskine8e338702019-07-30 20:06:31 +02005527 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005528
Gilles Peskinee56e8782019-04-26 17:34:02 +02005529 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005530 return( PSA_ERROR_INVALID_ARGUMENT );
5531
Gilles Peskinee59236f2018-01-27 23:32:46 +01005532 if( key_type_is_raw_bytes( type ) )
5533 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005534 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005535 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5536 if( status != PSA_SUCCESS )
5537 return( status );
5538 status = psa_generate_random( slot->data.raw.data,
5539 slot->data.raw.bytes );
5540 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005541 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005542#if defined(MBEDTLS_DES_C)
5543 if( type == PSA_KEY_TYPE_DES )
5544 psa_des_set_key_parity( slot->data.raw.data,
5545 slot->data.raw.bytes );
5546#endif /* MBEDTLS_DES_C */
5547 }
5548 else
5549
5550#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005551 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005552 {
5553 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005554 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005555 int exponent;
5556 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005557 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5558 return( PSA_ERROR_NOT_SUPPORTED );
5559 /* Accept only byte-aligned keys, for the same reasons as
5560 * in psa_import_rsa_key(). */
5561 if( bits % 8 != 0 )
5562 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005563 status = psa_read_rsa_exponent( domain_parameters,
5564 domain_parameters_size,
5565 &exponent );
5566 if( status != PSA_SUCCESS )
5567 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005568 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5569 if( rsa == NULL )
5570 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5571 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5572 ret = mbedtls_rsa_gen_key( rsa,
5573 mbedtls_ctr_drbg_random,
5574 &global_data.ctr_drbg,
5575 (unsigned int) bits,
5576 exponent );
5577 if( ret != 0 )
5578 {
5579 mbedtls_rsa_free( rsa );
5580 mbedtls_free( rsa );
5581 return( mbedtls_to_psa_error( ret ) );
5582 }
5583 slot->data.rsa = rsa;
5584 }
5585 else
5586#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5587
5588#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005589 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005590 {
Paul Elliott8ff510a2020-06-02 17:19:28 +01005591 psa_ecc_family_t curve = PSA_KEY_TYPE_ECC_GET_FAMILY( type );
Gilles Peskine4295e8b2019-12-02 21:39:10 +01005592 mbedtls_ecp_group_id grp_id =
5593 mbedtls_ecc_group_of_psa( curve, PSA_BITS_TO_BYTES( bits ) );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005594 const mbedtls_ecp_curve_info *curve_info =
5595 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5596 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005597 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005598 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005599 return( PSA_ERROR_NOT_SUPPORTED );
5600 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5601 return( PSA_ERROR_NOT_SUPPORTED );
5602 if( curve_info->bit_size != bits )
5603 return( PSA_ERROR_INVALID_ARGUMENT );
5604 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5605 if( ecp == NULL )
5606 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5607 mbedtls_ecp_keypair_init( ecp );
5608 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5609 mbedtls_ctr_drbg_random,
5610 &global_data.ctr_drbg );
5611 if( ret != 0 )
5612 {
5613 mbedtls_ecp_keypair_free( ecp );
5614 mbedtls_free( ecp );
5615 return( mbedtls_to_psa_error( ret ) );
5616 }
5617 slot->data.ecp = ecp;
5618 }
5619 else
5620#endif /* MBEDTLS_ECP_C */
5621
5622 return( PSA_ERROR_NOT_SUPPORTED );
5623
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005624 return( PSA_SUCCESS );
5625}
Darryl Green0c6575a2018-11-07 16:05:30 +00005626
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005627psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005628 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005629{
5630 psa_status_t status;
5631 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005632 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005633
Gilles Peskine0f84d622019-09-12 19:03:13 +02005634 /* Reject any attempt to create a zero-length key so that we don't
5635 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5636 if( psa_get_key_bits( attributes ) == 0 )
5637 return( PSA_ERROR_INVALID_ARGUMENT );
5638
Gilles Peskinedf179142019-07-15 22:02:14 +02005639 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5640 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005641 if( status != PSA_SUCCESS )
5642 goto exit;
5643
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005644#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5645 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005646 {
Gilles Peskine11792082019-08-06 18:36:36 +02005647 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5648 size_t pubkey_length = 0; /* We don't support this feature yet */
5649 if( drv->key_management == NULL ||
5650 drv->key_management->p_generate == NULL )
5651 {
5652 status = PSA_ERROR_NOT_SUPPORTED;
5653 goto exit;
5654 }
5655 status = drv->key_management->p_generate(
5656 psa_get_se_driver_context( driver ),
5657 slot->data.se.slot_number, attributes,
5658 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005659 }
Gilles Peskine11792082019-08-06 18:36:36 +02005660 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005661#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005662 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005663 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005664 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005665 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005666 }
Gilles Peskine11792082019-08-06 18:36:36 +02005667
5668exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005669 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005670 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005671 if( status != PSA_SUCCESS )
5672 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005673 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005674 *handle = 0;
5675 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005676 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005677}
5678
5679
Gilles Peskinee59236f2018-01-27 23:32:46 +01005680
5681/****************************************************************/
5682/* Module setup */
5683/****************************************************************/
5684
Gilles Peskine5e769522018-11-20 21:59:56 +01005685psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5686 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5687 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5688{
5689 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5690 return( PSA_ERROR_BAD_STATE );
5691 global_data.entropy_init = entropy_init;
5692 global_data.entropy_free = entropy_free;
5693 return( PSA_SUCCESS );
5694}
5695
Gilles Peskinee59236f2018-01-27 23:32:46 +01005696void mbedtls_psa_crypto_free( void )
5697{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005698 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005699 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5700 {
5701 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005702 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005703 }
5704 /* Wipe all remaining data, including configuration.
5705 * In particular, this sets all state indicator to the value
5706 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005707 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005708#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005709 /* Unregister all secure element drivers, so that we restart from
5710 * a pristine state. */
5711 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005712#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005713}
5714
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005715#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5716/** Recover a transaction that was interrupted by a power failure.
5717 *
5718 * This function is called during initialization, before psa_crypto_init()
5719 * returns. If this function returns a failure status, the initialization
5720 * fails.
5721 */
5722static psa_status_t psa_crypto_recover_transaction(
5723 const psa_crypto_transaction_t *transaction )
5724{
5725 switch( transaction->unknown.type )
5726 {
5727 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5728 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005729 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005730 * is implemented.
5731 * https://github.com/ARMmbed/mbed-crypto/issues/218
5732 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005733 default:
5734 /* We found an unsupported transaction in the storage.
5735 * We don't know what state the storage is in. Give up. */
5736 return( PSA_ERROR_STORAGE_FAILURE );
5737 }
5738}
5739#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5740
Gilles Peskinee59236f2018-01-27 23:32:46 +01005741psa_status_t psa_crypto_init( void )
5742{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005743 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005744 const unsigned char drbg_seed[] = "PSA";
5745
Gilles Peskinec6b69072018-11-20 21:42:52 +01005746 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005747 if( global_data.initialized != 0 )
5748 return( PSA_SUCCESS );
5749
Gilles Peskine5e769522018-11-20 21:59:56 +01005750 /* Set default configuration if
5751 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5752 if( global_data.entropy_init == NULL )
5753 global_data.entropy_init = mbedtls_entropy_init;
5754 if( global_data.entropy_free == NULL )
5755 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005756
Gilles Peskinec6b69072018-11-20 21:42:52 +01005757 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005758 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005759#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5760 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5761 /* The PSA entropy injection feature depends on using NV seed as an entropy
5762 * source. Add NV seed as an entropy source for PSA entropy injection. */
5763 mbedtls_entropy_add_source( &global_data.entropy,
5764 mbedtls_nv_seed_poll, NULL,
5765 MBEDTLS_ENTROPY_BLOCK_SIZE,
5766 MBEDTLS_ENTROPY_SOURCE_STRONG );
5767#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005768 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005769 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005770 status = mbedtls_to_psa_error(
5771 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5772 mbedtls_entropy_func,
5773 &global_data.entropy,
5774 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5775 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005776 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005777 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005778
Gilles Peskine66fb1262018-12-10 16:29:04 +01005779 status = psa_initialize_key_slots( );
5780 if( status != PSA_SUCCESS )
5781 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005782
Gilles Peskined9348f22019-10-01 15:22:29 +02005783#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5784 status = psa_init_all_se_drivers( );
5785 if( status != PSA_SUCCESS )
5786 goto exit;
5787#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5788
Gilles Peskine4b734222019-07-24 15:56:31 +02005789#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005790 status = psa_crypto_load_transaction( );
5791 if( status == PSA_SUCCESS )
5792 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005793 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5794 if( status != PSA_SUCCESS )
5795 goto exit;
5796 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005797 }
5798 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5799 {
5800 /* There's no transaction to complete. It's all good. */
5801 status = PSA_SUCCESS;
5802 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005803#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005804
Gilles Peskinec6b69072018-11-20 21:42:52 +01005805 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005806 global_data.initialized = 1;
5807
5808exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005809 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005810 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005811 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005812}
5813
5814#endif /* MBEDTLS_PSA_CRYPTO_C */