blob: 98166684e71660e2b3ceb851d9b059b2df3d638e [file] [log] [blame]
Gilles Peskinee59236f2018-01-27 23:32:46 +01001/*
2 * PSA crypto layer on top of Mbed TLS crypto
3 */
4/* Copyright (C) 2018, ARM Limited, All Rights Reserved
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * This file is part of mbed TLS (https://tls.mbed.org)
20 */
21
22#if !defined(MBEDTLS_CONFIG_FILE)
23#include "mbedtls/config.h"
24#else
25#include MBEDTLS_CONFIG_FILE
26#endif
27
28#if defined(MBEDTLS_PSA_CRYPTO_C)
mohammad160327010052018-07-03 13:16:15 +030029
itayzafrir7723ab12019-02-14 10:28:02 +020030#include "psa_crypto_service_integration.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010031#include "psa/crypto.h"
32
Gilles Peskine039b90c2018-12-07 18:24:41 +010033#include "psa_crypto_core.h"
Gilles Peskine5e769522018-11-20 21:59:56 +010034#include "psa_crypto_invasive.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020035#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +020036#include "psa_crypto_se.h"
Gilles Peskinea8ade162019-06-26 11:24:49 +020037#endif
Gilles Peskine961849f2018-11-30 18:54:54 +010038#include "psa_crypto_slot_management.h"
Darryl Greend49a4992018-06-18 17:27:26 +010039/* Include internal declarations that are useful for implementing persistently
40 * stored keys. */
41#include "psa_crypto_storage.h"
42
Gilles Peskineb46bef22019-07-30 21:32:04 +020043#include <assert.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010044#include <stdlib.h>
45#include <string.h>
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010046#include "mbedtls/platform.h"
Gilles Peskineff2d2002019-05-06 15:26:23 +020047#if !defined(MBEDTLS_PLATFORM_C)
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010048#define mbedtls_calloc calloc
49#define mbedtls_free free
50#endif
51
Gilles Peskinea5905292018-02-07 20:59:33 +010052#include "mbedtls/arc4.h"
Gilles Peskine9a944802018-06-21 09:35:35 +020053#include "mbedtls/asn1.h"
Jaeden Amero25384a22019-01-10 10:23:21 +000054#include "mbedtls/asn1write.h"
Gilles Peskinea81d85b2018-06-26 16:10:23 +020055#include "mbedtls/bignum.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010056#include "mbedtls/blowfish.h"
57#include "mbedtls/camellia.h"
Gilles Peskine26869f22019-05-06 15:25:00 +020058#include "mbedtls/chacha20.h"
59#include "mbedtls/chachapoly.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010060#include "mbedtls/cipher.h"
61#include "mbedtls/ccm.h"
62#include "mbedtls/cmac.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010063#include "mbedtls/ctr_drbg.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010064#include "mbedtls/des.h"
Gilles Peskineb7ecdf02018-09-18 12:11:27 +020065#include "mbedtls/ecdh.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010066#include "mbedtls/ecp.h"
Gilles Peskinee59236f2018-01-27 23:32:46 +010067#include "mbedtls/entropy.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010068#include "mbedtls/error.h"
69#include "mbedtls/gcm.h"
70#include "mbedtls/md2.h"
71#include "mbedtls/md4.h"
72#include "mbedtls/md5.h"
Gilles Peskine20035e32018-02-03 22:44:14 +010073#include "mbedtls/md.h"
74#include "mbedtls/md_internal.h"
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +010075#include "mbedtls/pk.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010076#include "mbedtls/pk_internal.h"
Gilles Peskine3f108122018-12-07 18:14:53 +010077#include "mbedtls/platform_util.h"
Janos Follath24eed8d2019-11-22 13:21:35 +000078#include "mbedtls/error.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010079#include "mbedtls/ripemd160.h"
Gilles Peskine969ac722018-01-28 18:16:59 +010080#include "mbedtls/rsa.h"
Gilles Peskinea5905292018-02-07 20:59:33 +010081#include "mbedtls/sha1.h"
82#include "mbedtls/sha256.h"
83#include "mbedtls/sha512.h"
84#include "mbedtls/xtea.h"
85
Gilles Peskine996deb12018-08-01 15:45:45 +020086#define ARRAY_LENGTH( array ) ( sizeof( array ) / sizeof( *( array ) ) )
87
Gilles Peskine9ef733f2018-02-07 21:05:37 +010088/* constant-time buffer comparison */
89static inline int safer_memcmp( const uint8_t *a, const uint8_t *b, size_t n )
90{
91 size_t i;
92 unsigned char diff = 0;
93
94 for( i = 0; i < n; i++ )
95 diff |= a[i] ^ b[i];
96
97 return( diff );
98}
99
100
101
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100102/****************************************************************/
103/* Global data, support functions and library management */
104/****************************************************************/
105
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200106static int key_type_is_raw_bytes( psa_key_type_t type )
107{
Gilles Peskine78b3bb62018-08-10 16:03:41 +0200108 return( PSA_KEY_TYPE_IS_UNSTRUCTURED( type ) );
Gilles Peskine48c0ea12018-06-21 14:15:31 +0200109}
110
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100111/* Values for psa_global_data_t::rng_state */
112#define RNG_NOT_INITIALIZED 0
113#define RNG_INITIALIZED 1
114#define RNG_SEEDED 2
Gilles Peskinec6b69072018-11-20 21:42:52 +0100115
Gilles Peskine2d277862018-06-18 15:41:12 +0200116typedef struct
117{
Gilles Peskine5e769522018-11-20 21:59:56 +0100118 void (* entropy_init )( mbedtls_entropy_context *ctx );
119 void (* entropy_free )( mbedtls_entropy_context *ctx );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100120 mbedtls_entropy_context entropy;
121 mbedtls_ctr_drbg_context ctr_drbg;
Gilles Peskinec6b69072018-11-20 21:42:52 +0100122 unsigned initialized : 1;
Gilles Peskine5a3c50e2018-12-04 12:27:09 +0100123 unsigned rng_state : 2;
Gilles Peskinee59236f2018-01-27 23:32:46 +0100124} psa_global_data_t;
125
126static psa_global_data_t global_data;
127
itayzafrir0adf0fc2018-09-06 16:24:41 +0300128#define GUARD_MODULE_INITIALIZED \
129 if( global_data.initialized == 0 ) \
130 return( PSA_ERROR_BAD_STATE );
131
Gilles Peskinee59236f2018-01-27 23:32:46 +0100132static psa_status_t mbedtls_to_psa_error( int ret )
133{
Gilles Peskinea5905292018-02-07 20:59:33 +0100134 /* If there's both a high-level code and low-level code, dispatch on
135 * the high-level code. */
136 switch( ret < -0x7f ? - ( -ret & 0x7f80 ) : ret )
Gilles Peskinee59236f2018-01-27 23:32:46 +0100137 {
138 case 0:
139 return( PSA_SUCCESS );
Gilles Peskinea5905292018-02-07 20:59:33 +0100140
141 case MBEDTLS_ERR_AES_INVALID_KEY_LENGTH:
142 case MBEDTLS_ERR_AES_INVALID_INPUT_LENGTH:
143 case MBEDTLS_ERR_AES_FEATURE_UNAVAILABLE:
144 return( PSA_ERROR_NOT_SUPPORTED );
145 case MBEDTLS_ERR_AES_HW_ACCEL_FAILED:
146 return( PSA_ERROR_HARDWARE_FAILURE );
147
148 case MBEDTLS_ERR_ARC4_HW_ACCEL_FAILED:
149 return( PSA_ERROR_HARDWARE_FAILURE );
150
Gilles Peskine9a944802018-06-21 09:35:35 +0200151 case MBEDTLS_ERR_ASN1_OUT_OF_DATA:
152 case MBEDTLS_ERR_ASN1_UNEXPECTED_TAG:
153 case MBEDTLS_ERR_ASN1_INVALID_LENGTH:
154 case MBEDTLS_ERR_ASN1_LENGTH_MISMATCH:
155 case MBEDTLS_ERR_ASN1_INVALID_DATA:
156 return( PSA_ERROR_INVALID_ARGUMENT );
157 case MBEDTLS_ERR_ASN1_ALLOC_FAILED:
158 return( PSA_ERROR_INSUFFICIENT_MEMORY );
159 case MBEDTLS_ERR_ASN1_BUF_TOO_SMALL:
160 return( PSA_ERROR_BUFFER_TOO_SMALL );
161
Jaeden Amero93e21112019-02-20 13:57:28 +0000162#if defined(MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000163 case MBEDTLS_ERR_BLOWFISH_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000164#elif defined(MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH)
165 case MBEDTLS_ERR_BLOWFISH_INVALID_KEY_LENGTH:
166#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100167 case MBEDTLS_ERR_BLOWFISH_INVALID_INPUT_LENGTH:
168 return( PSA_ERROR_NOT_SUPPORTED );
169 case MBEDTLS_ERR_BLOWFISH_HW_ACCEL_FAILED:
170 return( PSA_ERROR_HARDWARE_FAILURE );
171
Jaeden Amero93e21112019-02-20 13:57:28 +0000172#if defined(MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA)
Jaeden Amero892cd6d2019-02-11 12:21:12 +0000173 case MBEDTLS_ERR_CAMELLIA_BAD_INPUT_DATA:
Jaeden Amero93e21112019-02-20 13:57:28 +0000174#elif defined(MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH)
175 case MBEDTLS_ERR_CAMELLIA_INVALID_KEY_LENGTH:
176#endif
Gilles Peskinea5905292018-02-07 20:59:33 +0100177 case MBEDTLS_ERR_CAMELLIA_INVALID_INPUT_LENGTH:
178 return( PSA_ERROR_NOT_SUPPORTED );
179 case MBEDTLS_ERR_CAMELLIA_HW_ACCEL_FAILED:
180 return( PSA_ERROR_HARDWARE_FAILURE );
181
182 case MBEDTLS_ERR_CCM_BAD_INPUT:
183 return( PSA_ERROR_INVALID_ARGUMENT );
184 case MBEDTLS_ERR_CCM_AUTH_FAILED:
185 return( PSA_ERROR_INVALID_SIGNATURE );
186 case MBEDTLS_ERR_CCM_HW_ACCEL_FAILED:
187 return( PSA_ERROR_HARDWARE_FAILURE );
188
Gilles Peskine26869f22019-05-06 15:25:00 +0200189 case MBEDTLS_ERR_CHACHA20_BAD_INPUT_DATA:
190 return( PSA_ERROR_INVALID_ARGUMENT );
191
192 case MBEDTLS_ERR_CHACHAPOLY_BAD_STATE:
193 return( PSA_ERROR_BAD_STATE );
194 case MBEDTLS_ERR_CHACHAPOLY_AUTH_FAILED:
195 return( PSA_ERROR_INVALID_SIGNATURE );
196
Gilles Peskinea5905292018-02-07 20:59:33 +0100197 case MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE:
198 return( PSA_ERROR_NOT_SUPPORTED );
199 case MBEDTLS_ERR_CIPHER_BAD_INPUT_DATA:
200 return( PSA_ERROR_INVALID_ARGUMENT );
201 case MBEDTLS_ERR_CIPHER_ALLOC_FAILED:
202 return( PSA_ERROR_INSUFFICIENT_MEMORY );
203 case MBEDTLS_ERR_CIPHER_INVALID_PADDING:
204 return( PSA_ERROR_INVALID_PADDING );
205 case MBEDTLS_ERR_CIPHER_FULL_BLOCK_EXPECTED:
206 return( PSA_ERROR_BAD_STATE );
207 case MBEDTLS_ERR_CIPHER_AUTH_FAILED:
208 return( PSA_ERROR_INVALID_SIGNATURE );
209 case MBEDTLS_ERR_CIPHER_INVALID_CONTEXT:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200210 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100211 case MBEDTLS_ERR_CIPHER_HW_ACCEL_FAILED:
212 return( PSA_ERROR_HARDWARE_FAILURE );
213
214 case MBEDTLS_ERR_CMAC_HW_ACCEL_FAILED:
215 return( PSA_ERROR_HARDWARE_FAILURE );
216
217 case MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED:
218 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
219 case MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG:
220 case MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG:
221 return( PSA_ERROR_NOT_SUPPORTED );
222 case MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR:
223 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
224
225 case MBEDTLS_ERR_DES_INVALID_INPUT_LENGTH:
226 return( PSA_ERROR_NOT_SUPPORTED );
227 case MBEDTLS_ERR_DES_HW_ACCEL_FAILED:
228 return( PSA_ERROR_HARDWARE_FAILURE );
229
Gilles Peskinee59236f2018-01-27 23:32:46 +0100230 case MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED:
231 case MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE:
232 case MBEDTLS_ERR_ENTROPY_SOURCE_FAILED:
233 return( PSA_ERROR_INSUFFICIENT_ENTROPY );
Gilles Peskinea5905292018-02-07 20:59:33 +0100234
235 case MBEDTLS_ERR_GCM_AUTH_FAILED:
236 return( PSA_ERROR_INVALID_SIGNATURE );
237 case MBEDTLS_ERR_GCM_BAD_INPUT:
Gilles Peskine8cac2e62018-08-21 15:07:38 +0200238 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinea5905292018-02-07 20:59:33 +0100239 case MBEDTLS_ERR_GCM_HW_ACCEL_FAILED:
240 return( PSA_ERROR_HARDWARE_FAILURE );
241
242 case MBEDTLS_ERR_MD2_HW_ACCEL_FAILED:
243 case MBEDTLS_ERR_MD4_HW_ACCEL_FAILED:
244 case MBEDTLS_ERR_MD5_HW_ACCEL_FAILED:
245 return( PSA_ERROR_HARDWARE_FAILURE );
246
247 case MBEDTLS_ERR_MD_FEATURE_UNAVAILABLE:
248 return( PSA_ERROR_NOT_SUPPORTED );
249 case MBEDTLS_ERR_MD_BAD_INPUT_DATA:
250 return( PSA_ERROR_INVALID_ARGUMENT );
251 case MBEDTLS_ERR_MD_ALLOC_FAILED:
252 return( PSA_ERROR_INSUFFICIENT_MEMORY );
253 case MBEDTLS_ERR_MD_FILE_IO_ERROR:
254 return( PSA_ERROR_STORAGE_FAILURE );
255 case MBEDTLS_ERR_MD_HW_ACCEL_FAILED:
256 return( PSA_ERROR_HARDWARE_FAILURE );
257
Gilles Peskinef76aa772018-10-29 19:24:33 +0100258 case MBEDTLS_ERR_MPI_FILE_IO_ERROR:
259 return( PSA_ERROR_STORAGE_FAILURE );
260 case MBEDTLS_ERR_MPI_BAD_INPUT_DATA:
261 return( PSA_ERROR_INVALID_ARGUMENT );
262 case MBEDTLS_ERR_MPI_INVALID_CHARACTER:
263 return( PSA_ERROR_INVALID_ARGUMENT );
264 case MBEDTLS_ERR_MPI_BUFFER_TOO_SMALL:
265 return( PSA_ERROR_BUFFER_TOO_SMALL );
266 case MBEDTLS_ERR_MPI_NEGATIVE_VALUE:
267 return( PSA_ERROR_INVALID_ARGUMENT );
268 case MBEDTLS_ERR_MPI_DIVISION_BY_ZERO:
269 return( PSA_ERROR_INVALID_ARGUMENT );
270 case MBEDTLS_ERR_MPI_NOT_ACCEPTABLE:
271 return( PSA_ERROR_INVALID_ARGUMENT );
272 case MBEDTLS_ERR_MPI_ALLOC_FAILED:
273 return( PSA_ERROR_INSUFFICIENT_MEMORY );
274
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100275 case MBEDTLS_ERR_PK_ALLOC_FAILED:
276 return( PSA_ERROR_INSUFFICIENT_MEMORY );
277 case MBEDTLS_ERR_PK_TYPE_MISMATCH:
278 case MBEDTLS_ERR_PK_BAD_INPUT_DATA:
279 return( PSA_ERROR_INVALID_ARGUMENT );
280 case MBEDTLS_ERR_PK_FILE_IO_ERROR:
Gilles Peskinea5905292018-02-07 20:59:33 +0100281 return( PSA_ERROR_STORAGE_FAILURE );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100282 case MBEDTLS_ERR_PK_KEY_INVALID_VERSION:
283 case MBEDTLS_ERR_PK_KEY_INVALID_FORMAT:
284 return( PSA_ERROR_INVALID_ARGUMENT );
285 case MBEDTLS_ERR_PK_UNKNOWN_PK_ALG:
286 return( PSA_ERROR_NOT_SUPPORTED );
287 case MBEDTLS_ERR_PK_PASSWORD_REQUIRED:
288 case MBEDTLS_ERR_PK_PASSWORD_MISMATCH:
289 return( PSA_ERROR_NOT_PERMITTED );
290 case MBEDTLS_ERR_PK_INVALID_PUBKEY:
291 return( PSA_ERROR_INVALID_ARGUMENT );
292 case MBEDTLS_ERR_PK_INVALID_ALG:
293 case MBEDTLS_ERR_PK_UNKNOWN_NAMED_CURVE:
294 case MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE:
295 return( PSA_ERROR_NOT_SUPPORTED );
296 case MBEDTLS_ERR_PK_SIG_LEN_MISMATCH:
297 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskinea5905292018-02-07 20:59:33 +0100298 case MBEDTLS_ERR_PK_HW_ACCEL_FAILED:
299 return( PSA_ERROR_HARDWARE_FAILURE );
300
Gilles Peskineff2d2002019-05-06 15:26:23 +0200301 case MBEDTLS_ERR_PLATFORM_HW_ACCEL_FAILED:
302 return( PSA_ERROR_HARDWARE_FAILURE );
303 case MBEDTLS_ERR_PLATFORM_FEATURE_UNSUPPORTED:
304 return( PSA_ERROR_NOT_SUPPORTED );
305
Gilles Peskinea5905292018-02-07 20:59:33 +0100306 case MBEDTLS_ERR_RIPEMD160_HW_ACCEL_FAILED:
307 return( PSA_ERROR_HARDWARE_FAILURE );
308
309 case MBEDTLS_ERR_RSA_BAD_INPUT_DATA:
310 return( PSA_ERROR_INVALID_ARGUMENT );
311 case MBEDTLS_ERR_RSA_INVALID_PADDING:
312 return( PSA_ERROR_INVALID_PADDING );
313 case MBEDTLS_ERR_RSA_KEY_GEN_FAILED:
314 return( PSA_ERROR_HARDWARE_FAILURE );
315 case MBEDTLS_ERR_RSA_KEY_CHECK_FAILED:
316 return( PSA_ERROR_INVALID_ARGUMENT );
317 case MBEDTLS_ERR_RSA_PUBLIC_FAILED:
318 case MBEDTLS_ERR_RSA_PRIVATE_FAILED:
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200319 return( PSA_ERROR_CORRUPTION_DETECTED );
Gilles Peskinea5905292018-02-07 20:59:33 +0100320 case MBEDTLS_ERR_RSA_VERIFY_FAILED:
321 return( PSA_ERROR_INVALID_SIGNATURE );
322 case MBEDTLS_ERR_RSA_OUTPUT_TOO_LARGE:
323 return( PSA_ERROR_BUFFER_TOO_SMALL );
324 case MBEDTLS_ERR_RSA_RNG_FAILED:
325 return( PSA_ERROR_INSUFFICIENT_MEMORY );
326 case MBEDTLS_ERR_RSA_UNSUPPORTED_OPERATION:
327 return( PSA_ERROR_NOT_SUPPORTED );
328 case MBEDTLS_ERR_RSA_HW_ACCEL_FAILED:
329 return( PSA_ERROR_HARDWARE_FAILURE );
330
331 case MBEDTLS_ERR_SHA1_HW_ACCEL_FAILED:
332 case MBEDTLS_ERR_SHA256_HW_ACCEL_FAILED:
333 case MBEDTLS_ERR_SHA512_HW_ACCEL_FAILED:
334 return( PSA_ERROR_HARDWARE_FAILURE );
335
336 case MBEDTLS_ERR_XTEA_INVALID_INPUT_LENGTH:
337 return( PSA_ERROR_INVALID_ARGUMENT );
338 case MBEDTLS_ERR_XTEA_HW_ACCEL_FAILED:
339 return( PSA_ERROR_HARDWARE_FAILURE );
340
itayzafrir5c753392018-05-08 11:18:38 +0300341 case MBEDTLS_ERR_ECP_BAD_INPUT_DATA:
itayzafrir7b30f8b2018-05-09 16:07:36 +0300342 case MBEDTLS_ERR_ECP_INVALID_KEY:
itayzafrir5c753392018-05-08 11:18:38 +0300343 return( PSA_ERROR_INVALID_ARGUMENT );
itayzafrir7b30f8b2018-05-09 16:07:36 +0300344 case MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL:
345 return( PSA_ERROR_BUFFER_TOO_SMALL );
346 case MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE:
347 return( PSA_ERROR_NOT_SUPPORTED );
348 case MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH:
349 case MBEDTLS_ERR_ECP_VERIFY_FAILED:
350 return( PSA_ERROR_INVALID_SIGNATURE );
351 case MBEDTLS_ERR_ECP_ALLOC_FAILED:
352 return( PSA_ERROR_INSUFFICIENT_MEMORY );
353 case MBEDTLS_ERR_ECP_HW_ACCEL_FAILED:
354 return( PSA_ERROR_HARDWARE_FAILURE );
Janos Follatha13b9052019-11-22 12:48:59 +0000355 case MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED:
356 return( PSA_ERROR_CORRUPTION_DETECTED );
itayzafrir5c753392018-05-08 11:18:38 +0300357
Gilles Peskinee59236f2018-01-27 23:32:46 +0100358 default:
David Saadab4ecc272019-02-14 13:48:10 +0200359 return( PSA_ERROR_GENERIC_ERROR );
Gilles Peskinee59236f2018-01-27 23:32:46 +0100360 }
361}
362
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200363
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +0200364
365
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100366/****************************************************************/
367/* Key management */
368/****************************************************************/
369
Gilles Peskine73167e12019-07-12 23:44:37 +0200370#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
371static inline int psa_key_slot_is_external( const psa_key_slot_t *slot )
372{
Gilles Peskine8e338702019-07-30 20:06:31 +0200373 return( psa_key_lifetime_is_external( slot->attr.lifetime ) );
Gilles Peskine73167e12019-07-12 23:44:37 +0200374}
375#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
376
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100377#if defined(MBEDTLS_ECP_C)
Gilles Peskine34ef7f52018-06-18 20:47:51 +0200378static psa_ecc_curve_t mbedtls_ecc_group_to_psa( mbedtls_ecp_group_id grpid )
379{
380 switch( grpid )
381 {
382 case MBEDTLS_ECP_DP_SECP192R1:
383 return( PSA_ECC_CURVE_SECP192R1 );
384 case MBEDTLS_ECP_DP_SECP224R1:
385 return( PSA_ECC_CURVE_SECP224R1 );
386 case MBEDTLS_ECP_DP_SECP256R1:
387 return( PSA_ECC_CURVE_SECP256R1 );
388 case MBEDTLS_ECP_DP_SECP384R1:
389 return( PSA_ECC_CURVE_SECP384R1 );
390 case MBEDTLS_ECP_DP_SECP521R1:
391 return( PSA_ECC_CURVE_SECP521R1 );
392 case MBEDTLS_ECP_DP_BP256R1:
393 return( PSA_ECC_CURVE_BRAINPOOL_P256R1 );
394 case MBEDTLS_ECP_DP_BP384R1:
395 return( PSA_ECC_CURVE_BRAINPOOL_P384R1 );
396 case MBEDTLS_ECP_DP_BP512R1:
397 return( PSA_ECC_CURVE_BRAINPOOL_P512R1 );
398 case MBEDTLS_ECP_DP_CURVE25519:
399 return( PSA_ECC_CURVE_CURVE25519 );
400 case MBEDTLS_ECP_DP_SECP192K1:
401 return( PSA_ECC_CURVE_SECP192K1 );
402 case MBEDTLS_ECP_DP_SECP224K1:
403 return( PSA_ECC_CURVE_SECP224K1 );
404 case MBEDTLS_ECP_DP_SECP256K1:
405 return( PSA_ECC_CURVE_SECP256K1 );
406 case MBEDTLS_ECP_DP_CURVE448:
407 return( PSA_ECC_CURVE_CURVE448 );
408 default:
409 return( 0 );
410 }
411}
412
Gilles Peskine12313cd2018-06-20 00:20:32 +0200413static mbedtls_ecp_group_id mbedtls_ecc_group_of_psa( psa_ecc_curve_t curve )
414{
415 switch( curve )
416 {
417 case PSA_ECC_CURVE_SECP192R1:
418 return( MBEDTLS_ECP_DP_SECP192R1 );
419 case PSA_ECC_CURVE_SECP224R1:
420 return( MBEDTLS_ECP_DP_SECP224R1 );
421 case PSA_ECC_CURVE_SECP256R1:
422 return( MBEDTLS_ECP_DP_SECP256R1 );
423 case PSA_ECC_CURVE_SECP384R1:
424 return( MBEDTLS_ECP_DP_SECP384R1 );
425 case PSA_ECC_CURVE_SECP521R1:
426 return( MBEDTLS_ECP_DP_SECP521R1 );
427 case PSA_ECC_CURVE_BRAINPOOL_P256R1:
428 return( MBEDTLS_ECP_DP_BP256R1 );
429 case PSA_ECC_CURVE_BRAINPOOL_P384R1:
430 return( MBEDTLS_ECP_DP_BP384R1 );
431 case PSA_ECC_CURVE_BRAINPOOL_P512R1:
432 return( MBEDTLS_ECP_DP_BP512R1 );
433 case PSA_ECC_CURVE_CURVE25519:
434 return( MBEDTLS_ECP_DP_CURVE25519 );
435 case PSA_ECC_CURVE_SECP192K1:
436 return( MBEDTLS_ECP_DP_SECP192K1 );
437 case PSA_ECC_CURVE_SECP224K1:
438 return( MBEDTLS_ECP_DP_SECP224K1 );
439 case PSA_ECC_CURVE_SECP256K1:
440 return( MBEDTLS_ECP_DP_SECP256K1 );
441 case PSA_ECC_CURVE_CURVE448:
442 return( MBEDTLS_ECP_DP_CURVE448 );
443 default:
444 return( MBEDTLS_ECP_DP_NONE );
445 }
446}
Darryl Green8f8aa8f2018-07-24 15:44:51 +0100447#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine12313cd2018-06-20 00:20:32 +0200448
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200449static psa_status_t prepare_raw_data_slot( psa_key_type_t type,
450 size_t bits,
451 struct raw_data *raw )
452{
453 /* Check that the bit size is acceptable for the key type */
454 switch( type )
455 {
456 case PSA_KEY_TYPE_RAW_DATA:
457#if defined(MBEDTLS_MD_C)
458 case PSA_KEY_TYPE_HMAC:
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200459#endif
Gilles Peskineea0fb492018-07-12 17:17:20 +0200460 case PSA_KEY_TYPE_DERIVE:
461 break;
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200462#if defined(MBEDTLS_AES_C)
463 case PSA_KEY_TYPE_AES:
464 if( bits != 128 && bits != 192 && bits != 256 )
465 return( PSA_ERROR_INVALID_ARGUMENT );
466 break;
467#endif
468#if defined(MBEDTLS_CAMELLIA_C)
469 case PSA_KEY_TYPE_CAMELLIA:
470 if( bits != 128 && bits != 192 && bits != 256 )
471 return( PSA_ERROR_INVALID_ARGUMENT );
472 break;
473#endif
474#if defined(MBEDTLS_DES_C)
475 case PSA_KEY_TYPE_DES:
476 if( bits != 64 && bits != 128 && bits != 192 )
477 return( PSA_ERROR_INVALID_ARGUMENT );
478 break;
479#endif
480#if defined(MBEDTLS_ARC4_C)
481 case PSA_KEY_TYPE_ARC4:
482 if( bits < 8 || bits > 2048 )
483 return( PSA_ERROR_INVALID_ARGUMENT );
484 break;
485#endif
Gilles Peskine26869f22019-05-06 15:25:00 +0200486#if defined(MBEDTLS_CHACHA20_C)
487 case PSA_KEY_TYPE_CHACHA20:
488 if( bits != 256 )
489 return( PSA_ERROR_INVALID_ARGUMENT );
490 break;
491#endif
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200492 default:
493 return( PSA_ERROR_NOT_SUPPORTED );
494 }
Gilles Peskineb54979a2018-06-21 09:32:47 +0200495 if( bits % 8 != 0 )
496 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200497
498 /* Allocate memory for the key */
499 raw->bytes = PSA_BITS_TO_BYTES( bits );
500 raw->data = mbedtls_calloc( 1, raw->bytes );
501 if( raw->data == NULL )
502 {
503 raw->bytes = 0;
504 return( PSA_ERROR_INSUFFICIENT_MEMORY );
505 }
506 return( PSA_SUCCESS );
507}
508
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200509#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine86a440b2018-11-12 18:39:40 +0100510/* Mbed TLS doesn't support non-byte-aligned key sizes (i.e. key sizes
511 * that are not a multiple of 8) well. For example, there is only
512 * mbedtls_rsa_get_len(), which returns a number of bytes, and no
513 * way to return the exact bit size of a key.
514 * To keep things simple, reject non-byte-aligned key sizes. */
515static psa_status_t psa_check_rsa_key_byte_aligned(
516 const mbedtls_rsa_context *rsa )
517{
518 mbedtls_mpi n;
519 psa_status_t status;
520 mbedtls_mpi_init( &n );
521 status = mbedtls_to_psa_error(
522 mbedtls_rsa_export( rsa, &n, NULL, NULL, NULL, NULL ) );
523 if( status == PSA_SUCCESS )
524 {
525 if( mbedtls_mpi_bitlen( &n ) % 8 != 0 )
526 status = PSA_ERROR_NOT_SUPPORTED;
527 }
528 mbedtls_mpi_free( &n );
529 return( status );
530}
531
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000532static psa_status_t psa_import_rsa_key( psa_key_type_t type,
533 const uint8_t *data,
534 size_t data_length,
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200535 mbedtls_rsa_context **p_rsa )
536{
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000537 psa_status_t status;
538 mbedtls_pk_context pk;
539 mbedtls_rsa_context *rsa;
540 size_t bits;
541
542 mbedtls_pk_init( &pk );
543
544 /* Parse the data. */
Gilles Peskinec93b80c2019-05-16 19:39:54 +0200545 if( PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000546 status = mbedtls_to_psa_error(
547 mbedtls_pk_parse_key( &pk, data, data_length, NULL, 0 ) );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200548 else
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000549 status = mbedtls_to_psa_error(
550 mbedtls_pk_parse_public_key( &pk, data, data_length ) );
551 if( status != PSA_SUCCESS )
552 goto exit;
553
554 /* We have something that the pkparse module recognizes. If it is a
555 * valid RSA key, store it. */
556 if( mbedtls_pk_get_type( &pk ) != MBEDTLS_PK_RSA )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200557 {
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000558 status = PSA_ERROR_INVALID_ARGUMENT;
559 goto exit;
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200560 }
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000561
562 rsa = mbedtls_pk_rsa( pk );
563 /* The size of an RSA key doesn't have to be a multiple of 8. Mbed TLS
564 * supports non-byte-aligned key sizes, but not well. For example,
565 * mbedtls_rsa_get_len() returns the key size in bytes, not in bits. */
566 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( rsa ) );
567 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
568 {
569 status = PSA_ERROR_NOT_SUPPORTED;
570 goto exit;
571 }
572 status = psa_check_rsa_key_byte_aligned( rsa );
573
574exit:
575 /* Free the content of the pk object only on error. */
576 if( status != PSA_SUCCESS )
577 {
578 mbedtls_pk_free( &pk );
579 return( status );
580 }
581
582 /* On success, store the content of the object in the RSA context. */
583 *p_rsa = rsa;
584
585 return( PSA_SUCCESS );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200586}
587#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
588
Jaeden Ameroccdce902019-01-10 11:42:27 +0000589#if defined(MBEDTLS_ECP_C)
Gilles Peskine4cd32772019-12-02 20:49:42 +0100590static psa_status_t psa_prepare_import_ec_key( psa_ecc_curve_t curve,
591 mbedtls_ecp_keypair **p_ecp )
592{
593 mbedtls_ecp_group_id grp_id = MBEDTLS_ECP_DP_NONE;
594 *p_ecp = mbedtls_calloc( 1, sizeof( mbedtls_ecp_keypair ) );
595 if( *p_ecp == NULL )
596 return( PSA_ERROR_INSUFFICIENT_MEMORY );
597 mbedtls_ecp_keypair_init( *p_ecp );
598
599 /* Load the group. */
600 grp_id = mbedtls_ecc_group_of_psa( curve );
601 return( mbedtls_to_psa_error(
602 mbedtls_ecp_group_load( &( *p_ecp )->grp, grp_id ) ) );
603}
Jaeden Ameroccdce902019-01-10 11:42:27 +0000604
605/* Import a public key given as the uncompressed representation defined by SEC1
606 * 2.3.3 as the content of an ECPoint. */
607static psa_status_t psa_import_ec_public_key( psa_ecc_curve_t curve,
608 const uint8_t *data,
609 size_t data_length,
610 mbedtls_ecp_keypair **p_ecp )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200611{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200612 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000613 mbedtls_ecp_keypair *ecp = NULL;
Jaeden Ameroccdce902019-01-10 11:42:27 +0000614
Gilles Peskine4cd32772019-12-02 20:49:42 +0100615 status = psa_prepare_import_ec_key( curve, &ecp );
Jaeden Ameroccdce902019-01-10 11:42:27 +0000616 if( status != PSA_SUCCESS )
617 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100618
Jaeden Ameroccdce902019-01-10 11:42:27 +0000619 /* Load the public value. */
620 status = mbedtls_to_psa_error(
621 mbedtls_ecp_point_read_binary( &ecp->grp, &ecp->Q,
622 data, data_length ) );
623 if( status != PSA_SUCCESS )
624 goto exit;
625
626 /* Check that the point is on the curve. */
627 status = mbedtls_to_psa_error(
628 mbedtls_ecp_check_pubkey( &ecp->grp, &ecp->Q ) );
629 if( status != PSA_SUCCESS )
630 goto exit;
631
632 *p_ecp = ecp;
633 return( PSA_SUCCESS );
634
635exit:
636 if( ecp != NULL )
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200637 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000638 mbedtls_ecp_keypair_free( ecp );
639 mbedtls_free( ecp );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200640 }
Jaeden Ameroccdce902019-01-10 11:42:27 +0000641 return( status );
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200642}
Gilles Peskineaf89fd72018-06-29 19:52:37 +0200643
Gilles Peskinef76aa772018-10-29 19:24:33 +0100644/* Import a private key given as a byte string which is the private value
645 * in big-endian order. */
646static psa_status_t psa_import_ec_private_key( psa_ecc_curve_t curve,
647 const uint8_t *data,
648 size_t data_length,
649 mbedtls_ecp_keypair **p_ecp )
650{
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200651 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100652 mbedtls_ecp_keypair *ecp = NULL;
Gilles Peskinef76aa772018-10-29 19:24:33 +0100653
Gilles Peskine2c86ebc2019-05-13 14:21:57 +0200654 if( PSA_BITS_TO_BYTES( PSA_ECC_CURVE_BITS( curve ) ) != data_length )
655 return( PSA_ERROR_INVALID_ARGUMENT );
656
Gilles Peskine4cd32772019-12-02 20:49:42 +0100657 status = psa_prepare_import_ec_key( curve, &ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100658 if( status != PSA_SUCCESS )
659 goto exit;
Gilles Peskine4cd32772019-12-02 20:49:42 +0100660
Gilles Peskinef76aa772018-10-29 19:24:33 +0100661 /* Load the secret value. */
662 status = mbedtls_to_psa_error(
663 mbedtls_mpi_read_binary( &ecp->d, data, data_length ) );
664 if( status != PSA_SUCCESS )
665 goto exit;
666 /* Validate the private key. */
667 status = mbedtls_to_psa_error(
668 mbedtls_ecp_check_privkey( &ecp->grp, &ecp->d ) );
669 if( status != PSA_SUCCESS )
670 goto exit;
671 /* Calculate the public key from the private key. */
672 status = mbedtls_to_psa_error(
673 mbedtls_ecp_mul( &ecp->grp, &ecp->Q, &ecp->d, &ecp->grp.G,
674 mbedtls_ctr_drbg_random, &global_data.ctr_drbg ) );
675 if( status != PSA_SUCCESS )
676 goto exit;
677
678 *p_ecp = ecp;
679 return( PSA_SUCCESS );
680
681exit:
682 if( ecp != NULL )
683 {
684 mbedtls_ecp_keypair_free( ecp );
685 mbedtls_free( ecp );
686 }
687 return( status );
688}
689#endif /* defined(MBEDTLS_ECP_C) */
690
Gilles Peskineb46bef22019-07-30 21:32:04 +0200691
692/** Return the size of the key in the given slot, in bits.
693 *
694 * \param[in] slot A key slot.
695 *
696 * \return The key size in bits, read from the metadata in the slot.
697 */
698static inline size_t psa_get_key_slot_bits( const psa_key_slot_t *slot )
699{
700 return( slot->attr.bits );
701}
702
703/** Calculate the size of the key in the given slot, in bits.
704 *
705 * \param[in] slot A key slot containing a transparent key.
706 *
707 * \return The key size in bits, calculated from the key data.
708 */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200709static psa_key_bits_t psa_calculate_key_bits( const psa_key_slot_t *slot )
Gilles Peskineb46bef22019-07-30 21:32:04 +0200710{
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200711 size_t bits = 0; /* return 0 on an empty slot */
712
Gilles Peskineb46bef22019-07-30 21:32:04 +0200713 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200714 bits = PSA_BYTES_TO_BITS( slot->data.raw.bytes );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200715#if defined(MBEDTLS_RSA_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200716 else if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
717 bits = PSA_BYTES_TO_BITS( mbedtls_rsa_get_len( slot->data.rsa ) );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200718#endif /* defined(MBEDTLS_RSA_C) */
719#if defined(MBEDTLS_ECP_C)
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200720 else if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
721 bits = slot->data.ecp->grp.pbits;
Gilles Peskineb46bef22019-07-30 21:32:04 +0200722#endif /* defined(MBEDTLS_ECP_C) */
Gilles Peskine8908c5e2019-07-31 18:55:00 +0200723
724 /* We know that the size fits in psa_key_bits_t thanks to checks
725 * when the key was created. */
726 return( (psa_key_bits_t) bits );
Gilles Peskineb46bef22019-07-30 21:32:04 +0200727}
728
Gilles Peskine8e338702019-07-30 20:06:31 +0200729/** Import key data into a slot. `slot->attr.type` must have been set
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100730 * previously. This function assumes that the slot does not contain
731 * any key material yet. On failure, the slot content is unchanged. */
Gilles Peskinefa4135b2018-12-10 16:48:53 +0100732psa_status_t psa_import_key_into_slot( psa_key_slot_t *slot,
733 const uint8_t *data,
734 size_t data_length )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100735{
Gilles Peskineb0b255c2018-07-06 17:01:38 +0200736 psa_status_t status = PSA_SUCCESS;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100737
Gilles Peskine8e338702019-07-30 20:06:31 +0200738 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100739 {
Gilles Peskinec744d992019-07-30 17:26:54 +0200740 size_t bit_size = PSA_BYTES_TO_BITS( data_length );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200741 /* Ensure that the bytes-to-bit conversion didn't overflow. */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100742 if( data_length > SIZE_MAX / 8 )
743 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine1b9505c2019-08-07 10:59:45 +0200744 /* Enforce a size limit, and in particular ensure that the bit
745 * size fits in its representation type. */
Gilles Peskinec744d992019-07-30 17:26:54 +0200746 if( bit_size > PSA_MAX_KEY_BITS )
747 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine8e338702019-07-30 20:06:31 +0200748 status = prepare_raw_data_slot( slot->attr.type, bit_size,
Gilles Peskine0ff4b0f2018-06-19 21:31:50 +0200749 &slot->data.raw );
750 if( status != PSA_SUCCESS )
751 return( status );
Gilles Peskine46f1fd72018-06-28 19:31:31 +0200752 if( data_length != 0 )
753 memcpy( slot->data.raw.data, data, data_length );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100754 }
755 else
Gilles Peskinef76aa772018-10-29 19:24:33 +0100756#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200757 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) )
Gilles Peskinef76aa772018-10-29 19:24:33 +0100758 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200759 status = psa_import_ec_private_key( PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Gilles Peskinef76aa772018-10-29 19:24:33 +0100760 data, data_length,
761 &slot->data.ecp );
Gilles Peskinef76aa772018-10-29 19:24:33 +0100762 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200763 else if( PSA_KEY_TYPE_IS_ECC_PUBLIC_KEY( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100764 {
Jaeden Ameroccdce902019-01-10 11:42:27 +0000765 status = psa_import_ec_public_key(
Gilles Peskine8e338702019-07-30 20:06:31 +0200766 PSA_KEY_TYPE_GET_CURVE( slot->attr.type ),
Jaeden Ameroccdce902019-01-10 11:42:27 +0000767 data, data_length,
768 &slot->data.ecp );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100769 }
770 else
Gilles Peskine969ac722018-01-28 18:16:59 +0100771#endif /* MBEDTLS_ECP_C */
Jaeden Ameroccdce902019-01-10 11:42:27 +0000772#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200773 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100774 {
Gilles Peskine8e338702019-07-30 20:06:31 +0200775 status = psa_import_rsa_key( slot->attr.type,
Jaeden Amerocd09d8c2019-01-11 17:53:05 +0000776 data, data_length,
777 &slot->data.rsa );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100778 }
779 else
Jaeden Ameroccdce902019-01-10 11:42:27 +0000780#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PK_PARSE_C) */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +0100781 {
782 return( PSA_ERROR_NOT_SUPPORTED );
783 }
Darryl Green940d72c2018-07-13 13:18:51 +0100784
Gilles Peskineb46bef22019-07-30 21:32:04 +0200785 if( status == PSA_SUCCESS )
786 {
787 /* Write the actual key size to the slot.
788 * psa_start_key_creation() wrote the size declared by the
789 * caller, which may be 0 (meaning unspecified) or wrong. */
790 slot->attr.bits = psa_calculate_key_bits( slot );
791 }
Darryl Green06fd18d2018-07-16 11:21:11 +0100792 return( status );
793}
794
Gilles Peskinef603c712019-01-19 13:40:11 +0100795/** Calculate the intersection of two algorithm usage policies.
796 *
797 * Return 0 (which allows no operation) on incompatibility.
798 */
799static psa_algorithm_t psa_key_policy_algorithm_intersection(
800 psa_algorithm_t alg1,
801 psa_algorithm_t alg2 )
802{
Gilles Peskine549ea862019-05-22 11:45:59 +0200803 /* Common case: both sides actually specify the same policy. */
Gilles Peskinef603c712019-01-19 13:40:11 +0100804 if( alg1 == alg2 )
805 return( alg1 );
806 /* If the policies are from the same hash-and-sign family, check
807 * if one is a wildcard. If so the other has the specific algorithm. */
808 if( PSA_ALG_IS_HASH_AND_SIGN( alg1 ) &&
809 PSA_ALG_IS_HASH_AND_SIGN( alg2 ) &&
810 ( alg1 & ~PSA_ALG_HASH_MASK ) == ( alg2 & ~PSA_ALG_HASH_MASK ) )
811 {
812 if( PSA_ALG_SIGN_GET_HASH( alg1 ) == PSA_ALG_ANY_HASH )
813 return( alg2 );
814 if( PSA_ALG_SIGN_GET_HASH( alg2 ) == PSA_ALG_ANY_HASH )
815 return( alg1 );
816 }
817 /* If the policies are incompatible, allow nothing. */
818 return( 0 );
819}
820
Gilles Peskined6f371b2019-05-10 19:33:38 +0200821static int psa_key_algorithm_permits( psa_algorithm_t policy_alg,
822 psa_algorithm_t requested_alg )
823{
Gilles Peskine549ea862019-05-22 11:45:59 +0200824 /* Common case: the policy only allows requested_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200825 if( requested_alg == policy_alg )
826 return( 1 );
827 /* If policy_alg is a hash-and-sign with a wildcard for the hash,
Gilles Peskine549ea862019-05-22 11:45:59 +0200828 * and requested_alg is the same hash-and-sign family with any hash,
829 * then requested_alg is compliant with policy_alg. */
Gilles Peskined6f371b2019-05-10 19:33:38 +0200830 if( PSA_ALG_IS_HASH_AND_SIGN( requested_alg ) &&
831 PSA_ALG_SIGN_GET_HASH( policy_alg ) == PSA_ALG_ANY_HASH )
832 {
833 return( ( policy_alg & ~PSA_ALG_HASH_MASK ) ==
834 ( requested_alg & ~PSA_ALG_HASH_MASK ) );
835 }
836 /* If it isn't permitted, it's forbidden. */
837 return( 0 );
838}
839
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100840/** Test whether a policy permits an algorithm.
841 *
842 * The caller must test usage flags separately.
843 */
844static int psa_key_policy_permits( const psa_key_policy_t *policy,
845 psa_algorithm_t alg )
846{
Gilles Peskined6f371b2019-05-10 19:33:38 +0200847 return( psa_key_algorithm_permits( policy->alg, alg ) ||
848 psa_key_algorithm_permits( policy->alg2, alg ) );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100849}
850
Gilles Peskinef603c712019-01-19 13:40:11 +0100851/** Restrict a key policy based on a constraint.
852 *
853 * \param[in,out] policy The policy to restrict.
854 * \param[in] constraint The policy constraint to apply.
855 *
856 * \retval #PSA_SUCCESS
857 * \c *policy contains the intersection of the original value of
858 * \c *policy and \c *constraint.
859 * \retval #PSA_ERROR_INVALID_ARGUMENT
860 * \c *policy and \c *constraint are incompatible.
861 * \c *policy is unchanged.
862 */
863static psa_status_t psa_restrict_key_policy(
864 psa_key_policy_t *policy,
865 const psa_key_policy_t *constraint )
866{
867 psa_algorithm_t intersection_alg =
868 psa_key_policy_algorithm_intersection( policy->alg, constraint->alg );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200869 psa_algorithm_t intersection_alg2 =
870 psa_key_policy_algorithm_intersection( policy->alg2, constraint->alg2 );
Gilles Peskinef603c712019-01-19 13:40:11 +0100871 if( intersection_alg == 0 && policy->alg != 0 && constraint->alg != 0 )
872 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskined6f371b2019-05-10 19:33:38 +0200873 if( intersection_alg2 == 0 && policy->alg2 != 0 && constraint->alg2 != 0 )
874 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskinef603c712019-01-19 13:40:11 +0100875 policy->usage &= constraint->usage;
876 policy->alg = intersection_alg;
Gilles Peskined6f371b2019-05-10 19:33:38 +0200877 policy->alg2 = intersection_alg2;
Gilles Peskinef603c712019-01-19 13:40:11 +0100878 return( PSA_SUCCESS );
879}
880
Darryl Green06fd18d2018-07-16 11:21:11 +0100881/** Retrieve a slot which must contain a key. The key must have allow all the
882 * usage flags set in \p usage. If \p alg is nonzero, the key must allow
883 * operations with this algorithm. */
Gilles Peskinec5487a82018-12-03 18:08:14 +0100884static psa_status_t psa_get_key_from_slot( psa_key_handle_t handle,
Gilles Peskine2f060a82018-12-04 17:12:32 +0100885 psa_key_slot_t **p_slot,
Darryl Green06fd18d2018-07-16 11:21:11 +0100886 psa_key_usage_t usage,
887 psa_algorithm_t alg )
888{
889 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +0100890 psa_key_slot_t *slot = NULL;
Darryl Green06fd18d2018-07-16 11:21:11 +0100891
892 *p_slot = NULL;
893
Gilles Peskinec5487a82018-12-03 18:08:14 +0100894 status = psa_get_key_slot( handle, &slot );
Darryl Green06fd18d2018-07-16 11:21:11 +0100895 if( status != PSA_SUCCESS )
896 return( status );
Darryl Green06fd18d2018-07-16 11:21:11 +0100897
898 /* Enforce that usage policy for the key slot contains all the flags
899 * required by the usage parameter. There is one exception: public
900 * keys can always be exported, so we treat public key objects as
901 * if they had the export flag. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200902 if( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100903 usage &= ~PSA_KEY_USAGE_EXPORT;
Gilles Peskine8e338702019-07-30 20:06:31 +0200904 if( ( slot->attr.policy.usage & usage ) != usage )
Darryl Green06fd18d2018-07-16 11:21:11 +0100905 return( PSA_ERROR_NOT_PERMITTED );
Gilles Peskine30f77cd2019-01-14 16:06:39 +0100906
907 /* Enforce that the usage policy permits the requested algortihm. */
Gilles Peskine8e338702019-07-30 20:06:31 +0200908 if( alg != 0 && ! psa_key_policy_permits( &slot->attr.policy, alg ) )
Darryl Green06fd18d2018-07-16 11:21:11 +0100909 return( PSA_ERROR_NOT_PERMITTED );
910
911 *p_slot = slot;
912 return( PSA_SUCCESS );
913}
Darryl Green940d72c2018-07-13 13:18:51 +0100914
Gilles Peskine28f8f302019-07-24 13:30:31 +0200915/** Retrieve a slot which must contain a transparent key.
916 *
917 * A transparent key is a key for which the key material is directly
918 * available, as opposed to a key in a secure element.
919 *
Gilles Peskine60450a42019-07-25 11:32:45 +0200920 * This is a temporary function to use instead of psa_get_key_from_slot()
921 * until secure element support is fully implemented.
Gilles Peskine28f8f302019-07-24 13:30:31 +0200922 */
923#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
924static psa_status_t psa_get_transparent_key( psa_key_handle_t handle,
925 psa_key_slot_t **p_slot,
926 psa_key_usage_t usage,
927 psa_algorithm_t alg )
928{
929 psa_status_t status = psa_get_key_from_slot( handle, p_slot, usage, alg );
930 if( status != PSA_SUCCESS )
931 return( status );
Gilles Peskineadad8132019-07-25 11:31:23 +0200932 if( psa_key_slot_is_external( *p_slot ) )
Gilles Peskine28f8f302019-07-24 13:30:31 +0200933 {
934 *p_slot = NULL;
935 return( PSA_ERROR_NOT_SUPPORTED );
936 }
937 return( PSA_SUCCESS );
938}
939#else /* MBEDTLS_PSA_CRYPTO_SE_C */
940/* With no secure element support, all keys are transparent. */
941#define psa_get_transparent_key( handle, p_slot, usage, alg ) \
942 psa_get_key_from_slot( handle, p_slot, usage, alg )
943#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
944
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100945/** Wipe key data from a slot. Preserve metadata such as the policy. */
Gilles Peskine2f060a82018-12-04 17:12:32 +0100946static psa_status_t psa_remove_key_data_from_memory( psa_key_slot_t *slot )
Darryl Green40225ba2018-11-15 14:48:15 +0000947{
Gilles Peskine73167e12019-07-12 23:44:37 +0200948#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
949 if( psa_key_slot_is_external( slot ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000950 {
951 /* No key material to clean. */
952 }
Gilles Peskine73167e12019-07-12 23:44:37 +0200953 else
954#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine8e338702019-07-30 20:06:31 +0200955 if( slot->attr.type == PSA_KEY_TYPE_NONE )
Darryl Green40225ba2018-11-15 14:48:15 +0000956 {
957 /* No key material to clean. */
958 }
Gilles Peskine8e338702019-07-30 20:06:31 +0200959 else if( key_type_is_raw_bytes( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000960 {
961 mbedtls_free( slot->data.raw.data );
962 }
963 else
964#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200965 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000966 {
967 mbedtls_rsa_free( slot->data.rsa );
968 mbedtls_free( slot->data.rsa );
969 }
970 else
971#endif /* defined(MBEDTLS_RSA_C) */
972#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +0200973 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Darryl Green40225ba2018-11-15 14:48:15 +0000974 {
975 mbedtls_ecp_keypair_free( slot->data.ecp );
976 mbedtls_free( slot->data.ecp );
977 }
978 else
979#endif /* defined(MBEDTLS_ECP_C) */
980 {
981 /* Shouldn't happen: the key type is not any type that we
982 * put in. */
Gilles Peskine4b3eb692019-05-16 21:35:18 +0200983 return( PSA_ERROR_CORRUPTION_DETECTED );
Darryl Green40225ba2018-11-15 14:48:15 +0000984 }
985
986 return( PSA_SUCCESS );
987}
988
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100989/** Completely wipe a slot in memory, including its policy.
990 * Persistent storage is not affected. */
Gilles Peskine66fb1262018-12-10 16:29:04 +0100991psa_status_t psa_wipe_key_slot( psa_key_slot_t *slot )
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100992{
993 psa_status_t status = psa_remove_key_data_from_memory( slot );
Gilles Peskine3f7cd622019-08-13 15:01:08 +0200994 /* Multipart operations may still be using the key. This is safe
995 * because all multipart operation objects are independent from
996 * the key slot: if they need to access the key after the setup
997 * phase, they have a copy of the key. Note that this means that
998 * key material can linger until all operations are completed. */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +0100999 /* At this point, key material and other type-specific content has
1000 * been wiped. Clear remaining metadata. We can call memset and not
1001 * zeroize because the metadata is not particularly sensitive. */
1002 memset( slot, 0, sizeof( *slot ) );
1003 return( status );
1004}
1005
Gilles Peskinec5487a82018-12-03 18:08:14 +01001006psa_status_t psa_destroy_key( psa_key_handle_t handle )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001007{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001008 psa_key_slot_t *slot;
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001009 psa_status_t status; /* status of the last operation */
1010 psa_status_t overall_status = PSA_SUCCESS;
Gilles Peskine354f7672019-07-12 23:46:38 +02001011#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1012 psa_se_drv_table_entry_t *driver;
1013#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001014
Gilles Peskine1841cf42019-10-08 15:48:25 +02001015 if( handle == 0 )
1016 return( PSA_SUCCESS );
1017
Gilles Peskinec5487a82018-12-03 18:08:14 +01001018 status = psa_get_key_slot( handle, &slot );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001019 if( status != PSA_SUCCESS )
1020 return( status );
Gilles Peskine354f7672019-07-12 23:46:38 +02001021
1022#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001023 driver = psa_get_se_driver_entry( slot->attr.lifetime );
Gilles Peskine354f7672019-07-12 23:46:38 +02001024 if( driver != NULL )
Gilles Peskinefc762652019-07-22 19:30:34 +02001025 {
Gilles Peskine60450a42019-07-25 11:32:45 +02001026 /* For a key in a secure element, we need to do three things:
1027 * remove the key file in internal storage, destroy the
1028 * key inside the secure element, and update the driver's
1029 * persistent data. Start a transaction that will encompass these
1030 * three actions. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001031 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_DESTROY_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001032 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskinefc762652019-07-22 19:30:34 +02001033 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001034 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskinefc762652019-07-22 19:30:34 +02001035 status = psa_crypto_save_transaction( );
1036 if( status != PSA_SUCCESS )
1037 {
Gilles Peskine66be51c2019-07-25 18:02:52 +02001038 (void) psa_crypto_stop_transaction( );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001039 /* We should still try to destroy the key in the secure
1040 * element and the key metadata in storage. This is especially
1041 * important if the error is that the storage is full.
1042 * But how to do it exactly without risking an inconsistent
1043 * state after a reset?
1044 * https://github.com/ARMmbed/mbed-crypto/issues/215
1045 */
1046 overall_status = status;
1047 goto exit;
Gilles Peskinefc762652019-07-22 19:30:34 +02001048 }
1049
Gilles Peskine354f7672019-07-12 23:46:38 +02001050 status = psa_destroy_se_key( driver, slot->data.se.slot_number );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001051 if( overall_status == PSA_SUCCESS )
1052 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001053 }
Gilles Peskine354f7672019-07-12 23:46:38 +02001054#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1055
Darryl Greend49a4992018-06-18 17:27:26 +01001056#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskinecaec2782019-08-13 15:11:49 +02001057 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Greend49a4992018-06-18 17:27:26 +01001058 {
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001059 status = psa_destroy_persistent_key( slot->attr.id );
1060 if( overall_status == PSA_SUCCESS )
1061 overall_status = status;
1062
Gilles Peskine9ce31c42019-08-13 15:14:20 +02001063 /* TODO: other slots may have a copy of the same key. We should
1064 * invalidate them.
1065 * https://github.com/ARMmbed/mbed-crypto/issues/214
1066 */
Darryl Greend49a4992018-06-18 17:27:26 +01001067 }
1068#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
Gilles Peskine354f7672019-07-12 23:46:38 +02001069
Gilles Peskinefc762652019-07-22 19:30:34 +02001070#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1071 if( driver != NULL )
1072 {
Gilles Peskine725f22a2019-07-25 11:31:48 +02001073 status = psa_save_se_persistent_data( driver );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001074 if( overall_status == PSA_SUCCESS )
1075 overall_status = status;
1076 status = psa_crypto_stop_transaction( );
1077 if( overall_status == PSA_SUCCESS )
1078 overall_status = status;
Gilles Peskinefc762652019-07-22 19:30:34 +02001079 }
1080#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1081
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001082#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1083exit:
1084#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef77ed1f2018-12-03 11:58:46 +01001085 status = psa_wipe_key_slot( slot );
Gilles Peskine4b7f3402019-08-13 15:58:36 +02001086 /* Prioritize CORRUPTION_DETECTED from wiping over a storage error */
1087 if( overall_status == PSA_SUCCESS )
1088 overall_status = status;
1089 return( overall_status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001090}
1091
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001092void psa_reset_key_attributes( psa_key_attributes_t *attributes )
Gilles Peskineb870b182018-07-06 16:02:09 +02001093{
Gilles Peskineb699f072019-04-26 16:06:02 +02001094 mbedtls_free( attributes->domain_parameters );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001095 memset( attributes, 0, sizeof( *attributes ) );
Gilles Peskineb870b182018-07-06 16:02:09 +02001096}
1097
Gilles Peskineb699f072019-04-26 16:06:02 +02001098psa_status_t psa_set_key_domain_parameters( psa_key_attributes_t *attributes,
1099 psa_key_type_t type,
1100 const uint8_t *data,
1101 size_t data_length )
1102{
1103 uint8_t *copy = NULL;
1104
1105 if( data_length != 0 )
1106 {
1107 copy = mbedtls_calloc( 1, data_length );
1108 if( copy == NULL )
1109 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1110 memcpy( copy, data, data_length );
1111 }
1112 /* After this point, this function is guaranteed to succeed, so it
1113 * can start modifying `*attributes`. */
1114
1115 if( attributes->domain_parameters != NULL )
1116 {
1117 mbedtls_free( attributes->domain_parameters );
1118 attributes->domain_parameters = NULL;
1119 attributes->domain_parameters_size = 0;
1120 }
1121
1122 attributes->domain_parameters = copy;
1123 attributes->domain_parameters_size = data_length;
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001124 attributes->core.type = type;
Gilles Peskineb699f072019-04-26 16:06:02 +02001125 return( PSA_SUCCESS );
1126}
1127
1128psa_status_t psa_get_key_domain_parameters(
1129 const psa_key_attributes_t *attributes,
1130 uint8_t *data, size_t data_size, size_t *data_length )
1131{
1132 if( attributes->domain_parameters_size > data_size )
1133 return( PSA_ERROR_BUFFER_TOO_SMALL );
1134 *data_length = attributes->domain_parameters_size;
1135 if( attributes->domain_parameters_size != 0 )
1136 memcpy( data, attributes->domain_parameters,
1137 attributes->domain_parameters_size );
1138 return( PSA_SUCCESS );
1139}
1140
1141#if defined(MBEDTLS_RSA_C)
1142static psa_status_t psa_get_rsa_public_exponent(
1143 const mbedtls_rsa_context *rsa,
1144 psa_key_attributes_t *attributes )
1145{
1146 mbedtls_mpi mpi;
Janos Follath24eed8d2019-11-22 13:21:35 +00001147 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineb699f072019-04-26 16:06:02 +02001148 uint8_t *buffer = NULL;
1149 size_t buflen;
1150 mbedtls_mpi_init( &mpi );
1151
1152 ret = mbedtls_rsa_export( rsa, NULL, NULL, NULL, NULL, &mpi );
1153 if( ret != 0 )
1154 goto exit;
Gilles Peskine772c8b12019-04-26 17:37:21 +02001155 if( mbedtls_mpi_cmp_int( &mpi, 65537 ) == 0 )
1156 {
1157 /* It's the default value, which is reported as an empty string,
1158 * so there's nothing to do. */
1159 goto exit;
1160 }
Gilles Peskineb699f072019-04-26 16:06:02 +02001161
1162 buflen = mbedtls_mpi_size( &mpi );
1163 buffer = mbedtls_calloc( 1, buflen );
1164 if( buffer == NULL )
1165 {
1166 ret = MBEDTLS_ERR_MPI_ALLOC_FAILED;
1167 goto exit;
1168 }
1169 ret = mbedtls_mpi_write_binary( &mpi, buffer, buflen );
1170 if( ret != 0 )
1171 goto exit;
1172 attributes->domain_parameters = buffer;
1173 attributes->domain_parameters_size = buflen;
1174
1175exit:
1176 mbedtls_mpi_free( &mpi );
1177 if( ret != 0 )
1178 mbedtls_free( buffer );
1179 return( mbedtls_to_psa_error( ret ) );
1180}
1181#endif /* MBEDTLS_RSA_C */
1182
Gilles Peskinebfd322f2019-07-23 11:58:03 +02001183/** Retrieve all the publicly-accessible attributes of a key.
1184 */
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001185psa_status_t psa_get_key_attributes( psa_key_handle_t handle,
1186 psa_key_attributes_t *attributes )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001187{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001188 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001189 psa_status_t status;
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001190
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001191 psa_reset_key_attributes( attributes );
1192
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001193 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02001194 if( status != PSA_SUCCESS )
1195 return( status );
Gilles Peskineb870b182018-07-06 16:02:09 +02001196
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001197 attributes->core = slot->attr;
Gilles Peskinec8000c02019-08-02 20:15:51 +02001198 attributes->core.flags &= ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1199 MBEDTLS_PSA_KA_MASK_DUAL_USE );
1200
1201#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1202 if( psa_key_slot_is_external( slot ) )
1203 psa_set_key_slot_number( attributes, slot->data.se.slot_number );
1204#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001205
Gilles Peskine8e338702019-07-30 20:06:31 +02001206 switch( slot->attr.type )
Gilles Peskineb699f072019-04-26 16:06:02 +02001207 {
1208#if defined(MBEDTLS_RSA_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02001209 case PSA_KEY_TYPE_RSA_KEY_PAIR:
Gilles Peskineb699f072019-04-26 16:06:02 +02001210 case PSA_KEY_TYPE_RSA_PUBLIC_KEY:
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001211#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001212 /* TODO: reporting the public exponent for opaque keys
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001213 * is not yet implemented.
1214 * https://github.com/ARMmbed/mbed-crypto/issues/216
1215 */
Gilles Peskinec8000c02019-08-02 20:15:51 +02001216 if( psa_key_slot_is_external( slot ) )
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001217 break;
1218#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001219 status = psa_get_rsa_public_exponent( slot->data.rsa, attributes );
1220 break;
Gilles Peskinedc5bfe92019-07-24 19:09:30 +02001221#endif /* MBEDTLS_RSA_C */
Gilles Peskineb699f072019-04-26 16:06:02 +02001222 default:
1223 /* Nothing else to do. */
1224 break;
1225 }
1226
1227 if( status != PSA_SUCCESS )
1228 psa_reset_key_attributes( attributes );
1229 return( status );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001230}
1231
Gilles Peskinec8000c02019-08-02 20:15:51 +02001232#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1233psa_status_t psa_get_key_slot_number(
1234 const psa_key_attributes_t *attributes,
1235 psa_key_slot_number_t *slot_number )
1236{
1237 if( attributes->core.flags & MBEDTLS_PSA_KA_FLAG_HAS_SLOT_NUMBER )
1238 {
1239 *slot_number = attributes->slot_number;
1240 return( PSA_SUCCESS );
1241 }
1242 else
1243 return( PSA_ERROR_INVALID_ARGUMENT );
1244}
1245#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1246
Jaeden Ameroccdce902019-01-10 11:42:27 +00001247#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C)
Jaeden Amero25384a22019-01-10 10:23:21 +00001248static int pk_write_pubkey_simple( mbedtls_pk_context *key,
1249 unsigned char *buf, size_t size )
1250{
Janos Follath24eed8d2019-11-22 13:21:35 +00001251 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero25384a22019-01-10 10:23:21 +00001252 unsigned char *c;
1253 size_t len = 0;
1254
1255 c = buf + size;
1256
1257 MBEDTLS_ASN1_CHK_ADD( len, mbedtls_pk_write_pubkey( &c, buf, key ) );
1258
1259 return( (int) len );
1260}
Jaeden Ameroccdce902019-01-10 11:42:27 +00001261#endif /* defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECP_C) */
Jaeden Amero25384a22019-01-10 10:23:21 +00001262
Gilles Peskinef603c712019-01-19 13:40:11 +01001263static psa_status_t psa_internal_export_key( const psa_key_slot_t *slot,
1264 uint8_t *data,
1265 size_t data_size,
1266 size_t *data_length,
1267 int export_public_key )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001268{
Gilles Peskine5d309672019-07-12 23:47:28 +02001269#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1270 const psa_drv_se_t *drv;
1271 psa_drv_se_context_t *drv_context;
1272#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1273
Jaeden Amerof24c7f82018-06-27 17:20:43 +01001274 *data_length = 0;
1275
Gilles Peskine8e338702019-07-30 20:06:31 +02001276 if( export_public_key && ! PSA_KEY_TYPE_IS_ASYMMETRIC( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001277 return( PSA_ERROR_INVALID_ARGUMENT );
mohammad160306e79202018-03-28 13:17:44 +03001278
Gilles Peskinef9168942019-09-12 19:20:29 +02001279 /* Reject a zero-length output buffer now, since this can never be a
1280 * valid key representation. This way we know that data must be a valid
1281 * pointer and we can do things like memset(data, ..., data_size). */
1282 if( data_size == 0 )
1283 return( PSA_ERROR_BUFFER_TOO_SMALL );
1284
Gilles Peskine5d309672019-07-12 23:47:28 +02001285#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001286 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
Gilles Peskine5d309672019-07-12 23:47:28 +02001287 {
1288 psa_drv_se_export_key_t method;
1289 if( drv->key_management == NULL )
1290 return( PSA_ERROR_NOT_SUPPORTED );
1291 method = ( export_public_key ?
1292 drv->key_management->p_export_public :
1293 drv->key_management->p_export );
1294 if( method == NULL )
1295 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine2e0f3882019-07-25 11:34:33 +02001296 return( method( drv_context,
1297 slot->data.se.slot_number,
1298 data, data_size, data_length ) );
Gilles Peskine5d309672019-07-12 23:47:28 +02001299 }
1300#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1301
Gilles Peskine8e338702019-07-30 20:06:31 +02001302 if( key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001303 {
1304 if( slot->data.raw.bytes > data_size )
1305 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinef9168942019-09-12 19:20:29 +02001306 memcpy( data, slot->data.raw.data, slot->data.raw.bytes );
1307 memset( data + slot->data.raw.bytes, 0,
1308 data_size - slot->data.raw.bytes );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001309 *data_length = slot->data.raw.bytes;
1310 return( PSA_SUCCESS );
1311 }
Gilles Peskine188c71e2018-10-29 19:26:02 +01001312#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001313 if( PSA_KEY_TYPE_IS_ECC_KEY_PAIR( slot->attr.type ) && !export_public_key )
Gilles Peskine188c71e2018-10-29 19:26:02 +01001314 {
Darryl Greendd8fb772018-11-07 16:00:44 +00001315 psa_status_t status;
1316
Gilles Peskineb46bef22019-07-30 21:32:04 +02001317 size_t bytes = PSA_BITS_TO_BYTES( slot->attr.bits );
Gilles Peskine188c71e2018-10-29 19:26:02 +01001318 if( bytes > data_size )
1319 return( PSA_ERROR_BUFFER_TOO_SMALL );
1320 status = mbedtls_to_psa_error(
1321 mbedtls_mpi_write_binary( &slot->data.ecp->d, data, bytes ) );
1322 if( status != PSA_SUCCESS )
1323 return( status );
1324 memset( data + bytes, 0, data_size - bytes );
1325 *data_length = bytes;
1326 return( PSA_SUCCESS );
1327 }
1328#endif
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001329 else
Moran Peker17e36e12018-05-02 12:55:20 +03001330 {
Gilles Peskine969ac722018-01-28 18:16:59 +01001331#if defined(MBEDTLS_PK_WRITE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001332 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) ||
1333 PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine969ac722018-01-28 18:16:59 +01001334 {
Moran Pekera998bc62018-04-16 18:16:20 +03001335 mbedtls_pk_context pk;
Janos Follath24eed8d2019-11-22 13:21:35 +00001336 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine8e338702019-07-30 20:06:31 +02001337 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Moran Pekera998bc62018-04-16 18:16:20 +03001338 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001339#if defined(MBEDTLS_RSA_C)
1340 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001341 pk.pk_info = &mbedtls_rsa_info;
1342 pk.pk_ctx = slot->data.rsa;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001343#else
1344 return( PSA_ERROR_NOT_SUPPORTED );
1345#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001346 }
1347 else
1348 {
Darryl Green9e2d7a02018-07-24 16:33:30 +01001349#if defined(MBEDTLS_ECP_C)
1350 mbedtls_pk_init( &pk );
Moran Pekera998bc62018-04-16 18:16:20 +03001351 pk.pk_info = &mbedtls_eckey_info;
1352 pk.pk_ctx = slot->data.ecp;
Darryl Green9e2d7a02018-07-24 16:33:30 +01001353#else
1354 return( PSA_ERROR_NOT_SUPPORTED );
1355#endif
Moran Pekera998bc62018-04-16 18:16:20 +03001356 }
Gilles Peskine8e338702019-07-30 20:06:31 +02001357 if( export_public_key || PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) )
Jaeden Amero25384a22019-01-10 10:23:21 +00001358 {
Jaeden Ameroccdce902019-01-10 11:42:27 +00001359 ret = pk_write_pubkey_simple( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001360 }
Moran Peker17e36e12018-05-02 12:55:20 +03001361 else
Jaeden Amero25384a22019-01-10 10:23:21 +00001362 {
Moran Peker17e36e12018-05-02 12:55:20 +03001363 ret = mbedtls_pk_write_key_der( &pk, data, data_size );
Jaeden Amero25384a22019-01-10 10:23:21 +00001364 }
Moran Peker60364322018-04-29 11:34:58 +03001365 if( ret < 0 )
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001366 {
Gilles Peskinef9168942019-09-12 19:20:29 +02001367 memset( data, 0, data_size );
Moran Pekera998bc62018-04-16 18:16:20 +03001368 return( mbedtls_to_psa_error( ret ) );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001369 }
Gilles Peskine0e231582018-06-20 00:11:07 +02001370 /* The mbedtls_pk_xxx functions write to the end of the buffer.
1371 * Move the data to the beginning and erase remaining data
1372 * at the original location. */
1373 if( 2 * (size_t) ret <= data_size )
1374 {
1375 memcpy( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001376 memset( data + data_size - ret, 0, ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001377 }
1378 else if( (size_t) ret < data_size )
1379 {
1380 memmove( data, data + data_size - ret, ret );
Gilles Peskinee66ca3b2018-06-20 00:11:45 +02001381 memset( data + ret, 0, data_size - ret );
Gilles Peskine0e231582018-06-20 00:11:07 +02001382 }
Moran Pekera998bc62018-04-16 18:16:20 +03001383 *data_length = ret;
1384 return( PSA_SUCCESS );
Gilles Peskine969ac722018-01-28 18:16:59 +01001385 }
1386 else
Gilles Peskine6d912132018-03-07 16:41:37 +01001387#endif /* defined(MBEDTLS_PK_WRITE_C) */
Moran Pekera998bc62018-04-16 18:16:20 +03001388 {
1389 /* This shouldn't happen in the reference implementation, but
Gilles Peskine785fd552018-06-04 15:08:56 +02001390 it is valid for a special-purpose implementation to omit
1391 support for exporting certain key types. */
Moran Pekera998bc62018-04-16 18:16:20 +03001392 return( PSA_ERROR_NOT_SUPPORTED );
1393 }
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001394 }
1395}
1396
Gilles Peskinec5487a82018-12-03 18:08:14 +01001397psa_status_t psa_export_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001398 uint8_t *data,
1399 size_t data_size,
1400 size_t *data_length )
Moran Pekera998bc62018-04-16 18:16:20 +03001401{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001402 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001403 psa_status_t status;
1404
1405 /* Set the key to empty now, so that even when there are errors, we always
1406 * set data_length to a value between 0 and data_size. On error, setting
1407 * the key to empty is a good choice because an empty key representation is
1408 * unlikely to be accepted anywhere. */
1409 *data_length = 0;
1410
1411 /* Export requires the EXPORT flag. There is an exception for public keys,
1412 * which don't require any flag, but psa_get_key_from_slot takes
1413 * care of this. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001414 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_EXPORT, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001415 if( status != PSA_SUCCESS )
1416 return( status );
1417 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001418 data_length, 0 ) );
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001419}
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01001420
Gilles Peskinec5487a82018-12-03 18:08:14 +01001421psa_status_t psa_export_public_key( psa_key_handle_t handle,
Gilles Peskine2d277862018-06-18 15:41:12 +02001422 uint8_t *data,
1423 size_t data_size,
1424 size_t *data_length )
Moran Pekerdd4ea382018-04-03 15:30:03 +03001425{
Gilles Peskine2f060a82018-12-04 17:12:32 +01001426 psa_key_slot_t *slot;
Darryl Greendd8fb772018-11-07 16:00:44 +00001427 psa_status_t status;
1428
1429 /* Set the key to empty now, so that even when there are errors, we always
1430 * set data_length to a value between 0 and data_size. On error, setting
1431 * the key to empty is a good choice because an empty key representation is
1432 * unlikely to be accepted anywhere. */
1433 *data_length = 0;
1434
1435 /* Exporting a public key doesn't require a usage flag. */
Gilles Peskinec5487a82018-12-03 18:08:14 +01001436 status = psa_get_key_from_slot( handle, &slot, 0, 0 );
Darryl Greendd8fb772018-11-07 16:00:44 +00001437 if( status != PSA_SUCCESS )
1438 return( status );
1439 return( psa_internal_export_key( slot, data, data_size,
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02001440 data_length, 1 ) );
Moran Pekerdd4ea382018-04-03 15:30:03 +03001441}
1442
Gilles Peskine91e8c332019-08-02 19:19:39 +02001443#if defined(static_assert)
1444static_assert( ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
1445 "One or more key attribute flag is listed as both external-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001446static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_DUAL_USE ) == 0,
Gilles Peskine094dac12019-08-07 18:19:46 +02001447 "One or more key attribute flag is listed as both internal-only and dual-use" );
Gilles Peskine5a680562019-08-05 17:32:13 +02001448static_assert( ( PSA_KA_MASK_INTERNAL_ONLY & MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY ) == 0,
Gilles Peskine91e8c332019-08-02 19:19:39 +02001449 "One or more key attribute flag is listed as both internal-only and external-only" );
1450#endif
1451
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001452/** Validate that a key policy is internally well-formed.
1453 *
1454 * This function only rejects invalid policies. It does not validate the
1455 * consistency of the policy with respect to other attributes of the key
1456 * such as the key type.
1457 */
1458static psa_status_t psa_validate_key_policy( const psa_key_policy_t *policy )
Gilles Peskine4747d192019-04-17 15:05:45 +02001459{
1460 if( ( policy->usage & ~( PSA_KEY_USAGE_EXPORT |
Gilles Peskine8e0206a2019-05-14 14:24:28 +02001461 PSA_KEY_USAGE_COPY |
Gilles Peskine4747d192019-04-17 15:05:45 +02001462 PSA_KEY_USAGE_ENCRYPT |
1463 PSA_KEY_USAGE_DECRYPT |
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01001464 PSA_KEY_USAGE_SIGN_HASH |
1465 PSA_KEY_USAGE_VERIFY_HASH |
Gilles Peskine4747d192019-04-17 15:05:45 +02001466 PSA_KEY_USAGE_DERIVE ) ) != 0 )
1467 return( PSA_ERROR_INVALID_ARGUMENT );
1468
Gilles Peskine4747d192019-04-17 15:05:45 +02001469 return( PSA_SUCCESS );
1470}
1471
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001472/** Validate the internal consistency of key attributes.
1473 *
1474 * This function only rejects invalid attribute values. If does not
1475 * validate the consistency of the attributes with any key data that may
1476 * be involved in the creation of the key.
1477 *
1478 * Call this function early in the key creation process.
1479 *
1480 * \param[in] attributes Key attributes for the new key.
1481 * \param[out] p_drv On any return, the driver for the key, if any.
1482 * NULL for a transparent key.
1483 *
1484 */
1485static psa_status_t psa_validate_key_attributes(
1486 const psa_key_attributes_t *attributes,
1487 psa_se_drv_table_entry_t **p_drv )
Darryl Green0c6575a2018-11-07 16:05:30 +00001488{
1489 psa_status_t status;
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001490
1491 if( attributes->core.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Darryl Green0c6575a2018-11-07 16:05:30 +00001492 {
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001493 status = psa_validate_persistent_key_parameters(
1494 attributes->core.lifetime, attributes->core.id,
1495 p_drv, 1 );
1496 if( status != PSA_SUCCESS )
1497 return( status );
Darryl Green0c6575a2018-11-07 16:05:30 +00001498 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001499
1500 status = psa_validate_key_policy( &attributes->core.policy );
Darryl Green0c6575a2018-11-07 16:05:30 +00001501 if( status != PSA_SUCCESS )
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001502 return( status );
1503
1504 /* Refuse to create overly large keys.
1505 * Note that this doesn't trigger on import if the attributes don't
1506 * explicitly specify a size (so psa_get_key_bits returns 0), so
1507 * psa_import_key() needs its own checks. */
1508 if( psa_get_key_bits( attributes ) > PSA_MAX_KEY_BITS )
1509 return( PSA_ERROR_NOT_SUPPORTED );
1510
Gilles Peskine91e8c332019-08-02 19:19:39 +02001511 /* Reject invalid flags. These should not be reachable through the API. */
1512 if( attributes->core.flags & ~ ( MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY |
1513 MBEDTLS_PSA_KA_MASK_DUAL_USE ) )
1514 return( PSA_ERROR_INVALID_ARGUMENT );
1515
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001516 return( PSA_SUCCESS );
1517}
1518
Gilles Peskine4747d192019-04-17 15:05:45 +02001519/** Prepare a key slot to receive key material.
1520 *
1521 * This function allocates a key slot and sets its metadata.
1522 *
1523 * If this function fails, call psa_fail_key_creation().
1524 *
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001525 * This function is intended to be used as follows:
1526 * -# Call psa_start_key_creation() to allocate a key slot, prepare
1527 * it with the specified attributes, and assign it a handle.
1528 * -# Populate the slot with the key material.
1529 * -# Call psa_finish_key_creation() to finalize the creation of the slot.
1530 * In case of failure at any step, stop the sequence and call
1531 * psa_fail_key_creation().
1532 *
Gilles Peskinedf179142019-07-15 22:02:14 +02001533 * \param method An identification of the calling function.
Gilles Peskine011e4282019-06-26 18:34:38 +02001534 * \param[in] attributes Key attributes for the new key.
1535 * \param[out] handle On success, a handle for the allocated slot.
1536 * \param[out] p_slot On success, a pointer to the prepared slot.
1537 * \param[out] p_drv On any return, the driver for the key, if any.
1538 * NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001539 *
1540 * \retval #PSA_SUCCESS
1541 * The key slot is ready to receive key material.
1542 * \return If this function fails, the key slot is an invalid state.
1543 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001544 */
1545static psa_status_t psa_start_key_creation(
Gilles Peskinedf179142019-07-15 22:02:14 +02001546 psa_key_creation_method_t method,
Gilles Peskine4747d192019-04-17 15:05:45 +02001547 const psa_key_attributes_t *attributes,
1548 psa_key_handle_t *handle,
Gilles Peskine011e4282019-06-26 18:34:38 +02001549 psa_key_slot_t **p_slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001550 psa_se_drv_table_entry_t **p_drv )
Gilles Peskine4747d192019-04-17 15:05:45 +02001551{
1552 psa_status_t status;
1553 psa_key_slot_t *slot;
1554
Gilles Peskinedf179142019-07-15 22:02:14 +02001555 (void) method;
Gilles Peskine011e4282019-06-26 18:34:38 +02001556 *p_drv = NULL;
1557
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001558 status = psa_validate_key_attributes( attributes, p_drv );
1559 if( status != PSA_SUCCESS )
1560 return( status );
1561
Gilles Peskineedbed562019-08-07 18:19:59 +02001562 status = psa_get_empty_key_slot( handle, p_slot );
Gilles Peskine4747d192019-04-17 15:05:45 +02001563 if( status != PSA_SUCCESS )
1564 return( status );
1565 slot = *p_slot;
1566
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001567 /* We're storing the declared bit-size of the key. It's up to each
1568 * creation mechanism to verify that this information is correct.
1569 * It's automatically correct for mechanisms that use the bit-size as
Gilles Peskineb46bef22019-07-30 21:32:04 +02001570 * an input (generate, device) but not for those where the bit-size
1571 * is optional (import, copy). */
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001572
1573 slot->attr = attributes->core;
Gilles Peskinec744d992019-07-30 17:26:54 +02001574
Gilles Peskine91e8c332019-08-02 19:19:39 +02001575 /* Erase external-only flags from the internal copy. To access
Gilles Peskine013f5472019-08-07 15:42:14 +02001576 * external-only flags, query `attributes`. Thanks to the check
1577 * in psa_validate_key_attributes(), this leaves the dual-use
Gilles Peskineedbed562019-08-07 18:19:59 +02001578 * flags and any internal flag that psa_get_empty_key_slot()
Gilles Peskine013f5472019-08-07 15:42:14 +02001579 * may have set. */
1580 slot->attr.flags &= ~MBEDTLS_PSA_KA_MASK_EXTERNAL_ONLY;
Gilles Peskine91e8c332019-08-02 19:19:39 +02001581
Gilles Peskinecbaff462019-07-12 23:46:04 +02001582#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001583 /* For a key in a secure element, we need to do three things
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001584 * when creating or registering a key:
Gilles Peskine60450a42019-07-25 11:32:45 +02001585 * create the key file in internal storage, create the
1586 * key inside the secure element, and update the driver's
1587 * persistent data. Start a transaction that will encompass these
1588 * three actions. */
1589 /* The first thing to do is to find a slot number for the new key.
1590 * We save the slot number in persistent storage as part of the
1591 * transaction data. It will be needed to recover if the power
1592 * fails during the key creation process, to clean up on the secure
1593 * element side after restarting. Obtaining a slot number from the
1594 * secure element driver updates its persistent state, but we do not yet
1595 * save the driver's persistent state, so that if the power fails,
Gilles Peskinefc762652019-07-22 19:30:34 +02001596 * we can roll back to a state where the key doesn't exist. */
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001597 if( *p_drv != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00001598 {
Gilles Peskinee88c2c12019-08-05 16:44:14 +02001599 status = psa_find_se_slot_for_key( attributes, method, *p_drv,
Gilles Peskinecbaff462019-07-12 23:46:04 +02001600 &slot->data.se.slot_number );
1601 if( status != PSA_SUCCESS )
1602 return( status );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001603 psa_crypto_prepare_transaction( PSA_CRYPTO_TRANSACTION_CREATE_KEY );
Gilles Peskine8e338702019-07-30 20:06:31 +02001604 psa_crypto_transaction.key.lifetime = slot->attr.lifetime;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001605 psa_crypto_transaction.key.slot = slot->data.se.slot_number;
Gilles Peskine8e338702019-07-30 20:06:31 +02001606 psa_crypto_transaction.key.id = slot->attr.id;
Gilles Peskine4aea1032019-07-25 17:38:34 +02001607 status = psa_crypto_save_transaction( );
1608 if( status != PSA_SUCCESS )
Gilles Peskine66be51c2019-07-25 18:02:52 +02001609 {
1610 (void) psa_crypto_stop_transaction( );
Gilles Peskine4aea1032019-07-25 17:38:34 +02001611 return( status );
Gilles Peskine66be51c2019-07-25 18:02:52 +02001612 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001613 }
Gilles Peskine3efcebb2019-10-01 14:18:35 +02001614
1615 if( *p_drv == NULL && method == PSA_KEY_CREATION_REGISTER )
1616 {
1617 /* Key registration only makes sense with a secure element. */
1618 return( PSA_ERROR_INVALID_ARGUMENT );
1619 }
Gilles Peskinecbaff462019-07-12 23:46:04 +02001620#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1621
Darryl Green0c6575a2018-11-07 16:05:30 +00001622 return( status );
1623}
Gilles Peskine4747d192019-04-17 15:05:45 +02001624
1625/** Finalize the creation of a key once its key material has been set.
1626 *
1627 * This entails writing the key to persistent storage.
1628 *
1629 * If this function fails, call psa_fail_key_creation().
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001630 * See the documentation of psa_start_key_creation() for the intended use
1631 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001632 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001633 * \param[in,out] slot Pointer to the slot with key material.
1634 * \param[in] driver The secure element driver for the key,
1635 * or NULL for a transparent key.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001636 *
1637 * \retval #PSA_SUCCESS
1638 * The key was successfully created. The handle is now valid.
1639 * \return If this function fails, the key slot is an invalid state.
1640 * You must call psa_fail_key_creation() to wipe and free the slot.
Gilles Peskine4747d192019-04-17 15:05:45 +02001641 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001642static psa_status_t psa_finish_key_creation(
1643 psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001644 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001645{
1646 psa_status_t status = PSA_SUCCESS;
Gilles Peskine30afafd2019-04-25 13:47:40 +02001647 (void) slot;
Gilles Peskine011e4282019-06-26 18:34:38 +02001648 (void) driver;
Gilles Peskine4747d192019-04-17 15:05:45 +02001649
1650#if defined(MBEDTLS_PSA_CRYPTO_STORAGE_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001651 if( slot->attr.lifetime != PSA_KEY_LIFETIME_VOLATILE )
Gilles Peskine4747d192019-04-17 15:05:45 +02001652 {
Gilles Peskine1df83d42019-07-23 16:13:14 +02001653#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1654 if( driver != NULL )
1655 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001656 psa_se_key_data_storage_t data;
1657#if defined(static_assert)
1658 static_assert( sizeof( slot->data.se.slot_number ) ==
1659 sizeof( data.slot_number ),
1660 "Slot number size does not match psa_se_key_data_storage_t" );
1661 static_assert( sizeof( slot->attr.bits ) == sizeof( data.bits ),
1662 "Bit-size size does not match psa_se_key_data_storage_t" );
1663#endif
1664 memcpy( &data.slot_number, &slot->data.se.slot_number,
1665 sizeof( slot->data.se.slot_number ) );
1666 memcpy( &data.bits, &slot->attr.bits,
1667 sizeof( slot->attr.bits ) );
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001668 status = psa_save_persistent_key( &slot->attr,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001669 (uint8_t*) &data,
1670 sizeof( data ) );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001671 }
1672 else
1673#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1674 {
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001675 size_t buffer_size =
Gilles Peskine8e338702019-07-30 20:06:31 +02001676 PSA_KEY_EXPORT_MAX_SIZE( slot->attr.type,
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001677 slot->attr.bits );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001678 uint8_t *buffer = mbedtls_calloc( 1, buffer_size );
1679 size_t length = 0;
Gilles Peskinef9168942019-09-12 19:20:29 +02001680 if( buffer == NULL )
Gilles Peskine1df83d42019-07-23 16:13:14 +02001681 return( PSA_ERROR_INSUFFICIENT_MEMORY );
1682 status = psa_internal_export_key( slot,
1683 buffer, buffer_size, &length,
1684 0 );
Gilles Peskinee60d1d02019-07-24 20:27:59 +02001685 if( status == PSA_SUCCESS )
Gilles Peskine76aa09c2019-07-31 14:15:34 +02001686 status = psa_save_persistent_key( &slot->attr,
Gilles Peskine4ed0e6f2019-07-30 20:22:33 +02001687 buffer, length );
Gilles Peskine4747d192019-04-17 15:05:45 +02001688
Gilles Peskinef9168942019-09-12 19:20:29 +02001689 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine1df83d42019-07-23 16:13:14 +02001690 mbedtls_free( buffer );
1691 }
Gilles Peskine4747d192019-04-17 15:05:45 +02001692 }
Darryl Green0c6575a2018-11-07 16:05:30 +00001693#endif /* defined(MBEDTLS_PSA_CRYPTO_STORAGE_C) */
1694
Gilles Peskinecbaff462019-07-12 23:46:04 +02001695#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined7729582019-08-05 15:55:54 +02001696 /* Finish the transaction for a key creation. This does not
1697 * happen when registering an existing key. Detect this case
1698 * by checking whether a transaction is in progress (actual
1699 * creation of a key in a secure element requires a transaction,
1700 * but registration doesn't use one). */
1701 if( driver != NULL &&
1702 psa_crypto_transaction.unknown.type == PSA_CRYPTO_TRANSACTION_CREATE_KEY )
Gilles Peskinecbaff462019-07-12 23:46:04 +02001703 {
1704 status = psa_save_se_persistent_data( driver );
1705 if( status != PSA_SUCCESS )
1706 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001707 psa_destroy_persistent_key( slot->attr.id );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001708 return( status );
1709 }
Gilles Peskinefc762652019-07-22 19:30:34 +02001710 status = psa_crypto_stop_transaction( );
1711 if( status != PSA_SUCCESS )
1712 return( status );
Gilles Peskinecbaff462019-07-12 23:46:04 +02001713 }
1714#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1715
Gilles Peskine4747d192019-04-17 15:05:45 +02001716 return( status );
1717}
1718
1719/** Abort the creation of a key.
1720 *
1721 * You may call this function after calling psa_start_key_creation(),
1722 * or after psa_finish_key_creation() fails. In other circumstances, this
1723 * function may not clean up persistent storage.
Gilles Peskine9bc88c62019-04-28 11:37:03 +02001724 * See the documentation of psa_start_key_creation() for the intended use
1725 * of this function.
Gilles Peskine4747d192019-04-17 15:05:45 +02001726 *
Gilles Peskine011e4282019-06-26 18:34:38 +02001727 * \param[in,out] slot Pointer to the slot with key material.
1728 * \param[in] driver The secure element driver for the key,
1729 * or NULL for a transparent key.
Gilles Peskine4747d192019-04-17 15:05:45 +02001730 */
Gilles Peskine011e4282019-06-26 18:34:38 +02001731static void psa_fail_key_creation( psa_key_slot_t *slot,
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001732 psa_se_drv_table_entry_t *driver )
Gilles Peskine4747d192019-04-17 15:05:45 +02001733{
Gilles Peskine011e4282019-06-26 18:34:38 +02001734 (void) driver;
1735
Gilles Peskine4747d192019-04-17 15:05:45 +02001736 if( slot == NULL )
1737 return;
Gilles Peskine011e4282019-06-26 18:34:38 +02001738
1739#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Janos Follath1d57a202019-08-13 12:15:34 +01001740 /* TODO: If the key has already been created in the secure
Gilles Peskine011e4282019-06-26 18:34:38 +02001741 * element, and the failure happened later (when saving metadata
1742 * to internal storage), we need to destroy the key in the secure
Gilles Peskinec9d7f942019-08-13 16:17:16 +02001743 * element.
1744 * https://github.com/ARMmbed/mbed-crypto/issues/217
1745 */
Gilles Peskinefc762652019-07-22 19:30:34 +02001746
Gilles Peskined7729582019-08-05 15:55:54 +02001747 /* Abort the ongoing transaction if any (there may not be one if
1748 * the creation process failed before starting one, or if the
1749 * key creation is a registration of a key in a secure element).
1750 * Earlier functions must already have done what it takes to undo any
1751 * partial creation. All that's left is to update the transaction data
1752 * itself. */
Gilles Peskinefc762652019-07-22 19:30:34 +02001753 (void) psa_crypto_stop_transaction( );
Gilles Peskine011e4282019-06-26 18:34:38 +02001754#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1755
Gilles Peskine4747d192019-04-17 15:05:45 +02001756 psa_wipe_key_slot( slot );
1757}
1758
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001759/** Validate optional attributes during key creation.
1760 *
1761 * Some key attributes are optional during key creation. If they are
1762 * specified in the attributes structure, check that they are consistent
1763 * with the data in the slot.
1764 *
1765 * This function should be called near the end of key creation, after
1766 * the slot in memory is fully populated but before saving persistent data.
1767 */
1768static psa_status_t psa_validate_optional_attributes(
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001769 const psa_key_slot_t *slot,
1770 const psa_key_attributes_t *attributes )
1771{
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001772 if( attributes->core.type != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001773 {
Gilles Peskine8e338702019-07-30 20:06:31 +02001774 if( attributes->core.type != slot->attr.type )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001775 return( PSA_ERROR_INVALID_ARGUMENT );
1776 }
1777
1778 if( attributes->domain_parameters_size != 0 )
1779 {
1780#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02001781 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001782 {
1783 mbedtls_mpi actual, required;
Janos Follath24eed8d2019-11-22 13:21:35 +00001784 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001785 mbedtls_mpi_init( &actual );
1786 mbedtls_mpi_init( &required );
1787 ret = mbedtls_rsa_export( slot->data.rsa,
1788 NULL, NULL, NULL, NULL, &actual );
1789 if( ret != 0 )
1790 goto rsa_exit;
1791 ret = mbedtls_mpi_read_binary( &required,
1792 attributes->domain_parameters,
1793 attributes->domain_parameters_size );
1794 if( ret != 0 )
1795 goto rsa_exit;
1796 if( mbedtls_mpi_cmp_mpi( &actual, &required ) != 0 )
1797 ret = MBEDTLS_ERR_RSA_BAD_INPUT_DATA;
1798 rsa_exit:
1799 mbedtls_mpi_free( &actual );
1800 mbedtls_mpi_free( &required );
1801 if( ret != 0)
1802 return( mbedtls_to_psa_error( ret ) );
1803 }
1804 else
1805#endif
1806 {
1807 return( PSA_ERROR_INVALID_ARGUMENT );
1808 }
1809 }
1810
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001811 if( attributes->core.bits != 0 )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001812 {
Gilles Peskineb46bef22019-07-30 21:32:04 +02001813 if( attributes->core.bits != slot->attr.bits )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001814 return( PSA_ERROR_INVALID_ARGUMENT );
1815 }
1816
1817 return( PSA_SUCCESS );
1818}
1819
Gilles Peskine4747d192019-04-17 15:05:45 +02001820psa_status_t psa_import_key( const psa_key_attributes_t *attributes,
Gilles Peskine4747d192019-04-17 15:05:45 +02001821 const uint8_t *data,
Gilles Peskine73676cb2019-05-15 20:15:10 +02001822 size_t data_length,
1823 psa_key_handle_t *handle )
Gilles Peskine4747d192019-04-17 15:05:45 +02001824{
1825 psa_status_t status;
1826 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001827 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001828
Gilles Peskine0f84d622019-09-12 19:03:13 +02001829 /* Reject zero-length symmetric keys (including raw data key objects).
1830 * This also rejects any key which might be encoded as an empty string,
1831 * which is never valid. */
1832 if( data_length == 0 )
1833 return( PSA_ERROR_INVALID_ARGUMENT );
1834
Gilles Peskinedf179142019-07-15 22:02:14 +02001835 status = psa_start_key_creation( PSA_KEY_CREATION_IMPORT, attributes,
1836 handle, &slot, &driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001837 if( status != PSA_SUCCESS )
1838 goto exit;
1839
Gilles Peskine5d309672019-07-12 23:47:28 +02001840#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1841 if( driver != NULL )
1842 {
1843 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
Darryl Green0892d0f2019-08-20 09:50:14 +01001844 /* The driver should set the number of key bits, however in
1845 * case it doesn't, we initialize bits to an invalid value. */
1846 size_t bits = PSA_MAX_KEY_BITS + 1;
Gilles Peskine5d309672019-07-12 23:47:28 +02001847 if( drv->key_management == NULL ||
1848 drv->key_management->p_import == NULL )
1849 {
1850 status = PSA_ERROR_NOT_SUPPORTED;
1851 goto exit;
1852 }
1853 status = drv->key_management->p_import(
1854 psa_get_se_driver_context( driver ),
Gilles Peskinef3801ff2019-08-06 17:32:04 +02001855 slot->data.se.slot_number, attributes, data, data_length,
Gilles Peskineb46bef22019-07-30 21:32:04 +02001856 &bits );
1857 if( status != PSA_SUCCESS )
1858 goto exit;
1859 if( bits > PSA_MAX_KEY_BITS )
1860 {
1861 status = PSA_ERROR_NOT_SUPPORTED;
1862 goto exit;
1863 }
1864 slot->attr.bits = (psa_key_bits_t) bits;
Gilles Peskine5d309672019-07-12 23:47:28 +02001865 }
1866 else
1867#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1868 {
1869 status = psa_import_key_into_slot( slot, data, data_length );
1870 if( status != PSA_SUCCESS )
1871 goto exit;
Gilles Peskine5d309672019-07-12 23:47:28 +02001872 }
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001873 status = psa_validate_optional_attributes( slot, attributes );
Gilles Peskine18017402019-07-24 20:25:59 +02001874 if( status != PSA_SUCCESS )
1875 goto exit;
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001876
Gilles Peskine011e4282019-06-26 18:34:38 +02001877 status = psa_finish_key_creation( slot, driver );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001878exit:
Gilles Peskine4747d192019-04-17 15:05:45 +02001879 if( status != PSA_SUCCESS )
1880 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001881 psa_fail_key_creation( slot, driver );
Gilles Peskine4747d192019-04-17 15:05:45 +02001882 *handle = 0;
1883 }
1884 return( status );
1885}
1886
Gilles Peskined7729582019-08-05 15:55:54 +02001887#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1888psa_status_t mbedtls_psa_register_se_key(
1889 const psa_key_attributes_t *attributes )
1890{
1891 psa_status_t status;
1892 psa_key_slot_t *slot = NULL;
1893 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskined7729582019-08-05 15:55:54 +02001894 psa_key_handle_t handle = 0;
1895
1896 /* Leaving attributes unspecified is not currently supported.
1897 * It could make sense to query the key type and size from the
1898 * secure element, but not all secure elements support this
1899 * and the driver HAL doesn't currently support it. */
1900 if( psa_get_key_type( attributes ) == PSA_KEY_TYPE_NONE )
1901 return( PSA_ERROR_NOT_SUPPORTED );
1902 if( psa_get_key_bits( attributes ) == 0 )
1903 return( PSA_ERROR_NOT_SUPPORTED );
1904
1905 status = psa_start_key_creation( PSA_KEY_CREATION_REGISTER, attributes,
1906 &handle, &slot, &driver );
1907 if( status != PSA_SUCCESS )
1908 goto exit;
1909
Gilles Peskined7729582019-08-05 15:55:54 +02001910 status = psa_finish_key_creation( slot, driver );
1911
1912exit:
1913 if( status != PSA_SUCCESS )
1914 {
1915 psa_fail_key_creation( slot, driver );
1916 }
1917 /* Registration doesn't keep the key in RAM. */
1918 psa_close_key( handle );
1919 return( status );
1920}
1921#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
1922
Gilles Peskinef603c712019-01-19 13:40:11 +01001923static psa_status_t psa_copy_key_material( const psa_key_slot_t *source,
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001924 psa_key_slot_t *target )
Gilles Peskinef603c712019-01-19 13:40:11 +01001925{
1926 psa_status_t status;
1927 uint8_t *buffer = NULL;
1928 size_t buffer_size = 0;
1929 size_t length;
1930
Gilles Peskine8e338702019-07-30 20:06:31 +02001931 buffer_size = PSA_KEY_EXPORT_MAX_SIZE( source->attr.type,
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02001932 psa_get_key_slot_bits( source ) );
Gilles Peskinef603c712019-01-19 13:40:11 +01001933 buffer = mbedtls_calloc( 1, buffer_size );
Gilles Peskinef9168942019-09-12 19:20:29 +02001934 if( buffer == NULL )
Gilles Peskine122d0022019-01-23 10:55:43 +01001935 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Gilles Peskinef603c712019-01-19 13:40:11 +01001936 status = psa_internal_export_key( source, buffer, buffer_size, &length, 0 );
1937 if( status != PSA_SUCCESS )
1938 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02001939 target->attr.type = source->attr.type;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001940 status = psa_import_key_into_slot( target, buffer, length );
Gilles Peskinef603c712019-01-19 13:40:11 +01001941
1942exit:
Gilles Peskinef9168942019-09-12 19:20:29 +02001943 mbedtls_platform_zeroize( buffer, buffer_size );
Gilles Peskine122d0022019-01-23 10:55:43 +01001944 mbedtls_free( buffer );
Gilles Peskinef603c712019-01-19 13:40:11 +01001945 return( status );
1946}
1947
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001948psa_status_t psa_copy_key( psa_key_handle_t source_handle,
1949 const psa_key_attributes_t *specified_attributes,
1950 psa_key_handle_t *target_handle )
Gilles Peskinef603c712019-01-19 13:40:11 +01001951{
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001952 psa_status_t status;
Gilles Peskinef603c712019-01-19 13:40:11 +01001953 psa_key_slot_t *source_slot = NULL;
1954 psa_key_slot_t *target_slot = NULL;
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001955 psa_key_attributes_t actual_attributes = *specified_attributes;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02001956 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskinef603c712019-01-19 13:40:11 +01001957
Gilles Peskine28f8f302019-07-24 13:30:31 +02001958 status = psa_get_transparent_key( source_handle, &source_slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02001959 PSA_KEY_USAGE_COPY, 0 );
Gilles Peskinef603c712019-01-19 13:40:11 +01001960 if( status != PSA_SUCCESS )
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001961 goto exit;
1962
Gilles Peskine1b8594a2019-07-31 17:21:46 +02001963 status = psa_validate_optional_attributes( source_slot,
1964 specified_attributes );
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001965 if( status != PSA_SUCCESS )
1966 goto exit;
1967
Gilles Peskine7e0cff92019-07-30 13:48:52 +02001968 status = psa_restrict_key_policy( &actual_attributes.core.policy,
Gilles Peskine8e338702019-07-30 20:06:31 +02001969 &source_slot->attr.policy );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001970 if( status != PSA_SUCCESS )
1971 goto exit;
1972
Gilles Peskinedf179142019-07-15 22:02:14 +02001973 status = psa_start_key_creation( PSA_KEY_CREATION_COPY,
1974 &actual_attributes,
Gilles Peskine011e4282019-06-26 18:34:38 +02001975 target_handle, &target_slot, &driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001976 if( status != PSA_SUCCESS )
1977 goto exit;
1978
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001979#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
1980 if( driver != NULL )
Gilles Peskinef603c712019-01-19 13:40:11 +01001981 {
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001982 /* Copying to a secure element is not implemented yet. */
1983 status = PSA_ERROR_NOT_SUPPORTED;
1984 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001985 }
Gilles Peskinef4ee6622019-07-24 13:44:30 +02001986#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinef603c712019-01-19 13:40:11 +01001987
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001988 status = psa_copy_key_material( source_slot, target_slot );
Gilles Peskinef603c712019-01-19 13:40:11 +01001989 if( status != PSA_SUCCESS )
Gilles Peskine4ce2a9d2019-05-03 16:57:15 +02001990 goto exit;
Gilles Peskinef603c712019-01-19 13:40:11 +01001991
Gilles Peskine011e4282019-06-26 18:34:38 +02001992 status = psa_finish_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001993exit:
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001994 if( status != PSA_SUCCESS )
1995 {
Gilles Peskine011e4282019-06-26 18:34:38 +02001996 psa_fail_key_creation( target_slot, driver );
Gilles Peskine8c8f2ab2019-04-18 21:44:46 +02001997 *target_handle = 0;
1998 }
1999 return( status );
Gilles Peskinef603c712019-01-19 13:40:11 +01002000}
2001
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02002002
2003
Gilles Peskine2f9c4dc2018-01-28 13:16:24 +01002004/****************************************************************/
Gilles Peskine20035e32018-02-03 22:44:14 +01002005/* Message digests */
2006/****************************************************************/
2007
Gilles Peskineb16841e2019-10-10 20:36:12 +02002008#if defined(MBEDTLS_RSA_C) || defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002009static const mbedtls_md_info_t *mbedtls_md_info_from_psa( psa_algorithm_t alg )
Gilles Peskine20035e32018-02-03 22:44:14 +01002010{
2011 switch( alg )
2012 {
2013#if defined(MBEDTLS_MD2_C)
2014 case PSA_ALG_MD2:
2015 return( &mbedtls_md2_info );
2016#endif
2017#if defined(MBEDTLS_MD4_C)
2018 case PSA_ALG_MD4:
2019 return( &mbedtls_md4_info );
2020#endif
2021#if defined(MBEDTLS_MD5_C)
2022 case PSA_ALG_MD5:
2023 return( &mbedtls_md5_info );
2024#endif
2025#if defined(MBEDTLS_RIPEMD160_C)
2026 case PSA_ALG_RIPEMD160:
2027 return( &mbedtls_ripemd160_info );
2028#endif
2029#if defined(MBEDTLS_SHA1_C)
2030 case PSA_ALG_SHA_1:
2031 return( &mbedtls_sha1_info );
2032#endif
2033#if defined(MBEDTLS_SHA256_C)
2034 case PSA_ALG_SHA_224:
2035 return( &mbedtls_sha224_info );
2036 case PSA_ALG_SHA_256:
2037 return( &mbedtls_sha256_info );
2038#endif
2039#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002040#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine20035e32018-02-03 22:44:14 +01002041 case PSA_ALG_SHA_384:
2042 return( &mbedtls_sha384_info );
Manuel Pégourié-Gonnardd6020842019-07-17 16:28:21 +02002043#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002044 case PSA_ALG_SHA_512:
2045 return( &mbedtls_sha512_info );
2046#endif
2047 default:
2048 return( NULL );
2049 }
2050}
Gilles Peskineb16841e2019-10-10 20:36:12 +02002051#endif
Gilles Peskine20035e32018-02-03 22:44:14 +01002052
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002053psa_status_t psa_hash_abort( psa_hash_operation_t *operation )
2054{
2055 switch( operation->alg )
2056 {
Gilles Peskine81736312018-06-26 15:04:31 +02002057 case 0:
2058 /* The object has (apparently) been initialized but it is not
2059 * in use. It's ok to call abort on such an object, and there's
2060 * nothing to do. */
2061 break;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002062#if defined(MBEDTLS_MD2_C)
2063 case PSA_ALG_MD2:
2064 mbedtls_md2_free( &operation->ctx.md2 );
2065 break;
2066#endif
2067#if defined(MBEDTLS_MD4_C)
2068 case PSA_ALG_MD4:
2069 mbedtls_md4_free( &operation->ctx.md4 );
2070 break;
2071#endif
2072#if defined(MBEDTLS_MD5_C)
2073 case PSA_ALG_MD5:
2074 mbedtls_md5_free( &operation->ctx.md5 );
2075 break;
2076#endif
2077#if defined(MBEDTLS_RIPEMD160_C)
2078 case PSA_ALG_RIPEMD160:
2079 mbedtls_ripemd160_free( &operation->ctx.ripemd160 );
2080 break;
2081#endif
2082#if defined(MBEDTLS_SHA1_C)
2083 case PSA_ALG_SHA_1:
2084 mbedtls_sha1_free( &operation->ctx.sha1 );
2085 break;
2086#endif
2087#if defined(MBEDTLS_SHA256_C)
2088 case PSA_ALG_SHA_224:
2089 case PSA_ALG_SHA_256:
2090 mbedtls_sha256_free( &operation->ctx.sha256 );
2091 break;
2092#endif
2093#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002094#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002095 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002096#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002097 case PSA_ALG_SHA_512:
2098 mbedtls_sha512_free( &operation->ctx.sha512 );
2099 break;
2100#endif
2101 default:
Gilles Peskinef9c2c092018-06-21 16:57:07 +02002102 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002103 }
2104 operation->alg = 0;
2105 return( PSA_SUCCESS );
2106}
2107
Gilles Peskineda8191d1c2018-07-08 19:46:38 +02002108psa_status_t psa_hash_setup( psa_hash_operation_t *operation,
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002109 psa_algorithm_t alg )
2110{
Janos Follath24eed8d2019-11-22 13:21:35 +00002111 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002112
2113 /* A context must be freshly initialized before it can be set up. */
2114 if( operation->alg != 0 )
2115 {
2116 return( PSA_ERROR_BAD_STATE );
2117 }
2118
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002119 switch( alg )
2120 {
2121#if defined(MBEDTLS_MD2_C)
2122 case PSA_ALG_MD2:
2123 mbedtls_md2_init( &operation->ctx.md2 );
2124 ret = mbedtls_md2_starts_ret( &operation->ctx.md2 );
2125 break;
2126#endif
2127#if defined(MBEDTLS_MD4_C)
2128 case PSA_ALG_MD4:
2129 mbedtls_md4_init( &operation->ctx.md4 );
2130 ret = mbedtls_md4_starts_ret( &operation->ctx.md4 );
2131 break;
2132#endif
2133#if defined(MBEDTLS_MD5_C)
2134 case PSA_ALG_MD5:
2135 mbedtls_md5_init( &operation->ctx.md5 );
2136 ret = mbedtls_md5_starts_ret( &operation->ctx.md5 );
2137 break;
2138#endif
2139#if defined(MBEDTLS_RIPEMD160_C)
2140 case PSA_ALG_RIPEMD160:
2141 mbedtls_ripemd160_init( &operation->ctx.ripemd160 );
2142 ret = mbedtls_ripemd160_starts_ret( &operation->ctx.ripemd160 );
2143 break;
2144#endif
2145#if defined(MBEDTLS_SHA1_C)
2146 case PSA_ALG_SHA_1:
2147 mbedtls_sha1_init( &operation->ctx.sha1 );
2148 ret = mbedtls_sha1_starts_ret( &operation->ctx.sha1 );
2149 break;
2150#endif
2151#if defined(MBEDTLS_SHA256_C)
2152 case PSA_ALG_SHA_224:
2153 mbedtls_sha256_init( &operation->ctx.sha256 );
2154 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 1 );
2155 break;
2156 case PSA_ALG_SHA_256:
2157 mbedtls_sha256_init( &operation->ctx.sha256 );
2158 ret = mbedtls_sha256_starts_ret( &operation->ctx.sha256, 0 );
2159 break;
2160#endif
2161#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002162#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002163 case PSA_ALG_SHA_384:
2164 mbedtls_sha512_init( &operation->ctx.sha512 );
2165 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 1 );
2166 break;
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002167#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002168 case PSA_ALG_SHA_512:
2169 mbedtls_sha512_init( &operation->ctx.sha512 );
2170 ret = mbedtls_sha512_starts_ret( &operation->ctx.sha512, 0 );
2171 break;
2172#endif
2173 default:
Gilles Peskinec06e0712018-06-20 16:21:04 +02002174 return( PSA_ALG_IS_HASH( alg ) ?
2175 PSA_ERROR_NOT_SUPPORTED :
2176 PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002177 }
2178 if( ret == 0 )
2179 operation->alg = alg;
2180 else
2181 psa_hash_abort( operation );
2182 return( mbedtls_to_psa_error( ret ) );
2183}
2184
2185psa_status_t psa_hash_update( psa_hash_operation_t *operation,
2186 const uint8_t *input,
2187 size_t input_length )
2188{
Janos Follath24eed8d2019-11-22 13:21:35 +00002189 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine94e44542018-07-12 16:58:43 +02002190
2191 /* Don't require hash implementations to behave correctly on a
2192 * zero-length input, which may have an invalid pointer. */
2193 if( input_length == 0 )
2194 return( PSA_SUCCESS );
2195
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002196 switch( operation->alg )
2197 {
2198#if defined(MBEDTLS_MD2_C)
2199 case PSA_ALG_MD2:
2200 ret = mbedtls_md2_update_ret( &operation->ctx.md2,
2201 input, input_length );
2202 break;
2203#endif
2204#if defined(MBEDTLS_MD4_C)
2205 case PSA_ALG_MD4:
2206 ret = mbedtls_md4_update_ret( &operation->ctx.md4,
2207 input, input_length );
2208 break;
2209#endif
2210#if defined(MBEDTLS_MD5_C)
2211 case PSA_ALG_MD5:
2212 ret = mbedtls_md5_update_ret( &operation->ctx.md5,
2213 input, input_length );
2214 break;
2215#endif
2216#if defined(MBEDTLS_RIPEMD160_C)
2217 case PSA_ALG_RIPEMD160:
2218 ret = mbedtls_ripemd160_update_ret( &operation->ctx.ripemd160,
2219 input, input_length );
2220 break;
2221#endif
2222#if defined(MBEDTLS_SHA1_C)
2223 case PSA_ALG_SHA_1:
2224 ret = mbedtls_sha1_update_ret( &operation->ctx.sha1,
2225 input, input_length );
2226 break;
2227#endif
2228#if defined(MBEDTLS_SHA256_C)
2229 case PSA_ALG_SHA_224:
2230 case PSA_ALG_SHA_256:
2231 ret = mbedtls_sha256_update_ret( &operation->ctx.sha256,
2232 input, input_length );
2233 break;
2234#endif
2235#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002236#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002237 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002238#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002239 case PSA_ALG_SHA_512:
2240 ret = mbedtls_sha512_update_ret( &operation->ctx.sha512,
2241 input, input_length );
2242 break;
2243#endif
2244 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002245 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002246 }
Gilles Peskine94e44542018-07-12 16:58:43 +02002247
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002248 if( ret != 0 )
2249 psa_hash_abort( operation );
2250 return( mbedtls_to_psa_error( ret ) );
2251}
2252
2253psa_status_t psa_hash_finish( psa_hash_operation_t *operation,
2254 uint8_t *hash,
2255 size_t hash_size,
2256 size_t *hash_length )
2257{
itayzafrir40835d42018-08-02 13:14:17 +03002258 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00002259 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine71bb7b72018-04-19 08:29:59 +02002260 size_t actual_hash_length = PSA_HASH_SIZE( operation->alg );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002261
2262 /* Fill the output buffer with something that isn't a valid hash
2263 * (barring an attack on the hash and deliberately-crafted input),
2264 * in case the caller doesn't check the return status properly. */
Gilles Peskineaee13332018-07-02 12:15:28 +02002265 *hash_length = hash_size;
Gilles Peskine46f1fd72018-06-28 19:31:31 +02002266 /* If hash_size is 0 then hash may be NULL and then the
2267 * call to memset would have undefined behavior. */
2268 if( hash_size != 0 )
2269 memset( hash, '!', hash_size );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002270
2271 if( hash_size < actual_hash_length )
itayzafrir40835d42018-08-02 13:14:17 +03002272 {
2273 status = PSA_ERROR_BUFFER_TOO_SMALL;
2274 goto exit;
2275 }
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002276
2277 switch( operation->alg )
2278 {
2279#if defined(MBEDTLS_MD2_C)
2280 case PSA_ALG_MD2:
2281 ret = mbedtls_md2_finish_ret( &operation->ctx.md2, hash );
2282 break;
2283#endif
2284#if defined(MBEDTLS_MD4_C)
2285 case PSA_ALG_MD4:
2286 ret = mbedtls_md4_finish_ret( &operation->ctx.md4, hash );
2287 break;
2288#endif
2289#if defined(MBEDTLS_MD5_C)
2290 case PSA_ALG_MD5:
2291 ret = mbedtls_md5_finish_ret( &operation->ctx.md5, hash );
2292 break;
2293#endif
2294#if defined(MBEDTLS_RIPEMD160_C)
2295 case PSA_ALG_RIPEMD160:
2296 ret = mbedtls_ripemd160_finish_ret( &operation->ctx.ripemd160, hash );
2297 break;
2298#endif
2299#if defined(MBEDTLS_SHA1_C)
2300 case PSA_ALG_SHA_1:
2301 ret = mbedtls_sha1_finish_ret( &operation->ctx.sha1, hash );
2302 break;
2303#endif
2304#if defined(MBEDTLS_SHA256_C)
2305 case PSA_ALG_SHA_224:
2306 case PSA_ALG_SHA_256:
2307 ret = mbedtls_sha256_finish_ret( &operation->ctx.sha256, hash );
2308 break;
2309#endif
2310#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002311#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002312 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002313#endif
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002314 case PSA_ALG_SHA_512:
2315 ret = mbedtls_sha512_finish_ret( &operation->ctx.sha512, hash );
2316 break;
2317#endif
2318 default:
Jaeden Ameroa0f625a2019-02-15 13:52:25 +00002319 return( PSA_ERROR_BAD_STATE );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002320 }
itayzafrir40835d42018-08-02 13:14:17 +03002321 status = mbedtls_to_psa_error( ret );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002322
itayzafrir40835d42018-08-02 13:14:17 +03002323exit:
2324 if( status == PSA_SUCCESS )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002325 {
Gilles Peskineaee13332018-07-02 12:15:28 +02002326 *hash_length = actual_hash_length;
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002327 return( psa_hash_abort( operation ) );
2328 }
2329 else
2330 {
2331 psa_hash_abort( operation );
itayzafrir40835d42018-08-02 13:14:17 +03002332 return( status );
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002333 }
2334}
2335
Gilles Peskine2d277862018-06-18 15:41:12 +02002336psa_status_t psa_hash_verify( psa_hash_operation_t *operation,
2337 const uint8_t *hash,
2338 size_t hash_length )
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002339{
2340 uint8_t actual_hash[MBEDTLS_MD_MAX_SIZE];
2341 size_t actual_hash_length;
2342 psa_status_t status = psa_hash_finish( operation,
2343 actual_hash, sizeof( actual_hash ),
2344 &actual_hash_length );
2345 if( status != PSA_SUCCESS )
2346 return( status );
2347 if( actual_hash_length != hash_length )
2348 return( PSA_ERROR_INVALID_SIGNATURE );
2349 if( safer_memcmp( hash, actual_hash, actual_hash_length ) != 0 )
2350 return( PSA_ERROR_INVALID_SIGNATURE );
2351 return( PSA_SUCCESS );
2352}
2353
Gilles Peskine0a749c82019-11-28 19:33:58 +01002354psa_status_t psa_hash_compute( psa_algorithm_t alg,
2355 const uint8_t *input, size_t input_length,
2356 uint8_t *hash, size_t hash_size,
2357 size_t *hash_length )
2358{
2359 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2360 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2361
2362 *hash_length = hash_size;
2363 status = psa_hash_setup( &operation, alg );
2364 if( status != PSA_SUCCESS )
2365 goto exit;
2366 status = psa_hash_update( &operation, input, input_length );
2367 if( status != PSA_SUCCESS )
2368 goto exit;
2369 status = psa_hash_finish( &operation, hash, hash_size, hash_length );
2370 if( status != PSA_SUCCESS )
2371 goto exit;
2372
2373exit:
2374 if( status == PSA_SUCCESS )
2375 status = psa_hash_abort( &operation );
2376 else
2377 psa_hash_abort( &operation );
2378 return( status );
2379}
2380
2381psa_status_t psa_hash_compare( psa_algorithm_t alg,
2382 const uint8_t *input, size_t input_length,
2383 const uint8_t *hash, size_t hash_length )
2384{
2385 psa_hash_operation_t operation = PSA_HASH_OPERATION_INIT;
2386 psa_status_t status = PSA_ERROR_CORRUPTION_DETECTED;
2387
2388 status = psa_hash_setup( &operation, alg );
2389 if( status != PSA_SUCCESS )
2390 goto exit;
2391 status = psa_hash_update( &operation, input, input_length );
2392 if( status != PSA_SUCCESS )
2393 goto exit;
2394 status = psa_hash_verify( &operation, hash, hash_length );
2395 if( status != PSA_SUCCESS )
2396 goto exit;
2397
2398exit:
2399 if( status == PSA_SUCCESS )
2400 status = psa_hash_abort( &operation );
2401 else
2402 psa_hash_abort( &operation );
2403 return( status );
2404}
2405
Gilles Peskineeb35d782019-01-22 17:56:16 +01002406psa_status_t psa_hash_clone( const psa_hash_operation_t *source_operation,
2407 psa_hash_operation_t *target_operation )
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002408{
2409 if( target_operation->alg != 0 )
2410 return( PSA_ERROR_BAD_STATE );
2411
2412 switch( source_operation->alg )
2413 {
2414 case 0:
2415 return( PSA_ERROR_BAD_STATE );
2416#if defined(MBEDTLS_MD2_C)
2417 case PSA_ALG_MD2:
2418 mbedtls_md2_clone( &target_operation->ctx.md2,
2419 &source_operation->ctx.md2 );
2420 break;
2421#endif
2422#if defined(MBEDTLS_MD4_C)
2423 case PSA_ALG_MD4:
2424 mbedtls_md4_clone( &target_operation->ctx.md4,
2425 &source_operation->ctx.md4 );
2426 break;
2427#endif
2428#if defined(MBEDTLS_MD5_C)
2429 case PSA_ALG_MD5:
2430 mbedtls_md5_clone( &target_operation->ctx.md5,
2431 &source_operation->ctx.md5 );
2432 break;
2433#endif
2434#if defined(MBEDTLS_RIPEMD160_C)
2435 case PSA_ALG_RIPEMD160:
2436 mbedtls_ripemd160_clone( &target_operation->ctx.ripemd160,
2437 &source_operation->ctx.ripemd160 );
2438 break;
2439#endif
2440#if defined(MBEDTLS_SHA1_C)
2441 case PSA_ALG_SHA_1:
2442 mbedtls_sha1_clone( &target_operation->ctx.sha1,
2443 &source_operation->ctx.sha1 );
2444 break;
2445#endif
2446#if defined(MBEDTLS_SHA256_C)
2447 case PSA_ALG_SHA_224:
2448 case PSA_ALG_SHA_256:
2449 mbedtls_sha256_clone( &target_operation->ctx.sha256,
2450 &source_operation->ctx.sha256 );
2451 break;
2452#endif
2453#if defined(MBEDTLS_SHA512_C)
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002454#if !defined(MBEDTLS_SHA512_NO_SHA384)
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002455 case PSA_ALG_SHA_384:
Manuel Pégourié-Gonnard792b16d2020-01-07 10:13:18 +01002456#endif
Gilles Peskineebb2c3e2019-01-19 12:03:41 +01002457 case PSA_ALG_SHA_512:
2458 mbedtls_sha512_clone( &target_operation->ctx.sha512,
2459 &source_operation->ctx.sha512 );
2460 break;
2461#endif
2462 default:
2463 return( PSA_ERROR_NOT_SUPPORTED );
2464 }
2465
2466 target_operation->alg = source_operation->alg;
2467 return( PSA_SUCCESS );
2468}
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002469
2470
Gilles Peskine9ef733f2018-02-07 21:05:37 +01002471/****************************************************************/
Gilles Peskine8c9def32018-02-08 10:02:12 +01002472/* MAC */
2473/****************************************************************/
2474
Gilles Peskinedc2fc842018-03-07 16:42:59 +01002475static const mbedtls_cipher_info_t *mbedtls_cipher_info_from_psa(
Gilles Peskine8c9def32018-02-08 10:02:12 +01002476 psa_algorithm_t alg,
2477 psa_key_type_t key_type,
Gilles Peskine2d277862018-06-18 15:41:12 +02002478 size_t key_bits,
mohammad1603f4f0d612018-06-03 15:04:51 +03002479 mbedtls_cipher_id_t* cipher_id )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002480{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002481 mbedtls_cipher_mode_t mode;
mohammad1603f4f0d612018-06-03 15:04:51 +03002482 mbedtls_cipher_id_t cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002483
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002484 if( PSA_ALG_IS_AEAD( alg ) )
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002485 alg = PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02002486
Gilles Peskine8c9def32018-02-08 10:02:12 +01002487 if( PSA_ALG_IS_CIPHER( alg ) || PSA_ALG_IS_AEAD( alg ) )
2488 {
Nir Sonnenscheine9664c32018-06-17 14:41:30 +03002489 switch( alg )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002490 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002491 case PSA_ALG_ARC4:
Gilles Peskine26869f22019-05-06 15:25:00 +02002492 case PSA_ALG_CHACHA20:
Gilles Peskine8c9def32018-02-08 10:02:12 +01002493 mode = MBEDTLS_MODE_STREAM;
2494 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002495 case PSA_ALG_CTR:
2496 mode = MBEDTLS_MODE_CTR;
2497 break;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02002498 case PSA_ALG_CFB:
2499 mode = MBEDTLS_MODE_CFB;
2500 break;
2501 case PSA_ALG_OFB:
2502 mode = MBEDTLS_MODE_OFB;
2503 break;
2504 case PSA_ALG_CBC_NO_PADDING:
2505 mode = MBEDTLS_MODE_CBC;
2506 break;
2507 case PSA_ALG_CBC_PKCS7:
2508 mode = MBEDTLS_MODE_CBC;
2509 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002510 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002511 mode = MBEDTLS_MODE_CCM;
2512 break;
Gilles Peskine57fbdb12018-10-17 18:29:17 +02002513 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
Gilles Peskine8c9def32018-02-08 10:02:12 +01002514 mode = MBEDTLS_MODE_GCM;
2515 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002516 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
2517 mode = MBEDTLS_MODE_CHACHAPOLY;
2518 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002519 default:
2520 return( NULL );
2521 }
2522 }
2523 else if( alg == PSA_ALG_CMAC )
2524 mode = MBEDTLS_MODE_ECB;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002525 else
2526 return( NULL );
2527
2528 switch( key_type )
2529 {
2530 case PSA_KEY_TYPE_AES:
mohammad1603f4f0d612018-06-03 15:04:51 +03002531 cipher_id_tmp = MBEDTLS_CIPHER_ID_AES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002532 break;
2533 case PSA_KEY_TYPE_DES:
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002534 /* key_bits is 64 for Single-DES, 128 for two-key Triple-DES,
2535 * and 192 for three-key Triple-DES. */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002536 if( key_bits == 64 )
mohammad1603f4f0d612018-06-03 15:04:51 +03002537 cipher_id_tmp = MBEDTLS_CIPHER_ID_DES;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002538 else
mohammad1603f4f0d612018-06-03 15:04:51 +03002539 cipher_id_tmp = MBEDTLS_CIPHER_ID_3DES;
Gilles Peskine9ad29e22018-06-21 09:40:04 +02002540 /* mbedtls doesn't recognize two-key Triple-DES as an algorithm,
2541 * but two-key Triple-DES is functionally three-key Triple-DES
2542 * with K1=K3, so that's how we present it to mbedtls. */
2543 if( key_bits == 128 )
2544 key_bits = 192;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002545 break;
2546 case PSA_KEY_TYPE_CAMELLIA:
mohammad1603f4f0d612018-06-03 15:04:51 +03002547 cipher_id_tmp = MBEDTLS_CIPHER_ID_CAMELLIA;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002548 break;
2549 case PSA_KEY_TYPE_ARC4:
mohammad1603f4f0d612018-06-03 15:04:51 +03002550 cipher_id_tmp = MBEDTLS_CIPHER_ID_ARC4;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002551 break;
Gilles Peskine26869f22019-05-06 15:25:00 +02002552 case PSA_KEY_TYPE_CHACHA20:
2553 cipher_id_tmp = MBEDTLS_CIPHER_ID_CHACHA20;
2554 break;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002555 default:
2556 return( NULL );
2557 }
mohammad1603f4f0d612018-06-03 15:04:51 +03002558 if( cipher_id != NULL )
mohammad160360a64d02018-06-03 17:20:42 +03002559 *cipher_id = cipher_id_tmp;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002560
Jaeden Amero23bbb752018-06-26 14:16:54 +01002561 return( mbedtls_cipher_info_from_values( cipher_id_tmp,
2562 (int) key_bits, mode ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002563}
2564
Gilles Peskinea05219c2018-11-16 16:02:56 +01002565#if defined(MBEDTLS_MD_C)
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002566static size_t psa_get_hash_block_size( psa_algorithm_t alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002567{
Gilles Peskine2d277862018-06-18 15:41:12 +02002568 switch( alg )
Nir Sonnenschein96272412018-06-17 14:41:10 +03002569 {
2570 case PSA_ALG_MD2:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002571 return( 16 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002572 case PSA_ALG_MD4:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002573 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002574 case PSA_ALG_MD5:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002575 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002576 case PSA_ALG_RIPEMD160:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002577 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002578 case PSA_ALG_SHA_1:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002579 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002580 case PSA_ALG_SHA_224:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002581 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002582 case PSA_ALG_SHA_256:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002583 return( 64 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002584 case PSA_ALG_SHA_384:
Nir Sonnenscheinaa5aea02018-06-18 12:24:33 +03002585 return( 128 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002586 case PSA_ALG_SHA_512:
Gilles Peskine2d277862018-06-18 15:41:12 +02002587 return( 128 );
2588 default:
2589 return( 0 );
Nir Sonnenschein96272412018-06-17 14:41:10 +03002590 }
2591}
Gilles Peskinea05219c2018-11-16 16:02:56 +01002592#endif /* MBEDTLS_MD_C */
Nir Sonnenschein0c9ec532018-06-07 13:27:47 +03002593
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002594/* Initialize the MAC operation structure. Once this function has been
2595 * called, psa_mac_abort can run and will do the right thing. */
2596static psa_status_t psa_mac_init( psa_mac_operation_t *operation,
2597 psa_algorithm_t alg )
2598{
2599 psa_status_t status = PSA_ERROR_NOT_SUPPORTED;
2600
2601 operation->alg = alg;
2602 operation->key_set = 0;
2603 operation->iv_set = 0;
2604 operation->iv_required = 0;
2605 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002606 operation->is_sign = 0;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002607
2608#if defined(MBEDTLS_CMAC_C)
2609 if( alg == PSA_ALG_CMAC )
2610 {
2611 operation->iv_required = 0;
2612 mbedtls_cipher_init( &operation->ctx.cmac );
2613 status = PSA_SUCCESS;
2614 }
2615 else
2616#endif /* MBEDTLS_CMAC_C */
2617#if defined(MBEDTLS_MD_C)
2618 if( PSA_ALG_IS_HMAC( operation->alg ) )
2619 {
Gilles Peskineff94abd2018-07-12 17:07:52 +02002620 /* We'll set up the hash operation later in psa_hmac_setup_internal. */
2621 operation->ctx.hmac.hash_ctx.alg = 0;
2622 status = PSA_SUCCESS;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002623 }
2624 else
2625#endif /* MBEDTLS_MD_C */
2626 {
Gilles Peskinec06e0712018-06-20 16:21:04 +02002627 if( ! PSA_ALG_IS_MAC( alg ) )
2628 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002629 }
2630
2631 if( status != PSA_SUCCESS )
2632 memset( operation, 0, sizeof( *operation ) );
2633 return( status );
2634}
2635
Gilles Peskine01126fa2018-07-12 17:04:55 +02002636#if defined(MBEDTLS_MD_C)
2637static psa_status_t psa_hmac_abort_internal( psa_hmac_internal_data *hmac )
2638{
Gilles Peskine3f108122018-12-07 18:14:53 +01002639 mbedtls_platform_zeroize( hmac->opad, sizeof( hmac->opad ) );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002640 return( psa_hash_abort( &hmac->hash_ctx ) );
2641}
2642#endif /* MBEDTLS_MD_C */
2643
Gilles Peskine8c9def32018-02-08 10:02:12 +01002644psa_status_t psa_mac_abort( psa_mac_operation_t *operation )
2645{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002646 if( operation->alg == 0 )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002647 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002648 /* The object has (apparently) been initialized but it is not
2649 * in use. It's ok to call abort on such an object, and there's
2650 * nothing to do. */
2651 return( PSA_SUCCESS );
2652 }
2653 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002654#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002655 if( operation->alg == PSA_ALG_CMAC )
2656 {
2657 mbedtls_cipher_free( &operation->ctx.cmac );
2658 }
2659 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002660#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002661#if defined(MBEDTLS_MD_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002662 if( PSA_ALG_IS_HMAC( operation->alg ) )
2663 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02002664 psa_hmac_abort_internal( &operation->ctx.hmac );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002665 }
2666 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002667#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002668 {
2669 /* Sanity check (shouldn't happen: operation->alg should
2670 * always have been initialized to a valid value). */
2671 goto bad_state;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002672 }
Moran Peker41deec42018-04-04 15:43:05 +03002673
Gilles Peskine8c9def32018-02-08 10:02:12 +01002674 operation->alg = 0;
2675 operation->key_set = 0;
2676 operation->iv_set = 0;
2677 operation->iv_required = 0;
2678 operation->has_input = 0;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002679 operation->is_sign = 0;
Moran Peker41deec42018-04-04 15:43:05 +03002680
Gilles Peskine8c9def32018-02-08 10:02:12 +01002681 return( PSA_SUCCESS );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002682
2683bad_state:
2684 /* If abort is called on an uninitialized object, we can't trust
2685 * anything. Wipe the object in case it contains confidential data.
2686 * This may result in a memory leak if a pointer gets overwritten,
2687 * but it's too late to do anything about this. */
2688 memset( operation, 0, sizeof( *operation ) );
2689 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002690}
2691
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002692#if defined(MBEDTLS_CMAC_C)
Gilles Peskine89167cb2018-07-08 20:12:23 +02002693static int psa_cmac_setup( psa_mac_operation_t *operation,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002694 size_t key_bits,
Gilles Peskine2f060a82018-12-04 17:12:32 +01002695 psa_key_slot_t *slot,
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002696 const mbedtls_cipher_info_t *cipher_info )
2697{
Janos Follath24eed8d2019-11-22 13:21:35 +00002698 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002699
2700 operation->mac_size = cipher_info->block_size;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002701
2702 ret = mbedtls_cipher_setup( &operation->ctx.cmac, cipher_info );
2703 if( ret != 0 )
2704 return( ret );
2705
2706 ret = mbedtls_cipher_cmac_starts( &operation->ctx.cmac,
2707 slot->data.raw.data,
2708 key_bits );
2709 return( ret );
2710}
Gilles Peskinee3b07d82018-06-19 11:57:35 +02002711#endif /* MBEDTLS_CMAC_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002712
Gilles Peskine248051a2018-06-20 16:09:38 +02002713#if defined(MBEDTLS_MD_C)
Gilles Peskine01126fa2018-07-12 17:04:55 +02002714static psa_status_t psa_hmac_setup_internal( psa_hmac_internal_data *hmac,
2715 const uint8_t *key,
2716 size_t key_length,
2717 psa_algorithm_t hash_alg )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002718{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002719 uint8_t ipad[PSA_HMAC_MAX_HASH_BLOCK_SIZE];
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002720 size_t i;
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002721 size_t hash_size = PSA_HASH_SIZE( hash_alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002722 size_t block_size = psa_get_hash_block_size( hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002723 psa_status_t status;
2724
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002725 /* Sanity checks on block_size, to guarantee that there won't be a buffer
2726 * overflow below. This should never trigger if the hash algorithm
2727 * is implemented correctly. */
2728 /* The size checks against the ipad and opad buffers cannot be written
2729 * `block_size > sizeof( ipad ) || block_size > sizeof( hmac->opad )`
2730 * because that triggers -Wlogical-op on GCC 7.3. */
2731 if( block_size > sizeof( ipad ) )
2732 return( PSA_ERROR_NOT_SUPPORTED );
2733 if( block_size > sizeof( hmac->opad ) )
2734 return( PSA_ERROR_NOT_SUPPORTED );
2735 if( block_size < hash_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002736 return( PSA_ERROR_NOT_SUPPORTED );
2737
Gilles Peskined223b522018-06-11 18:12:58 +02002738 if( key_length > block_size )
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002739 {
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002740 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002741 if( status != PSA_SUCCESS )
Gilles Peskine1e6bfdf2018-07-17 16:22:47 +02002742 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002743 status = psa_hash_update( &hmac->hash_ctx, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002744 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002745 goto cleanup;
Gilles Peskine01126fa2018-07-12 17:04:55 +02002746 status = psa_hash_finish( &hmac->hash_ctx,
Gilles Peskined223b522018-06-11 18:12:58 +02002747 ipad, sizeof( ipad ), &key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002748 if( status != PSA_SUCCESS )
Gilles Peskineb8be2882018-07-17 16:24:34 +02002749 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002750 }
Gilles Peskine96889972018-07-12 17:07:03 +02002751 /* A 0-length key is not commonly used in HMAC when used as a MAC,
2752 * but it is permitted. It is common when HMAC is used in HKDF, for
2753 * example. Don't call `memcpy` in the 0-length because `key` could be
2754 * an invalid pointer which would make the behavior undefined. */
2755 else if( key_length != 0 )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002756 memcpy( ipad, key, key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002757
Gilles Peskined223b522018-06-11 18:12:58 +02002758 /* ipad contains the key followed by garbage. Xor and fill with 0x36
2759 * to create the ipad value. */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002760 for( i = 0; i < key_length; i++ )
Gilles Peskined223b522018-06-11 18:12:58 +02002761 ipad[i] ^= 0x36;
2762 memset( ipad + key_length, 0x36, block_size - key_length );
2763
2764 /* Copy the key material from ipad to opad, flipping the requisite bits,
2765 * and filling the rest of opad with the requisite constant. */
2766 for( i = 0; i < key_length; i++ )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002767 hmac->opad[i] = ipad[i] ^ 0x36 ^ 0x5C;
2768 memset( hmac->opad + key_length, 0x5C, block_size - key_length );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002769
Gilles Peskine01126fa2018-07-12 17:04:55 +02002770 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002771 if( status != PSA_SUCCESS )
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002772 goto cleanup;
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002773
Gilles Peskine01126fa2018-07-12 17:04:55 +02002774 status = psa_hash_update( &hmac->hash_ctx, ipad, block_size );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002775
2776cleanup:
Ron Eldor296eca62019-09-10 15:21:37 +03002777 mbedtls_platform_zeroize( ipad, sizeof(ipad) );
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002778
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002779 return( status );
2780}
Gilles Peskine248051a2018-06-20 16:09:38 +02002781#endif /* MBEDTLS_MD_C */
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002782
Gilles Peskine89167cb2018-07-08 20:12:23 +02002783static psa_status_t psa_mac_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002784 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002785 psa_algorithm_t alg,
2786 int is_sign )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002787{
Gilles Peskine8c9def32018-02-08 10:02:12 +01002788 psa_status_t status;
Gilles Peskine2f060a82018-12-04 17:12:32 +01002789 psa_key_slot_t *slot;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002790 size_t key_bits;
Gilles Peskine89167cb2018-07-08 20:12:23 +02002791 psa_key_usage_t usage =
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01002792 is_sign ? PSA_KEY_USAGE_SIGN_HASH : PSA_KEY_USAGE_VERIFY_HASH;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002793 uint8_t truncated = PSA_MAC_TRUNCATED_LENGTH( alg );
Gilles Peskinee0e9c7c2018-10-17 18:28:05 +02002794 psa_algorithm_t full_length_alg = PSA_ALG_FULL_LENGTH_MAC( alg );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002795
Jaeden Amero36ee5d02019-02-19 09:25:10 +00002796 /* A context must be freshly initialized before it can be set up. */
2797 if( operation->alg != 0 )
2798 {
2799 return( PSA_ERROR_BAD_STATE );
2800 }
2801
Gilles Peskined911eb72018-08-14 15:18:45 +02002802 status = psa_mac_init( operation, full_length_alg );
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02002803 if( status != PSA_SUCCESS )
2804 return( status );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002805 if( is_sign )
2806 operation->is_sign = 1;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002807
Gilles Peskine28f8f302019-07-24 13:30:31 +02002808 status = psa_get_transparent_key( handle, &slot, usage, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02002809 if( status != PSA_SUCCESS )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002810 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02002811 key_bits = psa_get_key_slot_bits( slot );
Gilles Peskineab1d7ab2018-07-06 16:07:47 +02002812
Gilles Peskine8c9def32018-02-08 10:02:12 +01002813#if defined(MBEDTLS_CMAC_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002814 if( full_length_alg == PSA_ALG_CMAC )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002815 {
2816 const mbedtls_cipher_info_t *cipher_info =
Gilles Peskined911eb72018-08-14 15:18:45 +02002817 mbedtls_cipher_info_from_psa( full_length_alg,
Gilles Peskine8e338702019-07-30 20:06:31 +02002818 slot->attr.type, key_bits, NULL );
Janos Follath24eed8d2019-11-22 13:21:35 +00002819 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002820 if( cipher_info == NULL )
2821 {
2822 status = PSA_ERROR_NOT_SUPPORTED;
2823 goto exit;
2824 }
2825 operation->mac_size = cipher_info->block_size;
2826 ret = psa_cmac_setup( operation, key_bits, slot, cipher_info );
2827 status = mbedtls_to_psa_error( ret );
2828 }
2829 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002830#endif /* MBEDTLS_CMAC_C */
Gilles Peskine8c9def32018-02-08 10:02:12 +01002831#if defined(MBEDTLS_MD_C)
Gilles Peskined911eb72018-08-14 15:18:45 +02002832 if( PSA_ALG_IS_HMAC( full_length_alg ) )
Gilles Peskinefbfac682018-07-08 20:51:54 +02002833 {
Gilles Peskine00709fa2018-08-22 18:25:41 +02002834 psa_algorithm_t hash_alg = PSA_ALG_HMAC_GET_HASH( alg );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002835 if( hash_alg == 0 )
2836 {
2837 status = PSA_ERROR_NOT_SUPPORTED;
2838 goto exit;
2839 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002840
2841 operation->mac_size = PSA_HASH_SIZE( hash_alg );
2842 /* Sanity check. This shouldn't fail on a valid configuration. */
2843 if( operation->mac_size == 0 ||
2844 operation->mac_size > sizeof( operation->ctx.hmac.opad ) )
2845 {
2846 status = PSA_ERROR_NOT_SUPPORTED;
2847 goto exit;
2848 }
2849
Gilles Peskine8e338702019-07-30 20:06:31 +02002850 if( slot->attr.type != PSA_KEY_TYPE_HMAC )
Gilles Peskine01126fa2018-07-12 17:04:55 +02002851 {
2852 status = PSA_ERROR_INVALID_ARGUMENT;
2853 goto exit;
2854 }
Gilles Peskine9aa369e2018-07-16 00:36:29 +02002855
Gilles Peskine01126fa2018-07-12 17:04:55 +02002856 status = psa_hmac_setup_internal( &operation->ctx.hmac,
2857 slot->data.raw.data,
2858 slot->data.raw.bytes,
2859 hash_alg );
Gilles Peskinefbfac682018-07-08 20:51:54 +02002860 }
2861 else
Gilles Peskine8c9def32018-02-08 10:02:12 +01002862#endif /* MBEDTLS_MD_C */
Gilles Peskinefbfac682018-07-08 20:51:54 +02002863 {
Jaeden Amero82df32e2018-11-23 15:11:20 +00002864 (void) key_bits;
Gilles Peskinefbfac682018-07-08 20:51:54 +02002865 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002866 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002867
Gilles Peskined911eb72018-08-14 15:18:45 +02002868 if( truncated == 0 )
2869 {
2870 /* The "normal" case: untruncated algorithm. Nothing to do. */
2871 }
2872 else if( truncated < 4 )
2873 {
Gilles Peskine6d72ff92018-08-21 14:55:08 +02002874 /* A very short MAC is too short for security since it can be
2875 * brute-forced. Ancient protocols with 32-bit MACs do exist,
2876 * so we make this our minimum, even though 32 bits is still
2877 * too small for security. */
Gilles Peskined911eb72018-08-14 15:18:45 +02002878 status = PSA_ERROR_NOT_SUPPORTED;
2879 }
2880 else if( truncated > operation->mac_size )
2881 {
2882 /* It's impossible to "truncate" to a larger length. */
2883 status = PSA_ERROR_INVALID_ARGUMENT;
2884 }
2885 else
2886 operation->mac_size = truncated;
2887
Gilles Peskinefbfac682018-07-08 20:51:54 +02002888exit:
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002889 if( status != PSA_SUCCESS )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002890 {
Gilles Peskine6a0a44e2018-06-11 17:42:48 +02002891 psa_mac_abort( operation );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002892 }
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002893 else
2894 {
Gilles Peskine7e454bc2018-06-11 17:26:17 +02002895 operation->key_set = 1;
2896 }
2897 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002898}
2899
Gilles Peskine89167cb2018-07-08 20:12:23 +02002900psa_status_t psa_mac_sign_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002901 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002902 psa_algorithm_t alg )
2903{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002904 return( psa_mac_setup( operation, handle, alg, 1 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002905}
2906
2907psa_status_t psa_mac_verify_setup( psa_mac_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01002908 psa_key_handle_t handle,
Gilles Peskine89167cb2018-07-08 20:12:23 +02002909 psa_algorithm_t alg )
2910{
Gilles Peskinec5487a82018-12-03 18:08:14 +01002911 return( psa_mac_setup( operation, handle, alg, 0 ) );
Gilles Peskine89167cb2018-07-08 20:12:23 +02002912}
2913
Gilles Peskine8c9def32018-02-08 10:02:12 +01002914psa_status_t psa_mac_update( psa_mac_operation_t *operation,
2915 const uint8_t *input,
2916 size_t input_length )
2917{
Gilles Peskinefbfac682018-07-08 20:51:54 +02002918 psa_status_t status = PSA_ERROR_BAD_STATE;
Gilles Peskine8c9def32018-02-08 10:02:12 +01002919 if( ! operation->key_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002920 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002921 if( operation->iv_required && ! operation->iv_set )
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002922 return( PSA_ERROR_BAD_STATE );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002923 operation->has_input = 1;
2924
Gilles Peskine8c9def32018-02-08 10:02:12 +01002925#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02002926 if( operation->alg == PSA_ALG_CMAC )
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002927 {
Gilles Peskinefbfac682018-07-08 20:51:54 +02002928 int ret = mbedtls_cipher_cmac_update( &operation->ctx.cmac,
2929 input, input_length );
2930 status = mbedtls_to_psa_error( ret );
2931 }
2932 else
2933#endif /* MBEDTLS_CMAC_C */
2934#if defined(MBEDTLS_MD_C)
2935 if( PSA_ALG_IS_HMAC( operation->alg ) )
2936 {
2937 status = psa_hash_update( &operation->ctx.hmac.hash_ctx, input,
2938 input_length );
2939 }
2940 else
2941#endif /* MBEDTLS_MD_C */
2942 {
2943 /* This shouldn't happen if `operation` was initialized by
2944 * a setup function. */
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00002945 return( PSA_ERROR_BAD_STATE );
Nir Sonnenscheindcd636a2018-06-04 16:03:32 +03002946 }
2947
Gilles Peskinefbfac682018-07-08 20:51:54 +02002948 if( status != PSA_SUCCESS )
2949 psa_mac_abort( operation );
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02002950 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01002951}
2952
Gilles Peskine01126fa2018-07-12 17:04:55 +02002953#if defined(MBEDTLS_MD_C)
2954static psa_status_t psa_hmac_finish_internal( psa_hmac_internal_data *hmac,
2955 uint8_t *mac,
2956 size_t mac_size )
2957{
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02002958 uint8_t tmp[MBEDTLS_MD_MAX_SIZE];
Gilles Peskine01126fa2018-07-12 17:04:55 +02002959 psa_algorithm_t hash_alg = hmac->hash_ctx.alg;
2960 size_t hash_size = 0;
2961 size_t block_size = psa_get_hash_block_size( hash_alg );
2962 psa_status_t status;
2963
Gilles Peskine01126fa2018-07-12 17:04:55 +02002964 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2965 if( status != PSA_SUCCESS )
2966 return( status );
2967 /* From here on, tmp needs to be wiped. */
2968
2969 status = psa_hash_setup( &hmac->hash_ctx, hash_alg );
2970 if( status != PSA_SUCCESS )
2971 goto exit;
2972
2973 status = psa_hash_update( &hmac->hash_ctx, hmac->opad, block_size );
2974 if( status != PSA_SUCCESS )
2975 goto exit;
2976
2977 status = psa_hash_update( &hmac->hash_ctx, tmp, hash_size );
2978 if( status != PSA_SUCCESS )
2979 goto exit;
2980
Gilles Peskined911eb72018-08-14 15:18:45 +02002981 status = psa_hash_finish( &hmac->hash_ctx, tmp, sizeof( tmp ), &hash_size );
2982 if( status != PSA_SUCCESS )
2983 goto exit;
2984
2985 memcpy( mac, tmp, mac_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002986
2987exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01002988 mbedtls_platform_zeroize( tmp, hash_size );
Gilles Peskine01126fa2018-07-12 17:04:55 +02002989 return( status );
2990}
2991#endif /* MBEDTLS_MD_C */
2992
mohammad16036df908f2018-04-02 08:34:15 -07002993static psa_status_t psa_mac_finish_internal( psa_mac_operation_t *operation,
Gilles Peskine2d277862018-06-18 15:41:12 +02002994 uint8_t *mac,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02002995 size_t mac_size )
Gilles Peskine8c9def32018-02-08 10:02:12 +01002996{
Gilles Peskine1d96fff2018-07-02 12:15:39 +02002997 if( ! operation->key_set )
2998 return( PSA_ERROR_BAD_STATE );
2999 if( operation->iv_required && ! operation->iv_set )
3000 return( PSA_ERROR_BAD_STATE );
3001
Gilles Peskine8c9def32018-02-08 10:02:12 +01003002 if( mac_size < operation->mac_size )
3003 return( PSA_ERROR_BUFFER_TOO_SMALL );
3004
Gilles Peskine8c9def32018-02-08 10:02:12 +01003005#if defined(MBEDTLS_CMAC_C)
Gilles Peskinefbfac682018-07-08 20:51:54 +02003006 if( operation->alg == PSA_ALG_CMAC )
3007 {
Gilles Peskined911eb72018-08-14 15:18:45 +02003008 uint8_t tmp[PSA_MAX_BLOCK_CIPHER_BLOCK_SIZE];
3009 int ret = mbedtls_cipher_cmac_finish( &operation->ctx.cmac, tmp );
3010 if( ret == 0 )
Gilles Peskine87b0ac42018-08-21 14:55:49 +02003011 memcpy( mac, tmp, operation->mac_size );
Gilles Peskine3f108122018-12-07 18:14:53 +01003012 mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003013 return( mbedtls_to_psa_error( ret ) );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003014 }
Gilles Peskinefbfac682018-07-08 20:51:54 +02003015 else
3016#endif /* MBEDTLS_CMAC_C */
3017#if defined(MBEDTLS_MD_C)
3018 if( PSA_ALG_IS_HMAC( operation->alg ) )
3019 {
Gilles Peskine01126fa2018-07-12 17:04:55 +02003020 return( psa_hmac_finish_internal( &operation->ctx.hmac,
Gilles Peskined911eb72018-08-14 15:18:45 +02003021 mac, operation->mac_size ) );
Gilles Peskinefbfac682018-07-08 20:51:54 +02003022 }
3023 else
3024#endif /* MBEDTLS_MD_C */
3025 {
3026 /* This shouldn't happen if `operation` was initialized by
3027 * a setup function. */
3028 return( PSA_ERROR_BAD_STATE );
3029 }
Gilles Peskine8c9def32018-02-08 10:02:12 +01003030}
3031
Gilles Peskineacd4be32018-07-08 19:56:25 +02003032psa_status_t psa_mac_sign_finish( psa_mac_operation_t *operation,
3033 uint8_t *mac,
3034 size_t mac_size,
3035 size_t *mac_length )
mohammad16036df908f2018-04-02 08:34:15 -07003036{
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003037 psa_status_t status;
3038
Jaeden Amero252ef282019-02-15 14:05:35 +00003039 if( operation->alg == 0 )
3040 {
3041 return( PSA_ERROR_BAD_STATE );
3042 }
3043
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003044 /* Fill the output buffer with something that isn't a valid mac
3045 * (barring an attack on the mac and deliberately-crafted input),
3046 * in case the caller doesn't check the return status properly. */
3047 *mac_length = mac_size;
3048 /* If mac_size is 0 then mac may be NULL and then the
3049 * call to memset would have undefined behavior. */
3050 if( mac_size != 0 )
3051 memset( mac, '!', mac_size );
3052
Gilles Peskine89167cb2018-07-08 20:12:23 +02003053 if( ! operation->is_sign )
3054 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003055 return( PSA_ERROR_BAD_STATE );
Gilles Peskine89167cb2018-07-08 20:12:23 +02003056 }
mohammad16036df908f2018-04-02 08:34:15 -07003057
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003058 status = psa_mac_finish_internal( operation, mac, mac_size );
3059
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003060 if( status == PSA_SUCCESS )
3061 {
3062 status = psa_mac_abort( operation );
3063 if( status == PSA_SUCCESS )
3064 *mac_length = operation->mac_size;
3065 else
3066 memset( mac, '!', mac_size );
3067 }
3068 else
3069 psa_mac_abort( operation );
3070 return( status );
mohammad16036df908f2018-04-02 08:34:15 -07003071}
3072
Gilles Peskineacd4be32018-07-08 19:56:25 +02003073psa_status_t psa_mac_verify_finish( psa_mac_operation_t *operation,
3074 const uint8_t *mac,
3075 size_t mac_length )
Gilles Peskine8c9def32018-02-08 10:02:12 +01003076{
Gilles Peskine828ed142018-06-18 23:25:51 +02003077 uint8_t actual_mac[PSA_MAC_MAX_SIZE];
mohammad16036df908f2018-04-02 08:34:15 -07003078 psa_status_t status;
3079
Jaeden Amero252ef282019-02-15 14:05:35 +00003080 if( operation->alg == 0 )
3081 {
3082 return( PSA_ERROR_BAD_STATE );
3083 }
3084
Gilles Peskine89167cb2018-07-08 20:12:23 +02003085 if( operation->is_sign )
3086 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003087 return( PSA_ERROR_BAD_STATE );
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003088 }
3089 if( operation->mac_size != mac_length )
3090 {
3091 status = PSA_ERROR_INVALID_SIGNATURE;
3092 goto cleanup;
Gilles Peskine89167cb2018-07-08 20:12:23 +02003093 }
mohammad16036df908f2018-04-02 08:34:15 -07003094
3095 status = psa_mac_finish_internal( operation,
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003096 actual_mac, sizeof( actual_mac ) );
3097
3098 if( safer_memcmp( mac, actual_mac, mac_length ) != 0 )
3099 status = PSA_ERROR_INVALID_SIGNATURE;
3100
3101cleanup:
3102 if( status == PSA_SUCCESS )
3103 status = psa_mac_abort( operation );
3104 else
3105 psa_mac_abort( operation );
3106
Gilles Peskine3f108122018-12-07 18:14:53 +01003107 mbedtls_platform_zeroize( actual_mac, sizeof( actual_mac ) );
Gilles Peskined911eb72018-08-14 15:18:45 +02003108
Gilles Peskine5d0b8642018-07-08 20:35:02 +02003109 return( status );
Gilles Peskine8c9def32018-02-08 10:02:12 +01003110}
3111
3112
Gilles Peskine20035e32018-02-03 22:44:14 +01003113
Gilles Peskine20035e32018-02-03 22:44:14 +01003114/****************************************************************/
3115/* Asymmetric cryptography */
3116/****************************************************************/
3117
Gilles Peskine2b450e32018-06-27 15:42:46 +02003118#if defined(MBEDTLS_RSA_C)
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003119/* Decode the hash algorithm from alg and store the mbedtls encoding in
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003120 * md_alg. Verify that the hash length is acceptable. */
Gilles Peskine8b18a4f2018-06-08 16:34:46 +02003121static psa_status_t psa_rsa_decode_md_type( psa_algorithm_t alg,
3122 size_t hash_length,
3123 mbedtls_md_type_t *md_alg )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003124{
Gilles Peskine7ed29c52018-06-26 15:50:08 +02003125 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003126 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003127 *md_alg = mbedtls_md_get_type( md_info );
3128
3129 /* The Mbed TLS RSA module uses an unsigned int for hash length
3130 * parameters. Validate that it fits so that we don't risk an
3131 * overflow later. */
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003132#if SIZE_MAX > UINT_MAX
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003133 if( hash_length > UINT_MAX )
3134 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003135#endif
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003136
3137#if defined(MBEDTLS_PKCS1_V15)
3138 /* For PKCS#1 v1.5 signature, if using a hash, the hash length
3139 * must be correct. */
3140 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) &&
3141 alg != PSA_ALG_RSA_PKCS1V15_SIGN_RAW )
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003142 {
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003143 if( md_info == NULL )
3144 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine61b91d42018-06-08 16:09:36 +02003145 if( mbedtls_md_get_size( md_info ) != hash_length )
3146 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003147 }
3148#endif /* MBEDTLS_PKCS1_V15 */
3149
3150#if defined(MBEDTLS_PKCS1_V21)
3151 /* PSS requires a hash internally. */
3152 if( PSA_ALG_IS_RSA_PSS( alg ) )
3153 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003154 if( md_info == NULL )
3155 return( PSA_ERROR_NOT_SUPPORTED );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003156 }
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003157#endif /* MBEDTLS_PKCS1_V21 */
3158
Gilles Peskine61b91d42018-06-08 16:09:36 +02003159 return( PSA_SUCCESS );
Nir Sonnenschein4db79eb2018-06-04 16:40:31 +03003160}
3161
Gilles Peskine2b450e32018-06-27 15:42:46 +02003162static psa_status_t psa_rsa_sign( mbedtls_rsa_context *rsa,
3163 psa_algorithm_t alg,
3164 const uint8_t *hash,
3165 size_t hash_length,
3166 uint8_t *signature,
3167 size_t signature_size,
3168 size_t *signature_length )
3169{
3170 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003171 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003172 mbedtls_md_type_t md_alg;
3173
3174 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3175 if( status != PSA_SUCCESS )
3176 return( status );
3177
Gilles Peskine630a18a2018-06-29 17:49:35 +02003178 if( signature_size < mbedtls_rsa_get_len( rsa ) )
Gilles Peskine2b450e32018-06-27 15:42:46 +02003179 return( PSA_ERROR_BUFFER_TOO_SMALL );
3180
3181#if defined(MBEDTLS_PKCS1_V15)
3182 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3183 {
3184 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3185 MBEDTLS_MD_NONE );
3186 ret = mbedtls_rsa_pkcs1_sign( rsa,
3187 mbedtls_ctr_drbg_random,
3188 &global_data.ctr_drbg,
3189 MBEDTLS_RSA_PRIVATE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003190 md_alg,
3191 (unsigned int) hash_length,
3192 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003193 signature );
3194 }
3195 else
3196#endif /* MBEDTLS_PKCS1_V15 */
3197#if defined(MBEDTLS_PKCS1_V21)
3198 if( PSA_ALG_IS_RSA_PSS( alg ) )
3199 {
3200 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3201 ret = mbedtls_rsa_rsassa_pss_sign( rsa,
3202 mbedtls_ctr_drbg_random,
3203 &global_data.ctr_drbg,
3204 MBEDTLS_RSA_PRIVATE,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003205 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003206 (unsigned int) hash_length,
3207 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003208 signature );
3209 }
3210 else
3211#endif /* MBEDTLS_PKCS1_V21 */
3212 {
3213 return( PSA_ERROR_INVALID_ARGUMENT );
3214 }
3215
3216 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003217 *signature_length = mbedtls_rsa_get_len( rsa );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003218 return( mbedtls_to_psa_error( ret ) );
3219}
3220
3221static psa_status_t psa_rsa_verify( mbedtls_rsa_context *rsa,
3222 psa_algorithm_t alg,
3223 const uint8_t *hash,
3224 size_t hash_length,
3225 const uint8_t *signature,
3226 size_t signature_length )
3227{
3228 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003229 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine2b450e32018-06-27 15:42:46 +02003230 mbedtls_md_type_t md_alg;
3231
3232 status = psa_rsa_decode_md_type( alg, hash_length, &md_alg );
3233 if( status != PSA_SUCCESS )
3234 return( status );
3235
Gilles Peskine89cc74f2019-09-12 22:08:23 +02003236 if( signature_length != mbedtls_rsa_get_len( rsa ) )
3237 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003238
3239#if defined(MBEDTLS_PKCS1_V15)
3240 if( PSA_ALG_IS_RSA_PKCS1V15_SIGN( alg ) )
3241 {
3242 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V15,
3243 MBEDTLS_MD_NONE );
3244 ret = mbedtls_rsa_pkcs1_verify( rsa,
3245 mbedtls_ctr_drbg_random,
3246 &global_data.ctr_drbg,
3247 MBEDTLS_RSA_PUBLIC,
3248 md_alg,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003249 (unsigned int) hash_length,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003250 hash,
3251 signature );
3252 }
3253 else
3254#endif /* MBEDTLS_PKCS1_V15 */
3255#if defined(MBEDTLS_PKCS1_V21)
3256 if( PSA_ALG_IS_RSA_PSS( alg ) )
3257 {
3258 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3259 ret = mbedtls_rsa_rsassa_pss_verify( rsa,
3260 mbedtls_ctr_drbg_random,
3261 &global_data.ctr_drbg,
3262 MBEDTLS_RSA_PUBLIC,
Gilles Peskine71ac7b12018-06-29 23:36:35 +02003263 MBEDTLS_MD_NONE,
Jaeden Amerobbf97e32018-06-26 14:20:51 +01003264 (unsigned int) hash_length,
3265 hash,
Gilles Peskine2b450e32018-06-27 15:42:46 +02003266 signature );
3267 }
3268 else
3269#endif /* MBEDTLS_PKCS1_V21 */
3270 {
3271 return( PSA_ERROR_INVALID_ARGUMENT );
3272 }
Gilles Peskineef12c632018-09-13 20:37:48 +02003273
3274 /* Mbed TLS distinguishes "invalid padding" from "valid padding but
3275 * the rest of the signature is invalid". This has little use in
3276 * practice and PSA doesn't report this distinction. */
3277 if( ret == MBEDTLS_ERR_RSA_INVALID_PADDING )
3278 return( PSA_ERROR_INVALID_SIGNATURE );
Gilles Peskine2b450e32018-06-27 15:42:46 +02003279 return( mbedtls_to_psa_error( ret ) );
3280}
3281#endif /* MBEDTLS_RSA_C */
3282
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003283#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003284/* `ecp` cannot be const because `ecp->grp` needs to be non-const
3285 * for mbedtls_ecdsa_sign() and mbedtls_ecdsa_sign_det()
3286 * (even though these functions don't modify it). */
3287static psa_status_t psa_ecdsa_sign( mbedtls_ecp_keypair *ecp,
3288 psa_algorithm_t alg,
3289 const uint8_t *hash,
3290 size_t hash_length,
3291 uint8_t *signature,
3292 size_t signature_size,
3293 size_t *signature_length )
3294{
Janos Follath24eed8d2019-11-22 13:21:35 +00003295 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003296 mbedtls_mpi r, s;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003297 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003298 mbedtls_mpi_init( &r );
3299 mbedtls_mpi_init( &s );
3300
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003301 if( signature_size < 2 * curve_bytes )
3302 {
3303 ret = MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
3304 goto cleanup;
3305 }
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003306
Gilles Peskinea05219c2018-11-16 16:02:56 +01003307#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003308 if( PSA_ALG_DSA_IS_DETERMINISTIC( alg ) )
3309 {
3310 psa_algorithm_t hash_alg = PSA_ALG_SIGN_GET_HASH( alg );
3311 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3312 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
Darryl Green5e843fa2019-09-05 14:06:34 +01003313 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign_det_ext( &ecp->grp, &r, &s,
3314 &ecp->d, hash,
3315 hash_length, md_alg,
3316 mbedtls_ctr_drbg_random,
3317 &global_data.ctr_drbg ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003318 }
3319 else
Gilles Peskinea05219c2018-11-16 16:02:56 +01003320#endif /* MBEDTLS_ECDSA_DETERMINISTIC */
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003321 {
Gilles Peskinea05219c2018-11-16 16:02:56 +01003322 (void) alg;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003323 MBEDTLS_MPI_CHK( mbedtls_ecdsa_sign( &ecp->grp, &r, &s, &ecp->d,
3324 hash, hash_length,
3325 mbedtls_ctr_drbg_random,
3326 &global_data.ctr_drbg ) );
3327 }
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003328
3329 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &r,
3330 signature,
3331 curve_bytes ) );
3332 MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &s,
3333 signature + curve_bytes,
3334 curve_bytes ) );
3335
3336cleanup:
3337 mbedtls_mpi_free( &r );
3338 mbedtls_mpi_free( &s );
3339 if( ret == 0 )
3340 *signature_length = 2 * curve_bytes;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003341 return( mbedtls_to_psa_error( ret ) );
3342}
3343
3344static psa_status_t psa_ecdsa_verify( mbedtls_ecp_keypair *ecp,
3345 const uint8_t *hash,
3346 size_t hash_length,
3347 const uint8_t *signature,
3348 size_t signature_length )
3349{
Janos Follath24eed8d2019-11-22 13:21:35 +00003350 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003351 mbedtls_mpi r, s;
3352 size_t curve_bytes = PSA_BITS_TO_BYTES( ecp->grp.pbits );
3353 mbedtls_mpi_init( &r );
3354 mbedtls_mpi_init( &s );
3355
3356 if( signature_length != 2 * curve_bytes )
3357 return( PSA_ERROR_INVALID_SIGNATURE );
3358
3359 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &r,
3360 signature,
3361 curve_bytes ) );
3362 MBEDTLS_MPI_CHK( mbedtls_mpi_read_binary( &s,
3363 signature + curve_bytes,
3364 curve_bytes ) );
3365
3366 ret = mbedtls_ecdsa_verify( &ecp->grp, hash, hash_length,
3367 &ecp->Q, &r, &s );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003368
3369cleanup:
3370 mbedtls_mpi_free( &r );
3371 mbedtls_mpi_free( &s );
3372 return( mbedtls_to_psa_error( ret ) );
3373}
3374#endif /* MBEDTLS_ECDSA_C */
3375
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003376psa_status_t psa_sign_hash( psa_key_handle_t handle,
3377 psa_algorithm_t alg,
3378 const uint8_t *hash,
3379 size_t hash_length,
3380 uint8_t *signature,
3381 size_t signature_size,
3382 size_t *signature_length )
Gilles Peskine20035e32018-02-03 22:44:14 +01003383{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003384 psa_key_slot_t *slot;
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003385 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003386#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3387 const psa_drv_se_t *drv;
3388 psa_drv_se_context_t *drv_context;
3389#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003390
3391 *signature_length = signature_size;
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003392 /* Immediately reject a zero-length signature buffer. This guarantees
3393 * that signature must be a valid pointer. (On the other hand, the hash
3394 * buffer can in principle be empty since it doesn't actually have
3395 * to be a hash.) */
3396 if( signature_size == 0 )
3397 return( PSA_ERROR_BUFFER_TOO_SMALL );
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003398
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003399 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_SIGN_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003400 if( status != PSA_SUCCESS )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003401 goto exit;
Gilles Peskine8e338702019-07-30 20:06:31 +02003402 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003403 {
3404 status = PSA_ERROR_INVALID_ARGUMENT;
3405 goto exit;
3406 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003407
Gilles Peskineedc64242019-08-07 21:05:07 +02003408#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3409 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3410 {
3411 if( drv->asymmetric == NULL ||
3412 drv->asymmetric->p_sign == NULL )
3413 {
3414 status = PSA_ERROR_NOT_SUPPORTED;
3415 goto exit;
3416 }
3417 status = drv->asymmetric->p_sign( drv_context,
3418 slot->data.se.slot_number,
3419 alg,
3420 hash, hash_length,
3421 signature, signature_size,
3422 signature_length );
3423 }
3424 else
3425#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine20035e32018-02-03 22:44:14 +01003426#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003427 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskine20035e32018-02-03 22:44:14 +01003428 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003429 status = psa_rsa_sign( slot->data.rsa,
3430 alg,
3431 hash, hash_length,
3432 signature, signature_size,
3433 signature_length );
Gilles Peskine20035e32018-02-03 22:44:14 +01003434 }
3435 else
3436#endif /* defined(MBEDTLS_RSA_C) */
3437#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003438 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
Gilles Peskine20035e32018-02-03 22:44:14 +01003439 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003440#if defined(MBEDTLS_ECDSA_C)
Gilles Peskinea05219c2018-11-16 16:02:56 +01003441 if(
3442#if defined(MBEDTLS_ECDSA_DETERMINISTIC)
3443 PSA_ALG_IS_ECDSA( alg )
3444#else
3445 PSA_ALG_IS_RANDOMIZED_ECDSA( alg )
3446#endif
3447 )
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003448 status = psa_ecdsa_sign( slot->data.ecp,
3449 alg,
3450 hash, hash_length,
3451 signature, signature_size,
3452 signature_length );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003453 else
3454#endif /* defined(MBEDTLS_ECDSA_C) */
3455 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003456 status = PSA_ERROR_INVALID_ARGUMENT;
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003457 }
itayzafrir5c753392018-05-08 11:18:38 +03003458 }
3459 else
3460#endif /* defined(MBEDTLS_ECP_C) */
3461 {
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003462 status = PSA_ERROR_NOT_SUPPORTED;
Gilles Peskine20035e32018-02-03 22:44:14 +01003463 }
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003464
3465exit:
3466 /* Fill the unused part of the output buffer (the whole buffer on error,
3467 * the trailing part on success) with something that isn't a valid mac
3468 * (barring an attack on the mac and deliberately-crafted input),
3469 * in case the caller doesn't check the return status properly. */
3470 if( status == PSA_SUCCESS )
3471 memset( signature + *signature_length, '!',
3472 signature_size - *signature_length );
Gilles Peskine4019f0e2019-09-12 22:05:59 +02003473 else
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003474 memset( signature, '!', signature_size );
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003475 /* If signature_size is 0 then we have nothing to do. We must not call
3476 * memset because signature may be NULL in this case. */
Gilles Peskinea26ff6a2018-06-28 12:21:19 +02003477 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003478}
3479
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003480psa_status_t psa_verify_hash( psa_key_handle_t handle,
3481 psa_algorithm_t alg,
3482 const uint8_t *hash,
3483 size_t hash_length,
3484 const uint8_t *signature,
3485 size_t signature_length )
itayzafrir5c753392018-05-08 11:18:38 +03003486{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003487 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003488 psa_status_t status;
Gilles Peskineedc64242019-08-07 21:05:07 +02003489#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3490 const psa_drv_se_t *drv;
3491 psa_drv_se_context_t *drv_context;
3492#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine2b450e32018-06-27 15:42:46 +02003493
Gilles Peskine89d8c5c2019-11-26 17:01:59 +01003494 status = psa_get_key_from_slot( handle, &slot, PSA_KEY_USAGE_VERIFY_HASH, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003495 if( status != PSA_SUCCESS )
3496 return( status );
itayzafrir5c753392018-05-08 11:18:38 +03003497
Gilles Peskineedc64242019-08-07 21:05:07 +02003498#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
3499 if( psa_get_se_driver( slot->attr.lifetime, &drv, &drv_context ) )
3500 {
3501 if( drv->asymmetric == NULL ||
3502 drv->asymmetric->p_verify == NULL )
3503 return( PSA_ERROR_NOT_SUPPORTED );
3504 return( drv->asymmetric->p_verify( drv_context,
3505 slot->data.se.slot_number,
3506 alg,
3507 hash, hash_length,
3508 signature, signature_length ) );
3509 }
3510 else
3511#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskine61b91d42018-06-08 16:09:36 +02003512#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003513 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003514 {
Gilles Peskine2b450e32018-06-27 15:42:46 +02003515 return( psa_rsa_verify( slot->data.rsa,
3516 alg,
3517 hash, hash_length,
3518 signature, signature_length ) );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003519 }
3520 else
3521#endif /* defined(MBEDTLS_RSA_C) */
itayzafrir5c753392018-05-08 11:18:38 +03003522#if defined(MBEDTLS_ECP_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003523 if( PSA_KEY_TYPE_IS_ECC( slot->attr.type ) )
itayzafrir5c753392018-05-08 11:18:38 +03003524 {
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003525#if defined(MBEDTLS_ECDSA_C)
3526 if( PSA_ALG_IS_ECDSA( alg ) )
Gilles Peskineeae6eee2018-06-28 13:56:01 +02003527 return( psa_ecdsa_verify( slot->data.ecp,
3528 hash, hash_length,
3529 signature, signature_length ) );
Gilles Peskinea81d85b2018-06-26 16:10:23 +02003530 else
3531#endif /* defined(MBEDTLS_ECDSA_C) */
3532 {
3533 return( PSA_ERROR_INVALID_ARGUMENT );
3534 }
itayzafrir5c753392018-05-08 11:18:38 +03003535 }
Gilles Peskine20035e32018-02-03 22:44:14 +01003536 else
3537#endif /* defined(MBEDTLS_ECP_C) */
3538 {
3539 return( PSA_ERROR_NOT_SUPPORTED );
3540 }
3541}
3542
Gilles Peskine072ac562018-06-30 00:21:29 +02003543#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21)
3544static void psa_rsa_oaep_set_padding_mode( psa_algorithm_t alg,
3545 mbedtls_rsa_context *rsa )
3546{
3547 psa_algorithm_t hash_alg = PSA_ALG_RSA_OAEP_GET_HASH( alg );
3548 const mbedtls_md_info_t *md_info = mbedtls_md_info_from_psa( hash_alg );
3549 mbedtls_md_type_t md_alg = mbedtls_md_get_type( md_info );
3550 mbedtls_rsa_set_padding( rsa, MBEDTLS_RSA_PKCS_V21, md_alg );
3551}
3552#endif /* defined(MBEDTLS_RSA_C) && defined(MBEDTLS_PKCS1_V21) */
3553
Gilles Peskinec5487a82018-12-03 18:08:14 +01003554psa_status_t psa_asymmetric_encrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003555 psa_algorithm_t alg,
3556 const uint8_t *input,
3557 size_t input_length,
3558 const uint8_t *salt,
3559 size_t salt_length,
3560 uint8_t *output,
3561 size_t output_size,
3562 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003563{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003564 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003565 psa_status_t status;
3566
Darryl Green5cc689a2018-07-24 15:34:10 +01003567 (void) input;
3568 (void) input_length;
3569 (void) salt;
3570 (void) output;
3571 (void) output_size;
3572
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003573 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003574
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003575 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3576 return( PSA_ERROR_INVALID_ARGUMENT );
3577
Gilles Peskine28f8f302019-07-24 13:30:31 +02003578 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003579 if( status != PSA_SUCCESS )
3580 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003581 if( ! ( PSA_KEY_TYPE_IS_PUBLIC_KEY( slot->attr.type ) ||
3582 PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003583 return( PSA_ERROR_INVALID_ARGUMENT );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003584
3585#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003586 if( PSA_KEY_TYPE_IS_RSA( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003587 {
3588 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003589 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine630a18a2018-06-29 17:49:35 +02003590 if( output_size < mbedtls_rsa_get_len( rsa ) )
Vikas Katariya21599b62019-08-02 12:26:29 +01003591 return( PSA_ERROR_BUFFER_TOO_SMALL );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003592#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003593 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003594 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003595 ret = mbedtls_rsa_pkcs1_encrypt( rsa,
3596 mbedtls_ctr_drbg_random,
3597 &global_data.ctr_drbg,
3598 MBEDTLS_RSA_PUBLIC,
3599 input_length,
3600 input,
3601 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003602 }
3603 else
3604#endif /* MBEDTLS_PKCS1_V15 */
3605#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003606 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003607 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003608 psa_rsa_oaep_set_padding_mode( alg, rsa );
3609 ret = mbedtls_rsa_rsaes_oaep_encrypt( rsa,
3610 mbedtls_ctr_drbg_random,
3611 &global_data.ctr_drbg,
3612 MBEDTLS_RSA_PUBLIC,
3613 salt, salt_length,
3614 input_length,
3615 input,
3616 output );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003617 }
3618 else
3619#endif /* MBEDTLS_PKCS1_V21 */
3620 {
3621 return( PSA_ERROR_INVALID_ARGUMENT );
3622 }
3623 if( ret == 0 )
Gilles Peskine630a18a2018-06-29 17:49:35 +02003624 *output_length = mbedtls_rsa_get_len( rsa );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003625 return( mbedtls_to_psa_error( ret ) );
3626 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003627 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003628#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003629 {
3630 return( PSA_ERROR_NOT_SUPPORTED );
3631 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003632}
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003633
Gilles Peskinec5487a82018-12-03 18:08:14 +01003634psa_status_t psa_asymmetric_decrypt( psa_key_handle_t handle,
Gilles Peskine61b91d42018-06-08 16:09:36 +02003635 psa_algorithm_t alg,
3636 const uint8_t *input,
3637 size_t input_length,
3638 const uint8_t *salt,
3639 size_t salt_length,
3640 uint8_t *output,
3641 size_t output_size,
3642 size_t *output_length )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003643{
Gilles Peskine2f060a82018-12-04 17:12:32 +01003644 psa_key_slot_t *slot;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003645 psa_status_t status;
3646
Darryl Green5cc689a2018-07-24 15:34:10 +01003647 (void) input;
3648 (void) input_length;
3649 (void) salt;
3650 (void) output;
3651 (void) output_size;
3652
Nir Sonnenscheinca466c82018-06-04 16:43:12 +03003653 *output_length = 0;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003654
Gilles Peskineb3fc05d2018-06-30 19:04:35 +02003655 if( ! PSA_ALG_IS_RSA_OAEP( alg ) && salt_length != 0 )
3656 return( PSA_ERROR_INVALID_ARGUMENT );
3657
Gilles Peskine28f8f302019-07-24 13:30:31 +02003658 status = psa_get_transparent_key( handle, &slot, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003659 if( status != PSA_SUCCESS )
3660 return( status );
Gilles Peskine8e338702019-07-30 20:06:31 +02003661 if( ! PSA_KEY_TYPE_IS_KEY_PAIR( slot->attr.type ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003662 return( PSA_ERROR_INVALID_ARGUMENT );
3663
3664#if defined(MBEDTLS_RSA_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003665 if( slot->attr.type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003666 {
3667 mbedtls_rsa_context *rsa = slot->data.rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00003668 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003669
Gilles Peskine630a18a2018-06-29 17:49:35 +02003670 if( input_length != mbedtls_rsa_get_len( rsa ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003671 return( PSA_ERROR_INVALID_ARGUMENT );
3672
3673#if defined(MBEDTLS_PKCS1_V15)
Gilles Peskine6afe7892018-05-31 13:16:08 +02003674 if( alg == PSA_ALG_RSA_PKCS1V15_CRYPT )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003675 {
Gilles Peskine61b91d42018-06-08 16:09:36 +02003676 ret = mbedtls_rsa_pkcs1_decrypt( rsa,
3677 mbedtls_ctr_drbg_random,
3678 &global_data.ctr_drbg,
3679 MBEDTLS_RSA_PRIVATE,
3680 output_length,
3681 input,
3682 output,
3683 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003684 }
3685 else
3686#endif /* MBEDTLS_PKCS1_V15 */
3687#if defined(MBEDTLS_PKCS1_V21)
Gilles Peskine55bf3d12018-06-26 15:53:48 +02003688 if( PSA_ALG_IS_RSA_OAEP( alg ) )
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003689 {
Gilles Peskine072ac562018-06-30 00:21:29 +02003690 psa_rsa_oaep_set_padding_mode( alg, rsa );
3691 ret = mbedtls_rsa_rsaes_oaep_decrypt( rsa,
3692 mbedtls_ctr_drbg_random,
3693 &global_data.ctr_drbg,
3694 MBEDTLS_RSA_PRIVATE,
3695 salt, salt_length,
3696 output_length,
3697 input,
3698 output,
3699 output_size );
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003700 }
3701 else
3702#endif /* MBEDTLS_PKCS1_V21 */
3703 {
3704 return( PSA_ERROR_INVALID_ARGUMENT );
3705 }
Nir Sonnenschein717a0402018-06-04 16:36:15 +03003706
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003707 return( mbedtls_to_psa_error( ret ) );
3708 }
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003709 else
Gilles Peskineb75e4f12018-06-08 17:44:47 +02003710#endif /* defined(MBEDTLS_RSA_C) */
Nir Sonnenschein39e59142018-05-02 23:16:26 +03003711 {
3712 return( PSA_ERROR_NOT_SUPPORTED );
3713 }
Gilles Peskine5b051bc2018-05-31 13:25:48 +02003714}
Gilles Peskine20035e32018-02-03 22:44:14 +01003715
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02003716
3717
mohammad1603503973b2018-03-12 15:59:30 +02003718/****************************************************************/
3719/* Symmetric cryptography */
3720/****************************************************************/
3721
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003722/* Initialize the cipher operation structure. Once this function has been
3723 * called, psa_cipher_abort can run and will do the right thing. */
3724static psa_status_t psa_cipher_init( psa_cipher_operation_t *operation,
3725 psa_algorithm_t alg )
3726{
Gilles Peskinec06e0712018-06-20 16:21:04 +02003727 if( ! PSA_ALG_IS_CIPHER( alg ) )
3728 {
3729 memset( operation, 0, sizeof( *operation ) );
3730 return( PSA_ERROR_INVALID_ARGUMENT );
3731 }
3732
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003733 operation->alg = alg;
3734 operation->key_set = 0;
3735 operation->iv_set = 0;
3736 operation->iv_required = 1;
3737 operation->iv_size = 0;
3738 operation->block_size = 0;
3739 mbedtls_cipher_init( &operation->ctx.cipher );
3740 return( PSA_SUCCESS );
3741}
3742
Gilles Peskinee553c652018-06-04 16:22:46 +02003743static psa_status_t psa_cipher_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003744 psa_key_handle_t handle,
Gilles Peskine7e928852018-06-04 16:23:10 +02003745 psa_algorithm_t alg,
3746 mbedtls_operation_t cipher_operation )
mohammad1603503973b2018-03-12 15:59:30 +02003747{
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003748 int ret = 0;
3749 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Gilles Peskine2f060a82018-12-04 17:12:32 +01003750 psa_key_slot_t *slot;
mohammad1603503973b2018-03-12 15:59:30 +02003751 size_t key_bits;
3752 const mbedtls_cipher_info_t *cipher_info = NULL;
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003753 psa_key_usage_t usage = ( cipher_operation == MBEDTLS_ENCRYPT ?
3754 PSA_KEY_USAGE_ENCRYPT :
3755 PSA_KEY_USAGE_DECRYPT );
mohammad1603503973b2018-03-12 15:59:30 +02003756
Jaeden Amero36ee5d02019-02-19 09:25:10 +00003757 /* A context must be freshly initialized before it can be set up. */
3758 if( operation->alg != 0 )
3759 {
3760 return( PSA_ERROR_BAD_STATE );
3761 }
3762
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02003763 status = psa_cipher_init( operation, alg );
3764 if( status != PSA_SUCCESS )
3765 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003766
Gilles Peskine28f8f302019-07-24 13:30:31 +02003767 status = psa_get_transparent_key( handle, &slot, usage, alg);
Gilles Peskineb0b255c2018-07-06 17:01:38 +02003768 if( status != PSA_SUCCESS )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003769 goto exit;
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02003770 key_bits = psa_get_key_slot_bits( slot );
mohammad1603503973b2018-03-12 15:59:30 +02003771
Gilles Peskine8e338702019-07-30 20:06:31 +02003772 cipher_info = mbedtls_cipher_info_from_psa( alg, slot->attr.type, key_bits, NULL );
mohammad1603503973b2018-03-12 15:59:30 +02003773 if( cipher_info == NULL )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003774 {
3775 status = PSA_ERROR_NOT_SUPPORTED;
3776 goto exit;
3777 }
mohammad1603503973b2018-03-12 15:59:30 +02003778
mohammad1603503973b2018-03-12 15:59:30 +02003779 ret = mbedtls_cipher_setup( &operation->ctx.cipher, cipher_info );
Moran Peker41deec42018-04-04 15:43:05 +03003780 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003781 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003782
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003783#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02003784 if( slot->attr.type == PSA_KEY_TYPE_DES && key_bits == 128 )
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003785 {
3786 /* Two-key Triple-DES is 3-key Triple-DES with K1=K3 */
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02003787 uint8_t keys[24];
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003788 memcpy( keys, slot->data.raw.data, 16 );
3789 memcpy( keys + 16, slot->data.raw.data, 8 );
3790 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3791 keys,
3792 192, cipher_operation );
3793 }
3794 else
3795#endif
3796 {
3797 ret = mbedtls_cipher_setkey( &operation->ctx.cipher,
3798 slot->data.raw.data,
Jaeden Amero23bbb752018-06-26 14:16:54 +01003799 (int) key_bits, cipher_operation );
Gilles Peskine9ad29e22018-06-21 09:40:04 +02003800 }
Moran Peker41deec42018-04-04 15:43:05 +03003801 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003802 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003803
mohammad16038481e742018-03-18 13:57:31 +02003804#if defined(MBEDTLS_CIPHER_MODE_WITH_PADDING)
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003805 switch( alg )
mohammad16038481e742018-03-18 13:57:31 +02003806 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003807 case PSA_ALG_CBC_NO_PADDING:
3808 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3809 MBEDTLS_PADDING_NONE );
3810 break;
3811 case PSA_ALG_CBC_PKCS7:
3812 ret = mbedtls_cipher_set_padding_mode( &operation->ctx.cipher,
3813 MBEDTLS_PADDING_PKCS7 );
3814 break;
3815 default:
3816 /* The algorithm doesn't involve padding. */
3817 ret = 0;
3818 break;
3819 }
3820 if( ret != 0 )
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003821 goto exit;
mohammad16038481e742018-03-18 13:57:31 +02003822#endif //MBEDTLS_CIPHER_MODE_WITH_PADDING
3823
mohammad1603503973b2018-03-12 15:59:30 +02003824 operation->key_set = 1;
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003825 operation->block_size = ( PSA_ALG_IS_STREAM_CIPHER( alg ) ? 1 :
Gilles Peskine8e338702019-07-30 20:06:31 +02003826 PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type ) );
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003827 if( alg & PSA_ALG_CIPHER_FROM_BLOCK_FLAG )
mohammad16038481e742018-03-18 13:57:31 +02003828 {
Gilles Peskine8e338702019-07-30 20:06:31 +02003829 operation->iv_size = PSA_BLOCK_CIPHER_BLOCK_SIZE( slot->attr.type );
mohammad16038481e742018-03-18 13:57:31 +02003830 }
Gilles Peskine26869f22019-05-06 15:25:00 +02003831#if defined(MBEDTLS_CHACHA20_C)
3832 else
3833 if( alg == PSA_ALG_CHACHA20 )
3834 operation->iv_size = 12;
3835#endif
mohammad1603503973b2018-03-12 15:59:30 +02003836
Gilles Peskine9ab61b62019-02-25 17:43:14 +01003837exit:
3838 if( status == 0 )
3839 status = mbedtls_to_psa_error( ret );
3840 if( status != 0 )
3841 psa_cipher_abort( operation );
3842 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003843}
3844
Gilles Peskinefe119512018-07-08 21:39:34 +02003845psa_status_t psa_cipher_encrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003846 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003847 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003848{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003849 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_ENCRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003850}
3851
Gilles Peskinefe119512018-07-08 21:39:34 +02003852psa_status_t psa_cipher_decrypt_setup( psa_cipher_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01003853 psa_key_handle_t handle,
Gilles Peskinefe119512018-07-08 21:39:34 +02003854 psa_algorithm_t alg )
mohammad16038481e742018-03-18 13:57:31 +02003855{
Gilles Peskinec5487a82018-12-03 18:08:14 +01003856 return( psa_cipher_setup( operation, handle, alg, MBEDTLS_DECRYPT ) );
mohammad16038481e742018-03-18 13:57:31 +02003857}
3858
Gilles Peskinefe119512018-07-08 21:39:34 +02003859psa_status_t psa_cipher_generate_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003860 uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003861 size_t iv_size,
3862 size_t *iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003863{
itayzafrir534bd7c2018-08-02 13:56:32 +03003864 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003865 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003866 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003867 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003868 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003869 }
Moran Peker41deec42018-04-04 15:43:05 +03003870 if( iv_size < operation->iv_size )
mohammad1603503973b2018-03-12 15:59:30 +02003871 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003872 status = PSA_ERROR_BUFFER_TOO_SMALL;
Moran Peker41deec42018-04-04 15:43:05 +03003873 goto exit;
3874 }
Gilles Peskine7e928852018-06-04 16:23:10 +02003875 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
3876 iv, operation->iv_size );
Moran Peker41deec42018-04-04 15:43:05 +03003877 if( ret != 0 )
3878 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003879 status = mbedtls_to_psa_error( ret );
Gilles Peskinee553c652018-06-04 16:22:46 +02003880 goto exit;
mohammad1603503973b2018-03-12 15:59:30 +02003881 }
Gilles Peskinee553c652018-06-04 16:22:46 +02003882
mohammad16038481e742018-03-18 13:57:31 +02003883 *iv_length = operation->iv_size;
itayzafrir534bd7c2018-08-02 13:56:32 +03003884 status = psa_cipher_set_iv( operation, iv, *iv_length );
Moran Peker41deec42018-04-04 15:43:05 +03003885
Moran Peker395db872018-05-31 14:07:14 +03003886exit:
itayzafrir534bd7c2018-08-02 13:56:32 +03003887 if( status != PSA_SUCCESS )
Moran Peker395db872018-05-31 14:07:14 +03003888 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003889 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003890}
3891
Gilles Peskinefe119512018-07-08 21:39:34 +02003892psa_status_t psa_cipher_set_iv( psa_cipher_operation_t *operation,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003893 const uint8_t *iv,
Gilles Peskinefe119512018-07-08 21:39:34 +02003894 size_t iv_length )
mohammad1603503973b2018-03-12 15:59:30 +02003895{
itayzafrir534bd7c2018-08-02 13:56:32 +03003896 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003897 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinec1bb6c82018-06-18 16:04:39 +02003898 if( operation->iv_set || ! operation->iv_required )
itayzafrir534bd7c2018-08-02 13:56:32 +03003899 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003900 return( PSA_ERROR_BAD_STATE );
itayzafrir534bd7c2018-08-02 13:56:32 +03003901 }
Moran Pekera28258c2018-05-29 16:25:04 +03003902 if( iv_length != operation->iv_size )
Moran Peker71f19ae2018-04-22 20:23:16 +03003903 {
itayzafrir534bd7c2018-08-02 13:56:32 +03003904 status = PSA_ERROR_INVALID_ARGUMENT;
3905 goto exit;
Moran Peker71f19ae2018-04-22 20:23:16 +03003906 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003907 ret = mbedtls_cipher_set_iv( &operation->ctx.cipher, iv, iv_length );
3908 status = mbedtls_to_psa_error( ret );
3909exit:
3910 if( status == PSA_SUCCESS )
3911 operation->iv_set = 1;
3912 else
mohammad1603503973b2018-03-12 15:59:30 +02003913 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003914 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003915}
3916
Gilles Peskinee553c652018-06-04 16:22:46 +02003917psa_status_t psa_cipher_update( psa_cipher_operation_t *operation,
3918 const uint8_t *input,
3919 size_t input_length,
Andrew Thoelke163639b2019-05-15 12:33:23 +01003920 uint8_t *output,
Gilles Peskinee553c652018-06-04 16:22:46 +02003921 size_t output_size,
3922 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003923{
itayzafrir534bd7c2018-08-02 13:56:32 +03003924 psa_status_t status;
Janos Follath24eed8d2019-11-22 13:21:35 +00003925 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskine89d789c2018-06-04 17:17:16 +02003926 size_t expected_output_size;
Jaeden Ameroab439972019-02-15 14:12:05 +00003927
3928 if( operation->alg == 0 )
3929 {
3930 return( PSA_ERROR_BAD_STATE );
3931 }
3932
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003933 if( ! PSA_ALG_IS_STREAM_CIPHER( operation->alg ) )
Gilles Peskine89d789c2018-06-04 17:17:16 +02003934 {
3935 /* Take the unprocessed partial block left over from previous
3936 * update calls, if any, plus the input to this call. Remove
3937 * the last partial block, if any. You get the data that will be
3938 * output in this call. */
3939 expected_output_size =
3940 ( operation->ctx.cipher.unprocessed_len + input_length )
3941 / operation->block_size * operation->block_size;
3942 }
3943 else
3944 {
3945 expected_output_size = input_length;
3946 }
itayzafrir534bd7c2018-08-02 13:56:32 +03003947
Gilles Peskine89d789c2018-06-04 17:17:16 +02003948 if( output_size < expected_output_size )
itayzafrir534bd7c2018-08-02 13:56:32 +03003949 {
3950 status = PSA_ERROR_BUFFER_TOO_SMALL;
3951 goto exit;
3952 }
mohammad160382759612018-03-12 18:16:40 +02003953
mohammad1603503973b2018-03-12 15:59:30 +02003954 ret = mbedtls_cipher_update( &operation->ctx.cipher, input,
Gilles Peskinee553c652018-06-04 16:22:46 +02003955 input_length, output, output_length );
itayzafrir534bd7c2018-08-02 13:56:32 +03003956 status = mbedtls_to_psa_error( ret );
3957exit:
3958 if( status != PSA_SUCCESS )
mohammad1603503973b2018-03-12 15:59:30 +02003959 psa_cipher_abort( operation );
itayzafrir534bd7c2018-08-02 13:56:32 +03003960 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02003961}
3962
Gilles Peskinee553c652018-06-04 16:22:46 +02003963psa_status_t psa_cipher_finish( psa_cipher_operation_t *operation,
3964 uint8_t *output,
3965 size_t output_size,
3966 size_t *output_length )
mohammad1603503973b2018-03-12 15:59:30 +02003967{
David Saadab4ecc272019-02-14 13:48:10 +02003968 psa_status_t status = PSA_ERROR_GENERIC_ERROR;
Janos Follath315b51c2018-07-09 16:04:51 +01003969 int cipher_ret = MBEDTLS_ERR_CIPHER_FEATURE_UNAVAILABLE;
Gilles Peskinee553c652018-06-04 16:22:46 +02003970 uint8_t temp_output_buffer[MBEDTLS_MAX_BLOCK_LENGTH];
Moran Pekerbed71a22018-04-22 20:19:20 +03003971
mohammad1603503973b2018-03-12 15:59:30 +02003972 if( ! operation->key_set )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003973 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003974 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003975 }
3976 if( operation->iv_required && ! operation->iv_set )
3977 {
Jaeden Ameroe236c2a2019-02-20 15:37:15 +00003978 return( PSA_ERROR_BAD_STATE );
Moran Peker7cb22b82018-06-05 11:40:02 +03003979 }
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003980
Gilles Peskine2c5219a2018-06-06 15:12:32 +02003981 if( operation->ctx.cipher.operation == MBEDTLS_ENCRYPT &&
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003982 operation->alg == PSA_ALG_CBC_NO_PADDING &&
3983 operation->ctx.cipher.unprocessed_len != 0 )
Moran Peker7cb22b82018-06-05 11:40:02 +03003984 {
Gilles Peskinedaea26f2018-08-21 14:02:45 +02003985 status = PSA_ERROR_INVALID_ARGUMENT;
Janos Follath315b51c2018-07-09 16:04:51 +01003986 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03003987 }
3988
Janos Follath315b51c2018-07-09 16:04:51 +01003989 cipher_ret = mbedtls_cipher_finish( &operation->ctx.cipher,
3990 temp_output_buffer,
3991 output_length );
3992 if( cipher_ret != 0 )
mohammad1603503973b2018-03-12 15:59:30 +02003993 {
Janos Follath315b51c2018-07-09 16:04:51 +01003994 status = mbedtls_to_psa_error( cipher_ret );
3995 goto error;
mohammad1603503973b2018-03-12 15:59:30 +02003996 }
Janos Follath315b51c2018-07-09 16:04:51 +01003997
Gilles Peskine46f1fd72018-06-28 19:31:31 +02003998 if( *output_length == 0 )
Janos Follath315b51c2018-07-09 16:04:51 +01003999 ; /* Nothing to copy. Note that output may be NULL in this case. */
Gilles Peskine46f1fd72018-06-28 19:31:31 +02004000 else if( output_size >= *output_length )
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004001 memcpy( output, temp_output_buffer, *output_length );
4002 else
4003 {
Janos Follath315b51c2018-07-09 16:04:51 +01004004 status = PSA_ERROR_BUFFER_TOO_SMALL;
4005 goto error;
Moran Pekerdc38ebc2018-04-30 15:45:34 +03004006 }
mohammad1603503973b2018-03-12 15:59:30 +02004007
Gilles Peskine3f108122018-12-07 18:14:53 +01004008 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004009 status = psa_cipher_abort( operation );
4010
4011 return( status );
4012
4013error:
4014
4015 *output_length = 0;
4016
Gilles Peskine3f108122018-12-07 18:14:53 +01004017 mbedtls_platform_zeroize( temp_output_buffer, sizeof( temp_output_buffer ) );
Janos Follath315b51c2018-07-09 16:04:51 +01004018 (void) psa_cipher_abort( operation );
4019
4020 return( status );
mohammad1603503973b2018-03-12 15:59:30 +02004021}
4022
Gilles Peskinee553c652018-06-04 16:22:46 +02004023psa_status_t psa_cipher_abort( psa_cipher_operation_t *operation )
4024{
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004025 if( operation->alg == 0 )
Gilles Peskine81736312018-06-26 15:04:31 +02004026 {
4027 /* The object has (apparently) been initialized but it is not
4028 * in use. It's ok to call abort on such an object, and there's
4029 * nothing to do. */
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004030 return( PSA_SUCCESS );
Gilles Peskine81736312018-06-26 15:04:31 +02004031 }
Gilles Peskine16c0f4f2018-06-20 16:05:20 +02004032
Gilles Peskinef9c2c092018-06-21 16:57:07 +02004033 /* Sanity check (shouldn't happen: operation->alg should
4034 * always have been initialized to a valid value). */
4035 if( ! PSA_ALG_IS_CIPHER( operation->alg ) )
4036 return( PSA_ERROR_BAD_STATE );
4037
mohammad1603503973b2018-03-12 15:59:30 +02004038 mbedtls_cipher_free( &operation->ctx.cipher );
Gilles Peskinee553c652018-06-04 16:22:46 +02004039
Moran Peker41deec42018-04-04 15:43:05 +03004040 operation->alg = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004041 operation->key_set = 0;
4042 operation->iv_set = 0;
4043 operation->iv_size = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004044 operation->block_size = 0;
Moran Pekerad9d82c2018-04-30 12:31:04 +03004045 operation->iv_required = 0;
Moran Peker41deec42018-04-04 15:43:05 +03004046
Moran Peker395db872018-05-31 14:07:14 +03004047 return( PSA_SUCCESS );
mohammad1603503973b2018-03-12 15:59:30 +02004048}
4049
Gilles Peskinea0655c32018-04-30 17:06:50 +02004050
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004051
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004052
mohammad16035955c982018-04-26 00:53:03 +03004053/****************************************************************/
4054/* AEAD */
4055/****************************************************************/
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004056
Gilles Peskineedf9a652018-08-17 18:11:56 +02004057typedef struct
4058{
Gilles Peskine2f060a82018-12-04 17:12:32 +01004059 psa_key_slot_t *slot;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004060 const mbedtls_cipher_info_t *cipher_info;
4061 union
4062 {
4063#if defined(MBEDTLS_CCM_C)
4064 mbedtls_ccm_context ccm;
4065#endif /* MBEDTLS_CCM_C */
4066#if defined(MBEDTLS_GCM_C)
4067 mbedtls_gcm_context gcm;
4068#endif /* MBEDTLS_GCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004069#if defined(MBEDTLS_CHACHAPOLY_C)
4070 mbedtls_chachapoly_context chachapoly;
4071#endif /* MBEDTLS_CHACHAPOLY_C */
Gilles Peskineedf9a652018-08-17 18:11:56 +02004072 } ctx;
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004073 psa_algorithm_t core_alg;
4074 uint8_t full_tag_length;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004075 uint8_t tag_length;
4076} aead_operation_t;
4077
Gilles Peskine30a9e412019-01-14 18:36:12 +01004078static void psa_aead_abort_internal( aead_operation_t *operation )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004079{
Gilles Peskinef8a8fe62018-08-21 16:38:05 +02004080 switch( operation->core_alg )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004081 {
4082#if defined(MBEDTLS_CCM_C)
4083 case PSA_ALG_CCM:
4084 mbedtls_ccm_free( &operation->ctx.ccm );
4085 break;
4086#endif /* MBEDTLS_CCM_C */
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004087#if defined(MBEDTLS_GCM_C)
Gilles Peskineedf9a652018-08-17 18:11:56 +02004088 case PSA_ALG_GCM:
4089 mbedtls_gcm_free( &operation->ctx.gcm );
4090 break;
4091#endif /* MBEDTLS_GCM_C */
4092 }
4093}
4094
4095static psa_status_t psa_aead_setup( aead_operation_t *operation,
Gilles Peskinec5487a82018-12-03 18:08:14 +01004096 psa_key_handle_t handle,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004097 psa_key_usage_t usage,
4098 psa_algorithm_t alg )
4099{
4100 psa_status_t status;
4101 size_t key_bits;
4102 mbedtls_cipher_id_t cipher_id;
4103
Gilles Peskine28f8f302019-07-24 13:30:31 +02004104 status = psa_get_transparent_key( handle, &operation->slot, usage, alg );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004105 if( status != PSA_SUCCESS )
4106 return( status );
4107
Gilles Peskinedb4b3ab2019-04-18 12:53:01 +02004108 key_bits = psa_get_key_slot_bits( operation->slot );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004109
4110 operation->cipher_info =
Gilles Peskine8e338702019-07-30 20:06:31 +02004111 mbedtls_cipher_info_from_psa( alg, operation->slot->attr.type, key_bits,
Gilles Peskineedf9a652018-08-17 18:11:56 +02004112 &cipher_id );
4113 if( operation->cipher_info == NULL )
4114 return( PSA_ERROR_NOT_SUPPORTED );
4115
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004116 switch( PSA_ALG_AEAD_WITH_TAG_LENGTH( alg, 0 ) )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004117 {
4118#if defined(MBEDTLS_CCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004119 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CCM, 0 ):
4120 operation->core_alg = PSA_ALG_CCM;
4121 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004122 /* CCM allows the following tag lengths: 4, 6, 8, 10, 12, 14, 16.
4123 * The call to mbedtls_ccm_encrypt_and_tag or
4124 * mbedtls_ccm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004125 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004126 return( PSA_ERROR_INVALID_ARGUMENT );
4127 mbedtls_ccm_init( &operation->ctx.ccm );
4128 status = mbedtls_to_psa_error(
4129 mbedtls_ccm_setkey( &operation->ctx.ccm, cipher_id,
4130 operation->slot->data.raw.data,
4131 (unsigned int) key_bits ) );
4132 if( status != 0 )
4133 goto cleanup;
4134 break;
4135#endif /* MBEDTLS_CCM_C */
4136
4137#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004138 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_GCM, 0 ):
4139 operation->core_alg = PSA_ALG_GCM;
4140 operation->full_tag_length = 16;
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004141 /* GCM allows the following tag lengths: 4, 8, 12, 13, 14, 15, 16.
4142 * The call to mbedtls_gcm_crypt_and_tag or
4143 * mbedtls_gcm_auth_decrypt will validate the tag length. */
Gilles Peskine8e338702019-07-30 20:06:31 +02004144 if( PSA_BLOCK_CIPHER_BLOCK_SIZE( operation->slot->attr.type ) != 16 )
Gilles Peskineedf9a652018-08-17 18:11:56 +02004145 return( PSA_ERROR_INVALID_ARGUMENT );
4146 mbedtls_gcm_init( &operation->ctx.gcm );
4147 status = mbedtls_to_psa_error(
4148 mbedtls_gcm_setkey( &operation->ctx.gcm, cipher_id,
4149 operation->slot->data.raw.data,
4150 (unsigned int) key_bits ) );
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004151 if( status != 0 )
4152 goto cleanup;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004153 break;
4154#endif /* MBEDTLS_GCM_C */
4155
Gilles Peskine26869f22019-05-06 15:25:00 +02004156#if defined(MBEDTLS_CHACHAPOLY_C)
4157 case PSA_ALG_AEAD_WITH_TAG_LENGTH( PSA_ALG_CHACHA20_POLY1305, 0 ):
4158 operation->core_alg = PSA_ALG_CHACHA20_POLY1305;
4159 operation->full_tag_length = 16;
4160 /* We only support the default tag length. */
4161 if( alg != PSA_ALG_CHACHA20_POLY1305 )
4162 return( PSA_ERROR_NOT_SUPPORTED );
4163 mbedtls_chachapoly_init( &operation->ctx.chachapoly );
4164 status = mbedtls_to_psa_error(
4165 mbedtls_chachapoly_setkey( &operation->ctx.chachapoly,
4166 operation->slot->data.raw.data ) );
4167 if( status != 0 )
4168 goto cleanup;
4169 break;
4170#endif /* MBEDTLS_CHACHAPOLY_C */
4171
Gilles Peskineedf9a652018-08-17 18:11:56 +02004172 default:
4173 return( PSA_ERROR_NOT_SUPPORTED );
4174 }
4175
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004176 if( PSA_AEAD_TAG_LENGTH( alg ) > operation->full_tag_length )
4177 {
4178 status = PSA_ERROR_INVALID_ARGUMENT;
4179 goto cleanup;
4180 }
4181 operation->tag_length = PSA_AEAD_TAG_LENGTH( alg );
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004182
Gilles Peskineedf9a652018-08-17 18:11:56 +02004183 return( PSA_SUCCESS );
4184
4185cleanup:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004186 psa_aead_abort_internal( operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004187 return( status );
4188}
4189
Gilles Peskinec5487a82018-12-03 18:08:14 +01004190psa_status_t psa_aead_encrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004191 psa_algorithm_t alg,
4192 const uint8_t *nonce,
4193 size_t nonce_length,
4194 const uint8_t *additional_data,
4195 size_t additional_data_length,
4196 const uint8_t *plaintext,
4197 size_t plaintext_length,
4198 uint8_t *ciphertext,
4199 size_t ciphertext_size,
4200 size_t *ciphertext_length )
4201{
mohammad16035955c982018-04-26 00:53:03 +03004202 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004203 aead_operation_t operation;
mohammad160315223a82018-06-03 17:19:55 +03004204 uint8_t *tag;
Gilles Peskine2d277862018-06-18 15:41:12 +02004205
mohammad1603f08a5502018-06-03 15:05:47 +03004206 *ciphertext_length = 0;
mohammad1603e58e6842018-05-09 04:58:32 -07004207
Gilles Peskinec5487a82018-12-03 18:08:14 +01004208 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_ENCRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004209 if( status != PSA_SUCCESS )
4210 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004211
Gilles Peskineedf9a652018-08-17 18:11:56 +02004212 /* For all currently supported modes, the tag is at the end of the
4213 * ciphertext. */
4214 if( ciphertext_size < ( plaintext_length + operation.tag_length ) )
4215 {
4216 status = PSA_ERROR_BUFFER_TOO_SMALL;
4217 goto exit;
4218 }
4219 tag = ciphertext + plaintext_length;
mohammad16035955c982018-04-26 00:53:03 +03004220
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004221#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004222 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004223 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004224 status = mbedtls_to_psa_error(
4225 mbedtls_gcm_crypt_and_tag( &operation.ctx.gcm,
4226 MBEDTLS_GCM_ENCRYPT,
4227 plaintext_length,
4228 nonce, nonce_length,
4229 additional_data, additional_data_length,
4230 plaintext, ciphertext,
4231 operation.tag_length, tag ) );
mohammad16035955c982018-04-26 00:53:03 +03004232 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004233 else
4234#endif /* MBEDTLS_GCM_C */
4235#if defined(MBEDTLS_CCM_C)
4236 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004237 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004238 status = mbedtls_to_psa_error(
4239 mbedtls_ccm_encrypt_and_tag( &operation.ctx.ccm,
4240 plaintext_length,
4241 nonce, nonce_length,
4242 additional_data,
4243 additional_data_length,
4244 plaintext, ciphertext,
4245 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004246 }
mohammad16035c8845f2018-05-09 05:40:09 -07004247 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004248#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004249#if defined(MBEDTLS_CHACHAPOLY_C)
4250 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4251 {
4252 if( nonce_length != 12 || operation.tag_length != 16 )
4253 {
4254 status = PSA_ERROR_NOT_SUPPORTED;
4255 goto exit;
4256 }
4257 status = mbedtls_to_psa_error(
4258 mbedtls_chachapoly_encrypt_and_tag( &operation.ctx.chachapoly,
4259 plaintext_length,
4260 nonce,
4261 additional_data,
4262 additional_data_length,
4263 plaintext,
4264 ciphertext,
4265 tag ) );
4266 }
4267 else
4268#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad16035c8845f2018-05-09 05:40:09 -07004269 {
mohammad1603554faad2018-06-03 15:07:38 +03004270 return( PSA_ERROR_NOT_SUPPORTED );
mohammad16035c8845f2018-05-09 05:40:09 -07004271 }
Gilles Peskine2d277862018-06-18 15:41:12 +02004272
Gilles Peskineedf9a652018-08-17 18:11:56 +02004273 if( status != PSA_SUCCESS && ciphertext_size != 0 )
4274 memset( ciphertext, 0, ciphertext_size );
Gilles Peskine2d277862018-06-18 15:41:12 +02004275
Gilles Peskineedf9a652018-08-17 18:11:56 +02004276exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004277 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004278 if( status == PSA_SUCCESS )
4279 *ciphertext_length = plaintext_length + operation.tag_length;
4280 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004281}
4282
Gilles Peskineee652a32018-06-01 19:23:52 +02004283/* Locate the tag in a ciphertext buffer containing the encrypted data
4284 * followed by the tag. Return the length of the part preceding the tag in
4285 * *plaintext_length. This is the size of the plaintext in modes where
4286 * the encrypted data has the same size as the plaintext, such as
4287 * CCM and GCM. */
4288static psa_status_t psa_aead_unpadded_locate_tag( size_t tag_length,
4289 const uint8_t *ciphertext,
4290 size_t ciphertext_length,
4291 size_t plaintext_size,
Gilles Peskineee652a32018-06-01 19:23:52 +02004292 const uint8_t **p_tag )
4293{
4294 size_t payload_length;
4295 if( tag_length > ciphertext_length )
4296 return( PSA_ERROR_INVALID_ARGUMENT );
4297 payload_length = ciphertext_length - tag_length;
4298 if( payload_length > plaintext_size )
4299 return( PSA_ERROR_BUFFER_TOO_SMALL );
4300 *p_tag = ciphertext + payload_length;
Gilles Peskineee652a32018-06-01 19:23:52 +02004301 return( PSA_SUCCESS );
4302}
4303
Gilles Peskinec5487a82018-12-03 18:08:14 +01004304psa_status_t psa_aead_decrypt( psa_key_handle_t handle,
mohammad16035955c982018-04-26 00:53:03 +03004305 psa_algorithm_t alg,
4306 const uint8_t *nonce,
4307 size_t nonce_length,
4308 const uint8_t *additional_data,
4309 size_t additional_data_length,
4310 const uint8_t *ciphertext,
4311 size_t ciphertext_length,
4312 uint8_t *plaintext,
4313 size_t plaintext_size,
4314 size_t *plaintext_length )
4315{
mohammad16035955c982018-04-26 00:53:03 +03004316 psa_status_t status;
Gilles Peskineedf9a652018-08-17 18:11:56 +02004317 aead_operation_t operation;
4318 const uint8_t *tag = NULL;
Gilles Peskine2d277862018-06-18 15:41:12 +02004319
Gilles Peskineee652a32018-06-01 19:23:52 +02004320 *plaintext_length = 0;
mohammad16039e5a5152018-04-26 12:07:35 +03004321
Gilles Peskinec5487a82018-12-03 18:08:14 +01004322 status = psa_aead_setup( &operation, handle, PSA_KEY_USAGE_DECRYPT, alg );
Gilles Peskineb0b255c2018-07-06 17:01:38 +02004323 if( status != PSA_SUCCESS )
4324 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004325
Gilles Peskinef7e7b012019-05-06 15:27:16 +02004326 status = psa_aead_unpadded_locate_tag( operation.tag_length,
4327 ciphertext, ciphertext_length,
4328 plaintext_size, &tag );
4329 if( status != PSA_SUCCESS )
4330 goto exit;
4331
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004332#if defined(MBEDTLS_GCM_C)
Gilles Peskine23cc2ff2018-08-17 19:47:52 +02004333 if( operation.core_alg == PSA_ALG_GCM )
mohammad16035955c982018-04-26 00:53:03 +03004334 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004335 status = mbedtls_to_psa_error(
4336 mbedtls_gcm_auth_decrypt( &operation.ctx.gcm,
4337 ciphertext_length - operation.tag_length,
4338 nonce, nonce_length,
4339 additional_data,
4340 additional_data_length,
4341 tag, operation.tag_length,
4342 ciphertext, plaintext ) );
mohammad16035955c982018-04-26 00:53:03 +03004343 }
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004344 else
4345#endif /* MBEDTLS_GCM_C */
4346#if defined(MBEDTLS_CCM_C)
4347 if( operation.core_alg == PSA_ALG_CCM )
mohammad16035955c982018-04-26 00:53:03 +03004348 {
Gilles Peskineedf9a652018-08-17 18:11:56 +02004349 status = mbedtls_to_psa_error(
4350 mbedtls_ccm_auth_decrypt( &operation.ctx.ccm,
4351 ciphertext_length - operation.tag_length,
4352 nonce, nonce_length,
4353 additional_data,
4354 additional_data_length,
4355 ciphertext, plaintext,
4356 tag, operation.tag_length ) );
mohammad16035955c982018-04-26 00:53:03 +03004357 }
mohammad160339574652018-06-01 04:39:53 -07004358 else
Gilles Peskineb0b189f2018-11-28 17:30:58 +01004359#endif /* MBEDTLS_CCM_C */
Gilles Peskine26869f22019-05-06 15:25:00 +02004360#if defined(MBEDTLS_CHACHAPOLY_C)
4361 if( operation.core_alg == PSA_ALG_CHACHA20_POLY1305 )
4362 {
4363 if( nonce_length != 12 || operation.tag_length != 16 )
4364 {
4365 status = PSA_ERROR_NOT_SUPPORTED;
4366 goto exit;
4367 }
4368 status = mbedtls_to_psa_error(
4369 mbedtls_chachapoly_auth_decrypt( &operation.ctx.chachapoly,
4370 ciphertext_length - operation.tag_length,
4371 nonce,
4372 additional_data,
4373 additional_data_length,
4374 tag,
4375 ciphertext,
4376 plaintext ) );
4377 }
4378 else
4379#endif /* MBEDTLS_CHACHAPOLY_C */
mohammad160339574652018-06-01 04:39:53 -07004380 {
mohammad1603554faad2018-06-03 15:07:38 +03004381 return( PSA_ERROR_NOT_SUPPORTED );
mohammad160339574652018-06-01 04:39:53 -07004382 }
Gilles Peskinea40d7742018-06-01 16:28:30 +02004383
Gilles Peskineedf9a652018-08-17 18:11:56 +02004384 if( status != PSA_SUCCESS && plaintext_size != 0 )
4385 memset( plaintext, 0, plaintext_size );
mohammad160360a64d02018-06-03 17:20:42 +03004386
Gilles Peskineedf9a652018-08-17 18:11:56 +02004387exit:
Gilles Peskine30a9e412019-01-14 18:36:12 +01004388 psa_aead_abort_internal( &operation );
Gilles Peskineedf9a652018-08-17 18:11:56 +02004389 if( status == PSA_SUCCESS )
4390 *plaintext_length = ciphertext_length - operation.tag_length;
4391 return( status );
mohammad16035955c982018-04-26 00:53:03 +03004392}
4393
Gilles Peskinea0655c32018-04-30 17:06:50 +02004394
Gilles Peskine7bcfc0a2018-06-18 21:49:39 +02004395
Gilles Peskine20035e32018-02-03 22:44:14 +01004396/****************************************************************/
Gilles Peskineeab56e42018-07-12 17:12:33 +02004397/* Generators */
4398/****************************************************************/
4399
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004400#define HKDF_STATE_INIT 0 /* no input yet */
4401#define HKDF_STATE_STARTED 1 /* got salt */
4402#define HKDF_STATE_KEYED 2 /* got key */
4403#define HKDF_STATE_OUTPUT 3 /* output started */
4404
Gilles Peskinecbe66502019-05-16 16:59:18 +02004405static psa_algorithm_t psa_key_derivation_get_kdf_alg(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004406 const psa_key_derivation_operation_t *operation )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004407{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004408 if ( PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
4409 return( PSA_ALG_KEY_AGREEMENT_GET_KDF( operation->alg ) );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004410 else
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004411 return( operation->alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004412}
4413
4414
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004415psa_status_t psa_key_derivation_abort( psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004416{
4417 psa_status_t status = PSA_SUCCESS;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004418 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004419 if( kdf_alg == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004420 {
4421 /* The object has (apparently) been initialized but it is not
4422 * in use. It's ok to call abort on such an object, and there's
4423 * nothing to do. */
4424 }
4425 else
Gilles Peskinebef7f142018-07-12 17:22:21 +02004426#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004427 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004428 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004429 mbedtls_free( operation->ctx.hkdf.info );
4430 status = psa_hmac_abort_internal( &operation->ctx.hkdf.hmac );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004431 }
Gilles Peskine969c5d62019-01-16 15:53:06 +01004432 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004433 /* TLS-1.2 PSK-to-MS KDF uses the same core as TLS-1.2 PRF */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004434 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004435 {
Janos Follath6a1d2622019-06-11 10:37:28 +01004436 if( operation->ctx.tls12_prf.seed != NULL )
4437 {
4438 mbedtls_platform_zeroize( operation->ctx.tls12_prf.seed,
4439 operation->ctx.tls12_prf.seed_length );
4440 mbedtls_free( operation->ctx.tls12_prf.seed );
4441 }
4442
4443 if( operation->ctx.tls12_prf.label != NULL )
4444 {
4445 mbedtls_platform_zeroize( operation->ctx.tls12_prf.label,
4446 operation->ctx.tls12_prf.label_length );
4447 mbedtls_free( operation->ctx.tls12_prf.label );
4448 }
4449
4450 status = psa_hmac_abort_internal( &operation->ctx.tls12_prf.hmac );
4451
4452 /* We leave the fields Ai and output_block to be erased safely by the
4453 * mbedtls_platform_zeroize() in the end of this function. */
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004454 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004455 else
4456#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004457 {
4458 status = PSA_ERROR_BAD_STATE;
4459 }
Janos Follath083036a2019-06-11 10:22:26 +01004460 mbedtls_platform_zeroize( operation, sizeof( *operation ) );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004461 return( status );
4462}
4463
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004464psa_status_t psa_key_derivation_get_capacity(const psa_key_derivation_operation_t *operation,
Gilles Peskineeab56e42018-07-12 17:12:33 +02004465 size_t *capacity)
4466{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004467 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004468 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004469 /* This is a blank key derivation operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004470 return PSA_ERROR_BAD_STATE;
4471 }
4472
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004473 *capacity = operation->capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004474 return( PSA_SUCCESS );
4475}
4476
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004477psa_status_t psa_key_derivation_set_capacity( psa_key_derivation_operation_t *operation,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004478 size_t capacity )
4479{
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004480 if( operation->alg == 0 )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004481 return( PSA_ERROR_BAD_STATE );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004482 if( capacity > operation->capacity )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004483 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004484 operation->capacity = capacity;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004485 return( PSA_SUCCESS );
4486}
4487
Gilles Peskinebef7f142018-07-12 17:22:21 +02004488#if defined(MBEDTLS_MD_C)
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004489/* Read some bytes from an HKDF-based operation. This performs a chunk
Gilles Peskinebef7f142018-07-12 17:22:21 +02004490 * of the expand phase of the HKDF algorithm. */
Gilles Peskinecbe66502019-05-16 16:59:18 +02004491static psa_status_t psa_key_derivation_hkdf_read( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004492 psa_algorithm_t hash_alg,
4493 uint8_t *output,
4494 size_t output_length )
4495{
4496 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4497 psa_status_t status;
4498
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004499 if( hkdf->state < HKDF_STATE_KEYED || ! hkdf->info_set )
4500 return( PSA_ERROR_BAD_STATE );
4501 hkdf->state = HKDF_STATE_OUTPUT;
4502
Gilles Peskinebef7f142018-07-12 17:22:21 +02004503 while( output_length != 0 )
4504 {
4505 /* Copy what remains of the current block */
4506 uint8_t n = hash_length - hkdf->offset_in_block;
4507 if( n > output_length )
4508 n = (uint8_t) output_length;
4509 memcpy( output, hkdf->output_block + hkdf->offset_in_block, n );
4510 output += n;
4511 output_length -= n;
4512 hkdf->offset_in_block += n;
Gilles Peskined54931c2018-07-17 21:06:59 +02004513 if( output_length == 0 )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004514 break;
Gilles Peskined54931c2018-07-17 21:06:59 +02004515 /* We can't be wanting more output after block 0xff, otherwise
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004516 * the capacity check in psa_key_derivation_output_bytes() would have
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004517 * prevented this call. It could happen only if the operation
Gilles Peskined54931c2018-07-17 21:06:59 +02004518 * object was corrupted or if this function is called directly
4519 * inside the library. */
4520 if( hkdf->block_number == 0xff )
4521 return( PSA_ERROR_BAD_STATE );
Gilles Peskinebef7f142018-07-12 17:22:21 +02004522
4523 /* We need a new block */
4524 ++hkdf->block_number;
4525 hkdf->offset_in_block = 0;
4526 status = psa_hmac_setup_internal( &hkdf->hmac,
4527 hkdf->prk, hash_length,
4528 hash_alg );
4529 if( status != PSA_SUCCESS )
4530 return( status );
4531 if( hkdf->block_number != 1 )
4532 {
4533 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4534 hkdf->output_block,
4535 hash_length );
4536 if( status != PSA_SUCCESS )
4537 return( status );
4538 }
4539 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4540 hkdf->info,
4541 hkdf->info_length );
4542 if( status != PSA_SUCCESS )
4543 return( status );
4544 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4545 &hkdf->block_number, 1 );
4546 if( status != PSA_SUCCESS )
4547 return( status );
4548 status = psa_hmac_finish_internal( &hkdf->hmac,
4549 hkdf->output_block,
4550 sizeof( hkdf->output_block ) );
4551 if( status != PSA_SUCCESS )
4552 return( status );
4553 }
4554
4555 return( PSA_SUCCESS );
4556}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004557
Janos Follath7742fee2019-06-17 12:58:10 +01004558static psa_status_t psa_key_derivation_tls12_prf_generate_next_block(
4559 psa_tls12_prf_key_derivation_t *tls12_prf,
4560 psa_algorithm_t alg )
4561{
4562 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( alg );
4563 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
Janos Follathea29bfb2019-06-19 12:21:20 +01004564 psa_hash_operation_t backup = PSA_HASH_OPERATION_INIT;
4565 psa_status_t status, cleanup_status;
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004566
Janos Follath7742fee2019-06-17 12:58:10 +01004567 /* We can't be wanting more output after block 0xff, otherwise
4568 * the capacity check in psa_key_derivation_output_bytes() would have
4569 * prevented this call. It could happen only if the operation
4570 * object was corrupted or if this function is called directly
4571 * inside the library. */
4572 if( tls12_prf->block_number == 0xff )
Janos Follath30090bc2019-06-25 10:15:04 +01004573 return( PSA_ERROR_CORRUPTION_DETECTED );
Janos Follath7742fee2019-06-17 12:58:10 +01004574
4575 /* We need a new block */
4576 ++tls12_prf->block_number;
Janos Follath844eb0e2019-06-19 12:10:49 +01004577 tls12_prf->left_in_block = hash_length;
Janos Follath7742fee2019-06-17 12:58:10 +01004578
4579 /* Recall the definition of the TLS-1.2-PRF from RFC 5246:
4580 *
4581 * PRF(secret, label, seed) = P_<hash>(secret, label + seed)
4582 *
4583 * P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) +
4584 * HMAC_hash(secret, A(2) + seed) +
4585 * HMAC_hash(secret, A(3) + seed) + ...
4586 *
4587 * A(0) = seed
Janos Follathea29bfb2019-06-19 12:21:20 +01004588 * A(i) = HMAC_hash(secret, A(i-1))
Janos Follath7742fee2019-06-17 12:58:10 +01004589 *
Janos Follathea29bfb2019-06-19 12:21:20 +01004590 * The `psa_tls12_prf_key_derivation` structure saves the block
Janos Follath7742fee2019-06-17 12:58:10 +01004591 * `HMAC_hash(secret, A(i) + seed)` from which the output
Janos Follath76c39842019-06-26 12:50:36 +01004592 * is currently extracted as `output_block` and where i is
4593 * `block_number`.
Janos Follath7742fee2019-06-17 12:58:10 +01004594 */
4595
Janos Follathea29bfb2019-06-19 12:21:20 +01004596 /* Save the hash context before using it, to preserve the hash state with
4597 * only the inner padding in it. We need this, because inner padding depends
4598 * on the key (secret in the RFC's terminology). */
4599 status = psa_hash_clone( &tls12_prf->hmac.hash_ctx, &backup );
4600 if( status != PSA_SUCCESS )
4601 goto cleanup;
4602
4603 /* Calculate A(i) where i = tls12_prf->block_number. */
4604 if( tls12_prf->block_number == 1 )
4605 {
4606 /* A(1) = HMAC_hash(secret, A(0)), where A(0) = seed. (The RFC overloads
4607 * the variable seed and in this instance means it in the context of the
4608 * P_hash function, where seed = label + seed.) */
4609 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4610 tls12_prf->label, tls12_prf->label_length );
4611 if( status != PSA_SUCCESS )
4612 goto cleanup;
4613 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4614 tls12_prf->seed, tls12_prf->seed_length );
4615 if( status != PSA_SUCCESS )
4616 goto cleanup;
4617 }
4618 else
4619 {
4620 /* A(i) = HMAC_hash(secret, A(i-1)) */
4621 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4622 tls12_prf->Ai, hash_length );
4623 if( status != PSA_SUCCESS )
4624 goto cleanup;
4625 }
4626
4627 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4628 tls12_prf->Ai, hash_length );
4629 if( status != PSA_SUCCESS )
4630 goto cleanup;
4631 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4632 if( status != PSA_SUCCESS )
4633 goto cleanup;
4634
4635 /* Calculate HMAC_hash(secret, A(i) + label + seed). */
4636 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4637 tls12_prf->Ai, hash_length );
4638 if( status != PSA_SUCCESS )
4639 goto cleanup;
4640 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4641 tls12_prf->label, tls12_prf->label_length );
4642 if( status != PSA_SUCCESS )
4643 goto cleanup;
4644 status = psa_hash_update( &tls12_prf->hmac.hash_ctx,
4645 tls12_prf->seed, tls12_prf->seed_length );
4646 if( status != PSA_SUCCESS )
4647 goto cleanup;
4648 status = psa_hmac_finish_internal( &tls12_prf->hmac,
4649 tls12_prf->output_block, hash_length );
4650 if( status != PSA_SUCCESS )
4651 goto cleanup;
4652 status = psa_hash_clone( &backup, &tls12_prf->hmac.hash_ctx );
4653 if( status != PSA_SUCCESS )
4654 goto cleanup;
4655
Janos Follath7742fee2019-06-17 12:58:10 +01004656
4657cleanup:
4658
Janos Follathea29bfb2019-06-19 12:21:20 +01004659 cleanup_status = psa_hash_abort( &backup );
4660 if( status == PSA_SUCCESS && cleanup_status != PSA_SUCCESS )
4661 status = cleanup_status;
4662
4663 return( status );
Janos Follath7742fee2019-06-17 12:58:10 +01004664}
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004665
Janos Follath844eb0e2019-06-19 12:10:49 +01004666static psa_status_t psa_key_derivation_tls12_prf_read(
4667 psa_tls12_prf_key_derivation_t *tls12_prf,
4668 psa_algorithm_t alg,
4669 uint8_t *output,
4670 size_t output_length )
4671{
4672 psa_algorithm_t hash_alg = PSA_ALG_TLS12_PRF_GET_HASH( alg );
4673 uint8_t hash_length = PSA_HASH_SIZE( hash_alg );
4674 psa_status_t status;
4675 uint8_t offset, length;
4676
4677 while( output_length != 0 )
4678 {
4679 /* Check if we have fully processed the current block. */
4680 if( tls12_prf->left_in_block == 0 )
4681 {
4682 status = psa_key_derivation_tls12_prf_generate_next_block( tls12_prf,
4683 alg );
4684 if( status != PSA_SUCCESS )
4685 return( status );
4686
4687 continue;
4688 }
4689
4690 if( tls12_prf->left_in_block > output_length )
4691 length = (uint8_t) output_length;
4692 else
4693 length = tls12_prf->left_in_block;
4694
4695 offset = hash_length - tls12_prf->left_in_block;
4696 memcpy( output, tls12_prf->output_block + offset, length );
4697 output += length;
4698 output_length -= length;
4699 tls12_prf->left_in_block -= length;
4700 }
4701
4702 return( PSA_SUCCESS );
4703}
Gilles Peskinebef7f142018-07-12 17:22:21 +02004704#endif /* MBEDTLS_MD_C */
4705
Janos Follath6c6c8fc2019-06-17 12:38:20 +01004706psa_status_t psa_key_derivation_output_bytes(
4707 psa_key_derivation_operation_t *operation,
4708 uint8_t *output,
4709 size_t output_length )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004710{
4711 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004712 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004713
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004714 if( operation->alg == 0 )
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004715 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004716 /* This is a blank operation. */
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004717 return PSA_ERROR_BAD_STATE;
4718 }
4719
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004720 if( output_length > operation->capacity )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004721 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004722 operation->capacity = 0;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004723 /* Go through the error path to wipe all confidential data now
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004724 * that the operation object is useless. */
David Saadab4ecc272019-02-14 13:48:10 +02004725 status = PSA_ERROR_INSUFFICIENT_DATA;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004726 goto exit;
4727 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004728 if( output_length == 0 && operation->capacity == 0 )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004729 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004730 /* Edge case: this is a finished operation, and 0 bytes
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004731 * were requested. The right error in this case could
Gilles Peskineeab56e42018-07-12 17:12:33 +02004732 * be either INSUFFICIENT_CAPACITY or BAD_STATE. Return
4733 * INSUFFICIENT_CAPACITY, which is right for a finished
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004734 * operation, for consistency with the case when
Gilles Peskineeab56e42018-07-12 17:12:33 +02004735 * output_length > 0. */
David Saadab4ecc272019-02-14 13:48:10 +02004736 return( PSA_ERROR_INSUFFICIENT_DATA );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004737 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004738 operation->capacity -= output_length;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004739
Gilles Peskinebef7f142018-07-12 17:22:21 +02004740#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004741 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskinebef7f142018-07-12 17:22:21 +02004742 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004743 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004744 status = psa_key_derivation_hkdf_read( &operation->ctx.hkdf, hash_alg,
Gilles Peskinebef7f142018-07-12 17:22:21 +02004745 output, output_length );
4746 }
Janos Follathadbec812019-06-14 11:05:39 +01004747 else
Janos Follathadbec812019-06-14 11:05:39 +01004748 if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
Gilles Peskine969c5d62019-01-16 15:53:06 +01004749 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004750 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004751 status = psa_key_derivation_tls12_prf_read( &operation->ctx.tls12_prf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004752 kdf_alg, output,
Hanno Beckerc8a41d72018-10-09 17:33:01 +01004753 output_length );
4754 }
Gilles Peskinebef7f142018-07-12 17:22:21 +02004755 else
4756#endif /* MBEDTLS_MD_C */
Gilles Peskineeab56e42018-07-12 17:12:33 +02004757 {
4758 return( PSA_ERROR_BAD_STATE );
4759 }
4760
4761exit:
4762 if( status != PSA_SUCCESS )
4763 {
Jaeden Amerocf2010c2019-02-15 13:05:49 +00004764 /* Preserve the algorithm upon errors, but clear all sensitive state.
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004765 * This allows us to differentiate between exhausted operations and
4766 * blank operations, so we can return PSA_ERROR_BAD_STATE on blank
4767 * operations. */
4768 psa_algorithm_t alg = operation->alg;
4769 psa_key_derivation_abort( operation );
4770 operation->alg = alg;
Gilles Peskineeab56e42018-07-12 17:12:33 +02004771 memset( output, '!', output_length );
4772 }
4773 return( status );
4774}
4775
Gilles Peskine08542d82018-07-19 17:05:42 +02004776#if defined(MBEDTLS_DES_C)
4777static void psa_des_set_key_parity( uint8_t *data, size_t data_size )
4778{
4779 if( data_size >= 8 )
4780 mbedtls_des_key_set_parity( data );
4781 if( data_size >= 16 )
4782 mbedtls_des_key_set_parity( data + 8 );
4783 if( data_size >= 24 )
4784 mbedtls_des_key_set_parity( data + 16 );
4785}
4786#endif /* MBEDTLS_DES_C */
4787
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004788static psa_status_t psa_generate_derived_key_internal(
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004789 psa_key_slot_t *slot,
4790 size_t bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004791 psa_key_derivation_operation_t *operation )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004792{
4793 uint8_t *data = NULL;
4794 size_t bytes = PSA_BITS_TO_BYTES( bits );
4795 psa_status_t status;
4796
Gilles Peskine8e338702019-07-30 20:06:31 +02004797 if( ! key_type_is_raw_bytes( slot->attr.type ) )
Gilles Peskineeab56e42018-07-12 17:12:33 +02004798 return( PSA_ERROR_INVALID_ARGUMENT );
4799 if( bits % 8 != 0 )
4800 return( PSA_ERROR_INVALID_ARGUMENT );
4801 data = mbedtls_calloc( 1, bytes );
4802 if( data == NULL )
4803 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4804
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004805 status = psa_key_derivation_output_bytes( operation, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004806 if( status != PSA_SUCCESS )
4807 goto exit;
Gilles Peskine08542d82018-07-19 17:05:42 +02004808#if defined(MBEDTLS_DES_C)
Gilles Peskine8e338702019-07-30 20:06:31 +02004809 if( slot->attr.type == PSA_KEY_TYPE_DES )
Gilles Peskine08542d82018-07-19 17:05:42 +02004810 psa_des_set_key_parity( data, bytes );
4811#endif /* MBEDTLS_DES_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004812 status = psa_import_key_into_slot( slot, data, bytes );
Gilles Peskineeab56e42018-07-12 17:12:33 +02004813
4814exit:
4815 mbedtls_free( data );
4816 return( status );
4817}
4818
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02004819psa_status_t psa_key_derivation_output_key( const psa_key_attributes_t *attributes,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004820 psa_key_derivation_operation_t *operation,
Gilles Peskine98dd7792019-05-15 19:43:49 +02004821 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004822{
4823 psa_status_t status;
4824 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02004825 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine0f84d622019-09-12 19:03:13 +02004826
4827 /* Reject any attempt to create a zero-length key so that we don't
4828 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
4829 if( psa_get_key_bits( attributes ) == 0 )
4830 return( PSA_ERROR_INVALID_ARGUMENT );
4831
Gilles Peskine178c9aa2019-09-24 18:21:06 +02004832 if( ! operation->can_output_key )
4833 return( PSA_ERROR_NOT_PERMITTED );
4834
Gilles Peskinedf179142019-07-15 22:02:14 +02004835 status = psa_start_key_creation( PSA_KEY_CREATION_DERIVE,
4836 attributes, handle, &slot, &driver );
Gilles Peskinef4ee6622019-07-24 13:44:30 +02004837#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
4838 if( driver != NULL )
4839 {
4840 /* Deriving a key in a secure element is not implemented yet. */
4841 status = PSA_ERROR_NOT_SUPPORTED;
4842 }
4843#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004844 if( status == PSA_SUCCESS )
4845 {
Adrian L. Shaw5a5a79a2019-05-03 15:44:28 +01004846 status = psa_generate_derived_key_internal( slot,
Gilles Peskine7e0cff92019-07-30 13:48:52 +02004847 attributes->core.bits,
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004848 operation );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004849 }
4850 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02004851 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004852 if( status != PSA_SUCCESS )
4853 {
Gilles Peskine011e4282019-06-26 18:34:38 +02004854 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02004855 *handle = 0;
4856 }
4857 return( status );
4858}
4859
Gilles Peskineeab56e42018-07-12 17:12:33 +02004860
4861
4862/****************************************************************/
Gilles Peskineea0fb492018-07-12 17:17:20 +02004863/* Key derivation */
4864/****************************************************************/
4865
Gilles Peskine969c5d62019-01-16 15:53:06 +01004866static psa_status_t psa_key_derivation_setup_kdf(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004867 psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004868 psa_algorithm_t kdf_alg )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004869{
Janos Follath5fe19732019-06-20 15:09:30 +01004870 /* Make sure that operation->ctx is properly zero-initialised. (Macro
4871 * initialisers for this union leave some bytes unspecified.) */
4872 memset( &operation->ctx, 0, sizeof( operation->ctx ) );
4873
Gilles Peskine969c5d62019-01-16 15:53:06 +01004874 /* Make sure that kdf_alg is a supported key derivation algorithm. */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004875#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01004876 if( PSA_ALG_IS_HKDF( kdf_alg ) ||
4877 PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4878 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004879 {
Gilles Peskine969c5d62019-01-16 15:53:06 +01004880 psa_algorithm_t hash_alg = PSA_ALG_HKDF_GET_HASH( kdf_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004881 size_t hash_size = PSA_HASH_SIZE( hash_alg );
4882 if( hash_size == 0 )
4883 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004884 if( ( PSA_ALG_IS_TLS12_PRF( kdf_alg ) ||
4885 PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) ) &&
Gilles Peskineab4b2012019-04-12 15:06:27 +02004886 ! ( hash_alg == PSA_ALG_SHA_256 || hash_alg == PSA_ALG_SHA_384 ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004887 {
4888 return( PSA_ERROR_NOT_SUPPORTED );
4889 }
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004890 operation->capacity = 255 * hash_size;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004891 return( PSA_SUCCESS );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004892 }
4893#endif /* MBEDTLS_MD_C */
Gilles Peskine969c5d62019-01-16 15:53:06 +01004894 else
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004895 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004896}
4897
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004898psa_status_t psa_key_derivation_setup( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01004899 psa_algorithm_t alg )
4900{
4901 psa_status_t status;
4902
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004903 if( operation->alg != 0 )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004904 return( PSA_ERROR_BAD_STATE );
4905
Gilles Peskine6843c292019-01-18 16:44:49 +01004906 if( PSA_ALG_IS_RAW_KEY_AGREEMENT( alg ) )
4907 return( PSA_ERROR_INVALID_ARGUMENT );
4908 else if( PSA_ALG_IS_KEY_AGREEMENT( alg ) )
Gilles Peskine969c5d62019-01-16 15:53:06 +01004909 {
4910 psa_algorithm_t kdf_alg = PSA_ALG_KEY_AGREEMENT_GET_KDF( alg );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004911 status = psa_key_derivation_setup_kdf( operation, kdf_alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004912 }
4913 else if( PSA_ALG_IS_KEY_DERIVATION( alg ) )
4914 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004915 status = psa_key_derivation_setup_kdf( operation, alg );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004916 }
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004917 else
4918 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine969c5d62019-01-16 15:53:06 +01004919
4920 if( status == PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02004921 operation->alg = alg;
Gilles Peskine969c5d62019-01-16 15:53:06 +01004922 return( status );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004923}
4924
4925#if defined(MBEDTLS_MD_C)
Gilles Peskinecbe66502019-05-16 16:59:18 +02004926static psa_status_t psa_hkdf_input( psa_hkdf_key_derivation_t *hkdf,
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004927 psa_algorithm_t hash_alg,
4928 psa_key_derivation_step_t step,
4929 const uint8_t *data,
4930 size_t data_length )
4931{
4932 psa_status_t status;
4933 switch( step )
4934 {
Gilles Peskine03410b52019-05-16 16:05:19 +02004935 case PSA_KEY_DERIVATION_INPUT_SALT:
Gilles Peskine2b522db2019-04-12 15:11:49 +02004936 if( hkdf->state != HKDF_STATE_INIT )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004937 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004938 status = psa_hmac_setup_internal( &hkdf->hmac,
4939 data, data_length,
4940 hash_alg );
4941 if( status != PSA_SUCCESS )
4942 return( status );
4943 hkdf->state = HKDF_STATE_STARTED;
4944 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004945 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004946 /* If no salt was provided, use an empty salt. */
4947 if( hkdf->state == HKDF_STATE_INIT )
4948 {
4949 status = psa_hmac_setup_internal( &hkdf->hmac,
4950 NULL, 0,
Gilles Peskinef9ee6332019-04-11 21:22:52 +02004951 hash_alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004952 if( status != PSA_SUCCESS )
4953 return( status );
4954 hkdf->state = HKDF_STATE_STARTED;
4955 }
Gilles Peskine2b522db2019-04-12 15:11:49 +02004956 if( hkdf->state != HKDF_STATE_STARTED )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004957 return( PSA_ERROR_BAD_STATE );
Gilles Peskine2b522db2019-04-12 15:11:49 +02004958 status = psa_hash_update( &hkdf->hmac.hash_ctx,
4959 data, data_length );
4960 if( status != PSA_SUCCESS )
4961 return( status );
4962 status = psa_hmac_finish_internal( &hkdf->hmac,
4963 hkdf->prk,
4964 sizeof( hkdf->prk ) );
4965 if( status != PSA_SUCCESS )
4966 return( status );
4967 hkdf->offset_in_block = PSA_HASH_SIZE( hash_alg );
4968 hkdf->block_number = 0;
4969 hkdf->state = HKDF_STATE_KEYED;
4970 return( PSA_SUCCESS );
Gilles Peskine03410b52019-05-16 16:05:19 +02004971 case PSA_KEY_DERIVATION_INPUT_INFO:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01004972 if( hkdf->state == HKDF_STATE_OUTPUT )
4973 return( PSA_ERROR_BAD_STATE );
4974 if( hkdf->info_set )
4975 return( PSA_ERROR_BAD_STATE );
4976 hkdf->info_length = data_length;
4977 if( data_length != 0 )
4978 {
4979 hkdf->info = mbedtls_calloc( 1, data_length );
4980 if( hkdf->info == NULL )
4981 return( PSA_ERROR_INSUFFICIENT_MEMORY );
4982 memcpy( hkdf->info, data, data_length );
4983 }
4984 hkdf->info_set = 1;
4985 return( PSA_SUCCESS );
4986 default:
4987 return( PSA_ERROR_INVALID_ARGUMENT );
4988 }
4989}
Janos Follathb03233e2019-06-11 15:30:30 +01004990
Janos Follathf08e2652019-06-13 09:05:41 +01004991static psa_status_t psa_tls12_prf_set_seed( psa_tls12_prf_key_derivation_t *prf,
4992 const uint8_t *data,
4993 size_t data_length )
4994{
4995 if( prf->state != TLS12_PRF_STATE_INIT )
4996 return( PSA_ERROR_BAD_STATE );
4997
Janos Follathd6dce9f2019-07-04 09:11:38 +01004998 if( data_length != 0 )
4999 {
5000 prf->seed = mbedtls_calloc( 1, data_length );
5001 if( prf->seed == NULL )
5002 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follathf08e2652019-06-13 09:05:41 +01005003
Janos Follathd6dce9f2019-07-04 09:11:38 +01005004 memcpy( prf->seed, data, data_length );
5005 prf->seed_length = data_length;
5006 }
Janos Follathf08e2652019-06-13 09:05:41 +01005007
5008 prf->state = TLS12_PRF_STATE_SEED_SET;
5009
5010 return( PSA_SUCCESS );
5011}
5012
Janos Follath81550542019-06-13 14:26:34 +01005013static psa_status_t psa_tls12_prf_set_key( psa_tls12_prf_key_derivation_t *prf,
5014 psa_algorithm_t hash_alg,
5015 const uint8_t *data,
5016 size_t data_length )
5017{
5018 psa_status_t status;
5019 if( prf->state != TLS12_PRF_STATE_SEED_SET )
5020 return( PSA_ERROR_BAD_STATE );
5021
5022 status = psa_hmac_setup_internal( &prf->hmac, data, data_length, hash_alg );
5023 if( status != PSA_SUCCESS )
5024 return( status );
5025
5026 prf->state = TLS12_PRF_STATE_KEY_SET;
5027
5028 return( PSA_SUCCESS );
5029}
5030
Janos Follath6660f0e2019-06-17 08:44:03 +01005031static psa_status_t psa_tls12_prf_psk_to_ms_set_key(
5032 psa_tls12_prf_key_derivation_t *prf,
5033 psa_algorithm_t hash_alg,
5034 const uint8_t *data,
5035 size_t data_length )
5036{
5037 psa_status_t status;
Gilles Peskinec11c4dc2019-07-15 11:06:38 +02005038 uint8_t pms[ 4 + 2 * PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN ];
5039 uint8_t *cur = pms;
Janos Follath6660f0e2019-06-17 08:44:03 +01005040
5041 if( data_length > PSA_ALG_TLS12_PSK_TO_MS_MAX_PSK_LEN )
5042 return( PSA_ERROR_INVALID_ARGUMENT );
5043
5044 /* Quoting RFC 4279, Section 2:
5045 *
5046 * The premaster secret is formed as follows: if the PSK is N octets
5047 * long, concatenate a uint16 with the value N, N zero octets, a second
5048 * uint16 with the value N, and the PSK itself.
5049 */
5050
Janos Follath40e13932019-06-26 13:22:29 +01005051 *cur++ = ( data_length >> 8 ) & 0xff;
5052 *cur++ = ( data_length >> 0 ) & 0xff;
5053 memset( cur, 0, data_length );
5054 cur += data_length;
5055 *cur++ = pms[0];
5056 *cur++ = pms[1];
5057 memcpy( cur, data, data_length );
5058 cur += data_length;
Janos Follath6660f0e2019-06-17 08:44:03 +01005059
Janos Follath40e13932019-06-26 13:22:29 +01005060 status = psa_tls12_prf_set_key( prf, hash_alg, pms, cur - pms );
Janos Follath6660f0e2019-06-17 08:44:03 +01005061
5062 mbedtls_platform_zeroize( pms, sizeof( pms ) );
5063 return( status );
5064}
5065
Janos Follath63028dd2019-06-13 09:15:47 +01005066static psa_status_t psa_tls12_prf_set_label( psa_tls12_prf_key_derivation_t *prf,
5067 const uint8_t *data,
5068 size_t data_length )
5069{
5070 if( prf->state != TLS12_PRF_STATE_KEY_SET )
5071 return( PSA_ERROR_BAD_STATE );
5072
Janos Follathd6dce9f2019-07-04 09:11:38 +01005073 if( data_length != 0 )
5074 {
5075 prf->label = mbedtls_calloc( 1, data_length );
5076 if( prf->label == NULL )
5077 return( PSA_ERROR_INSUFFICIENT_MEMORY );
Janos Follath63028dd2019-06-13 09:15:47 +01005078
Janos Follathd6dce9f2019-07-04 09:11:38 +01005079 memcpy( prf->label, data, data_length );
5080 prf->label_length = data_length;
5081 }
Janos Follath63028dd2019-06-13 09:15:47 +01005082
5083 prf->state = TLS12_PRF_STATE_LABEL_SET;
5084
5085 return( PSA_SUCCESS );
5086}
5087
Janos Follathb03233e2019-06-11 15:30:30 +01005088static psa_status_t psa_tls12_prf_input( psa_tls12_prf_key_derivation_t *prf,
5089 psa_algorithm_t hash_alg,
5090 psa_key_derivation_step_t step,
5091 const uint8_t *data,
5092 size_t data_length )
5093{
Janos Follathb03233e2019-06-11 15:30:30 +01005094 switch( step )
5095 {
Janos Follathf08e2652019-06-13 09:05:41 +01005096 case PSA_KEY_DERIVATION_INPUT_SEED:
5097 return( psa_tls12_prf_set_seed( prf, data, data_length ) );
Janos Follath81550542019-06-13 14:26:34 +01005098 case PSA_KEY_DERIVATION_INPUT_SECRET:
5099 return( psa_tls12_prf_set_key( prf, hash_alg, data, data_length ) );
Janos Follath63028dd2019-06-13 09:15:47 +01005100 case PSA_KEY_DERIVATION_INPUT_LABEL:
5101 return( psa_tls12_prf_set_label( prf, data, data_length ) );
Janos Follathb03233e2019-06-11 15:30:30 +01005102 default:
5103 return( PSA_ERROR_INVALID_ARGUMENT );
5104 }
5105}
Janos Follath6660f0e2019-06-17 08:44:03 +01005106
5107static psa_status_t psa_tls12_prf_psk_to_ms_input(
5108 psa_tls12_prf_key_derivation_t *prf,
5109 psa_algorithm_t hash_alg,
5110 psa_key_derivation_step_t step,
5111 const uint8_t *data,
5112 size_t data_length )
5113{
5114 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
Janos Follath0c1ed842019-06-28 13:35:36 +01005115 {
Janos Follath6660f0e2019-06-17 08:44:03 +01005116 return( psa_tls12_prf_psk_to_ms_set_key( prf, hash_alg,
5117 data, data_length ) );
Janos Follath0c1ed842019-06-28 13:35:36 +01005118 }
Janos Follath6660f0e2019-06-17 08:44:03 +01005119
5120 return( psa_tls12_prf_input( prf, hash_alg, step, data, data_length ) );
5121}
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005122#endif /* MBEDTLS_MD_C */
5123
Gilles Peskineb8965192019-09-24 16:21:10 +02005124/** Check whether the given key type is acceptable for the given
5125 * input step of a key derivation.
5126 *
5127 * Secret inputs must have the type #PSA_KEY_TYPE_DERIVE.
5128 * Non-secret inputs must have the type #PSA_KEY_TYPE_RAW_DATA.
5129 * Both secret and non-secret inputs can alternatively have the type
5130 * #PSA_KEY_TYPE_NONE, which is never the type of a key object, meaning
5131 * that the input was passed as a buffer rather than via a key object.
5132 */
Gilles Peskine224b0d62019-09-23 18:13:17 +02005133static int psa_key_derivation_check_input_type(
5134 psa_key_derivation_step_t step,
5135 psa_key_type_t key_type )
5136{
5137 switch( step )
5138 {
5139 case PSA_KEY_DERIVATION_INPUT_SECRET:
Gilles Peskineb8965192019-09-24 16:21:10 +02005140 if( key_type == PSA_KEY_TYPE_DERIVE )
5141 return( PSA_SUCCESS );
5142 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005143 return( PSA_SUCCESS );
5144 break;
5145 case PSA_KEY_DERIVATION_INPUT_LABEL:
5146 case PSA_KEY_DERIVATION_INPUT_SALT:
5147 case PSA_KEY_DERIVATION_INPUT_INFO:
5148 case PSA_KEY_DERIVATION_INPUT_SEED:
Gilles Peskineb8965192019-09-24 16:21:10 +02005149 if( key_type == PSA_KEY_TYPE_RAW_DATA )
5150 return( PSA_SUCCESS );
5151 if( key_type == PSA_KEY_TYPE_NONE )
Gilles Peskine224b0d62019-09-23 18:13:17 +02005152 return( PSA_SUCCESS );
5153 break;
5154 }
5155 return( PSA_ERROR_INVALID_ARGUMENT );
5156}
5157
Janos Follathb80a94e2019-06-12 15:54:46 +01005158static psa_status_t psa_key_derivation_input_internal(
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005159 psa_key_derivation_operation_t *operation,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005160 psa_key_derivation_step_t step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005161 psa_key_type_t key_type,
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005162 const uint8_t *data,
5163 size_t data_length )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005164{
Gilles Peskine46d7faf2019-09-23 19:22:55 +02005165 psa_status_t status;
5166 psa_algorithm_t kdf_alg = psa_key_derivation_get_kdf_alg( operation );
5167
5168 status = psa_key_derivation_check_input_type( step, key_type );
Gilles Peskine224b0d62019-09-23 18:13:17 +02005169 if( status != PSA_SUCCESS )
5170 goto exit;
5171
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005172#if defined(MBEDTLS_MD_C)
Gilles Peskine969c5d62019-01-16 15:53:06 +01005173 if( PSA_ALG_IS_HKDF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005174 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005175 status = psa_hkdf_input( &operation->ctx.hkdf,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005176 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005177 step, data, data_length );
5178 }
k-stachowiak012dcc42019-08-13 14:55:03 +02005179 else if( PSA_ALG_IS_TLS12_PRF( kdf_alg ) )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005180 {
Janos Follathb03233e2019-06-11 15:30:30 +01005181 status = psa_tls12_prf_input( &operation->ctx.tls12_prf,
5182 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5183 step, data, data_length );
Janos Follath6660f0e2019-06-17 08:44:03 +01005184 }
5185 else if( PSA_ALG_IS_TLS12_PSK_TO_MS( kdf_alg ) )
5186 {
5187 status = psa_tls12_prf_psk_to_ms_input( &operation->ctx.tls12_prf,
5188 PSA_ALG_HKDF_GET_HASH( kdf_alg ),
5189 step, data, data_length );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005190 }
5191 else
5192#endif /* MBEDTLS_MD_C */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005193 {
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005194 /* This can't happen unless the operation object was not initialized */
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005195 return( PSA_ERROR_BAD_STATE );
5196 }
5197
Gilles Peskine224b0d62019-09-23 18:13:17 +02005198exit:
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005199 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005200 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005201 return( status );
5202}
5203
Janos Follath51f4a0f2019-06-14 11:35:55 +01005204psa_status_t psa_key_derivation_input_bytes(
5205 psa_key_derivation_operation_t *operation,
5206 psa_key_derivation_step_t step,
5207 const uint8_t *data,
5208 size_t data_length )
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005209{
Gilles Peskineb8965192019-09-24 16:21:10 +02005210 return( psa_key_derivation_input_internal( operation, step,
5211 PSA_KEY_TYPE_NONE,
Janos Follathc5621512019-06-14 11:27:57 +01005212 data, data_length ) );
Gilles Peskine6cdfdb72019-01-08 10:31:27 +01005213}
5214
Janos Follath51f4a0f2019-06-14 11:35:55 +01005215psa_status_t psa_key_derivation_input_key(
5216 psa_key_derivation_operation_t *operation,
5217 psa_key_derivation_step_t step,
5218 psa_key_handle_t handle )
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005219{
5220 psa_key_slot_t *slot;
5221 psa_status_t status;
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005222
Gilles Peskine28f8f302019-07-24 13:30:31 +02005223 status = psa_get_transparent_key( handle, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005224 PSA_KEY_USAGE_DERIVE,
5225 operation->alg );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005226 if( status != PSA_SUCCESS )
Gilles Peskine593773d2019-09-23 18:17:40 +02005227 {
5228 psa_key_derivation_abort( operation );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005229 return( status );
Gilles Peskine593773d2019-09-23 18:17:40 +02005230 }
Gilles Peskine178c9aa2019-09-24 18:21:06 +02005231
5232 /* Passing a key object as a SECRET input unlocks the permission
5233 * to output to a key object. */
5234 if( step == PSA_KEY_DERIVATION_INPUT_SECRET )
5235 operation->can_output_key = 1;
5236
Janos Follathb80a94e2019-06-12 15:54:46 +01005237 return( psa_key_derivation_input_internal( operation,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005238 step, slot->attr.type,
Janos Follathb80a94e2019-06-12 15:54:46 +01005239 slot->data.raw.data,
5240 slot->data.raw.bytes ) );
Gilles Peskineb70a0fd2019-01-07 22:59:38 +01005241}
Gilles Peskineea0fb492018-07-12 17:17:20 +02005242
5243
5244
5245/****************************************************************/
Gilles Peskine01d718c2018-09-18 12:01:02 +02005246/* Key agreement */
5247/****************************************************************/
5248
Gilles Peskinea05219c2018-11-16 16:02:56 +01005249#if defined(MBEDTLS_ECDH_C)
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005250static psa_status_t psa_key_agreement_ecdh( const uint8_t *peer_key,
5251 size_t peer_key_length,
5252 const mbedtls_ecp_keypair *our_key,
5253 uint8_t *shared_secret,
5254 size_t shared_secret_size,
5255 size_t *shared_secret_length )
5256{
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005257 mbedtls_ecp_keypair *their_key = NULL;
5258 mbedtls_ecdh_context ecdh;
Jaeden Amero97271b32019-01-10 19:38:51 +00005259 psa_status_t status;
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005260 mbedtls_ecdh_init( &ecdh );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005261
Jaeden Ameroccdce902019-01-10 11:42:27 +00005262 status = psa_import_ec_public_key(
5263 mbedtls_ecc_group_to_psa( our_key->grp.id ),
5264 peer_key, peer_key_length,
5265 &their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005266 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005267 goto exit;
5268
Jaeden Amero97271b32019-01-10 19:38:51 +00005269 status = mbedtls_to_psa_error(
5270 mbedtls_ecdh_get_params( &ecdh, their_key, MBEDTLS_ECDH_THEIRS ) );
5271 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005272 goto exit;
Jaeden Amero97271b32019-01-10 19:38:51 +00005273 status = mbedtls_to_psa_error(
5274 mbedtls_ecdh_get_params( &ecdh, our_key, MBEDTLS_ECDH_OURS ) );
5275 if( status != PSA_SUCCESS )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005276 goto exit;
5277
Jaeden Amero97271b32019-01-10 19:38:51 +00005278 status = mbedtls_to_psa_error(
5279 mbedtls_ecdh_calc_secret( &ecdh,
5280 shared_secret_length,
5281 shared_secret, shared_secret_size,
5282 mbedtls_ctr_drbg_random,
5283 &global_data.ctr_drbg ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005284
5285exit:
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005286 mbedtls_ecdh_free( &ecdh );
Jaeden Ameroccdce902019-01-10 11:42:27 +00005287 mbedtls_ecp_keypair_free( their_key );
5288 mbedtls_free( their_key );
Jaeden Amero97271b32019-01-10 19:38:51 +00005289 return( status );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005290}
Gilles Peskinea05219c2018-11-16 16:02:56 +01005291#endif /* MBEDTLS_ECDH_C */
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005292
Gilles Peskine01d718c2018-09-18 12:01:02 +02005293#define PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE MBEDTLS_ECP_MAX_BYTES
5294
Gilles Peskine0216fe12019-04-11 21:23:21 +02005295static psa_status_t psa_key_agreement_raw_internal( psa_algorithm_t alg,
5296 psa_key_slot_t *private_key,
5297 const uint8_t *peer_key,
5298 size_t peer_key_length,
5299 uint8_t *shared_secret,
5300 size_t shared_secret_size,
5301 size_t *shared_secret_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005302{
Gilles Peskine0216fe12019-04-11 21:23:21 +02005303 switch( alg )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005304 {
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005305#if defined(MBEDTLS_ECDH_C)
Gilles Peskine0216fe12019-04-11 21:23:21 +02005306 case PSA_ALG_ECDH:
Gilles Peskine8e338702019-07-30 20:06:31 +02005307 if( ! PSA_KEY_TYPE_IS_ECC_KEY_PAIR( private_key->attr.type ) )
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005308 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005309 return( psa_key_agreement_ecdh( peer_key, peer_key_length,
5310 private_key->data.ecp,
5311 shared_secret, shared_secret_size,
5312 shared_secret_length ) );
Gilles Peskineb7ecdf02018-09-18 12:11:27 +02005313#endif /* MBEDTLS_ECDH_C */
Gilles Peskine01d718c2018-09-18 12:01:02 +02005314 default:
Gilles Peskine93f85002018-11-16 16:43:31 +01005315 (void) private_key;
5316 (void) peer_key;
5317 (void) peer_key_length;
Gilles Peskine0216fe12019-04-11 21:23:21 +02005318 (void) shared_secret;
5319 (void) shared_secret_size;
5320 (void) shared_secret_length;
Gilles Peskine01d718c2018-09-18 12:01:02 +02005321 return( PSA_ERROR_NOT_SUPPORTED );
5322 }
Gilles Peskine0216fe12019-04-11 21:23:21 +02005323}
5324
Gilles Peskinea99d3fb2019-05-16 15:28:51 +02005325/* Note that if this function fails, you must call psa_key_derivation_abort()
Gilles Peskine01d718c2018-09-18 12:01:02 +02005326 * to potentially free embedded data structures and wipe confidential data.
5327 */
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005328static psa_status_t psa_key_agreement_internal( psa_key_derivation_operation_t *operation,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005329 psa_key_derivation_step_t step,
Gilles Peskine01d718c2018-09-18 12:01:02 +02005330 psa_key_slot_t *private_key,
5331 const uint8_t *peer_key,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005332 size_t peer_key_length )
Gilles Peskine01d718c2018-09-18 12:01:02 +02005333{
5334 psa_status_t status;
5335 uint8_t shared_secret[PSA_KEY_AGREEMENT_MAX_SHARED_SECRET_SIZE];
5336 size_t shared_secret_length = 0;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005337 psa_algorithm_t ka_alg = PSA_ALG_KEY_AGREEMENT_GET_BASE( operation->alg );
Gilles Peskine01d718c2018-09-18 12:01:02 +02005338
5339 /* Step 1: run the secret agreement algorithm to generate the shared
5340 * secret. */
Gilles Peskine0216fe12019-04-11 21:23:21 +02005341 status = psa_key_agreement_raw_internal( ka_alg,
5342 private_key,
5343 peer_key, peer_key_length,
itayzafrir0adf0fc2018-09-06 16:24:41 +03005344 shared_secret,
5345 sizeof( shared_secret ),
Gilles Peskine05d69892018-06-19 22:00:52 +02005346 &shared_secret_length );
Gilles Peskine53d991e2018-07-12 01:14:59 +02005347 if( status != PSA_SUCCESS )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005348 goto exit;
5349
Gilles Peskineb0b255c2018-07-06 17:01:38 +02005350 /* Step 2: set up the key derivation to generate key material from
Gilles Peskine224b0d62019-09-23 18:13:17 +02005351 * the shared secret. A shared secret is permitted wherever a key
5352 * of type DERIVE is permitted. */
Janos Follathb80a94e2019-06-12 15:54:46 +01005353 status = psa_key_derivation_input_internal( operation, step,
Gilles Peskine224b0d62019-09-23 18:13:17 +02005354 PSA_KEY_TYPE_DERIVE,
Janos Follathb80a94e2019-06-12 15:54:46 +01005355 shared_secret,
5356 shared_secret_length );
Gilles Peskine969c5d62019-01-16 15:53:06 +01005357
Gilles Peskine12313cd2018-06-20 00:20:32 +02005358exit:
Gilles Peskine3f108122018-12-07 18:14:53 +01005359 mbedtls_platform_zeroize( shared_secret, shared_secret_length );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005360 return( status );
5361}
5362
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005363psa_status_t psa_key_derivation_key_agreement( psa_key_derivation_operation_t *operation,
Gilles Peskinecf7292e2019-05-16 17:53:40 +02005364 psa_key_derivation_step_t step,
5365 psa_key_handle_t private_key,
5366 const uint8_t *peer_key,
5367 size_t peer_key_length )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005368{
Gilles Peskine2f060a82018-12-04 17:12:32 +01005369 psa_key_slot_t *slot;
Gilles Peskine08542d82018-07-19 17:05:42 +02005370 psa_status_t status;
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005371 if( ! PSA_ALG_IS_KEY_AGREEMENT( operation->alg ) )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005372 return( PSA_ERROR_INVALID_ARGUMENT );
Gilles Peskine28f8f302019-07-24 13:30:31 +02005373 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005374 PSA_KEY_USAGE_DERIVE, operation->alg );
Gilles Peskine12313cd2018-06-20 00:20:32 +02005375 if( status != PSA_SUCCESS )
5376 return( status );
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005377 status = psa_key_agreement_internal( operation, step,
Gilles Peskine346797d2018-11-16 16:05:06 +01005378 slot,
Gilles Peskine969c5d62019-01-16 15:53:06 +01005379 peer_key, peer_key_length );
Gilles Peskine346797d2018-11-16 16:05:06 +01005380 if( status != PSA_SUCCESS )
Gilles Peskine51ae0e42019-05-16 17:31:03 +02005381 psa_key_derivation_abort( operation );
Gilles Peskine346797d2018-11-16 16:05:06 +01005382 return( status );
Gilles Peskineaf3baab2018-06-27 22:55:52 +02005383}
5384
Gilles Peskinebe697d82019-05-16 18:00:41 +02005385psa_status_t psa_raw_key_agreement( psa_algorithm_t alg,
5386 psa_key_handle_t private_key,
5387 const uint8_t *peer_key,
5388 size_t peer_key_length,
5389 uint8_t *output,
5390 size_t output_size,
5391 size_t *output_length )
Gilles Peskine0216fe12019-04-11 21:23:21 +02005392{
5393 psa_key_slot_t *slot;
5394 psa_status_t status;
5395
5396 if( ! PSA_ALG_IS_KEY_AGREEMENT( alg ) )
5397 {
5398 status = PSA_ERROR_INVALID_ARGUMENT;
5399 goto exit;
5400 }
Gilles Peskine28f8f302019-07-24 13:30:31 +02005401 status = psa_get_transparent_key( private_key, &slot,
Gilles Peskinef77a6ac2019-07-25 10:51:03 +02005402 PSA_KEY_USAGE_DERIVE, alg );
Gilles Peskine0216fe12019-04-11 21:23:21 +02005403 if( status != PSA_SUCCESS )
5404 goto exit;
5405
5406 status = psa_key_agreement_raw_internal( alg, slot,
5407 peer_key, peer_key_length,
5408 output, output_size,
5409 output_length );
5410
5411exit:
5412 if( status != PSA_SUCCESS )
5413 {
5414 /* If an error happens and is not handled properly, the output
5415 * may be used as a key to protect sensitive data. Arrange for such
5416 * a key to be random, which is likely to result in decryption or
5417 * verification errors. This is better than filling the buffer with
5418 * some constant data such as zeros, which would result in the data
5419 * being protected with a reproducible, easily knowable key.
5420 */
5421 psa_generate_random( output, output_size );
5422 *output_length = output_size;
5423 }
5424 return( status );
5425}
Gilles Peskine86a440b2018-11-12 18:39:40 +01005426
5427
5428/****************************************************************/
5429/* Random generation */
Gilles Peskine53d991e2018-07-12 01:14:59 +02005430/****************************************************************/
Gilles Peskine12313cd2018-06-20 00:20:32 +02005431
Gilles Peskine4c317f42018-07-12 01:24:09 +02005432psa_status_t psa_generate_random( uint8_t *output,
Gilles Peskine53d991e2018-07-12 01:14:59 +02005433 size_t output_size )
Gilles Peskine12313cd2018-06-20 00:20:32 +02005434{
Janos Follath24eed8d2019-11-22 13:21:35 +00005435 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005436 GUARD_MODULE_INITIALIZED;
5437
Gilles Peskinef181eca2019-08-07 13:49:00 +02005438 while( output_size > MBEDTLS_CTR_DRBG_MAX_REQUEST )
5439 {
5440 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg,
5441 output,
5442 MBEDTLS_CTR_DRBG_MAX_REQUEST );
5443 if( ret != 0 )
5444 return( mbedtls_to_psa_error( ret ) );
5445 output += MBEDTLS_CTR_DRBG_MAX_REQUEST;
5446 output_size -= MBEDTLS_CTR_DRBG_MAX_REQUEST;
5447 }
5448
Gilles Peskinee59236f2018-01-27 23:32:46 +01005449 ret = mbedtls_ctr_drbg_random( &global_data.ctr_drbg, output, output_size );
5450 return( mbedtls_to_psa_error( ret ) );
5451}
5452
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005453#if defined(MBEDTLS_PSA_INJECT_ENTROPY)
5454#include "mbedtls/entropy_poll.h"
5455
Gilles Peskine7228da22019-07-15 11:06:15 +02005456psa_status_t mbedtls_psa_inject_entropy( const uint8_t *seed,
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005457 size_t seed_size )
5458{
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005459 if( global_data.initialized )
5460 return( PSA_ERROR_NOT_PERMITTED );
Netanel Gonen21f37cb2018-11-19 11:53:55 +02005461
5462 if( ( ( seed_size < MBEDTLS_ENTROPY_MIN_PLATFORM ) ||
5463 ( seed_size < MBEDTLS_ENTROPY_BLOCK_SIZE ) ) ||
5464 ( seed_size > MBEDTLS_ENTROPY_MAX_SEED_SIZE ) )
5465 return( PSA_ERROR_INVALID_ARGUMENT );
5466
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005467 return( mbedtls_psa_storage_inject_entropy( seed, seed_size ) );
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005468}
Gilles Peskinee3dbdd82019-02-25 11:04:06 +01005469#endif /* MBEDTLS_PSA_INJECT_ENTROPY */
Netanel Gonen2bcd3122018-11-19 11:53:02 +02005470
Gilles Peskinee56e8782019-04-26 17:34:02 +02005471#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
5472static psa_status_t psa_read_rsa_exponent( const uint8_t *domain_parameters,
5473 size_t domain_parameters_size,
5474 int *exponent )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005475{
Gilles Peskinee56e8782019-04-26 17:34:02 +02005476 size_t i;
5477 uint32_t acc = 0;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005478
Gilles Peskinee56e8782019-04-26 17:34:02 +02005479 if( domain_parameters_size == 0 )
5480 {
5481 *exponent = 65537;
5482 return( PSA_SUCCESS );
5483 }
5484
5485 /* Mbed TLS encodes the public exponent as an int. For simplicity, only
5486 * support values that fit in a 32-bit integer, which is larger than
5487 * int on just about every platform anyway. */
5488 if( domain_parameters_size > sizeof( acc ) )
5489 return( PSA_ERROR_NOT_SUPPORTED );
5490 for( i = 0; i < domain_parameters_size; i++ )
5491 acc = ( acc << 8 ) | domain_parameters[i];
5492 if( acc > INT_MAX )
5493 return( PSA_ERROR_NOT_SUPPORTED );
5494 *exponent = acc;
5495 return( PSA_SUCCESS );
5496}
5497#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5498
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005499static psa_status_t psa_generate_key_internal(
Gilles Peskinee56e8782019-04-26 17:34:02 +02005500 psa_key_slot_t *slot, size_t bits,
5501 const uint8_t *domain_parameters, size_t domain_parameters_size )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005502{
Gilles Peskine8e338702019-07-30 20:06:31 +02005503 psa_key_type_t type = slot->attr.type;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005504
Gilles Peskinee56e8782019-04-26 17:34:02 +02005505 if( domain_parameters == NULL && domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005506 return( PSA_ERROR_INVALID_ARGUMENT );
5507
Gilles Peskinee59236f2018-01-27 23:32:46 +01005508 if( key_type_is_raw_bytes( type ) )
5509 {
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005510 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005511 status = prepare_raw_data_slot( type, bits, &slot->data.raw );
5512 if( status != PSA_SUCCESS )
5513 return( status );
5514 status = psa_generate_random( slot->data.raw.data,
5515 slot->data.raw.bytes );
5516 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005517 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005518#if defined(MBEDTLS_DES_C)
5519 if( type == PSA_KEY_TYPE_DES )
5520 psa_des_set_key_parity( slot->data.raw.data,
5521 slot->data.raw.bytes );
5522#endif /* MBEDTLS_DES_C */
5523 }
5524 else
5525
5526#if defined(MBEDTLS_RSA_C) && defined(MBEDTLS_GENPRIME)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005527 if ( type == PSA_KEY_TYPE_RSA_KEY_PAIR )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005528 {
5529 mbedtls_rsa_context *rsa;
Janos Follath24eed8d2019-11-22 13:21:35 +00005530 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005531 int exponent;
5532 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005533 if( bits > PSA_VENDOR_RSA_MAX_KEY_BITS )
5534 return( PSA_ERROR_NOT_SUPPORTED );
5535 /* Accept only byte-aligned keys, for the same reasons as
5536 * in psa_import_rsa_key(). */
5537 if( bits % 8 != 0 )
5538 return( PSA_ERROR_NOT_SUPPORTED );
Gilles Peskinee56e8782019-04-26 17:34:02 +02005539 status = psa_read_rsa_exponent( domain_parameters,
5540 domain_parameters_size,
5541 &exponent );
5542 if( status != PSA_SUCCESS )
5543 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005544 rsa = mbedtls_calloc( 1, sizeof( *rsa ) );
5545 if( rsa == NULL )
5546 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5547 mbedtls_rsa_init( rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_NONE );
5548 ret = mbedtls_rsa_gen_key( rsa,
5549 mbedtls_ctr_drbg_random,
5550 &global_data.ctr_drbg,
5551 (unsigned int) bits,
5552 exponent );
5553 if( ret != 0 )
5554 {
5555 mbedtls_rsa_free( rsa );
5556 mbedtls_free( rsa );
5557 return( mbedtls_to_psa_error( ret ) );
5558 }
5559 slot->data.rsa = rsa;
5560 }
5561 else
5562#endif /* MBEDTLS_RSA_C && MBEDTLS_GENPRIME */
5563
5564#if defined(MBEDTLS_ECP_C)
Gilles Peskinec93b80c2019-05-16 19:39:54 +02005565 if ( PSA_KEY_TYPE_IS_ECC( type ) && PSA_KEY_TYPE_IS_KEY_PAIR( type ) )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005566 {
5567 psa_ecc_curve_t curve = PSA_KEY_TYPE_GET_CURVE( type );
5568 mbedtls_ecp_group_id grp_id = mbedtls_ecc_group_of_psa( curve );
5569 const mbedtls_ecp_curve_info *curve_info =
5570 mbedtls_ecp_curve_info_from_grp_id( grp_id );
5571 mbedtls_ecp_keypair *ecp;
Janos Follath24eed8d2019-11-22 13:21:35 +00005572 int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
Gilles Peskinee56e8782019-04-26 17:34:02 +02005573 if( domain_parameters_size != 0 )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005574 return( PSA_ERROR_NOT_SUPPORTED );
5575 if( grp_id == MBEDTLS_ECP_DP_NONE || curve_info == NULL )
5576 return( PSA_ERROR_NOT_SUPPORTED );
5577 if( curve_info->bit_size != bits )
5578 return( PSA_ERROR_INVALID_ARGUMENT );
5579 ecp = mbedtls_calloc( 1, sizeof( *ecp ) );
5580 if( ecp == NULL )
5581 return( PSA_ERROR_INSUFFICIENT_MEMORY );
5582 mbedtls_ecp_keypair_init( ecp );
5583 ret = mbedtls_ecp_gen_key( grp_id, ecp,
5584 mbedtls_ctr_drbg_random,
5585 &global_data.ctr_drbg );
5586 if( ret != 0 )
5587 {
5588 mbedtls_ecp_keypair_free( ecp );
5589 mbedtls_free( ecp );
5590 return( mbedtls_to_psa_error( ret ) );
5591 }
5592 slot->data.ecp = ecp;
5593 }
5594 else
5595#endif /* MBEDTLS_ECP_C */
5596
5597 return( PSA_ERROR_NOT_SUPPORTED );
5598
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005599 return( PSA_SUCCESS );
5600}
Darryl Green0c6575a2018-11-07 16:05:30 +00005601
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005602psa_status_t psa_generate_key( const psa_key_attributes_t *attributes,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005603 psa_key_handle_t *handle )
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005604{
5605 psa_status_t status;
5606 psa_key_slot_t *slot = NULL;
Gilles Peskine8abe6a22019-07-12 23:40:35 +02005607 psa_se_drv_table_entry_t *driver = NULL;
Gilles Peskine11792082019-08-06 18:36:36 +02005608
Gilles Peskine0f84d622019-09-12 19:03:13 +02005609 /* Reject any attempt to create a zero-length key so that we don't
5610 * risk tripping up later, e.g. on a malloc(0) that returns NULL. */
5611 if( psa_get_key_bits( attributes ) == 0 )
5612 return( PSA_ERROR_INVALID_ARGUMENT );
5613
Gilles Peskinedf179142019-07-15 22:02:14 +02005614 status = psa_start_key_creation( PSA_KEY_CREATION_GENERATE,
5615 attributes, handle, &slot, &driver );
Gilles Peskine11792082019-08-06 18:36:36 +02005616 if( status != PSA_SUCCESS )
5617 goto exit;
5618
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005619#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5620 if( driver != NULL )
Darryl Green0c6575a2018-11-07 16:05:30 +00005621 {
Gilles Peskine11792082019-08-06 18:36:36 +02005622 const psa_drv_se_t *drv = psa_get_se_driver_methods( driver );
5623 size_t pubkey_length = 0; /* We don't support this feature yet */
5624 if( drv->key_management == NULL ||
5625 drv->key_management->p_generate == NULL )
5626 {
5627 status = PSA_ERROR_NOT_SUPPORTED;
5628 goto exit;
5629 }
5630 status = drv->key_management->p_generate(
5631 psa_get_se_driver_context( driver ),
5632 slot->data.se.slot_number, attributes,
5633 NULL, 0, &pubkey_length );
Darryl Green0c6575a2018-11-07 16:05:30 +00005634 }
Gilles Peskine11792082019-08-06 18:36:36 +02005635 else
Gilles Peskinef4ee6622019-07-24 13:44:30 +02005636#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005637 {
Gilles Peskine35ef36b2019-05-16 19:42:05 +02005638 status = psa_generate_key_internal(
Gilles Peskine7e0cff92019-07-30 13:48:52 +02005639 slot, attributes->core.bits,
Gilles Peskinee56e8782019-04-26 17:34:02 +02005640 attributes->domain_parameters, attributes->domain_parameters_size );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005641 }
Gilles Peskine11792082019-08-06 18:36:36 +02005642
5643exit:
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005644 if( status == PSA_SUCCESS )
Gilles Peskine011e4282019-06-26 18:34:38 +02005645 status = psa_finish_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005646 if( status != PSA_SUCCESS )
5647 {
Gilles Peskine011e4282019-06-26 18:34:38 +02005648 psa_fail_key_creation( slot, driver );
Gilles Peskineff5f0e72019-04-18 12:53:30 +02005649 *handle = 0;
5650 }
Darryl Green0c6575a2018-11-07 16:05:30 +00005651 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005652}
5653
5654
Gilles Peskinee59236f2018-01-27 23:32:46 +01005655
5656/****************************************************************/
5657/* Module setup */
5658/****************************************************************/
5659
Gilles Peskine5e769522018-11-20 21:59:56 +01005660psa_status_t mbedtls_psa_crypto_configure_entropy_sources(
5661 void (* entropy_init )( mbedtls_entropy_context *ctx ),
5662 void (* entropy_free )( mbedtls_entropy_context *ctx ) )
5663{
5664 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5665 return( PSA_ERROR_BAD_STATE );
5666 global_data.entropy_init = entropy_init;
5667 global_data.entropy_free = entropy_free;
5668 return( PSA_SUCCESS );
5669}
5670
Gilles Peskinee59236f2018-01-27 23:32:46 +01005671void mbedtls_psa_crypto_free( void )
5672{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005673 psa_wipe_all_key_slots( );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005674 if( global_data.rng_state != RNG_NOT_INITIALIZED )
5675 {
5676 mbedtls_ctr_drbg_free( &global_data.ctr_drbg );
Gilles Peskine5e769522018-11-20 21:59:56 +01005677 global_data.entropy_free( &global_data.entropy );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005678 }
5679 /* Wipe all remaining data, including configuration.
5680 * In particular, this sets all state indicator to the value
5681 * indicating "uninitialized". */
Gilles Peskine3f108122018-12-07 18:14:53 +01005682 mbedtls_platform_zeroize( &global_data, sizeof( global_data ) );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005683#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
Gilles Peskined0890212019-06-24 14:34:43 +02005684 /* Unregister all secure element drivers, so that we restart from
5685 * a pristine state. */
5686 psa_unregister_all_se_drivers( );
Gilles Peskinea8ade162019-06-26 11:24:49 +02005687#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005688}
5689
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005690#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
5691/** Recover a transaction that was interrupted by a power failure.
5692 *
5693 * This function is called during initialization, before psa_crypto_init()
5694 * returns. If this function returns a failure status, the initialization
5695 * fails.
5696 */
5697static psa_status_t psa_crypto_recover_transaction(
5698 const psa_crypto_transaction_t *transaction )
5699{
5700 switch( transaction->unknown.type )
5701 {
5702 case PSA_CRYPTO_TRANSACTION_CREATE_KEY:
5703 case PSA_CRYPTO_TRANSACTION_DESTROY_KEY:
Janos Follath1d57a202019-08-13 12:15:34 +01005704 /* TODO - fall through to the failure case until this
Gilles Peskinec9d7f942019-08-13 16:17:16 +02005705 * is implemented.
5706 * https://github.com/ARMmbed/mbed-crypto/issues/218
5707 */
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005708 default:
5709 /* We found an unsupported transaction in the storage.
5710 * We don't know what state the storage is in. Give up. */
5711 return( PSA_ERROR_STORAGE_FAILURE );
5712 }
5713}
5714#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
5715
Gilles Peskinee59236f2018-01-27 23:32:46 +01005716psa_status_t psa_crypto_init( void )
5717{
Gilles Peskine66fb1262018-12-10 16:29:04 +01005718 psa_status_t status;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005719 const unsigned char drbg_seed[] = "PSA";
5720
Gilles Peskinec6b69072018-11-20 21:42:52 +01005721 /* Double initialization is explicitly allowed. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005722 if( global_data.initialized != 0 )
5723 return( PSA_SUCCESS );
5724
Gilles Peskine5e769522018-11-20 21:59:56 +01005725 /* Set default configuration if
5726 * mbedtls_psa_crypto_configure_entropy_sources() hasn't been called. */
5727 if( global_data.entropy_init == NULL )
5728 global_data.entropy_init = mbedtls_entropy_init;
5729 if( global_data.entropy_free == NULL )
5730 global_data.entropy_free = mbedtls_entropy_free;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005731
Gilles Peskinec6b69072018-11-20 21:42:52 +01005732 /* Initialize the random generator. */
Gilles Peskine5e769522018-11-20 21:59:56 +01005733 global_data.entropy_init( &global_data.entropy );
Jaeden Amero76541612019-06-04 17:14:43 +01005734#if defined(MBEDTLS_PSA_INJECT_ENTROPY) && \
5735 defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
5736 /* The PSA entropy injection feature depends on using NV seed as an entropy
5737 * source. Add NV seed as an entropy source for PSA entropy injection. */
5738 mbedtls_entropy_add_source( &global_data.entropy,
5739 mbedtls_nv_seed_poll, NULL,
5740 MBEDTLS_ENTROPY_BLOCK_SIZE,
5741 MBEDTLS_ENTROPY_SOURCE_STRONG );
5742#endif
Gilles Peskinee59236f2018-01-27 23:32:46 +01005743 mbedtls_ctr_drbg_init( &global_data.ctr_drbg );
Gilles Peskinec6b69072018-11-20 21:42:52 +01005744 global_data.rng_state = RNG_INITIALIZED;
Gilles Peskine66fb1262018-12-10 16:29:04 +01005745 status = mbedtls_to_psa_error(
5746 mbedtls_ctr_drbg_seed( &global_data.ctr_drbg,
5747 mbedtls_entropy_func,
5748 &global_data.entropy,
5749 drbg_seed, sizeof( drbg_seed ) - 1 ) );
5750 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005751 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005752 global_data.rng_state = RNG_SEEDED;
Gilles Peskinee59236f2018-01-27 23:32:46 +01005753
Gilles Peskine66fb1262018-12-10 16:29:04 +01005754 status = psa_initialize_key_slots( );
5755 if( status != PSA_SUCCESS )
5756 goto exit;
Gilles Peskinec6b69072018-11-20 21:42:52 +01005757
Gilles Peskined9348f22019-10-01 15:22:29 +02005758#if defined(MBEDTLS_PSA_CRYPTO_SE_C)
5759 status = psa_init_all_se_drivers( );
5760 if( status != PSA_SUCCESS )
5761 goto exit;
5762#endif /* MBEDTLS_PSA_CRYPTO_SE_C */
5763
Gilles Peskine4b734222019-07-24 15:56:31 +02005764#if defined(PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS)
Gilles Peskinefc762652019-07-22 19:30:34 +02005765 status = psa_crypto_load_transaction( );
5766 if( status == PSA_SUCCESS )
5767 {
Gilles Peskinef9bb29e2019-07-25 17:52:59 +02005768 status = psa_crypto_recover_transaction( &psa_crypto_transaction );
5769 if( status != PSA_SUCCESS )
5770 goto exit;
5771 status = psa_crypto_stop_transaction( );
Gilles Peskinefc762652019-07-22 19:30:34 +02005772 }
5773 else if( status == PSA_ERROR_DOES_NOT_EXIST )
5774 {
5775 /* There's no transaction to complete. It's all good. */
5776 status = PSA_SUCCESS;
5777 }
Gilles Peskine4b734222019-07-24 15:56:31 +02005778#endif /* PSA_CRYPTO_STORAGE_HAS_TRANSACTIONS */
Gilles Peskinefc762652019-07-22 19:30:34 +02005779
Gilles Peskinec6b69072018-11-20 21:42:52 +01005780 /* All done. */
Gilles Peskinee59236f2018-01-27 23:32:46 +01005781 global_data.initialized = 1;
5782
5783exit:
Gilles Peskine66fb1262018-12-10 16:29:04 +01005784 if( status != PSA_SUCCESS )
Gilles Peskinee59236f2018-01-27 23:32:46 +01005785 mbedtls_psa_crypto_free( );
Gilles Peskine66fb1262018-12-10 16:29:04 +01005786 return( status );
Gilles Peskinee59236f2018-01-27 23:32:46 +01005787}
5788
5789#endif /* MBEDTLS_PSA_CRYPTO_C */